optional-chaining
v1.0.1
Published
Partial implementation for Optional Chaining in ES5
Downloads
68
Readme
optional-chaining
Partial implementation for Optional Chaining in ES5
API
var chain: (sourceObject: object, propertyString: string, defaultValue: any) => any
Examples
npm i optional-chaining -S
var chain = require('optional-chaining');
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