-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
b7c4e52
commit fd9ca04
Showing
4 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$/); |