service-core/core/defaults.mjs

26 lines
615 B
JavaScript
Raw Normal View History

2022-01-18 11:48:35 +00:00
// 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
}