putil-merge
Advanced tools
Comparing version 3.12.1 to 3.13.0
@@ -21,5 +21,12 @@ export as namespace mergeLib; | ||
combine?: boolean; | ||
/** | ||
* Copies field descriptors | ||
*/ | ||
descriptor?: boolean; | ||
filter?: FilterCallback; | ||
arrayMerge?: boolean | ArrayMergeCallback; | ||
/** | ||
* Copy fields which values are "undefined" | ||
*/ | ||
copyUndefined?: boolean; | ||
} | ||
@@ -26,0 +33,0 @@ |
@@ -18,12 +18,13 @@ /* putil-merge | ||
* @param {Boolean|Function} [options.arrayMerge] | ||
* @param {Function} [options.copyUndefined] | ||
* @return {Object} | ||
*/ | ||
function merge(target, source, options = {}) { | ||
if (!(isObject(target) || typeof target=== 'function')) | ||
if (!(isObject(target) || typeof target === 'function')) | ||
throw new TypeError('Property "target" requires object or function type'); | ||
if (typeof target=== 'function' && options.clone) | ||
if (typeof target === 'function' && options.clone) | ||
throw new TypeError('Can not clone a function'); | ||
if (!source) | ||
return target; | ||
if (!(isObject(source) || typeof source=== 'function')) | ||
if (!(isObject(source) || typeof source === 'function')) | ||
throw new TypeError('Property "source" requires object or function type'); | ||
@@ -35,2 +36,3 @@ const optionDeep = options.deep; | ||
const optionCombine = options.combine; | ||
const optionCopyUndefined = options.copyUndefined; | ||
const optionArrayMerge = options.arrayMerge; | ||
@@ -56,3 +58,3 @@ | ||
let srcVal = source[key]; | ||
if (srcVal === undefined) | ||
if (srcVal === undefined && !optionCopyUndefined) | ||
continue; | ||
@@ -70,3 +72,4 @@ | ||
if (isPlainObject(srcVal)) { | ||
if (optionDeep === true || (typeof optionDeep === 'function' && optionDeep(srcVal))) { | ||
if (optionDeep === true || | ||
(typeof optionDeep === 'function' && optionDeep(srcVal))) { | ||
if (!isObject(trgVal)) { | ||
@@ -115,6 +118,8 @@ descriptor.value = trgVal = {}; | ||
const isPlainObject = (obj) => { | ||
if (typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]') { | ||
if (typeof obj === 'object' && Object.prototype.toString.call(obj) === | ||
'[object Object]') { | ||
const proto = Object.getPrototypeOf(obj); | ||
if (proto) { | ||
const ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') && proto.constructor; | ||
const ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') && | ||
proto.constructor; | ||
return typeof ctor === 'function' && (ctor instanceof ctor) && | ||
@@ -125,4 +130,4 @@ Function.prototype.toString.call(ctor) === objCtorStr; | ||
return false; | ||
} | ||
}; | ||
module.exports = merge; |
{ | ||
"name": "putil-merge", | ||
"description": "Lightweight solution for merging multiple objects into one. Also it supports deep merge and deep clone", | ||
"version": "3.12.1", | ||
"version": "3.13.0", | ||
"author": "Panates Ltd.", | ||
@@ -22,8 +22,8 @@ "contributors": [ | ||
"devDependencies": { | ||
"@types/mocha": "^10.0.2", | ||
"@types/node": "^20.8.6", | ||
"eslint": "^8.51.0", | ||
"@types/mocha": "^10.0.7", | ||
"@types/node": "^20.14.14", | ||
"eslint": "^8.57.0", | ||
"eslint-config-google": "^0.14.0", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0" | ||
"mocha": "^10.7.0", | ||
"nyc": "^17.0.0" | ||
}, | ||
@@ -30,0 +30,0 @@ "engines": { |
11348
143