@@ -209,5 +209,129 @@ describe('fastMerge', () => {
209209 const result = utils . fastMerge < unknown > ( data , testObject ) ;
210210 expect ( result . result ) . toEqual ( testObject ) ;
211211 } ) ;
212+
213+ it ( 'should return the same reference when source values match target' , ( ) => {
214+ const target = { a : 1 , b : 'hello' , c : true } ;
215+ const source = { a : 1 , b : 'hello' } ;
216+ const result = utils . fastMerge ( target , source ) ;
217+ expect ( result . result ) . toBe ( target ) ;
218+ } ) ;
219+
220+ it ( 'should return a new reference when source adds a key' , ( ) => {
221+ const target = { a : 1 } ;
222+ const source = { a : 1 , b : 2 } ;
223+ const result = utils . fastMerge ( target , source ) ;
224+ expect ( result . result ) . not . toBe ( target ) ;
225+ expect ( result . result ) . toEqual ( { a : 1 , b : 2 } ) ;
226+ } ) ;
227+
228+ it ( 'should return a new reference when source changes a value' , ( ) => {
229+ const target = { a : 1 , b : 2 } ;
230+ const source = { b : 3 } ;
231+ const result = utils . fastMerge ( target , source ) ;
232+ expect ( result . result ) . not . toBe ( target ) ;
233+ expect ( result . result ) . toEqual ( { a : 1 , b : 3 } ) ;
234+ } ) ;
235+
236+ it ( 'should preserve nested object references when unchanged' , ( ) => {
237+ const nested = { x : 1 , y : 2 } ;
238+ const target = { a : 'hello' , b : nested } ;
239+ const source = { a : 'hello' } ;
240+ const result = utils . fastMerge < GenericDeepRecord > ( target , source ) ;
241+ expect ( result . result ) . toBe ( target ) ;
242+ expect ( result . result . b ) . toBe ( nested ) ;
243+ } ) ;
244+
245+ it ( 'should preserve unchanged nested references when sibling changes' , ( ) => {
246+ const nested = { x : 1 , y : 2 } ;
247+ const target = { a : nested , b : 'old' } ;
248+ const source = { b : 'new' } ;
249+ const result = utils . fastMerge < GenericDeepRecord > ( target , source ) ;
250+ expect ( result . result ) . not . toBe ( target ) ;
251+ expect ( result . result . a ) . toBe ( nested ) ;
252+ } ) ;
253+
254+ it ( 'should return a new reference when nested object changes' , ( ) => {
255+ const target = { a : { x : 1 , y : 2 } , b : 'hello' } ;
256+ const source = { a : { x : 99 } } ;
257+ const result = utils . fastMerge ( target , source ) ;
258+ expect ( result . result ) . not . toBe ( target ) ;
259+ expect ( result . result . a ) . not . toBe ( target . a ) ;
260+ expect ( result . result . a ) . toEqual ( { x : 99 , y : 2 } ) ;
261+ } ) ;
262+
263+ it ( 'should return a new reference when shouldRemoveNestedNulls removes a key' , ( ) => {
264+ const target = { a : 1 , b : null } ;
265+ const source = { a : 1 } ;
266+ const result = utils . fastMerge ( target , source , { shouldRemoveNestedNulls : true } ) ;
267+ expect ( result . result ) . not . toBe ( target ) ;
268+ expect ( result . result ) . toEqual ( { a : 1 } ) ;
269+ } ) ;
270+
271+ it ( 'should return the same reference when merging with empty source keys' , ( ) => {
272+ const target = { a : 1 , b : 2 } ;
273+ const source = { } ;
274+ const result = utils . fastMerge ( target , source ) ;
275+ expect ( result . result ) . toBe ( target ) ;
276+ } ) ;
277+ } ) ;
278+
279+ describe ( 'removeNestedNullValues' , ( ) => {
280+ it ( 'should return the same reference when no nulls exist' , ( ) => {
281+ const value = { a : 1 , b : 'hello' , c : true } ;
282+ const result = utils . removeNestedNullValues ( value ) ;
283+ expect ( result ) . toBe ( value ) ;
284+ } ) ;
285+
286+ it ( 'should return the same reference for nested objects without nulls' , ( ) => {
287+ const nested = { x : 1 , y : 2 } ;
288+ const value = { a : 'hello' , b : nested } ;
289+ const result = utils . removeNestedNullValues ( value ) ;
290+ expect ( result ) . toBe ( value ) ;
291+ expect ( ( result as Record < string , unknown > ) . b ) . toBe ( nested ) ;
292+ } ) ;
293+
294+ it ( 'should return a new reference when a null property is removed' , ( ) => {
295+ const value = { a : 1 , b : null } ;
296+ const result = utils . removeNestedNullValues ( value ) ;
297+ expect ( result ) . not . toBe ( value ) ;
298+ expect ( result ) . toEqual ( { a : 1 } ) ;
299+ } ) ;
300+
301+ it ( 'should return a new reference when an undefined property is removed' , ( ) => {
302+ const value = { a : 1 , b : undefined } ;
303+ const result = utils . removeNestedNullValues ( value ) ;
304+ expect ( result ) . not . toBe ( value ) ;
305+ expect ( result ) . toEqual ( { a : 1 } ) ;
306+ } ) ;
307+
308+ it ( 'should return a new reference when a deeply nested null is removed' , ( ) => {
309+ const value = { a : { b : { c : null , d : 1 } } } ;
310+ const result = utils . removeNestedNullValues ( value ) ;
311+ expect ( result ) . not . toBe ( value ) ;
312+ expect ( result ) . toEqual ( { a : { b : { d : 1 } } } ) ;
313+ } ) ;
314+
315+ it ( 'should preserve sibling references when a nested null is removed' , ( ) => {
316+ const sibling = { x : 1 } ;
317+ const value = { a : sibling , b : { c : null } } ;
318+ const result = utils . removeNestedNullValues ( value ) ;
319+ expect ( result ) . not . toBe ( value ) ;
320+ expect ( ( result as Record < string , unknown > ) . a ) . toBe ( sibling ) ;
321+ } ) ;
322+
323+ it ( 'should return the same array reference' , ( ) => {
324+ const arr = [ 1 , 2 , 3 ] ;
325+ const result = utils . removeNestedNullValues ( arr ) ;
326+ expect ( result ) . toBe ( arr ) ;
327+ } ) ;
328+
329+ it ( 'should return the same reference for objects containing arrays' , ( ) => {
330+ const arr = [ 'a' , 'b' ] ;
331+ const value = { items : arr , count : 2 } ;
332+ const result = utils . removeNestedNullValues ( value ) ;
333+ expect ( result ) . toBe ( value ) ;
334+ expect ( ( result as Record < string , unknown > ) . items ) . toBe ( arr ) ;
335+ } ) ;
212336 } ) ;
213337} ) ;
0 commit comments