bare-console 5.1.1 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +2 -2
  2. package/global.js +3 -0
  3. package/index.js +96 -51
  4. package/package.json +14 -10
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # bare-console
2
2
 
3
- Simple debugging console for JavaScript.
3
+ WHATWG debugging console for JavaScript.
4
4
 
5
5
  ```
6
6
  npm i bare-console
@@ -11,7 +11,7 @@ npm i bare-console
11
11
  ```js
12
12
  const Console = require('bare-console')
13
13
 
14
- const console = new Console({ stdout: process.stdout, stderr: process.stderr })
14
+ const console = new Console()
15
15
 
16
16
  console.log('Hello')
17
17
  console.error(new Error('Something happened'))
package/global.js ADDED
@@ -0,0 +1,3 @@
1
+ const Console = require('.')
2
+
3
+ global.console = new Console()
package/index.js CHANGED
@@ -1,72 +1,117 @@
1
- const { formatWithOptions } = require('bare-format')
1
+ const Log = require('bare-logger')
2
2
  const hrtime = require('bare-hrtime')
3
3
 
4
- module.exports = class Console {
5
- constructor (opts = {}) {
6
- this._stdout = adaptStream(opts.stdout)
7
- this._stderr = adaptStream(opts.stderr)
8
-
9
- this._colors = opts.colors === true
10
- this._timers = new Map()
11
-
12
- if (opts.bind) {
13
- this.log = this.log.bind(this)
14
- this.warn = this.warn.bind(this)
15
- this.error = this.error.bind(this)
16
- this.time = this.time.bind(this)
17
- this.timeEnd = this.timeEnd.bind(this)
18
- this.trace = this.trace.bind(this)
4
+ module.exports = exports = class Console {
5
+ constructor(log = new Log()) {
6
+ const timers = new Map()
7
+ const counters = new Map()
8
+
9
+ // https://console.spec.whatwg.org/#debug
10
+ this.debug = function debug(...data) {
11
+ log.debug(...data)
19
12
  }
20
- }
21
13
 
22
- log (...args) {
23
- this._stdout.write(formatWithOptions({ colors: this._colors }, ...args) + '\n')
24
- }
14
+ // https://console.spec.whatwg.org/#info
15
+ this.info = function info(...data) {
16
+ log.info(...data)
17
+ }
25
18
 
26
- warn (...args) {
27
- this._stderr.write(formatWithOptions({ colors: this._colors }, ...args) + '\n')
28
- }
19
+ // https://console.spec.whatwg.org/#warn
20
+ this.warn = function warn(...data) {
21
+ log.warn(...data)
22
+ }
29
23
 
30
- error (...args) {
31
- this._stderr.write(formatWithOptions({ colors: this._colors }, ...args) + '\n')
32
- }
24
+ // https://console.spec.whatwg.org/#error
25
+ this.error = function error(...data) {
26
+ log.error(...data)
27
+ }
33
28
 
34
- time (label = 'default') {
35
- if (this._timers.has(label)) {
36
- this.error('Warning: Label \'' + label + '\' already exists for console.time()')
37
- return
29
+ // https://console.spec.whatwg.org/#log
30
+ this.log = this.info
31
+
32
+ // https://console.spec.whatwg.org/#clear
33
+ this.clear = function clear() {
34
+ log.clear()
38
35
  }
39
36
 
40
- this._timers.set(label, hrtime())
41
- }
37
+ // https://console.spec.whatwg.org/#time
38
+ this.time = function time(label = 'default') {
39
+ if (timers.has(label)) {
40
+ this.warn(`Warning: Label '${label}' already exists for console.time()`)
41
+ return
42
+ }
43
+
44
+ timers.set(label, hrtime())
45
+ }
42
46
 
43
- timeEnd (label = 'default') {
44
- const started = this._timers.get(label)
47
+ // https://console.spec.whatwg.org/#timelog
48
+ this.timeLog = function timeLog(label = 'default', ...data) {
49
+ const started = timers.get(label)
45
50
 
46
- if (!started) {
47
- this.error('Warning: No such label \'' + label + '\' for console.timeEnd()')
48
- return
51
+ if (started === undefined) {
52
+ this.warn(`Warning: No such label '${label}' for console.timeEnd()`)
53
+ return
54
+ }
55
+
56
+ const elapsed = hrtime(started)
57
+ const ms = elapsed[0] * 1e3 + elapsed[1] / 1e6
58
+
59
+ if (ms > 1000) this.log(`${label}: ${(ms / 1000).toFixed(3)}s`, ...data)
60
+ else this.log(`${label}: ${ms.toFixed(3)}ms`, ...data)
49
61
  }
50
62
 
51
- const d = hrtime(started)
52
- const ms = d[0] * 1e3 + d[1] / 1e6
53
- this._timers.delete(label)
63
+ // https://console.spec.whatwg.org/#timeend
64
+ this.timeEnd = function timeEnd(label = 'default') {
65
+ this.timeLog(label)
54
66
 
55
- if (ms > 1000) this.log(label + ': ' + (ms / 1000).toFixed(3) + 's')
56
- else this.log(label + ': ' + ms.toFixed(3) + 'ms')
57
- }
67
+ timers.delete(label)
68
+ }
69
+
70
+ // https://console.spec.whatwg.org/#count
71
+ this.count = function count(label = 'default') {
72
+ const count = counters.get(label) || 1
73
+
74
+ this.log(`${label}: ${count}`)
75
+
76
+ counters.set(label, count + 1)
77
+ }
78
+
79
+ // https://console.spec.whatwg.org/#countreset
80
+ this.countReset = function countReset(label = 'default') {
81
+ counters.delete(label)
82
+ }
58
83
 
59
- trace (...args) {
60
- const err = { name: 'Trace', message: formatWithOptions({ colors: this._colors }, ...args) }
84
+ // https://console.spec.whatwg.org/#trace
85
+ this.trace = function trace(...data) {
86
+ const err = {
87
+ name: 'Trace',
88
+ message: log.format(...data),
89
+ stack: null
90
+ }
61
91
 
62
- if (Error.captureStackTrace) {
63
- Error.captureStackTrace(err, this.trace)
92
+ if (Error.captureStackTrace) {
93
+ Error.captureStackTrace(err, this.trace)
94
+ }
95
+
96
+ this.error(err.stack)
64
97
  }
65
98
 
66
- this.error(err.stack)
99
+ // https://console.spec.whatwg.org/#assert
100
+ this.assert = function assert(condition, ...data) {
101
+ if (condition) return
102
+
103
+ if (data.length === 0) data.push('Assertion failed')
104
+ else if (typeof data[0] !== 'string') data.unshift('Assertion failed')
105
+ else data[0] = `Assertion failed: ${data[0]}`
106
+
107
+ this.error(...data)
108
+ }
67
109
  }
68
- }
69
110
 
70
- function adaptStream (stream) {
71
- return typeof stream === 'function' ? { write: stream } : stream
111
+ // For Node.js compatibility
112
+ get Console() {
113
+ return Console
114
+ }
72
115
  }
116
+
117
+ exports.Console = exports // For Node.js compatibility
package/package.json CHANGED
@@ -1,13 +1,18 @@
1
1
  {
2
2
  "name": "bare-console",
3
- "version": "5.1.1",
4
- "description": "Simple debugging console for JavaScript",
5
- "main": "index.js",
3
+ "version": "6.0.0",
4
+ "description": "WHATWG debugging console for JavaScript",
5
+ "exports": {
6
+ ".": "./index.js",
7
+ "./package": "./package.json",
8
+ "./global": "./global.js"
9
+ },
6
10
  "files": [
7
- "index.js"
11
+ "index.js",
12
+ "global.js"
8
13
  ],
9
14
  "scripts": {
10
- "test": "standard && bare test.js"
15
+ "test": "prettier . --check && bare test.js"
11
16
  },
12
17
  "repository": {
13
18
  "type": "git",
@@ -20,13 +25,12 @@
20
25
  },
21
26
  "homepage": "https://github.com/holepunchto/bare-console#readme",
22
27
  "dependencies": {
23
- "bare-events": "^2.2.0",
24
- "bare-format": "^1.0.0",
25
- "bare-hrtime": "^2.0.0"
28
+ "bare-hrtime": "^2.0.0",
29
+ "bare-logger": "^1.0.0"
26
30
  },
27
31
  "devDependencies": {
28
- "bare-stream": "^2.0.0",
29
32
  "brittle": "^3.1.1",
30
- "standard": "^17.0.0"
33
+ "prettier": "^3.3.3",
34
+ "prettier-config-standard": "^7.0.0"
31
35
  }
32
36
  }