0.1.4 • Published 10 years ago

normalize-defaults v0.1.4

Weekly downloads
6
License
MIT
Repository
github
Last release
10 years ago

normalize-defaults

Normalize missing first-level keys/values in an object with those from defaults.

npm ver travis ci license

Examples

  • Example 1
var normalize = require('normalize-defaults');

var obj = { 
	rhinos: null, 
	wolves: 15,
	tigers: 42
};

var defaults = { 
	rhinos: 8,
	lions: 0,
	wolves: 14
};

obj = normalize(obj, defaults);

console.log(obj); 
// { rhinos: 8, wolves: 15, tigers: 42, lions: 0 }
  • Example 2
var normalize = require('normalize-defaults');

var obj = {};
var defaults = {
    arr: []
};

obj = normalize(obj, defaults);

obj.arr.push(5);

console.log(obj.arr); // [5]
console.log(defaults.arr); // [] - defaults' objects remains unaffected
  • Example 3
var normalize = require('normalize-defaults');

var obj = null;
var defaults = {
    a: 42
};

obj = normalize(obj, defaults);

console.log(obj.a); // 42

Changelog

v0.1.4

  • If the object to normalize is null, use the default object entirely
  • The previous change now requires that the return value be used, since the null first argument cannot be modified.

License

MIT

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago