Skip to content

Commit 73444b3

Browse files
committed
fix: preserve array reference stability in removeNestedNullValues
1 parent c67da03 commit 73444b3

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

lib/utils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,10 @@ function isMergeableObject<TObject extends Record<string, unknown>>(value: unkno
186186

187187
/** Deep removes the nested null values from the given value. Returns the original reference if no nulls were found. */
188188
function removeNestedNullValues<TValue extends OnyxInput<OnyxKey> | null>(value: TValue): TValue {
189-
if (value === null || value === undefined || typeof value !== 'object') {
189+
if (value === null || value === undefined || typeof value !== 'object' || Array.isArray(value)) {
190190
return value;
191191
}
192192

193-
if (Array.isArray(value)) {
194-
return [...value] as TValue;
195-
}
196-
197193
let hasChanged = false;
198194
const result: Record<string, unknown> = {};
199195

tests/unit/utilsTest.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,19 @@ describe('fastMerge', () => {
319319
expect(result).not.toBe(value);
320320
expect((result as Record<string, unknown>).a).toBe(sibling);
321321
});
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+
});
322336
});
323337
});

0 commit comments

Comments
 (0)