From d2ad13c87900d386245ccdd127108bc373551b11 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 5 Aug 2017 11:53:32 -0700 Subject: [PATCH 1/2] console: coerce label to string in console.time() Per the console spec, the label in console.time() is a string. --- lib/console.js | 2 ++ test/parallel/test-console.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/console.js b/lib/console.js index 48343bc3e87598..30ec5d53fc9cec 100644 --- a/lib/console.js +++ b/lib/console.js @@ -140,11 +140,13 @@ Console.prototype.dir = function dir(object, options) { Console.prototype.time = function time(label) { + label = `${label}`; this._times.set(label, process.hrtime()); }; Console.prototype.timeEnd = function timeEnd(label) { + label = `${label}`; const time = this._times.get(label); if (!time) { process.emitWarning(`No such label '${label}' for console.timeEnd()`); diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index bca70467c0caac..5dd2b70ef25a26 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -42,6 +42,12 @@ assert.doesNotThrow(function() { console.timeEnd('label'); }); +assert.throws(() => console.time(Symbol('test')), + /^TypeError: Cannot convert a Symbol value to a string$/); +assert.throws(() => console.timeEnd(Symbol('test')), + /^TypeError: Cannot convert a Symbol value to a string$/); + + // an Object with a custom .inspect() function const custom_inspect = { foo: 'bar', inspect: () => 'inspect' }; @@ -103,6 +109,18 @@ console.timeEnd('constructor'); console.time('hasOwnProperty'); console.timeEnd('hasOwnProperty'); +// verify that values are coerced to strings +console.time([]); +console.timeEnd([]); +console.time({}); +console.timeEnd({}); +console.time(null); +console.timeEnd(null); +console.time(undefined); +console.timeEnd(undefined); +console.time(NaN); +console.timeEnd(NaN); + assert.strictEqual(strings.length, process.stdout.writeTimes); assert.strictEqual(errStrings.length, process.stderr.writeTimes); common.restoreStdout(); From 7488fde5b34a4c27394569955f6c789c91d40a23 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 5 Aug 2017 12:11:19 -0700 Subject: [PATCH 2/2] console: use default value for console.time() Per the console spec, the default value of label is `'default'`. --- doc/api/console.md | 4 ++-- lib/console.js | 4 ++-- test/parallel/test-console.js | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/api/console.md b/doc/api/console.md index d56cf5b441667e..8dee507d27ab1a 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -321,7 +321,7 @@ See [`util.format()`][] for more information. -* `label` {string} +* `label` {string} Defaults to `'default'`. Starts a timer that can be used to compute the duration of an operation. Timers are identified by a unique `label`. Use the same `label` when calling @@ -337,7 +337,7 @@ changes: description: This method no longer supports multiple calls that don’t map to individual `console.time()` calls; see below for details. --> -* `label` {string} +* `label` {string} Defaults to `'default'`. Stops a timer that was previously started by calling [`console.time()`][] and prints the result to `stdout`: diff --git a/lib/console.js b/lib/console.js index 30ec5d53fc9cec..1c1906a833eaff 100644 --- a/lib/console.js +++ b/lib/console.js @@ -139,13 +139,13 @@ Console.prototype.dir = function dir(object, options) { }; -Console.prototype.time = function time(label) { +Console.prototype.time = function time(label = 'default') { label = `${label}`; this._times.set(label, process.hrtime()); }; -Console.prototype.timeEnd = function timeEnd(label) { +Console.prototype.timeEnd = function timeEnd(label = 'default') { label = `${label}`; const time = this._times.get(label); if (!time) { diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 5dd2b70ef25a26..10e1aa0ff288fc 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -117,7 +117,9 @@ console.timeEnd({}); console.time(null); console.timeEnd(null); console.time(undefined); -console.timeEnd(undefined); +console.timeEnd('default'); +console.time('default'); +console.timeEnd(); console.time(NaN); console.timeEnd(NaN);