Skip to content

Commit

Permalink
fix: list override (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed May 26, 2023
1 parent cfb8f48 commit a01815e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/utils/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ export function merge<T extends object>(...sources: T[]) {
function internalMerge(path: Path) {
const value = get(src, path);

if (isObject(value) || Array.isArray(value)) {
const isArr = Array.isArray(value);

if (isArr || isObject(value)) {
// Only add not loop obj
if (!loopSet.has(value)) {
loopSet.add(value);

// Init container if not exist
const originValue = get(clone, path);
if (!originValue || typeof originValue !== 'object') {

if (isArr) {
// Array will always be override
clone = set(clone, path, []);
} else if (!originValue || typeof originValue !== 'object') {
// Init container if not exist
clone = set(clone, path, createEmpty(value));
}

Expand Down
21 changes: 13 additions & 8 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,39 @@ describe('utils', () => {
});
});

it('array', () => {
it('array is replacement', () => {
const merged = merge([], [{ a: 1 }], [{ b: 2 }]);

expect(merged).toEqual([
{
a: 1,
b: 2,
},
]);
});

it('array is replacement - sub field', () => {
const merged = merge({ a: 1, users: [1, 2] }, { users: [] });

expect(merged).toEqual({ a: 1, users: [] });
});

it('not cover', () => {
const merged = merge(
[],
[{ a: { e: 8 }, b: 2 }],
[{ a: { f: 9 }, c: 3 }],
{},
{ _: { a: { e: 8 }, b: 2 } },
{ _: { a: { f: 9 }, c: 3 } },
);

expect(merged).toEqual([
{
expect(merged).toEqual({
_: {
a: {
e: 8,
f: 9,
},
b: 2,
c: 3,
},
]);
});
});

it('DayObject', () => {
Expand Down

1 comment on commit a01815e

@vercel
Copy link

@vercel vercel bot commented on a01815e May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

util – ./

util-git-master-react-component.vercel.app
util-react-component.vercel.app
util.vercel.app

Please sign in to comment.