nested-or-nothing

0.1.1 • Public • Published

Nested or Nothing

A utility function that helps you find deeply nested values in your objects when you don't know for sure if they are present. If any intermediate key is missing, the function returns undefined.

Installation

npm install nested-or-nothing

Usage

var non = require('nested-or-nothing');

var object = {
  a: {
    b: {
      c: 1
    }
  }
};

non(object, 'a', 'b');      // returns {c: 1}
non(object, 'a', 'b', 'c'); // returns 1

non(object, 'a', 'd', 'c'); // returns undefined

Caution

Since the function uses strings for keys, your code may break when using static code optimizers like Google Closure Compiler in advanced mode. In such cases it is recommended to use safer patterns:

((object.a || {}).b || {}).c;
// same as:
non(object, 'a', 'b', 'c');

Readme

Keywords

Package Sidebar

Install

npm i nested-or-nothing

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • artemshitov