mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
console: add console.count() and console.clear()
Both are simple utility functions defined by the WHATWG console spec (https://console.spec.whatwg.org/). PR-URL: https://github.com/nodejs/node/pull/12678 Ref: https://github.com/nodejs/node/issues/12675 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
parent
98bae29304
commit
cc43c8fb54
4 changed files with 194 additions and 0 deletions
|
@ -23,6 +23,7 @@
|
|||
|
||||
const errors = require('internal/errors');
|
||||
const util = require('util');
|
||||
const kCounts = Symbol('counts');
|
||||
|
||||
function Console(stdout, stderr, ignoreErrors = true) {
|
||||
if (!(this instanceof Console)) {
|
||||
|
@ -55,6 +56,8 @@ function Console(stdout, stderr, ignoreErrors = true) {
|
|||
prop.value = createWriteErrorHandler(stderr);
|
||||
Object.defineProperty(this, '_stderrErrorHandler', prop);
|
||||
|
||||
this[kCounts] = new Map();
|
||||
|
||||
// bind the prototype functions to this Console instance
|
||||
var keys = Object.keys(Console.prototype);
|
||||
for (var v = 0; v < keys.length; v++) {
|
||||
|
@ -166,6 +169,42 @@ Console.prototype.assert = function assert(expression, ...args) {
|
|||
}
|
||||
};
|
||||
|
||||
// Defined by: https://console.spec.whatwg.org/#clear
|
||||
Console.prototype.clear = function clear() {
|
||||
// It only makes sense to clear if _stdout is a TTY.
|
||||
// Otherwise, do nothing.
|
||||
if (this._stdout.isTTY) {
|
||||
// The require is here intentionally to avoid readline being
|
||||
// required too early when console is first loaded.
|
||||
const { cursorTo, clearScreenDown } = require('readline');
|
||||
cursorTo(this._stdout, 0, 0);
|
||||
clearScreenDown(this._stdout);
|
||||
}
|
||||
};
|
||||
|
||||
// Defined by: https://console.spec.whatwg.org/#count
|
||||
Console.prototype.count = function count(label = 'default') {
|
||||
// Ensures that label is a string, and only things that can be
|
||||
// coerced to strings. e.g. Symbol is not allowed
|
||||
label = `${label}`;
|
||||
const counts = this[kCounts];
|
||||
let count = counts.get(label);
|
||||
if (count === undefined)
|
||||
count = 1;
|
||||
else
|
||||
count++;
|
||||
counts.set(label, count);
|
||||
this.log(`${label}: ${count}`);
|
||||
};
|
||||
|
||||
// Not yet defined by the https://console.spec.whatwg.org, but
|
||||
// proposed to be added and currently implemented by Edge. Having
|
||||
// the ability to reset counters is important to help prevent
|
||||
// the counter from being a memory leak.
|
||||
Console.prototype.countReset = function countReset(label = 'default') {
|
||||
const counts = this[kCounts];
|
||||
counts.delete(`${label}`);
|
||||
};
|
||||
|
||||
module.exports = new Console(process.stdout, process.stderr);
|
||||
module.exports.Console = Console;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue