Created
November 14, 2013 17:12
-
-
Save acstll/7470524 to your computer and use it in GitHub Desktop.
Simple options merger.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better, tested module: https://github.com/nrf110/deepmerge