Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement console.groupCollapsed #1452

Merged
merged 2 commits into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add console.group & console.groupEnd #1363
- add console.group & console.groupEnd function
- add console.group & console.groupEnd test
- fix group when log info contains \n
  • Loading branch information
zhmushan authored and kt3k committed Jan 3, 2019
commit 02b6950ccd8078ddcc61d2d4af8e149f7f52766d
24 changes: 22 additions & 2 deletions js/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,23 @@ export function stringifyArgs(
);
}
}
return out.join(" ");
let outstr = out.join(" ");
if (groupIndent.length !== 0) {
if (outstr.indexOf("\n") !== -1) {
outstr = outstr.replace(/\n/g, `\n${groupIndent}`);
}
outstr = groupIndent + outstr;
}
return outstr;
}

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

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

/** TODO Do not expose this from "deno". */
let groupIndent = "";

export class Console {
constructor(private printFunc: PrintFunc) {}

Expand Down Expand Up @@ -473,6 +481,18 @@ export class Console {

this.info(`${label}: ${duration}ms`);
};

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

groupEnd = (): void => {
groupIndent = groupIndent.slice(0, groupIndent.length - 2);
};
}

/**
Expand Down
39 changes: 38 additions & 1 deletion js/console_test.ts
Original file line number Diff line number Diff line change
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] }"
"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] }"
);
// test inspect is working the same
assertEqual(inspect(nestedObj), nestedObjExpected);
Expand Down Expand Up @@ -145,6 +145,39 @@ 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 Expand Up @@ -189,6 +222,8 @@ test(function consoleDetachedLog() {
const consoleTime = console.time;
const consoleTimeLog = console.timeLog;
const consoleTimeEnd = console.timeEnd;
const consoleGroup = console.group;
const consoleGroupEnd = console.groupEnd;
log("Hello world");
dir("Hello world");
debug("Hello world");
Expand All @@ -201,4 +236,6 @@ test(function consoleDetachedLog() {
consoleTime("Hello world");
consoleTimeLog("Hello world");
consoleTimeEnd("Hello world");
consoleGroup("Hello world");
consoleGroupEnd();
});