safe-navigation-js

1.0.1 • Public • Published

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'

Package Sidebar

Install

npm i safe-navigation-js

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

3.41 kB

Total Files

3

Last publish

Collaborators

  • oss92