Skip to content

Instantly share code, notes, and snippets.

@acstll
Created November 14, 2013 17:12
Show Gist options
  • Save acstll/7470524 to your computer and use it in GitHub Desktop.
Save acstll/7470524 to your computer and use it in GitHub Desktop.
Simple options merger.
module.exports = defaults;
function defaults (target, source) {
target = target || {};
source = source || {};
var keys = Object.keys(source) || [];
if (!keys.length) return target;
if (Array.isArray(source)) {
target = source;
return target;
}
keys.forEach(function (key) {
if (target[key] && typeof source[key] === 'object' && source[key] !== null) {
target[key] = defaults(target[key], source[key]);
return;
}
target[key] = source[key];
});
return target;
}
@acstll
Copy link
Author

acstll commented Nov 14, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment