11import get from './get' ;
22
3- export type Path = ( string | number ) [ ] ;
3+ export type Path = ( string | number | symbol ) [ ] ;
44
55function internalSet < Entity = any , Output = Entity , Value = any > (
66 entity : Entity ,
@@ -35,7 +35,7 @@ function internalSet<Entity = any, Output = Entity, Value = any>(
3535
3636export 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,16 +64,18 @@ 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 */
7072export function merge < T extends object > ( ...sources : T [ ] ) {
7173 let clone = createEmpty ( sources [ 0 ] ) ;
7274
73- const loopSet = new Set < object > ( ) ;
74-
7575 sources . forEach ( src => {
76- function internalMerge ( path : Path ) {
76+ function internalMerge ( path : Path , parentLoopSet ?: Set < object > ) {
77+ const loopSet = new Set ( parentLoopSet ) ;
78+
7779 const value = get ( src , path ) ;
7880
7981 const isArr = Array . isArray ( value ) ;
@@ -93,8 +95,8 @@ export function merge<T extends object>(...sources: T[]) {
9395 clone = set ( clone , path , createEmpty ( value ) ) ;
9496 }
9597
96- Object . keys ( value ) . forEach ( key => {
97- internalMerge ( [ ...path , key ] ) ;
98+ keys ( value ) . forEach ( key => {
99+ internalMerge ( [ ...path , key ] , loopSet ) ;
98100 } ) ;
99101 }
100102 } else {
0 commit comments