@@ -30,28 +30,6 @@ const getNthKey = (
3030 return indexed [ n ] ;
3131} ;
3232
33- const rememberIndexedCollectionKeys = (
34- before : unknown ,
35- after : unknown ,
36- context : AccessDeepContext
37- ) => {
38- if ( ! isArray ( before ) ) {
39- return ;
40- }
41-
42- if ( isSet ( after ) ) {
43- context . set ( after , before . slice ( ) ) ;
44- return ;
45- }
46-
47- if ( isMap ( after ) ) {
48- context . set (
49- after ,
50- before . map ( entry => ( isArray ( entry ) ? entry [ 0 ] : undefined ) )
51- ) ;
52- }
53- } ;
54-
5533function validatePath ( path : ( string | number ) [ ] ) {
5634 if ( includes ( path , '__proto__' ) ) {
5735 throw new Error ( '__proto__ is not allowed as a property' ) ;
@@ -99,15 +77,13 @@ export const getDeep = (
9977export const setDeep = (
10078 object : any ,
10179 path : ( string | number ) [ ] ,
102- mapper : ( v : any ) => any ,
80+ mapper : ( v : any , context : AccessDeepContext ) => any ,
10381 context : AccessDeepContext
10482) : any => {
10583 validatePath ( path ) ;
10684
10785 if ( path . length === 0 ) {
108- const mapped = mapper ( object ) ;
109- rememberIndexedCollectionKeys ( object , mapped , context ) ;
110- return mapped ;
86+ return mapper ( object , context ) ;
11187 }
11288
11389 let parent = object ;
@@ -148,14 +124,12 @@ export const setDeep = (
148124
149125 if ( isArray ( parent ) ) {
150126 const oldValue = parent [ + lastKey ] ;
151- const newValue = mapper ( oldValue ) ;
127+ const newValue = mapper ( oldValue , context ) ;
152128 parent [ + lastKey ] = newValue ;
153- rememberIndexedCollectionKeys ( oldValue , newValue , context ) ;
154129 } else if ( isPlainObject ( parent ) ) {
155130 const oldValue = parent [ lastKey ] ;
156- const newValue = mapper ( oldValue ) ;
131+ const newValue = mapper ( oldValue , context ) ;
157132 parent [ lastKey ] = newValue ;
158- rememberIndexedCollectionKeys ( oldValue , newValue , context ) ;
159133 }
160134
161135 if ( isSet ( parent ) ) {
@@ -166,8 +140,7 @@ export const setDeep = (
166140 }
167141 const oldValue = indexed [ row ] ;
168142
169- const newValue = mapper ( oldValue ) ;
170- rememberIndexedCollectionKeys ( oldValue , newValue , context ) ;
143+ const newValue = mapper ( oldValue , context ) ;
171144
172145 if ( oldValue !== newValue ) {
173146 if ( row < parent . size ) {
@@ -191,8 +164,7 @@ export const setDeep = (
191164
192165 switch ( type ) {
193166 case 'key' : {
194- const newKey = mapper ( keyToRow ) ;
195- rememberIndexedCollectionKeys ( keyToRow , newKey , context ) ;
167+ const newKey = mapper ( keyToRow , context ) ;
196168 parent . set ( newKey , parent . get ( keyToRow ) ) ;
197169
198170 if ( ! isVirtualRow && newKey !== keyToRow ) {
@@ -205,8 +177,7 @@ export const setDeep = (
205177
206178 case 'value' : {
207179 const oldValue = parent . get ( keyToRow ) ;
208- const newValue = mapper ( oldValue ) ;
209- rememberIndexedCollectionKeys ( oldValue , newValue , context ) ;
180+ const newValue = mapper ( oldValue , context ) ;
210181 parent . set ( keyToRow , newValue ) ;
211182 break ;
212183 }
0 commit comments