service-core/core/defaults.mjs
Jonatan Nilsson 0948885671
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
More development
2022-01-18 11:48:35 +00:00

25 lines
615 B
JavaScript

// taken from isobject npm library
export function isObject(val) {
return val != null && typeof val === 'object' && Array.isArray(val) === false
}
export function defaults(original, def) {
if (!isObject(original)) throw new Error('defaults called with non-object type')
Object.keys(original).forEach(key => {
if (isObject(original[key]) && def && isObject(def[key])) {
defaults(original[key], def[key])
}
})
if (def) {
Object.keys(def).forEach(function(key) {
if (typeof original[key] === 'undefined') {
original[key] = def[key]
}
})
}
return original
}