Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6014d66
crypto: add randomFill and randomFillSync
evanlucas Dec 9, 2016
631b441
doc: improve randomfill and fix broken link
thefourtheye Apr 20, 2017
4afecfc
lib: return this from net.Socket.end()
sam-github Jun 5, 2017
19955c6
net: return this from getConnections()
sam-github Jun 7, 2017
5c18651
repl: improve require() autocompletion
aqrln Jul 21, 2017
79a3e37
console: add console.count() and console.clear()
jasnell Apr 26, 2017
5c20548
dgram: added setMulticastInterface()
lostnet Jul 23, 2016
0912453
test: don't skip when common.mustCall() is pending
cjihrig Sep 14, 2017
d7ac63e
test: crypto createClass instanceof Class
bengl Aug 19, 2016
809858c
crypto: expose ECDH class
bengl Aug 19, 2016
542f13f
test: fix flaky test-crypto-classes.js
bengl Sep 28, 2017
23ad5cb
tools, build: refactor macOS installer
jpwesselink Sep 4, 2017
1558a90
deps: upgrade libuv to 1.16.1
cjihrig Nov 10, 2017
61728e7
src: add process.ppid
cjihrig Oct 30, 2017
dd2102d
src: add --use-bundled-ca --use-openssl-ca check
danbev Mar 28, 2017
c3d5fb3
src: guard bundled_ca/openssl_ca with HAVE_OPENSSL
danbev Apr 10, 2017
8e89616
src: fix incorrect macro comment
danbev Apr 27, 2017
3c4bb3c
crypto: remove BIO_set_shutdown
danbev Dec 7, 2017
ba2af51
src: clean up MaybeStackBuffer
TimothyGu Nov 28, 2017
df835c4
test: add common.hasIntl
jasnell Oct 23, 2016
5a13d1a
url: update IDNA handling
TimothyGu Jun 1, 2017
3639d0c
url: adding WHATWG URL support
jasnell Nov 28, 2017
3e7f0fc
promises: more robust stringification
TimothyGu Jun 19, 2017
f374d60
test: fix truncation of argv
danbev Mar 29, 2017
b8e561e
http: overridable keep-alive behavior of `Agent`
indutny Jan 15, 2018
a511b49
src: add openssl-system-ca-path configure option
danbev Nov 6, 2017
ae9af7a
module: add builtinModules
maclover7 Nov 24, 2017
9f571ef
deps: ICU 59.1 bump
srl295 Apr 13, 2017
a164c9a
deps: ICU 60 bump
srl295 Sep 21, 2017
c0aee51
deps: ICU 60.2 bump
srl295 Dec 14, 2017
f14a715
fixup: potential hack for null_ptr
MylesBorins Feb 2, 2018
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
Prev Previous commit
Next Next commit
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: #12678
Ref: #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>
  • Loading branch information
jasnell authored and MylesBorins committed Jan 17, 2018
commit 79a3e37fb12a6dcfbbe053ec3b9ea34f5e858868
70 changes: 70 additions & 0 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,76 @@ console.assert(false, 'this message will print, but no error thrown');
console.log('this will also print');
```

### console.clear()
<!-- YAML
added: REPLACEME
-->

When `stdout` is a TTY, calling `console.clear()` will attempt to clear the
TTY. When `stdout` is not a TTY, this method does nothing.

*Note*: The specific operation of `console.clear()` can vary across operating
systems and terminal types. For most Linux operating systems, `console.clear()`
operates similarly to the `clear` shell command. On Windows, `console.clear()`
will clear only the output in the current terminal viewport for the Node.js
binary.

### console.count([label])
<!-- YAML
added: REPLACEME
-->

* `label` {string} The display label for the counter. Defaults to `'default'`.

Maintains an internal counter specific to `label` and outputs to `stdout` the
number of times `console.count()` has been called with the given `label`.

<!-- eslint-skip -->
```js
> console.count()
default: 1
undefined
> console.count('default')
default: 2
undefined
> console.count('abc')
abc: 1
undefined
> console.count('xyz')
xyz: 1
undefined
> console.count('abc')
abc: 2
undefined
> console.count()
default: 3
undefined
>
```

### console.countReset([label = 'default'])
<!-- YAML
added: REPLACEME
-->

* `label` {string} The display label for the counter. Defaults to `'default'`.

Resets the internal counter specific to `label`.

<!-- eslint-skip -->
```js
> console.count('abc');
abc: 1
undefined
> console.countReset('abc');
undefined
> console.count('abc');
abc: 1
undefined
>
```
<!-- eslint-enable -->

### console.dir(obj[, options])
<!-- YAML
added: v0.1.101
Expand Down
39 changes: 39 additions & 0 deletions lib/console.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const util = require('util');
const kCounts = Symbol('counts');

function Console(stdout, stderr) {
if (!(this instanceof Console)) {
Expand All @@ -27,6 +28,8 @@ function Console(stdout, stderr) {
prop.value = new Map();
Object.defineProperty(this, '_times', 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++) {
Expand Down Expand Up @@ -96,6 +99,42 @@ Console.prototype.assert = function(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;
22 changes: 22 additions & 0 deletions test/parallel/test-console-clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

require('../common');
const assert = require('assert');

const stdoutWrite = process.stdout.write;

// The sequence for moving the cursor to 0,0 and clearing screen down
const check = '\u001b[1;1H\u001b[0J';

function doTest(isTTY, check) {
let buf = '';
process.stdout.isTTY = isTTY;
process.stdout.write = (string) => buf += string;
console.clear();
process.stdout.write = stdoutWrite;
assert.strictEqual(buf, check);
}

// Fake TTY
doTest(true, check);
doTest(false, '');
63 changes: 63 additions & 0 deletions test/parallel/test-console-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

require('../common');
const assert = require('assert');

const stdoutWrite = process.stdout.write;

let buf = '';

process.stdout.write = (string) => buf = string;

console.count();
assert.strictEqual(buf, 'default: 1\n');

// 'default' and undefined are equivalent
console.count('default');
assert.strictEqual(buf, 'default: 2\n');

console.count('a');
assert.strictEqual(buf, 'a: 1\n');

console.count('b');
assert.strictEqual(buf, 'b: 1\n');

console.count('a');
assert.strictEqual(buf, 'a: 2\n');

console.count();
assert.strictEqual(buf, 'default: 3\n');

console.count({});
assert.strictEqual(buf, '[object Object]: 1\n');

console.count(1);
assert.strictEqual(buf, '1: 1\n');

console.count(null);
assert.strictEqual(buf, 'null: 1\n');

console.count('null');
assert.strictEqual(buf, 'null: 2\n');

console.countReset();
console.count();
assert.strictEqual(buf, 'default: 1\n');

console.countReset('a');
console.count('a');
assert.strictEqual(buf, 'a: 1\n');

// countReset('a') only reset the a counter
console.count();
assert.strictEqual(buf, 'default: 2\n');

process.stdout.write = stdoutWrite;

// Symbol labels do not work
assert.throws(
() => console.count(Symbol('test')),
/^TypeError: Cannot convert a Symbol value to a string$/);
assert.throws(
() => console.countReset(Symbol('test')),
/^TypeError: Cannot convert a Symbol value to a string$/);