From 4da8b99a74c9d00a3733d50c6348750360bf1c40 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 5 Aug 2017 11:53:32 -0700 Subject: [PATCH] console: coerce label to string in console.time() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per the console spec, the label in console.time() is a string. Per the console spec, the default value of label is `'default'`. PR-URL: https://github.com/nodejs/node/pull/14643 Reviewed-By: Refael Ackermann Reviewed-By: Timothy Gu Reviewed-By: Khaidi Chu Reviewed-By: Tobias Nießen Reviewed-By: Colin Ihrig --- doc/api/console.md | 4 ++-- lib/console.js | 8 ++++++-- test/parallel/test-console.js | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/doc/api/console.md b/doc/api/console.md index d56cf5b441..8dee507d27 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 48343bc3e8..b0701ba32c 100644 --- a/lib/console.js +++ b/lib/console.js @@ -139,12 +139,16 @@ Console.prototype.dir = function dir(object, options) { }; -Console.prototype.time = function time(label) { +Console.prototype.time = function time(label = 'default') { + // Coerces everything other than Symbol to a string + label = `${label}`; this._times.set(label, process.hrtime()); }; -Console.prototype.timeEnd = function timeEnd(label) { +Console.prototype.timeEnd = function timeEnd(label = 'default') { + // Coerces everything other than Symbol to a string + 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 bca70467c0..10e1aa0ff2 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,20 @@ 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('default'); +console.time('default'); +console.timeEnd(); +console.time(NaN); +console.timeEnd(NaN); + assert.strictEqual(strings.length, process.stdout.writeTimes); assert.strictEqual(errStrings.length, process.stderr.writeTimes); common.restoreStdout();