Skip to content

Commit

Permalink
fix: set should support number directly
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Dec 27, 2019
1 parent baade3e commit 5e68ca4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/utils/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ export default function set<Entity = any, Output = Entity, Value = any>(
return (value as unknown) as Output;
}

const clone = ((Array.isArray(entity)
? [...entity]
: { ...entity }) as unknown) as Output;

const [path, ...restPath] = paths;

let clone: Output;
if (!entity && typeof path === 'number') {
clone = ([] as unknown) as Output;
} else if (Array.isArray(entity)) {
clone = ([...entity] as unknown) as Output;
} else {
clone = ({ ...entity } as unknown) as Output;
}

clone[path] = set(clone[path], restPath, value);

return clone;
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('utils', () => {
[[[['light']]]],
]);
expect(set([[[[[0]]]]], [0, 0, 0, 0, 0, 0], 'bamboo')).toEqual([
[[[[{ 0: 'bamboo' }]]]],
[[[[['bamboo']]]]],
]);
});
});

0 comments on commit 5e68ca4

Please sign in to comment.