safe-navigation-js
v1.0.1
Published
Javascript ES6 safe navigation based on https://gist.github.com/dakaraphi/6a87168db66fd8f032d2
Downloads
7
Readme
Safe Navigation ES6
Based on experiment: https://gist.github.com/dakaraphi/6a87168db66fd8f032d2
Usage
let o = {
name: "User1",
address: {
street: "513"
},
getAddress: function() {
return this.address;
},
getNull: function() {
return null;
},
isNull: null
};
// '.$' signifies the end of the expression and to resolve the value
safe(o).getAddress().street.$ === '513'
safe(o).name.$ === 'User1'
// Example using a default value
safe(o,'name').name.noName.noName2.$ === 'name';
// Example resolving to an object
safe(o).address.$ === o.address
// Example undefined resolutions
safe(o).address.city.country.street.$ === 'undefined'
safe(o).isNull.next.next.$ === 'undefined'
// Example calling a function
safe(o).getNull().street.$ === 'undefined'
// Example calling non existent function
safe(o,'nothing').style().testing.$ === 'nothing'