node-objectify

0.0.3 • Public • Published

Objectify Build Status

This node library allows you to set, get or check for the predicate of a deeply nested objects.

It lets you avoid patterns such as:

if (myObj.options && myObj.options.site && myObj.options.site.url) {
  // do something with myObj.options.site.url
}

And it helps you avoid the popular undefined is not a function error.

Examples

var objectify = require('objectify');
 
var src = {};
 

Check for the existence of a nested attribute:

if (objectify(src).isSet('a.very.deeply.nested.attr')) {
  // true or false
  // do something with the nested attr here
  console.log(src.a.very.deeply.nested.attr);
}
 

Getting a long nested attribute:

 
src = {a: {very: {deeply: {nested: { attr: 1 }}}}};
 
objectify(src).get('a.very.deeply.nested.attr'); // returns 1
 
objectify(src).get('a.non.existant.attr'); // returns undefined
 

Setting a deep nested attribute:

 
objectify(src).set('a.different.nested.attribute', 2); // returns 2
 
console.log(src.a.different.nested.attribute) // 2

Package Sidebar

Install

npm i node-objectify

Weekly Downloads

4

Version

0.0.3

License

MIT

Last publish

Collaborators

  • danielo