Skip to content

Commit

Permalink
don't deep assign arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
KingSora committed Aug 27, 2023
1 parent 5c6af50 commit 3771592
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/overlayscrollbars/src/support/utils/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const assignDeep: AssignDeep = <T, U, V, W, X, Y, Z>(
const copyIsArray = isArray(copy);

// Recurse if we're merging plain objects or arrays
if (copy && (isPlainObject(copy) || copyIsArray)) {
if (copy && isPlainObject(copy)) {
const src = target[key];
let clone: any = src;

Expand All @@ -82,7 +82,7 @@ export const assignDeep: AssignDeep = <T, U, V, W, X, Y, Z>(
// Never move original objects, clone them
target[key] = assignDeep(clone, copy) as any;
} else {
target[key] = copy;
target[key] = copyIsArray ? copy.slice() : copy;
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ describe('object utilities', () => {
expect(deep1.foo2).toBe(document);

const arr = [1, 2, 3];
const o = {};
const nestedArray: NestedArray = { arr };
const nestedObj = { o };

expect(assignDeep({}, nestedArray).arr).not.toBe(arr);
expect(assignDeep({}, nestedObj).o).not.toBe(o);
expect(Array.isArray(assignDeep({ arr: {} }, nestedArray).arr)).toBeTruthy();
expect(Array.isArray(assignDeep({ arr: {} }, nestedArray).arr)).toBeTruthy();
expect(isPlainObject(assignDeep({ arr }, { arr: {} }).arr)).toBeTruthy();
Expand Down Expand Up @@ -163,6 +166,8 @@ describe('object utilities', () => {
foo: 1,
deep: { foo: null, text: '' },
});

console.log(assignDeep({ arr: ['a', 'b', 'c'] }, { arr: [] }));
});
});

Expand Down

0 comments on commit 3771592

Please sign in to comment.