1.0.1 • Published 6 years ago

@hspkg/chain v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

chain

NPM version npm download Travis Coverage

Partial implementation for Optional Chaining in ES5

API

var chain: (sourceObject: object, propertyString: string, defaultValue: any) => any

Examples

npm i @hspkg/chain -S

var chain = require('@hspkg/chain');

var testObj = {
  code: 200,
  data: {
    list: [
      { id: 1, name: 'name1' },
      { id: 2, name: 'name2' }
    ],
    page: {
      current: 1,
      total: 200
    }
  }
};

console.log(chain(testObj, '.code')); // 200
console.log(chain(testObj, '.data.page.current')); // 1
console.log(chain(testObj, '.data.list[0].id')); // 1
console.log(chain(testObj, '.wrong.path')); // undefined
console.log(chain(testObj, '.wrong.path', 666)); // 666

License

MIT