Skip to content

Commit

Permalink
Better Array log
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Jun 7, 2018
1 parent e138344 commit 1403456
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
32 changes: 22 additions & 10 deletions console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,37 @@ function stringify(ctx: ConsoleContext, value: any): string {
if (value === null) {
return "null";
}

if (ctx.has(value)) {
return "[Circular]";
}

ctx.add(value);
const valStrings = [];

const keys = Object.keys(value);
const keyStrings = [];
for (const key of keys) {
keyStrings.push(`${key}: ${stringify(ctx, value[key])}`);
}
if (Array.isArray(value)) {
for (const el of value) {
valStrings.push(stringify(ctx, el));
}

ctx.delete(value);

if (valStrings.length === 0) {
return "[]";
}
return `[${valStrings.join(", ")}]`;
} else {
for (const key of Object.keys(value)) {
valStrings.push(`${key}: ${stringify(ctx, value[key])}`);
}

ctx.delete(value);
ctx.delete(value);

if (keyStrings.length === 0) {
return "{}";
if (valStrings.length === 0) {
return "{}";
}
return `{ ${valStrings.join(", ")} }`;
}

return `{ ${keyStrings.join(", ")} }`;
default:
return "[Not Implemented]";
}
Expand Down
1 change: 1 addition & 0 deletions tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ test(function tests_console_stringify_circular() {
nu: null,
nested: nestedObj,
emptyObj: {},
arr: [1, "s", false, null, nestedObj],
};

nestedObj.o = circularObj;
Expand Down

0 comments on commit 1403456

Please sign in to comment.