Comparing version 0.18.0 to 0.19.0
@@ -5,2 +5,4 @@ /** | ||
var isObject = require('./is-object'); | ||
var isBoolean = require('./is-boolean'); | ||
var exists = require('./exists'); | ||
@@ -17,3 +19,3 @@ | ||
function defaults (target, source) { | ||
function defaults (target, source, deep) { | ||
if (arguments.length === 1) { | ||
@@ -24,11 +26,26 @@ source = target; | ||
}; | ||
} else if (isBoolean(source)) { | ||
deep = source; | ||
source = target; | ||
return function (target) { | ||
return defaults(target, source, deep); | ||
}; | ||
} | ||
target = target || {}; | ||
deep = deep || false; | ||
if (!source) { | ||
return target; | ||
} | ||
return reduceObject(target, source, deep); | ||
} | ||
function reduceObject (target, source, deep) { | ||
return Object.keys(source).reduce(function (target, key) { | ||
if (isObject(target[key]) && isObject(source[key]) && deep) { | ||
reduceObject(target[key], source[key]); | ||
return target; | ||
} | ||
target[key] = exists(target[key]) ? target[key] : source[key]; | ||
return target; | ||
}, target); | ||
} | ||
} |
{ | ||
"name": "101", | ||
"version": "0.18.0", | ||
"version": "0.19.0", | ||
"description": "common javascript utils that can be required selectively that assume es5+", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "lab -c -t 100 -a code test", | ||
"test-watch": "nodemon --exec lab -c test" | ||
"test": "lab -c -t 100 -a code", | ||
"test-watch": "nodemon --exec lab -c" | ||
}, | ||
@@ -10,0 +10,0 @@ "repository": { |
@@ -166,3 +166,3 @@ ![101](http://i.imgur.com/MFrmMt6.png) | ||
Fill non-existing object values with defaults. Use it to set defaults on options. | ||
Fill non-existing object values with defaults. Use it to set defaults on options. Works with supplying default values in sub-objects as well. | ||
Supports partial functionality (great with array functions). | ||
@@ -178,2 +178,16 @@ | ||
[opts].map(defaults(defs)); // [ { foo: 0, bar: 1, qux: 2 } ] | ||
var opts = { | ||
foo: { | ||
one: 1, | ||
two: 2 | ||
} | ||
}; | ||
var defs = { | ||
foo: { | ||
two: 20, | ||
three: 30 | ||
} | ||
}; | ||
defaults(opts, defs); // { foo: { one: 1, two: 2, three: 30 } } | ||
``` | ||
@@ -180,0 +194,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52614
1106
756