11'use strict' ;
22
33const util = require ( 'util' ) ;
4+ const kCounts = Symbol ( 'counts' ) ;
45
56function Console ( stdout , stderr ) {
67 if ( ! ( this instanceof Console ) ) {
@@ -27,6 +28,8 @@ function Console(stdout, stderr) {
2728 prop . value = new Map ( ) ;
2829 Object . defineProperty ( this , '_times' , prop ) ;
2930
31+ this [ kCounts ] = new Map ( ) ;
32+
3033 // bind the prototype functions to this Console instance
3134 var keys = Object . keys ( Console . prototype ) ;
3235 for ( var v = 0 ; v < keys . length ; v ++ ) {
@@ -96,6 +99,42 @@ Console.prototype.assert = function(expression, ...args) {
9699 }
97100} ;
98101
102+ // Defined by: https://console.spec.whatwg.org/#clear
103+ Console . prototype . clear = function clear ( ) {
104+ // It only makes sense to clear if _stdout is a TTY.
105+ // Otherwise, do nothing.
106+ if ( this . _stdout . isTTY ) {
107+ // The require is here intentionally to avoid readline being
108+ // required too early when console is first loaded.
109+ const { cursorTo, clearScreenDown } = require ( 'readline' ) ;
110+ cursorTo ( this . _stdout , 0 , 0 ) ;
111+ clearScreenDown ( this . _stdout ) ;
112+ }
113+ } ;
114+
115+ // Defined by: https://console.spec.whatwg.org/#count
116+ Console . prototype . count = function count ( label = 'default' ) {
117+ // Ensures that label is a string, and only things that can be
118+ // coerced to strings. e.g. Symbol is not allowed
119+ label = `${ label } ` ;
120+ const counts = this [ kCounts ] ;
121+ let count = counts . get ( label ) ;
122+ if ( count === undefined )
123+ count = 1 ;
124+ else
125+ count ++ ;
126+ counts . set ( label , count ) ;
127+ this . log ( `${ label } : ${ count } ` ) ;
128+ } ;
129+
130+ // Not yet defined by the https://console.spec.whatwg.org, but
131+ // proposed to be added and currently implemented by Edge. Having
132+ // the ability to reset counters is important to help prevent
133+ // the counter from being a memory leak.
134+ Console . prototype . countReset = function countReset ( label = 'default' ) {
135+ const counts = this [ kCounts ] ;
136+ counts . delete ( `${ label } ` ) ;
137+ } ;
99138
100139module . exports = new Console ( process . stdout , process . stderr ) ;
101140module . exports . Console = Console ;
0 commit comments