bare-console 2.0.2

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/LICENSE +201 -0
  2. package/README.md +27 -0
  3. package/index.js +379 -0
  4. package/package.json +30 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # bare-console
2
+
3
+ Simple debugging console for JavaScript.
4
+
5
+ ```
6
+ npm i bare-console
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ ```javascript
12
+ const Console = require('bare-console')
13
+ const console = new Console({ stdout: process.stdout, stderr: process.stderr })
14
+
15
+ console.log('Hello')
16
+ console.error(new Error('Something happened'))
17
+
18
+ console.time()
19
+ for (let i = 0; i < 1000000000; i++) {}
20
+ console.timeEnd()
21
+
22
+ console.trace('Show me')
23
+ ```
24
+
25
+ ## License
26
+
27
+ Apache-2.0
package/index.js ADDED
@@ -0,0 +1,379 @@
1
+ const { Crayon } = require('tiny-crayon')
2
+
3
+ module.exports = class Console {
4
+ constructor (opts = {}) {
5
+ const { isTTY } = adaptStream(opts.stdout || opts.stderr)
6
+ this.colors = typeof opts.colors === 'boolean' ? opts.colors : isTTY
7
+ this.crayon = new Crayon({ isTTY: this.colors })
8
+
9
+ this.log = this._print.bind(this, adaptStream(opts.stdout))
10
+ this.error = this._print.bind(this, adaptStream(opts.stderr))
11
+
12
+ this.times = new Map()
13
+ }
14
+
15
+ time (label = 'default') {
16
+ if (this.times.has(label)) {
17
+ this.error('Warning: Label \'' + label + '\' already exists for console.time()')
18
+ return
19
+ }
20
+
21
+ this.times.set(label, process.hrtime())
22
+ }
23
+
24
+ timeEnd (label = 'default') {
25
+ const started = this.times.get(label)
26
+
27
+ if (!started) {
28
+ this.error('Warning: No such label \'' + label + '\' for console.timeEnd()')
29
+ return
30
+ }
31
+
32
+ const d = process.hrtime(started)
33
+ const ms = d[0] * 1e3 + d[1] / 1e6
34
+ this.times.delete(label)
35
+
36
+ if (ms > 1000) this.log(label + ': ' + (ms / 1000).toFixed(3) + 's')
37
+ else this.log(label + ': ' + ms.toFixed(3) + 'ms')
38
+ }
39
+
40
+ trace (...messages) {
41
+ const { stack } = new Error()
42
+
43
+ const first = stack.indexOf('\n')
44
+ const second = stack.indexOf('\n', first + 1)
45
+ const start = second > -1 ? second : 0
46
+
47
+ this.error('Trace: ' + messages.join(' ') + stack.slice(start))
48
+ }
49
+
50
+ // + clear () {}
51
+
52
+ _print (stream, ...args) {
53
+ const paint = new Paint(this.crayon)
54
+ let identifier = 0
55
+
56
+ for (let i = 0; i < args.length; i++) {
57
+ const arg = args[i]
58
+
59
+ const single = generateSingleValue(arg, { escape: false })
60
+ if (single !== null) {
61
+ paint.push('value', single)
62
+ } else if (typeof arg === 'object') {
63
+ let levels = 0
64
+
65
+ iterateObject(arg)
66
+
67
+ function iterateObject (arg, backward = new WeakSet(), forward = new WeakSet(), add = true) {
68
+ if (add) backward.add(arg)
69
+ else forward.add(arg)
70
+
71
+ const id = identifier++
72
+ const isArray = Array.isArray(arg)
73
+ const isBuffer = Buffer.isBuffer(arg) && arg.constructor.name === 'Buffer'
74
+ const isInts = !isBuffer && isIntArray(arg)
75
+ const isObject = !(isArray || isInts || isBuffer)
76
+ const brackets = isBuffer ? '<>' : (isInts || isArray ? '[]' : '{}')
77
+
78
+ levels++
79
+
80
+ if (levels >= 4 && !isObjectEmpty(arg)) {
81
+ let type = isArray ? 'Array' : (typeof arg)
82
+ type = type[0].toUpperCase() + type.slice(1)
83
+ paint.push('value', '[' + type + ']', { id, crayon: 'cyan' })
84
+ levels--
85
+ return paint.width[id]
86
+ }
87
+
88
+ if (isInts) paint.push('open', arg.constructor.name + '(' + arg.length + ') ', { id })
89
+ paint.push('open', brackets[0], { id })
90
+ if (isBuffer) paint.push('open', 'Buffer', { id })
91
+
92
+ const MAX = isObject ? Infinity : (isBuffer ? 50 : 100)
93
+ let count = 0
94
+
95
+ for (const key in arg) {
96
+ const k = isObject ? key : parseInt(key, 10)
97
+ const isNumeric = !isObject && isFinite(k)
98
+ const v = arg[isNumeric ? k : key]
99
+
100
+ if (isBuffer && !isNumeric && Object.hasOwn(Object.getPrototypeOf(arg), key)) continue
101
+
102
+ if (count++ >= MAX) break
103
+
104
+ if (count === 1) {
105
+ paint.push('spacing-start', null, { id, levels, isArray, isInts, isBuffer, arg, k })
106
+ } else {
107
+ paint.push('separator', isBuffer && isNumeric ? '' : ',', { id })
108
+ paint.push('spacing-sep', null, { id, levels, isArray, isInts, isBuffer, arg, k })
109
+ }
110
+
111
+ if (!isNumeric) {
112
+ const singleKey = generateSingleKey(key)
113
+ paint.push('key', [singleKey, ': '], { id })
114
+ }
115
+
116
+ const single = generateSingleValue(v, { levels, stringColor: true, intToHex: isBuffer })
117
+ if (single !== null) {
118
+ paint.push('value', single, { id })
119
+ } else if (typeof v === 'object') {
120
+ if (backward.has(v) || (!add && forward.has(v))) {
121
+ paint.push('value', '[Circular]', { id, crayon: 'cyan' })
122
+ continue
123
+ }
124
+
125
+ const subWidth = iterateObject(v, backward, forward, false)
126
+ paint.width[id].child += subWidth.self + subWidth.child // + double check after colors fix
127
+ } else {
128
+ throw new Error('Argument not supported (' + (typeof v) + '): ' + v)
129
+ }
130
+ }
131
+
132
+ const symbols = Object.getOwnPropertySymbols(arg)
133
+
134
+ for (const symbol of symbols) {
135
+ count++
136
+
137
+ if (count === 1) {
138
+ paint.push('spacing-start', null, { id, levels })
139
+ } else {
140
+ paint.push('separator', ',', { id })
141
+ paint.push('spacing-sep', null, { id, levels })
142
+ }
143
+
144
+ if (!isArray) {
145
+ paint.push('key', ['[', { out: symbol.toString(), crayon: 'green' }, ']', ': '], { id })
146
+ }
147
+
148
+ const single = generateSingleValue(arg[symbol], { levels })
149
+ if (single === null) throw new Error('Symbol value not supported: (' + (typeof arg[symbol]) + '): ' + arg[symbol])
150
+ paint.push('value', single, { id })
151
+ }
152
+
153
+ if (!isObject && arg.length > MAX) paint.push('more', null, { id, levels, isArray, isInts, isBuffer, arg, left: (arg.length - MAX) })
154
+
155
+ if (count > 0) paint.push('spacing-end', null, { id, levels, isArray, isInts, isBuffer, arg })
156
+
157
+ if (count === 0 && isBuffer) paint.push('spacing-sep', null, { id, levels, isArray, isInts, isBuffer, arg })
158
+
159
+ paint.push('close', brackets[1], { id })
160
+
161
+ levels--
162
+
163
+ return paint.width[id]
164
+ }
165
+ } else {
166
+ throw new Error('Argument not supported (' + (typeof arg) + '): ' + arg)
167
+ }
168
+
169
+ if (i + 1 !== args.length) paint.push('space', ' ')
170
+ }
171
+
172
+ paint.push('break-line', '\n')
173
+
174
+ stream.write(paint.done())
175
+
176
+ function generateSingleKey (key) {
177
+ if (key === '') return { out: "''", crayon: 'green' }
178
+
179
+ const names = ['undefined', 'null', 'true', 'false', 'NaN', 'Infinity']
180
+ if (names.indexOf(key) > -1) return { out: key }
181
+
182
+ if (isKindOfAlphaNumeric(key) && !isFinite(key)) return { out: key }
183
+
184
+ return { out: "'" + key + "'", crayon: 'green' }
185
+ }
186
+
187
+ function generateSingleValue (value, { levels = 0, stringColor = false, escape = true, intToHex = false } = {}) {
188
+ if (typeof value === 'undefined') return { out: 'undefined', crayon: 'blackBright' }
189
+ if (value === null) return { out: 'null', crayon: 'bold' }
190
+
191
+ if (typeof value === 'string') return stringColor ? { out: dynamicQuotes(value, { escape }), crayon: 'green' } : { out: dynamicQuotes(value, { escape }) }
192
+ if (typeof value === 'number') return intToHex ? { out: numberToHex(value) } : { out: value.toString(), crayon: 'yellow' }
193
+ if (typeof value === 'boolean') return { out: value.toString(), crayon: 'yellow' }
194
+ if (typeof value === 'function') return { out: (value.name ? '[Function: ' + value.name + ']' : '[Function (anonymous)]'), crayon: 'cyan' }
195
+ if (typeof value === 'symbol') return { out: value.toString(), crayon: 'green' }
196
+ if (typeof value === 'bigint') return { out: value.toString() + 'n', crayon: 'yellow' } // + edge case: typeof Object(1n) === 'object'
197
+
198
+ if (value instanceof Promise) return { out: 'Promise' }
199
+ if (value instanceof RegExp) return { out: value.toString(), crayon: 'red' }
200
+
201
+ // + AggregateError?
202
+ if (value instanceof Error) return { out: value.stack } // This includes EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError
203
+ if (value instanceof String) return { out: '[String: ' + dynamicQuotes(value.toString()) + ']', crayon: 'green' }
204
+ if (value instanceof Number) return { out: '[Number: ' + value.toString() + ']', crayon: 'yellow' }
205
+ if (value instanceof Boolean) return { out: '[Boolean: ' + value.toString() + ']', crayon: 'yellow' }
206
+ if (value instanceof Date) return { out: value.toISOString(), crayon: 'magenta' }
207
+
208
+ if (value instanceof Map) return { out: 'Map(' + value.size + ') {' + (value.size ? ' ... ' : '') + '}' }
209
+ if (value instanceof Set) return { out: 'Set(' + value.size + ') {' + (value.size ? ' ... ' : '') + '}' }
210
+
211
+ if (value instanceof WeakMap) return [{ out: 'WeakMap { ' }, { out: '<items unknown>', crayon: 'cyan' }, { out: ' }' }]
212
+ if (value instanceof WeakSet) return [{ out: 'WeakSet { ' }, { out: '<items unknown>', crayon: 'cyan' }, { out: ' }' }]
213
+
214
+ return null
215
+ }
216
+ }
217
+ }
218
+
219
+ function numberToHex (value) {
220
+ return value.toString(16).padStart(2, '0')
221
+ }
222
+
223
+ function isIntArray (value) {
224
+ if (value instanceof Uint8Array || value instanceof Uint16Array || value instanceof Uint32Array) return true
225
+ if (value instanceof Int8Array || value instanceof Int16Array || value instanceof Int32Array) return true
226
+ return false
227
+ }
228
+
229
+ function dynamicQuotes (str, opts = {}) {
230
+ if (opts.escape === false) return str
231
+
232
+ if (str.indexOf("'") === -1) return "'" + escapeString(str) + "'"
233
+ if (str.indexOf('"') === -1) return '"' + escapeString(str) + '"'
234
+ if (str.indexOf('`') === -1) return '`' + escapeString(str) + '`'
235
+
236
+ return "'" + escapeString(str, true) + "'"
237
+ }
238
+
239
+ function escapeString (str, singled = false) {
240
+ str = str
241
+ .replace(/[\\]/g, '\\\\')
242
+ .replace(/[/]/g, '\\/')
243
+ .replace(/[\b]/g, '\\b')
244
+ .replace(/[\f]/g, '\\f')
245
+ .replace(/[\n]/g, '\\n')
246
+ .replace(/[\r]/g, '\\r')
247
+ .replace(/[\t]/g, '\\t')
248
+
249
+ if (singled) str = str.replace(/[']/g, '\\\'')
250
+
251
+ return str
252
+ }
253
+
254
+ function adaptStream (stream) {
255
+ if (typeof stream === 'function') {
256
+ return { isTTY: true, write: stream }
257
+ }
258
+ return stream
259
+ }
260
+
261
+ class Paint {
262
+ constructor (crayon) {
263
+ this.prints = []
264
+ this.width = { all: 0 }
265
+ this.crayon = crayon
266
+ }
267
+
268
+ push (type, chunk = null, opts = {}) {
269
+ if (Array.isArray(chunk)) return chunk.forEach(value => this.push(type, value, opts))
270
+
271
+ let color = opts.crayon
272
+
273
+ if (chunk && typeof chunk === 'object') {
274
+ color = chunk.crayon
275
+ chunk = chunk.out
276
+ }
277
+
278
+ if (chunk !== null) {
279
+ this.width.all += chunk.length // + it's not including spaces as it's mostly dynamic
280
+
281
+ if (opts && opts.id !== undefined) {
282
+ if (!this.width[opts.id]) this.width[opts.id] = { self: 0, child: 0 }
283
+ this.width[opts.id].self += chunk.length
284
+ }
285
+ }
286
+
287
+ if (color) chunk = this.crayon[color](chunk)
288
+
289
+ this.prints.push({ type, chunk, ...opts })
290
+ }
291
+
292
+ done () {
293
+ let output = ''
294
+
295
+ for (const print of this.prints) {
296
+ // raw
297
+ if (['open', 'close', 'key', 'value', 'separator', 'space', 'break-line'].indexOf(print.type) > -1) {
298
+ output += print.chunk
299
+ continue
300
+ }
301
+
302
+ // dynamic
303
+ if (print.type === 'spacing-start' || print.type === 'spacing-sep' || print.type === 'spacing-end') {
304
+ const type = print.type.replace('spacing-', '')
305
+ const totalWidth = this.width[print.id] ? (this.width[print.id].self + this.width[print.id].child) : this.width.all
306
+ const expand = print.isInts || print.isArray || totalWidth > 60 // + 64? double check after colors fix
307
+ let arrayBreakpoint = 0
308
+
309
+ if (print.isInts || print.isArray) {
310
+ const lengths = [7, 9, 13, 17, 23, 29, 37, 45, 53]
311
+
312
+ for (let i = lengths.length - 1; i >= 0; i--) {
313
+ if (print.arg.length >= lengths[i]) {
314
+ arrayBreakpoint = 4 + i // Range: 4-12
315
+ break
316
+ }
317
+ }
318
+ }
319
+
320
+ if (print.isBuffer) {
321
+ if (type === 'start' || type === 'sep') output += ' '
322
+ continue
323
+ }
324
+
325
+ if (print.isInts || print.isArray) {
326
+ const addSpacing = arrayBreakpoint !== 0
327
+ const skipSpacing = type === 'sep' && !(print.k % arrayBreakpoint === 0)
328
+
329
+ if (!addSpacing || skipSpacing) {
330
+ output += ' '
331
+ continue
332
+ }
333
+ }
334
+
335
+ if (!expand) output += ' '
336
+ else output += '\n' + ' '.repeat(print.levels - (type === 'end' ? 1 : 0))
337
+
338
+ continue
339
+ }
340
+
341
+ if (print.type === 'more') {
342
+ const addSpacing = !print.isBuffer
343
+ const sep = print.isBuffer ? '' : ','
344
+
345
+ output += addSpacing ? (',\n' + ' '.repeat(print.levels)) : (sep + ' ')
346
+ output += '... ' + print.left + ' more ' + (print.isBuffer ? 'byte' : 'item') + (print.left >= 2 ? 's' : '')
347
+
348
+ continue
349
+ }
350
+
351
+ throw new Error('Invalid print: ' + JSON.stringify(print))
352
+ }
353
+
354
+ return output
355
+ }
356
+ }
357
+
358
+ function isObjectEmpty (obj) {
359
+ for (const k in obj) return false // eslint-disable-line no-unreachable-loop
360
+ return true
361
+ }
362
+
363
+ function isKindOfAlphaNumeric (str) {
364
+ for (let i = 0; i < str.length; i++) {
365
+ const code = str.charCodeAt(i)
366
+
367
+ // first char, and numeric (0-9)
368
+ if (i === 0 && (code > 47 && code < 58)) return false
369
+
370
+ if (!(code > 47 && code < 58) && // numeric (0-9)
371
+ !(code > 64 && code < 91) && // upper alpha (A-Z)
372
+ !(code > 96 && code < 123) && // lower alpha (a-z)
373
+ !(code === 95)) { // underscore (_)
374
+ return false
375
+ }
376
+ }
377
+
378
+ return true
379
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "bare-console",
3
+ "version": "2.0.2",
4
+ "description": "Simple debugging console for JavaScript",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js"
8
+ ],
9
+ "scripts": {
10
+ "test": "standard && bare test.js"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/holepunchto/bare-console.git"
15
+ },
16
+ "author": "Holepunch",
17
+ "license": "Apache-2.0",
18
+ "bugs": {
19
+ "url": "https://github.com/holepunchto/bare-console/issues"
20
+ },
21
+ "homepage": "https://github.com/holepunchto/bare-console#readme",
22
+ "dependencies": {
23
+ "tiny-crayon": "^1.0.4"
24
+ },
25
+ "devDependencies": {
26
+ "brittle": "^3.1.1",
27
+ "standard": "^17.0.0",
28
+ "streamx": "^2.13.2"
29
+ }
30
+ }