Skip to content

Commit

Permalink
feat: implement console.groupCollapsed
Browse files Browse the repository at this point in the history
This implementation of groupCollapsed is intentionally different
from the spec defined by whatwg. See the conversation in denoland#1355
and denoland#1363.
  • Loading branch information
kt3k committed Jan 3, 2019
1 parent 889e12f commit 86b8617
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 46 deletions.
69 changes: 59 additions & 10 deletions js/console.ts
Expand Up @@ -7,6 +7,8 @@ type ConsoleOptions = Partial<{
showHidden: boolean;
depth: number;
colors: boolean;
indentLevel: number;
collapsedAt: number | null;
}>;

// Default depth of logging nested objects
Expand Down Expand Up @@ -322,13 +324,26 @@ function stringifyWithQuotes(
}
}

// Returns true when the console is collapsed.
function isCollapsed(
collapsedAt: number | null | undefined,
indentLevel: number | null | undefined
) {
if (collapsedAt == null || indentLevel == null) {
return false;
}

return collapsedAt <= indentLevel;
}

/** TODO Do not expose this from "deno" namespace. */
export function stringifyArgs(
// tslint:disable-next-line:no-any
args: any[],
options: ConsoleOptions = {}
): string {
const out: string[] = [];
const { collapsedAt, indentLevel } = options;
for (const a of args) {
if (typeof a === "string") {
out.push(a);
Expand All @@ -347,7 +362,12 @@ export function stringifyArgs(
}
}
let outstr = out.join(" ");
if (groupIndent.length !== 0) {
if (
!isCollapsed(collapsedAt, indentLevel) &&
indentLevel != null &&
indentLevel > 0
) {
const groupIndent = " ".repeat(indentLevel);
if (outstr.indexOf("\n") !== -1) {
outstr = outstr.replace(/\n/g, `\n${groupIndent}`);
}
Expand All @@ -356,20 +376,30 @@ export function stringifyArgs(
return outstr;
}

type PrintFunc = (x: string, isErr?: boolean) => void;
type PrintFunc = (x: string, isErr?: boolean, printsNewline?: boolean) => void;

const countMap = new Map<string, number>();
const timerMap = new Map<string, number>();

let groupIndent = "";

export class Console {
constructor(private printFunc: PrintFunc) {}
indentLevel: number;
collapsedAt: number | null;
constructor(private printFunc: PrintFunc) {
this.indentLevel = 0;
this.collapsedAt = null;
}

/** Writes the arguments to stdout */
// tslint:disable-next-line:no-any
log = (...args: any[]): void => {
this.printFunc(stringifyArgs(args));
this.printFunc(
stringifyArgs(args, {
indentLevel: this.indentLevel,
collapsedAt: this.collapsedAt
}),
false,
!isCollapsed(this.collapsedAt, this.indentLevel)
);
};

/** Writes the arguments to stdout */
Expand All @@ -380,13 +410,20 @@ export class Console {
/** Writes the properties of the supplied `obj` to stdout */
// tslint:disable-next-line:no-any
dir = (obj: any, options: ConsoleOptions = {}) => {
this.printFunc(stringifyArgs([obj], options));
this.log(stringifyArgs([obj], options));
};

/** Writes the arguments to stdout */
// tslint:disable-next-line:no-any
warn = (...args: any[]): void => {
this.printFunc(stringifyArgs(args), true);
this.printFunc(
stringifyArgs(args, {
indentLevel: this.indentLevel,
collapsedAt: this.collapsedAt
}),
true,
!isCollapsed(this.collapsedAt, this.indentLevel)
);
};

/** Writes the arguments to stdout */
Expand Down Expand Up @@ -484,14 +521,26 @@ export class Console {

// tslint:disable-next-line:no-any
group = (...label: any[]): void => {
groupIndent += " ";
if (label.length > 0) {
this.log(...label);
}
this.indentLevel += 2;
};

groupCollapsed = (...label: unknown[]): void => {
if (this.collapsedAt == null) {
this.collapsedAt = this.indentLevel;
}
this.group(...label);
};

groupEnd = (): void => {
groupIndent = groupIndent.slice(0, groupIndent.length - 2);
if (this.indentLevel > 0) {
this.indentLevel -= 2;
}
if (this.collapsedAt != null && this.collapsedAt <= this.indentLevel) {
this.collapsedAt = null;
}
};
}

Expand Down
35 changes: 1 addition & 34 deletions js/console_test.ts
Expand Up @@ -116,7 +116,7 @@ test(function consoleTestStringifyCircular() {
assertEqual(
stringify(console),
// tslint:disable-next-line:max-line-length
"Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function], count: [Function], countReset: [Function], time: [Function], timeLog: [Function], timeEnd: [Function], group: [Function], groupEnd: [Function] }"
"Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function], count: [Function], countReset: [Function], time: [Function], timeLog: [Function], timeEnd: [Function], group: [Function], groupCollapsed: [Function], groupEnd: [Function], indentLevel: 0, collapsedAt: null }"
);
// test inspect is working the same
assertEqual(inspect(nestedObj), nestedObjExpected);
Expand Down Expand Up @@ -145,39 +145,6 @@ test(function consoleTestStringifyWithDepth() {
);
});

test(function consoleStringifyWithGroup() {
const outstrs = [
"This is the outer level",
"Level 2",
"Level 3",
"More of level 3",
"Back to level 2",
"Back to the outer level",
"Still at the outer level"
];
const expectedOut = `${outstrs[0]} ${outstrs[1]} ${outstrs[2]} ${
outstrs[3]
}${outstrs[4]}${outstrs[5]}`;

const expectedErr = ` More of level 3`;
let out = "";
let outErr = "";
out += stringifyArgs([outstrs[0]]);
console.group();
out += stringifyArgs([outstrs[1]]);
console.group();
out += stringifyArgs([outstrs[2]]);
outErr += stringifyArgs([outstrs[3]]);
console.groupEnd();
out += stringifyArgs([outstrs[3]]);
console.groupEnd();
out += stringifyArgs([outstrs[4]]);
console.groupEnd();
out += stringifyArgs([outstrs[5]]);
assertEqual(out, expectedOut);
assertEqual(outErr, expectedErr);
});

test(function consoleTestCallToStringOnLabel() {
const methods = ["count", "countReset", "time", "timeLog", "timeEnd"];

Expand Down
8 changes: 6 additions & 2 deletions libdeno/binding.cc
Expand Up @@ -197,17 +197,21 @@ void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) {

void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK_LE(args.Length(), 2);
CHECK_LE(args.Length(), 3);
auto* isolate = args.GetIsolate();
DenoIsolate* d = FromIsolate(isolate);
auto context = d->context_.Get(d->isolate_);
v8::HandleScope handle_scope(isolate);
v8::String::Utf8Value str(isolate, args[0]);
bool is_err =
args.Length() >= 2 ? args[1]->BooleanValue(context).ToChecked() : false;
bool prints_newline =
args.Length() >= 3 ? args[2]->BooleanValue(context).ToChecked() : true;
FILE* file = is_err ? stderr : stdout;
fwrite(*str, sizeof(**str), str.length(), file);
fprintf(file, "\n");
if (prints_newline) {
fprintf(file, "\n");
}
fflush(file);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/console_group.test
@@ -0,0 +1,2 @@
args: tests/console_group.ts --reload
output: tests/console_group.ts.out
13 changes: 13 additions & 0 deletions tests/console_group.ts
@@ -0,0 +1,13 @@
console.group("1");
console.log("2");
console.group("3");
console.log("4");
console.groupEnd();
console.groupEnd();

console.groupCollapsed("5");
console.log("6");
console.group("7");
console.log("8");
console.groupEnd();
console.groupEnd();
5 changes: 5 additions & 0 deletions tests/console_group.ts.out
@@ -0,0 +1,5 @@
1
2
3
4
5678

0 comments on commit 86b8617

Please sign in to comment.