7
7
* Configuration.
8
8
* @property {boolean | null | undefined } [showPositions=true]
9
9
* Whether to include positional information (default: `true`).
10
+ * @property {boolean | null | undefined } [colors]
11
+ * Whether to include ANSI colors (default: `true` on Node, `false` otherwise).
10
12
*
11
13
* @typedef Style
12
14
* Styling functions.
27
29
* Rendering stylization.
28
30
*/
29
31
30
- import { color } from '#conditional-color'
32
+ import { color as useColorByDefault } from '#conditional-color'
31
33
32
34
/**
33
35
* Inspect a node, with color in Node, without color in browsers.
34
36
*
35
- * @param tree
36
- * Tree to inspect.
37
- * @param options
38
- * Configuration (optional).
39
- * @returns
40
- * Pretty printed `tree`.
41
- */
42
- /* c8 ignore next */
43
- export const inspect = color ? inspectColor : inspectNoColor
44
-
45
- const own = { } . hasOwnProperty
46
-
47
- /**
48
- * Inspect a node, without color.
49
- *
50
37
* @param {unknown } tree
51
38
* Tree to inspect.
52
39
* @param {Options | null | undefined } [options]
53
40
* Configuration.
54
41
* @returns {string }
55
42
* Pretty printed `tree`.
56
43
*/
57
- export function inspectNoColor ( tree , options ) {
44
+ /* c8 ignore next */
45
+ export function inspect ( tree , options ) {
46
+ const useColor =
47
+ ! options || options . colors === null || options . colors === undefined
48
+ ? useColorByDefault
49
+ : options . colors
50
+
51
+ const style = useColor
52
+ ? {
53
+ bold : ansiColor ( 1 , 22 ) ,
54
+ dim : ansiColor ( 2 , 22 ) ,
55
+ yellow : ansiColor ( 33 , 39 ) ,
56
+ green : ansiColor ( 32 , 39 )
57
+ }
58
+ : {
59
+ bold : noColor ,
60
+ dim : noColor ,
61
+ yellow : noColor ,
62
+ green : noColor
63
+ }
64
+
58
65
/** @type {State } */
59
66
const state = {
60
- style : {
61
- bold : noColor ,
62
- dim : noColor ,
63
- yellow : noColor ,
64
- green : noColor
65
- } ,
67
+ style,
66
68
showPositions :
67
69
! options ||
68
70
options . showPositions === null ||
@@ -74,34 +76,48 @@ export function inspectNoColor(tree, options) {
74
76
return inspectValue ( tree , state )
75
77
}
76
78
79
+ const own = { } . hasOwnProperty
80
+
81
+ /**
82
+ * Inspect a node, without color.
83
+ *
84
+ * @deprecated
85
+ * Use `inspect` with the option `{colors: false}`.
86
+ *
87
+ * @param {unknown } tree
88
+ * Tree to inspect.
89
+ * @param {Omit<Options, 'colors'> | null | undefined } [options]
90
+ * Configuration.
91
+ * @returns {string }
92
+ * Pretty printed `tree`.
93
+ */
94
+ export function inspectNoColor ( tree , options ) {
95
+ /* c8 ignore next 3 */
96
+ const optionsWithNoColor = useColorByDefault
97
+ ? Object . assign ( { } , options , { colors : false } )
98
+ : options
99
+ return inspect ( tree , optionsWithNoColor )
100
+ }
101
+
77
102
/**
78
103
* Inspects a node, using color.
79
104
*
105
+ * @deprecated
106
+ * Use `inspect` with the option `{colors: true}`.
107
+ *
80
108
* @param {unknown } tree
81
109
* Tree to inspect.
82
- * @param {Options | null | undefined } [options]
110
+ * @param {Omit< Options, 'colors'> | null | undefined } [options]
83
111
* Configuration (optional).
84
112
* @returns {string }
85
113
* Pretty printed `tree`.
86
114
*/
87
115
export function inspectColor ( tree , options ) {
88
- /** @type {State } */
89
- const state = {
90
- style : {
91
- bold : ansiColor ( 1 , 22 ) ,
92
- dim : ansiColor ( 2 , 22 ) ,
93
- yellow : ansiColor ( 33 , 39 ) ,
94
- green : ansiColor ( 32 , 39 )
95
- } ,
96
- showPositions :
97
- ! options ||
98
- options . showPositions === null ||
99
- options . showPositions === undefined
100
- ? true
101
- : options . showPositions
102
- }
103
-
104
- return inspectValue ( tree , state )
116
+ /* c8 ignore next 3 */
117
+ const optionsWithColor = useColorByDefault
118
+ ? options
119
+ : Object . assign ( { } , options , { colors : true } )
120
+ return inspect ( tree , optionsWithColor )
105
121
}
106
122
107
123
/**
0 commit comments