@@ -119,7 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
119
119
validateString ( text , 'text' ) ;
120
120
validateBoolean ( validateStream , 'options.validateStream' ) ;
121
121
122
- let skipColorize ;
122
+ let shouldColorize = true ;
123
123
if ( validateStream ) {
124
124
if (
125
125
! isReadableStream ( stream ) &&
@@ -130,26 +130,29 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
130
130
}
131
131
132
132
// If the stream is falsy or should not be colorized, set skipColorize to true
133
- skipColorize = ! lazyUtilColors ( ) . shouldColorize ( stream ) ;
133
+ shouldColorize = lazyUtilColors ( ) . shouldColorize ( stream ) ;
134
134
}
135
135
136
136
// If the format is not an array, convert it to an array
137
137
const formatArray = ArrayIsArray ( format ) ? format : [ format ] ;
138
+ const { colors } = inspect ;
138
139
139
- let left = '' ;
140
- let right = '' ;
141
- for ( const key of formatArray ) {
142
- const formatCodes = inspect . colors [ key ] ;
140
+ // We want to loop through the format array to check if the format is valid
141
+ // including if it's shouldn't be colorized
142
+ const { left , right } = formatArray . reduce ( ( acc , key ) => {
143
+ const formatCodes = colors [ key ] ;
143
144
// If the format is not a valid style, throw an error
144
145
if ( formatCodes == null ) {
145
- validateOneOf ( key , 'format' , ObjectKeys ( inspect . colors ) ) ;
146
+ validateOneOf ( key , 'format' , ObjectKeys ( colors ) ) ;
146
147
}
147
- if ( skipColorize ) continue ;
148
- left += escapeStyleCode ( formatCodes [ 0 ] ) ;
149
- right = `${ escapeStyleCode ( formatCodes [ 1 ] ) } ${ right } ` ;
150
- }
148
+ if ( shouldColorize ) {
149
+ acc . left += escapeStyleCode ( formatCodes [ 0 ] ) ;
150
+ acc . right = `${ escapeStyleCode ( formatCodes [ 1 ] ) } ${ acc . right } ` ;
151
+ }
152
+ return acc ;
153
+ } , { left : '' , right : '' } ) ;
151
154
152
- return skipColorize ? text : `${ left } ${ text } ${ right } ` ;
155
+ return shouldColorize ? `${ left } ${ text } ${ right } ` : text ;
153
156
}
154
157
155
158
/**
0 commit comments