31 lines
876 B
JavaScript
Executable file
31 lines
876 B
JavaScript
Executable file
module.exports = merge;
|
|
|
|
/**
|
|
* Augments `target` with properties in `options`. Does not override
|
|
* target's properties if they are defined and matches expected type in
|
|
* options
|
|
*
|
|
* @returns {Object} merged object
|
|
*/
|
|
function merge(target, options) {
|
|
var key;
|
|
if (!target) { target = {}; }
|
|
if (options) {
|
|
for (key in options) {
|
|
if (options.hasOwnProperty(key)) {
|
|
var targetHasIt = target.hasOwnProperty(key),
|
|
optionsValueType = typeof options[key],
|
|
shouldReplace = !targetHasIt || (typeof target[key] !== optionsValueType);
|
|
|
|
if (shouldReplace) {
|
|
target[key] = options[key];
|
|
} else if (optionsValueType === 'object') {
|
|
// go deep, don't care about loops here, we are simple API!:
|
|
target[key] = merge(target[key], options[key]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return target;
|
|
}
|