@@ -107,25 +107,21 @@ const JSONPatcherProxy = (function() {
107107 } ) ;
108108 newValue = instance . _proxifyObjectTreeRecursively ( target , newValue , key ) ;
109109 }
110+ // let's start with this operation, and may or may not update it later
111+ const operation = {
112+ op : 'remove' ,
113+ path : destinationPropKey
114+ } ;
110115 if ( typeof newValue == 'undefined' ) {
111- let reflectionResult ;
112- let operation ;
113- if ( target . hasOwnProperty ( key ) ) {
116+ // applying De Morgan's laws would be a tad faster, but less readable
117+ if ( ! Array . isArray ( target ) && ! target . hasOwnProperty ( key ) ) {
118+ // `undefined` is being set to an already undefined value, keep silent
119+ return Reflect . set ( target , key , newValue ) ;
120+ } else {
114121 // when array element is set to `undefined`, should generate replace to `null`
115122 if ( Array . isArray ( target ) ) {
116- reflectionResult = Reflect . set ( target , key , newValue ) ;
117- //undefined array elements are JSON.stringified to `null`
118- operation = {
119- op : 'replace' ,
120- path : destinationPropKey ,
121- value : null
122- } ;
123- } else {
124- reflectionResult = Reflect . set ( target , key , newValue ) ;
125- operation = {
126- op : 'remove' ,
127- path : destinationPropKey
128- } ;
123+ // undefined array elements are JSON.stringified to `null`
124+ ( operation . op = 'replace' ) , ( operation . value = null ) ;
129125 }
130126 const oldValue = instance . proxifiedObjectsMap . get ( target [ key ] ) ;
131127 // was the deleted a proxified object?
@@ -134,59 +130,23 @@ const JSONPatcherProxy = (function() {
134130 instance . disableTrapsForProxy ( oldValue ) ;
135131 instance . proxifiedObjectsMap . delete ( oldValue ) ;
136132 }
137- instance . defaultCallback ( operation ) ;
138- return reflectionResult ;
139- } else if ( ! Array . isArray ( target ) ) {
133+ }
134+ } else {
135+ if ( Array . isArray ( target ) && ! Number . isInteger ( + key . toString ( ) ) ) {
136+ /* array props (as opposed to indices) don't emit any patches, to avoid needless `length` patches */
140137 return Reflect . set ( target , key , newValue ) ;
141138 }
142- }
143- /* array props don't emit any patches, to avoid needless `length` patches */
144- if ( Array . isArray ( target ) && ! Number . isInteger ( + key . toString ( ) ) ) {
145- return Reflect . set ( target , key , newValue ) ;
146- }
147- let reflectionResult ;
148- let operation ;
149- if ( target . hasOwnProperty ( key ) ) {
150- if ( typeof target [ key ] == 'undefined' ) {
151- if ( Array . isArray ( target ) ) {
152- reflectionResult = Reflect . set ( target , key , newValue ) ;
153- operation = {
154- op : 'replace' ,
155- path : destinationPropKey ,
156- value : newValue
157- } ;
158- } else {
159- reflectionResult = Reflect . set ( target , key , newValue ) ;
160- operation = {
161- op : 'add' ,
162- path : destinationPropKey ,
163- value : newValue
164- } ;
139+ operation . op = 'add' ;
140+ if ( target . hasOwnProperty ( key ) ) {
141+ if ( typeof target [ key ] !== 'undefined' || Array . isArray ( target ) ) {
142+ operation . op = 'replace' ; // setting `undefined` array elements is a `replace` op
165143 }
166- instance . defaultCallback ( operation ) ;
167- return reflectionResult ;
168- } else {
169- reflectionResult = Reflect . set ( target , key , newValue ) ;
170- instance . defaultCallback (
171- /* operation = */ {
172- op : 'replace' ,
173- path : destinationPropKey ,
174- value : newValue
175- }
176- ) ;
177- return reflectionResult ;
178144 }
179- } else {
180- reflectionResult = Reflect . set ( target , key , newValue ) ;
181- instance . defaultCallback (
182- /* operation = */ {
183- op : 'add' ,
184- path : destinationPropKey ,
185- value : newValue
186- }
187- ) ;
188- return reflectionResult ;
145+ operation . value = newValue ;
189146 }
147+ const reflectionResult = Reflect . set ( target , key , newValue ) ;
148+ instance . defaultCallback ( operation ) ;
149+ return reflectionResult ;
190150 }
191151 /**
192152 * A callback to be used as th proxy delete trap callback.
0 commit comments