Skip to content

Commit ae92358

Browse files
committed
fix clone of array
1 parent 37575ca commit ae92358

File tree

4 files changed

+293
-124
lines changed

4 files changed

+293
-124
lines changed

lib/merge-deeply.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "merge-deeply",
3-
"version": "2.1.1",
3+
"version": "3.0.0",
44
"description": "This library allows you to Merge and Clone Object with prototypes",
55
"keywords": [
66
"merge",

src/merge-deeply.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import hasOwnPrototype from './has-own-prototype';
33
function mergeDeeplyInernally(target, source, opts) {
44
const isObject = (obj) => obj && typeof obj === 'object' && !Array.isArray(obj);
55
const isConcatArray = opts && opts.concatArray;
6+
const isCloneMode = opts.isCloneMode;
67

78
// assignToObject:
89
// Overwrites the properties of the specified existing object
@@ -32,7 +33,16 @@ function mergeDeeplyInernally(target, source, opts) {
3233
// OLD: else if (isObject(sourceValue) && target.hasOwnProperty(sourceKey)) {
3334
result[sourceKey] = mergeDeeplyInernally(targetValue, sourceValue, opts);
3435
} else {
35-
Object.assign(result, { [sourceKey]: sourceValue });
36+
let _sourceValue = sourceValue;
37+
if (isCloneMode && Array.isArray(sourceValue)) {
38+
const _clonedArray = [];
39+
for (const itemInSourceValue of sourceValue) {
40+
const copy = JSON.parse(JSON.stringify(itemInSourceValue));
41+
_clonedArray.push(copy);
42+
}
43+
_sourceValue = _clonedArray;
44+
}
45+
Object.assign(result, { [sourceKey]: _sourceValue });
3646
}
3747
}
3848
}
@@ -94,6 +104,7 @@ export default function mergeDeeply(opts) {
94104
{
95105
assignToObject: prototypeClonedObject,
96106
concatArray: (opts && opts.concatArray),
107+
isCloneMode,
97108
});
98109
resultObject = prototypeClonedObject;
99110
} else {

0 commit comments

Comments
 (0)