JSPM

nested-prop-resolver

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q49816F
  • License MIT

"This library helps you to find and get a nested property from a complex json object. It protects your code from null or undefined errors"

Package Exports

  • nested-prop-resolver

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nested-prop-resolver) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Nested Prop Resolver

This library helps you to find and get a nested property from a complex json object without any runtime error. You can use this to resolve json properties from an api response.

eg:

const responseObj = { user: { name: 'Hiran' } };

const officeAddress = responseObj.office.address // This will give you a runtime error since there is no 'office' property in api response object.

So, use the 'nested-prop-resolver' to prevent runtime errors.

const officeAddress = resolve(responseObj, 'office.address'); // Output is undefined. No runtime error.

const userName = resolve(responseObj, 'user.name'); // Output is 'Hiran'.

Install

yarn nested-prop-resolver

or

npm install nested-prop-resolver --save

Example

import { resolve } from 'nested-prop-resolver';

// For nested props
const responseObj = {
  user: {
    name: 'Hiran'
  }
};

const name = resolve(responseObj, 'user.name'); // Output the value of name property.


// For nested arrays
const responseObj = {
  users: [
    {
      name: 'Hiran'
    }
  ]
};

const name = resolve(responseObj, 'users[0].name'); // Output the value of name property.


// For complex nested arrays
const responseObj = [
  {
    users: [
      {
        name: 'Hiran'
      }
    ]
  }
];

const name = resolve(responseObj, '[0].users[0].name'); // Output the value of name property.


// For unpresent nested props.
const responseObj = {
  user: {
    name: 'Hiran'
  }
};

const name = resolve(responseObj, 'user.age'); // Output undefined.

License

MIT © H.R. Hiran Peiris