Skip to content

Commit 44bc803

Browse files
authored
feat: set support symbol (#453)
1 parent b18fd0a commit 44bc803

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/utils/get.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function get(
22
entity: any,
3-
path: (string | number)[] | readonly (string | number)[],
3+
path: (string | number | symbol)[] | readonly (string | number | symbol)[],
44
) {
55
let current = entity;
66

src/utils/set.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import get from './get';
22

3-
export type Path = (string | number)[];
3+
export type Path = (string | number | symbol)[];
44

55
function internalSet<Entity = any, Output = Entity, Value = any>(
66
entity: Entity,
@@ -35,7 +35,7 @@ function internalSet<Entity = any, Output = Entity, Value = any>(
3535

3636
export default function set<Entity = any, Output = Entity, Value = any>(
3737
entity: Entity,
38-
paths: (string | number)[],
38+
paths: Path,
3939
value: Value,
4040
removeIfUndefined: boolean = false,
4141
): Output {
@@ -64,6 +64,8 @@ function createEmpty<T>(source: T) {
6464
return (Array.isArray(source) ? [] : {}) as T;
6565
}
6666

67+
const keys = typeof Reflect === 'undefined' ? Object.keys : Reflect.ownKeys;
68+
6769
/**
6870
* Merge objects which will create
6971
*/
@@ -93,7 +95,7 @@ export function merge<T extends object>(...sources: T[]) {
9395
clone = set(clone, path, createEmpty(value));
9496
}
9597

96-
Object.keys(value).forEach(key => {
98+
keys(value).forEach(key => {
9799
internalMerge([...path, key], loopSet);
98100
});
99101
}

tests/utils.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ describe('utils', () => {
236236
b: obj,
237237
});
238238
});
239+
240+
it('support Symbol', () => {
241+
const symbol = Symbol();
242+
243+
const merged = merge({}, { [symbol]: 1 });
244+
245+
expect(merged).toEqual({
246+
[symbol]: 1,
247+
});
248+
});
239249
});
240250
});
241251

0 commit comments

Comments
 (0)