From 3854b39763a2a57f363bf9bf925adcc27002bde1 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sat, 4 Jan 2025 16:04:30 -0500 Subject: [PATCH] tests: added circular support to browser-safe inspect --- src.ts/_tests/utils.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src.ts/_tests/utils.ts b/src.ts/_tests/utils.ts index dc4d7cd54..08aa8b767 100644 --- a/src.ts/_tests/utils.ts +++ b/src.ts/_tests/utils.ts @@ -135,9 +135,9 @@ export const stats = new Stats(_guard); */ -export function inspect(value: any): string { +function _inspectString(value: any, done: Set): string { if (Array.isArray(value)) { - return "[" + value.map((v) => inspect(v)).join(", ") + "]"; + return "[" + value.map((v) => _inspect(v, done)).join(", ") + "]"; } switch (typeof(value)) { @@ -152,10 +152,24 @@ export function inspect(value: any): string { case "object": if (value == null) { return "null"; } return "{ " + Object.keys(value).map((key) => { - return `${ key }=${ inspect(value[key]) }`; + return `${ key }=${ _inspect(value[key], done) }`; }).join(", ") + " }"; } return `[ unknown type: ${ value } ]` } +function _inspect(value: any, done: Set): string { + if (done.has(value)) { return "[ Circular ]"; } + + done.add(value); + const result = _inspectString(value, done); + done.delete(value); + + return result; +} + +export function inspect(value: any): string { + return _inspect(value, new Set()); +} +