Skip to content

Commit 11061e1

Browse files
fix: add a circular reference check for getPaths
Co-Authored-By: Stewart McGown <stewart@mcgown.dev>
1 parent b2f4449 commit 11061e1

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/utils/getParticipantPaths.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@ import { Paths } from '../types/paths';
44
/**
55
* Returns all property paths for an object.
66
*/
7-
const getPaths = (o: Record<any, any>, currentPath = ''): string[] => {
8-
if (typeof o !== 'object' || o === null) {
7+
const getPaths = (
8+
o: Record<any, any>,
9+
currentPath = '',
10+
visited = new Set()
11+
): string[] => {
12+
if (typeof o !== 'object' || o === null || visited.has(o)) {
913
return [currentPath];
1014
}
1115

16+
visited.add(o);
17+
1218
const paths = [];
1319
for (const key in o) {
1420
if (Object.prototype.hasOwnProperty.call(o, key)) {
1521
const newPath = currentPath ? `${currentPath}.${key}` : key;
16-
paths.push(newPath, ...getPaths(o[key], newPath));
22+
paths.push(newPath, ...getPaths(o[key], newPath, visited));
1723
}
1824
}
1925

26+
visited.delete(o);
27+
2028
return paths;
2129
};
2230

0 commit comments

Comments
 (0)