-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: decycle potentially cyclic structures before serializing (#634)
Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
- Loading branch information
1 parent
5e02c18
commit 99f7c84
Showing
9 changed files
with
122 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/autocomplete-shared/src/__tests__/decycle.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { decycle } from '../decycle'; | ||
|
||
describe('decycle', () => { | ||
if (__DEV__) { | ||
test('leaves objects with no circular references intact', () => { | ||
const ref = { a: 1 }; | ||
const obj = { | ||
a: 'b', | ||
c: { d: [ref, () => {}, null, false, undefined] }, | ||
}; | ||
|
||
expect(decycle(obj)).toEqual({ | ||
a: 'b', | ||
c: { d: [{ a: 1 }, expect.any(Function), null, false, undefined] }, | ||
}); | ||
}); | ||
test('replaces circular references', () => { | ||
const circular = { a: 'b', self: null }; | ||
circular.self = circular; | ||
|
||
expect(decycle(circular)).toEqual({ a: 'b', self: '[Circular]' }); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Decycles objects with circular references. | ||
* This is used to print cyclic structures in development environment only. | ||
*/ | ||
export function decycle(obj: any, seen = new Set()) { | ||
if (!__DEV__ || !obj || typeof obj !== 'object') { | ||
return obj; | ||
} | ||
|
||
if (seen.has(obj)) { | ||
return '[Circular]'; | ||
} | ||
|
||
const newSeen = seen.add(obj); | ||
|
||
if (Array.isArray(obj)) { | ||
return obj.map((x) => decycle(x, newSeen)); | ||
} | ||
|
||
return Object.fromEntries( | ||
Object.entries(obj).map(([key, value]) => [key, decycle(value, newSeen)]) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters