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

add console count and time functions #1358

Merged
merged 4 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 68 additions & 1 deletion js/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ export function stringifyArgs(

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these be in the Console class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js has no private property (currently), if it is placed inside the Console class, these variables are accessed by by code outside the class. And these properties will be printed with console.

currently The printFunc method is rinted and can be used by external code. I think this is a wrong implementation, and this should be refactored in the future.

> console
Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function] }
> console.printFunc
[Function]
> console.printFunc.toString()
function () { [native code] }
> console.printFunc('hello')
hello
undefined


export class Console {
// @internal
constructor(private printFunc: PrintFunc) {}
Expand Down Expand Up @@ -387,7 +390,7 @@ export class Console {
* ref: https://console.spec.whatwg.org/#assert
*/
// tslint:disable-next-line:no-any
assert = (condition?: boolean, ...args: any[]): void => {
assert = (condition = false, ...args: any[]): void => {
if (condition) {
return;
}
Expand All @@ -406,4 +409,68 @@ export class Console {

this.error(`Assertion failed:`, ...args);
};

count = (label = "default"): void => {
label = String(label);

if (countMap.has(label)) {
const current = countMap.get(label) || 0;
countMap.set(label, current + 1);
} else {
countMap.set(label, 1);
}

this.info(`${label}: ${countMap.get(label)}`);
};

countReset = (label = "default"): void => {
label = String(label);

if (countMap.has(label)) {
countMap.set(label, 0);
} else {
this.warn(`Count for '${label}' does not exist`);
}
};

time = (label = "default"): void => {
label = String(label);

if (timerMap.has(label)) {
this.warn(`Timer '${label}' already exists`);
return;
}

timerMap.set(label, Date.now());
};

// tslint:disable-next-line:no-any
timeLog = (label = "default", ...args: any[]): void => {
label = String(label);

if (!timerMap.has(label)) {
this.warn(`Timer '${label}' does not exists`);
return;
}

const startTime = timerMap.get(label) as number;
const duration = Date.now() - startTime;

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

timeEnd = (label = "default"): void => {
label = String(label);

if (!timerMap.has(label)) {
this.warn(`Timer '${label}' does not exists`);
return;
}

const startTime = timerMap.get(label) as number;
timerMap.delete(label);
const duration = Date.now() - startTime;

this.info(`${label}: ${duration}ms`);
};
}
32 changes: 31 additions & 1 deletion js/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import { test, assert, assertEqual } from "./test_util.ts";
import { stringifyArgs } from "./console.ts";

import { Console } from "./console.ts";
import { libdeno } from "./libdeno";
const console = new Console(libdeno.print);

// tslint:disable-next-line:no-any
function stringify(...args: any[]): string {
return stringifyArgs(args);
Expand Down Expand Up @@ -114,7 +118,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] }"
"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] }"
);
});

Expand All @@ -136,6 +140,22 @@ test(function consoleTestStringifyWithDepth() {
);
});

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

for (const method of methods) {
let hasCalled = false;

console[method]({
toString() {
hasCalled = true;
}
});

assertEqual(hasCalled, true);
}
});

test(function consoleTestError() {
class MyError extends Error {
constructor(errStr: string) {
Expand All @@ -159,11 +179,21 @@ test(function consoleDetachedLog() {
const warn = console.warn;
const error = console.error;
const consoleAssert = console.assert;
const consoleCount = console.count;
const consoleCountReset = console.countReset;
const consoleTime = console.time;
const consoleTimeLog = console.timeLog;
const consoleTimeEnd = console.timeEnd;
log("Hello world");
dir("Hello world");
debug("Hello world");
info("Hello world");
warn("Hello world");
error("Hello world");
consoleAssert(true);
consoleCount("Hello world");
consoleCountReset("Hello world");
consoleTime("Hello world");
consoleTimeLog("Hello world");
consoleTimeEnd("Hello world");
});