spyne
Version:
Reactive Real-DOM Framework for Advanced Javascript applications
76 lines (66 loc) • 2.45 kB
JavaScript
function isMergeableObject(val) {
const nonNullObject = val && typeof val === 'object'
return nonNullObject && Object.prototype.toString.call(val) !== '[object RegExp]' && Object.prototype.toString.call(val) !== '[object Date]'
}
function emptyTarget(val) {
return Array.isArray(val) ? [] : {}
}
function cloneIfNecessary(value, optionsArgument) {
const clone = optionsArgument && optionsArgument.clone === true
return (clone && isMergeableObject(value))
? deepMerge(emptyTarget(value),
value, optionsArgument)
: value
}
function defaultArrayMerge(target, source, optionsArgument) {
const destination = target.slice()
source.forEach(function(e, i) {
if (typeof destination[i] === 'undefined') {
destination[i] = cloneIfNecessary(e, optionsArgument)
} else if (isMergeableObject(e)) {
destination[i] = deepMerge(target[i], e, optionsArgument)
} else if (target.indexOf(e) === -1) {
destination.push(cloneIfNecessary(e, optionsArgument))
}
})
return destination
}
function mergeObject(target, source, optionsArgument) {
const destination = {}
if (isMergeableObject(target)) {
Object.keys(target).forEach(function(key) {
destination[key] = cloneIfNecessary(target[key], optionsArgument)
})
}
Object.keys(source).forEach(function(key) {
if (!isMergeableObject(source[key]) || !target[key]) {
destination[key] = cloneIfNecessary(source[key], optionsArgument)
} else {
destination[key] = deepMerge(target[key], source[key], optionsArgument)
}
})
return destination
}
function deepMerge(target, source, optionsArgument) {
const array = Array.isArray(source)
const options = optionsArgument || { arrayMerge: defaultArrayMerge }
const arrayMerge = options.arrayMerge || defaultArrayMerge
if (array) {
return Array.isArray(target)
? arrayMerge(target, source, optionsArgument)
: cloneIfNecessary(source, optionsArgument)
} else {
return mergeObject(target, source, optionsArgument)
}
}
deepMerge.all = function deepmergeAll(array, optionsArgument) {
if (!Array.isArray(array) || array.length < 2) {
throw new Error(
'first argument should be an array with at least two elements')
}
// we are sure there are at least 2 values, so it is safe to have no initial value
return array.reduce(function(prev, next) {
return deepMerge(prev, next, optionsArgument)
})
}
export { deepMerge }