get-value-safely

1.0.1 • Public • Published

get-value-safely

JavaScript mini-library to get nested values safely.

Assures that a value of a child chain property is returned without runtime errors.

If the checked property does not exist in the provided object or one of the children in the chain is undefined, then it returns the provided fallback value.

The fallback can be also returned if the checked property is falsy (i.e. undefined, null, false, 0, '', etc.) and the fallbackOnFalsy flag is set to .

Arrays could be checked as well by passing the index in the path; ex: getValue(arr, '0.obj')

Install

$ npm i get-value-safely

Import

const get = require('get-value-safely');

Use

// Test object.
const obj = {
  c: {
    a: 2,
    b: {
      c: [1, 2, 3, {a: 15, b: 10}, 15]
    },
    c: undefined,
    d: null
  },
  d: ''
};

### Usage Examples:
get(null, 'c.a', 'fallback');

get(obj, 'c.d', 'fallback');

get(obj, 'c.c', 'fallback', true);

get(obj, 'c.b.c.2');

get(obj, 'c.b.c.3.a');

Package Sidebar

Install

npm i get-value-safely

Weekly Downloads

3

Version

1.0.1

License

ISC

Unpacked Size

5.66 kB

Total Files

4

Last publish

Collaborators

  • vsambor