Skip to content

Commit

Permalink
fix for assignRecursively - use deepClone for arrays instead of slice
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdenek Lastuvka authored and mjancarik committed Dec 20, 2023
1 parent 955e3eb commit c0f0359
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/slow-brooms-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ima/helpers": patch
---

fix for assignRecursively - use deepClone for arrays instead of slice, so any modifications in the assignRecursively result newly NOT mutate the other results
9 changes: 9 additions & 0 deletions packages/helpers/src/__tests__/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ describe('helper', () => {
);
expect(target).toStrictEqual({ a: 3, b: { c: 5, d: [5] }, d: 4 });
});

it('should clone array to the target object', () => {
const arrayWithObject = [{ x: 1 }];
let target = {};
helpers.assignRecursively(target, { a: arrayWithObject, d: 4 });

expect(target).toStrictEqual({ a: arrayWithObject, d: 4 });
expect(arrayWithObject[0]).not.toBe(target.a[0]);
});
});

describe('assignRecursivelyWithTracking', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/helpers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function assign(target, source, parentField = null, ignoreMeta = true) {
fieldList.push(fieldPath);

if (value instanceof Array) {
target[field] = value.slice();
target[field] = clone(value);
} else if (
value instanceof Object &&
!(value instanceof Function) &&
Expand Down

0 comments on commit c0f0359

Please sign in to comment.