Skip to content

Commit 359110c

Browse files
committed
internal: factor colors into style object
1 parent df179f7 commit 359110c

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

lib/index.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
* @property {boolean | null | undefined} [showPositions=true]
99
* Whether to include positional information (default: `true`).
1010
*
11+
* @typedef Style
12+
* Styling functions.
13+
* @property {(_: string) => string} bold
14+
* Style a node type.
15+
* @property {(_: string) => string} dim
16+
* Style structural punctuation.
17+
* @property {(_: string) => string} yellow
18+
* Style the numeric count of node children.
19+
* @property {(_: string) => string} green
20+
* Style a non-tree value.
21+
*
1122
* @typedef State
1223
* Info passed around.
1324
* @property {boolean} showPositions
@@ -31,10 +42,15 @@ export const inspect = color ? inspectColor : inspectNoColor
3142

3243
const own = {}.hasOwnProperty
3344

34-
const bold = ansiColor(1, 22)
35-
const dim = ansiColor(2, 22)
36-
const yellow = ansiColor(33, 39)
37-
const green = ansiColor(32, 39)
45+
/**
46+
* @type Style
47+
*/
48+
const style = {
49+
bold: ansiColor(1, 22),
50+
dim: ansiColor(2, 22),
51+
yellow: ansiColor(33, 39),
52+
green: ansiColor(32, 39)
53+
}
3854

3955
// ANSI color regex.
4056
/* eslint-disable no-control-regex */
@@ -132,15 +148,16 @@ function inspectNodes(nodes, state) {
132148

133149
while (++index < nodes.length) {
134150
result.push(
135-
dim(
151+
style.dim(
136152
(index < nodes.length - 1 ? '├' : '└') +
137153
'─' +
138154
String(index).padEnd(size)
139155
) +
140156
' ' +
141157
indent(
142158
inspectValue(nodes[index], state),
143-
(index < nodes.length - 1 ? dim('│') : ' ') + ' '.repeat(size + 2),
159+
(index < nodes.length - 1 ? style.dim('│') : ' ') +
160+
' '.repeat(size + 2),
144161
true
145162
)
146163
)
@@ -205,14 +222,17 @@ function inspectFields(object, state) {
205222
}
206223

207224
result.push(
208-
key + dim(':') + (/\s/.test(formatted.charAt(0)) ? '' : ' ') + formatted
225+
key +
226+
style.dim(':') +
227+
(/\s/.test(formatted.charAt(0)) ? '' : ' ') +
228+
formatted
209229
)
210230
}
211231

212232
return indent(
213233
result.join('\n'),
214234
(isArrayUnknown(object.children) && object.children.length > 0
215-
? dim('│')
235+
? style.dim('│')
216236
: ' ') + ' '
217237
)
218238
}
@@ -253,7 +273,7 @@ function inspectTree(node, state) {
253273
* Formatted node.
254274
*/
255275
function formatNode(node, state) {
256-
const result = [bold(node.type)]
276+
const result = [style.bold(node.type)]
257277
// Cast as record to allow indexing.
258278
const map = /** @type {Record<string, unknown>} */ (
259279
/** @type {unknown} */ (node)
@@ -266,13 +286,17 @@ function formatNode(node, state) {
266286
}
267287

268288
if (isArrayUnknown(map.children)) {
269-
result.push(dim('['), yellow(String(map.children.length)), dim(']'))
289+
result.push(
290+
style.dim('['),
291+
style.yellow(String(map.children.length)),
292+
style.dim(']')
293+
)
270294
} else if (typeof map.value === 'string') {
271-
result.push(' ', green(inspectNonTree(map.value)))
295+
result.push(' ', style.green(inspectNonTree(map.value)))
272296
}
273297

274298
if (position) {
275-
result.push(' ', dim('('), position, dim(')'))
299+
result.push(' ', style.dim('('), position, style.dim(')'))
276300
}
277301

278302
return result.join('')

0 commit comments

Comments
 (0)