Skip to content

Commit de7a775

Browse files
test(helpers): refactor to map and reduce in decycle
1 parent e9a14d8 commit de7a775

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

__tests__/helpers/decycle.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,25 @@ function deepCopy(
9292
visitedObjects.set(value, path);
9393

9494
if (Array.isArray(value)) {
95-
const copy: unknown[] = [];
96-
97-
value.forEach((element, index) => {
98-
copy[index] = deepCopy(
95+
return value.map((element, index) =>
96+
deepCopy(
9997
element,
10098
`${path}[${index.toString()}]`,
10199
visitedObjects,
102100
replacer,
103-
);
104-
});
105-
106-
return copy;
101+
),
102+
);
107103
}
108104

109105
const record = value as Record<string, unknown>;
110-
const copy: DecycledObject = {};
111-
112-
Object.keys(record).forEach((key) => {
113-
copy[key] = deepCopy(
114-
record[key],
115-
`${path}[${JSON.stringify(key)}]`,
116-
visitedObjects,
117-
replacer,
118-
);
119-
});
120106

121-
return copy;
107+
return Object.keys(record).reduce<DecycledObject>((copy, key) => {
108+
const value = record[key];
109+
const newPath = `${path}[${JSON.stringify(key)}]`;
110+
111+
copy[key] = deepCopy(value, newPath, visitedObjects, replacer);
112+
return copy;
113+
}, {});
122114
}
123115

124116
/**

0 commit comments

Comments
 (0)