Skip to content

Commit e9a14d8

Browse files
test(helpers): use early return in decycle
1 parent b665b48 commit e9a14d8

1 file changed

Lines changed: 32 additions & 30 deletions

File tree

__tests__/helpers/decycle.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,25 @@ function deepCopy(
7979
value = replacer(value);
8080
}
8181

82-
if (isPlainObjectOrArray(value)) {
83-
const existingPath = visitedObjects.get(value);
84-
85-
if (existingPath !== undefined) {
86-
return { $ref: existingPath };
87-
}
88-
89-
visitedObjects.set(value, path);
90-
91-
if (Array.isArray(value)) {
92-
const copy: unknown[] = [];
93-
value.forEach((element, index) => {
94-
copy[index] = deepCopy(
95-
element,
96-
`${path}[${index.toString()}]`,
97-
visitedObjects,
98-
replacer,
99-
);
100-
});
101-
return copy;
102-
}
103-
104-
const record = value as Record<string, unknown>;
105-
const copy: DecycledObject = {};
106-
107-
Object.keys(record).forEach((key) => {
108-
copy[key] = deepCopy(
109-
record[key],
110-
`${path}[${JSON.stringify(key)}]`,
82+
if (!isPlainObjectOrArray(value)) {
83+
return value;
84+
}
85+
86+
const existingPath = visitedObjects.get(value);
87+
88+
if (existingPath !== undefined) {
89+
return { $ref: existingPath };
90+
}
91+
92+
visitedObjects.set(value, path);
93+
94+
if (Array.isArray(value)) {
95+
const copy: unknown[] = [];
96+
97+
value.forEach((element, index) => {
98+
copy[index] = deepCopy(
99+
element,
100+
`${path}[${index.toString()}]`,
111101
visitedObjects,
112102
replacer,
113103
);
@@ -116,7 +106,19 @@ function deepCopy(
116106
return copy;
117107
}
118108

119-
return value;
109+
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+
});
120+
121+
return copy;
120122
}
121123

122124
/**

0 commit comments

Comments
 (0)