Skip to content

Commit 3854b39

Browse files
committed
tests: added circular support to browser-safe inspect
1 parent 831b21d commit 3854b39

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src.ts/_tests/utils.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ export const stats = new Stats(_guard);
135135
*/
136136

137137

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

143143
switch (typeof(value)) {
@@ -152,10 +152,24 @@ export function inspect(value: any): string {
152152
case "object":
153153
if (value == null) { return "null"; }
154154
return "{ " + Object.keys(value).map((key) => {
155-
return `${ key }=${ inspect(value[key]) }`;
155+
return `${ key }=${ _inspect(value[key], done) }`;
156156
}).join(", ") + " }";
157157
}
158158

159159
return `[ unknown type: ${ value } ]`
160160
}
161161

162+
function _inspect(value: any, done: Set<any>): string {
163+
if (done.has(value)) { return "[ Circular ]"; }
164+
165+
done.add(value);
166+
const result = _inspectString(value, done);
167+
done.delete(value);
168+
169+
return result;
170+
}
171+
172+
export function inspect(value: any): string {
173+
return _inspect(value, new Set());
174+
}
175+

0 commit comments

Comments
 (0)