Skip to content

Commit

Permalink
tests: added circular support to browser-safe inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 4, 2025
1 parent 831b21d commit 3854b39
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src.ts/_tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ export const stats = new Stats(_guard);
*/


export function inspect(value: any): string {
function _inspectString(value: any, done: Set<any>): string {
if (Array.isArray(value)) {
return "[" + value.map((v) => inspect(v)).join(", ") + "]";
return "[" + value.map((v) => _inspect(v, done)).join(", ") + "]";
}

switch (typeof(value)) {
Expand All @@ -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<any>): 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());
}

0 comments on commit 3854b39

Please sign in to comment.