npm install object-optional-chaining[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[npm-image]: http://img.shields.io/npm/v/object-optional-chaining.svg?style=flat-square
[npm-url]: http://npmjs.org/package/object-optional-chaining
[travis-image]: https://img.shields.io/travis/minwe/optional-chaining.svg?style=flat-square
[travis-url]: https://travis-ci.org/minwe/optional-chaining
[coveralls-image]: https://img.shields.io/coveralls/minwe/optional-chaining.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/minwe/optional-chaining?branch=master
ES5 implements of object optional chaining.
See:
- ECMAScript proposal for Optional Chaining
- Swift Optional Chaining
``javascript
import optionalChaining from 'object-optional-chaining';
const user = {
id: '0001',
name: 'Minwe',
repos: {
total: 5,
list: [
{ id: '1001', name: 'Repo1' },
{ id: '1002', name: 'Repo2' },
{ id: '1003', name: 'Repo3' },
{ id: '1004', name: 'Repo4' },
{ id: '1005', name: 'Repo5' },
],
},
};
optionalChaining(user, 'name'); // Minwe
optionalChaining(user, 'repos.total'); // 5
optionalChaining(user, 'repos.list'); // array
optionalChaining(user, 'repos.list[0]'); // { id: '1001', name: 'Repo1' }
optionalChaining(user, 'repos.list[0].id'); // 1001
optionalChaining(user, 'repos.notExist'); // undefined
``
Your environment should support Array.prototype.reduce().
- es5-shim
MIT © Minwe