1.0.1 • Published 7 years ago

object-optional-chaining v1.0.1

Weekly downloads
231
License
MIT
Repository
github
Last release
7 years ago

ES Object Optional Chaining

NPM version build status Test coverage

ES5 implements of object optional chaining.

See:

Usage

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

Note

Your environment should support Array.prototype.reduce().

License

MIT © Minwe