merge-jsobjects
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -10,3 +10,3 @@ { | ||
"postAttachCommand": { | ||
"server": "npm start" | ||
"server": "npm test" | ||
}, | ||
@@ -13,0 +13,0 @@ "customizations": { |
13
index.js
@@ -99,9 +99,11 @@ const basicOperationsGenerator = { | ||
const mergedObject = {} | ||
//use set to get unique keys from both objects | ||
//use set to get unique keys from both objects' | ||
const freezedTarget = Object.freeze(target)//freeze target to prevent it from being modified by one of operations and create side effects to other operations | ||
const freezedSource = Object.freeze(source)//freeze source to prevent it from being modified by one of operations and create side effects to other operations | ||
new Set([...(Object.keys(target)), ...(Object.keys(source))]).forEach(propertyName => { | ||
if (propertyName in operations) { | ||
//for operations, pass target[key], source[key] and key to the function | ||
//for operations, pass target[key], source[key] key, target, source to the function | ||
mergedObject[propertyName] = (typeof (operations[propertyName]) === "function") ? | ||
operations[propertyName](target[propertyName], source[propertyName]) : | ||
operationsKeysFunction[propertyName](target[propertyName], source[propertyName], objectsKey) | ||
operations[propertyName](target[propertyName], source[propertyName], objectsKey, freezedTarget, freezedSource) : | ||
operationsKeysFunction[propertyName](target[propertyName], source[propertyName], objectsKey, freezedTarget, freezedSource) | ||
} else { | ||
@@ -127,3 +129,4 @@ //replace only if source[key] is not undefined | ||
* @param {Function||Object} operations used to determine how to merge objects, if operations is a function, it will be called with (target,source) and return the merged object, | ||
* if operations is an object, it will be used as key -> merge function map, if key is found in operations, the merge function will be called with (target,source) and return the merged value of the key of the object. | ||
* if operations is an object, it will be used as key -> merge function map, if key is found in operations, | ||
* the merge function will be called with (targetProperty,sourceProperty,key,objectsKey,freezedTarget,freezedSource) and return the merged value of the key of the object. | ||
* if operations is not provided, the source object will be merged into target object using Object.assign(target,source) | ||
@@ -130,0 +133,0 @@ * built-in operations are: |
{ | ||
"name": "merge-jsobjects", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "merge javascript objects", | ||
@@ -12,4 +12,3 @@ "main": "index.js", | ||
"nodejs", | ||
"microserice", | ||
"fastify" | ||
"merge objects" | ||
], | ||
@@ -25,6 +24,3 @@ "author": "Pornchai Rungsirijaratthong", | ||
"test": "node --test" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^2.0.19" | ||
} | ||
} |
40949
0
10
599