Skip to content

Commit 115a2db

Browse files
Merge pull request #652 from callstack-internal/feature/improve-removeNestedNullValues
Refactor removeNestedNullValues to be performant
2 parents 5861455 + bd1cf4b commit 115a2db

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

lib/utils.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,34 @@ function fastMerge<TValue>(target: TValue, source: TValue, shouldRemoveNestedNul
106106

107107
/** Deep removes the nested null values from the given value. */
108108
function removeNestedNullValues<TValue extends OnyxInput<OnyxKey> | null>(value: TValue): TValue {
109-
if (typeof value === 'object' && !Array.isArray(value)) {
110-
const objectValue = value as Record<string, unknown>;
111-
return fastMerge(objectValue, objectValue) as TValue;
109+
if (value === null || value === undefined) {
110+
return value;
112111
}
113112

114-
return value;
113+
if (typeof value !== 'object' || Array.isArray(value)) {
114+
return value;
115+
}
116+
117+
const result: Record<string, unknown> = {};
118+
119+
// eslint-disable-next-line no-restricted-syntax, guard-for-in
120+
for (const key in value) {
121+
const propertyValue = value[key];
122+
123+
if (propertyValue === null || propertyValue === undefined) {
124+
// eslint-disable-next-line no-continue
125+
continue;
126+
}
127+
128+
if (typeof propertyValue === 'object' && !Array.isArray(propertyValue)) {
129+
const valueWithoutNestedNulls = removeNestedNullValues(propertyValue);
130+
result[key] = valueWithoutNestedNulls;
131+
} else {
132+
result[key] = propertyValue;
133+
}
134+
}
135+
136+
return result as TValue;
115137
}
116138

117139
/** Formats the action name by uppercasing and adding the key if provided. */

0 commit comments

Comments
 (0)