8
8
* @property {boolean | null | undefined } [showPositions=true]
9
9
* Whether to include positional information (default: `true`).
10
10
*
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
+ *
11
22
* @typedef State
12
23
* Info passed around.
13
24
* @property {boolean } showPositions
@@ -31,10 +42,15 @@ export const inspect = color ? inspectColor : inspectNoColor
31
42
32
43
const own = { } . hasOwnProperty
33
44
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
+ }
38
54
39
55
// ANSI color regex.
40
56
/* eslint-disable no-control-regex */
@@ -132,15 +148,16 @@ function inspectNodes(nodes, state) {
132
148
133
149
while ( ++ index < nodes . length ) {
134
150
result . push (
135
- dim (
151
+ style . dim (
136
152
( index < nodes . length - 1 ? '├' : '└' ) +
137
153
'─' +
138
154
String ( index ) . padEnd ( size )
139
155
) +
140
156
' ' +
141
157
indent (
142
158
inspectValue ( nodes [ index ] , state ) ,
143
- ( index < nodes . length - 1 ? dim ( '│' ) : ' ' ) + ' ' . repeat ( size + 2 ) ,
159
+ ( index < nodes . length - 1 ? style . dim ( '│' ) : ' ' ) +
160
+ ' ' . repeat ( size + 2 ) ,
144
161
true
145
162
)
146
163
)
@@ -205,14 +222,17 @@ function inspectFields(object, state) {
205
222
}
206
223
207
224
result . push (
208
- key + dim ( ':' ) + ( / \s / . test ( formatted . charAt ( 0 ) ) ? '' : ' ' ) + formatted
225
+ key +
226
+ style . dim ( ':' ) +
227
+ ( / \s / . test ( formatted . charAt ( 0 ) ) ? '' : ' ' ) +
228
+ formatted
209
229
)
210
230
}
211
231
212
232
return indent (
213
233
result . join ( '\n' ) ,
214
234
( isArrayUnknown ( object . children ) && object . children . length > 0
215
- ? dim ( '│' )
235
+ ? style . dim ( '│' )
216
236
: ' ' ) + ' '
217
237
)
218
238
}
@@ -253,7 +273,7 @@ function inspectTree(node, state) {
253
273
* Formatted node.
254
274
*/
255
275
function formatNode ( node , state ) {
256
- const result = [ bold ( node . type ) ]
276
+ const result = [ style . bold ( node . type ) ]
257
277
// Cast as record to allow indexing.
258
278
const map = /** @type {Record<string, unknown> } */ (
259
279
/** @type {unknown } */ ( node )
@@ -266,13 +286,17 @@ function formatNode(node, state) {
266
286
}
267
287
268
288
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
+ )
270
294
} else if ( typeof map . value === 'string' ) {
271
- result . push ( ' ' , green ( inspectNonTree ( map . value ) ) )
295
+ result . push ( ' ' , style . green ( inspectNonTree ( map . value ) ) )
272
296
}
273
297
274
298
if ( position ) {
275
- result . push ( ' ' , dim ( '(' ) , position , dim ( ')' ) )
299
+ result . push ( ' ' , style . dim ( '(' ) , position , style . dim ( ')' ) )
276
300
}
277
301
278
302
return result . join ( '' )
0 commit comments