30 lines
794 B
JavaScript
30 lines
794 B
JavaScript
/* eslint-disable no-restricted-syntax, no-continue */
|
|
|
|
const isPlainObject = require('./is_plain_object');
|
|
|
|
function defaults(deep, target, ...sources) {
|
|
for (const source of sources) {
|
|
if (!isPlainObject(source)) {
|
|
continue;
|
|
}
|
|
for (const [key, value] of Object.entries(source)) {
|
|
/* istanbul ignore if */
|
|
if (key === '__proto__' || key === 'constructor') {
|
|
continue;
|
|
}
|
|
if (typeof target[key] === 'undefined' && typeof value !== 'undefined') {
|
|
target[key] = value;
|
|
}
|
|
|
|
if (deep && isPlainObject(target[key]) && isPlainObject(value)) {
|
|
defaults(true, target[key], value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return target;
|
|
}
|
|
|
|
module.exports = defaults.bind(undefined, false);
|
|
module.exports.deep = defaults.bind(undefined, true);
|