Skip to content

Commit bac1c4c

Browse files
committed
Add some comments explaining what we do here
1 parent 3223048 commit bac1c4c

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

packages/utils/src/object.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,14 @@ export function dropUndefinedKeys<T>(inputValue: T): T {
220220

221221
function _dropUndefinedKeys<T>(inputValue: T, memoizationMap: Map<unknown, unknown>): T {
222222
if (isPlainObject(inputValue)) {
223+
// If this node has already been visited due to a circular reference, return the object it was mapped to in the new object
223224
const memoVal = memoizationMap.get(inputValue);
224225
if (memoVal !== undefined) {
225226
return memoVal as T;
226227
}
227228

228229
const returnValue: { [key: string]: any } = {};
230+
// Store the mapping of this value in case we visit it again, in case of circular data
229231
memoizationMap.set(inputValue, returnValue);
230232

231233
for (const key of Object.keys(inputValue)) {
@@ -238,12 +240,14 @@ function _dropUndefinedKeys<T>(inputValue: T, memoizationMap: Map<unknown, unkno
238240
}
239241

240242
if (Array.isArray(inputValue)) {
243+
// If this node has already been visited due to a circular reference, return the array it was mapped to in the new object
241244
const memoVal = memoizationMap.get(inputValue);
242245
if (memoVal !== undefined) {
243246
return memoVal as T;
244247
}
245248

246249
const returnValue: unknown[] = [];
250+
// Store the mapping of this value in case we visit it again, in case of circular data
247251
memoizationMap.set(inputValue, returnValue);
248252

249253
inputValue.forEach((item: unknown) => {

0 commit comments

Comments
 (0)