Skip to content

Commit 5e68ca4

Browse files
committed
fix: set should support number directly
1 parent baade3e commit 5e68ca4

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/utils/set.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ export default function set<Entity = any, Output = Entity, Value = any>(
77
return (value as unknown) as Output;
88
}
99

10-
const clone = ((Array.isArray(entity)
11-
? [...entity]
12-
: { ...entity }) as unknown) as Output;
13-
1410
const [path, ...restPath] = paths;
11+
12+
let clone: Output;
13+
if (!entity && typeof path === 'number') {
14+
clone = ([] as unknown) as Output;
15+
} else if (Array.isArray(entity)) {
16+
clone = ([...entity] as unknown) as Output;
17+
} else {
18+
clone = ({ ...entity } as unknown) as Output;
19+
}
20+
1521
clone[path] = set(clone[path], restPath, value);
1622

1723
return clone;

tests/utils.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('utils', () => {
4646
[[[['light']]]],
4747
]);
4848
expect(set([[[[[0]]]]], [0, 0, 0, 0, 0, 0], 'bamboo')).toEqual([
49-
[[[[{ 0: 'bamboo' }]]]],
49+
[[[[['bamboo']]]]],
5050
]);
5151
});
5252
});

0 commit comments

Comments
 (0)