object-optional-chaining

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i object-optional-chaining

Weekly Downloads

44

Version

1.0.1

License

MIT

Last publish

Collaborators

  • minwe