@@ -236,6 +236,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
236
236
validateString ( text , 'text' ) ;
237
237
validateBoolean ( validateStream , 'options.validateStream' ) ;
238
238
239
+ let skipColorize ;
239
240
if ( validateStream ) {
240
241
if (
241
242
! isReadableStream ( stream ) &&
@@ -244,40 +245,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
244
245
) {
245
246
throw new ERR_INVALID_ARG_TYPE ( 'stream' , [ 'ReadableStream' , 'WritableStream' , 'Stream' ] , stream ) ;
246
247
}
247
- }
248
-
249
- if ( ArrayIsArray ( format ) ) {
250
- let left = '' ;
251
- let right = '' ;
252
- for ( const key of format ) {
253
- const formatCodes = inspect . colors [ key ] ;
254
- if ( formatCodes == null ) {
255
- validateOneOf ( key , 'format' , ObjectKeys ( inspect . colors ) ) ;
256
- }
257
- left += escapeStyleCode ( formatCodes [ 0 ] ) ;
258
- right = `${ escapeStyleCode ( formatCodes [ 1 ] ) } ${ right } ` ;
259
- }
260
248
261
- return `${ left } ${ text } ${ right } ` ;
249
+ // If the stream is falsy or should not be colorized, set skipColorize to true
250
+ skipColorize = ! lazyUtilColors ( ) . shouldColorize ( stream ) ;
262
251
}
263
252
264
- const formatCodes = inspect . colors [ format ] ;
265
- if ( formatCodes == null ) {
266
- validateOneOf ( format , 'format' , ObjectKeys ( inspect . colors ) ) ;
267
- }
253
+ // If the format is not an array, convert it to an array
254
+ const formatArray = ArrayIsArray ( format ) ? format : [ format ] ;
268
255
269
- // Check colorize only after validating arg type and value
270
- if (
271
- validateStream &&
272
- (
273
- ! stream ||
274
- ! lazyUtilColors ( ) . shouldColorize ( stream )
275
- )
276
- ) {
277
- return text ;
256
+ let left = '' ;
257
+ let right = '' ;
258
+ for ( const key of formatArray ) {
259
+ const formatCodes = inspect . colors [ key ] ;
260
+ // If the format is not a valid style, throw an error
261
+ if ( formatCodes == null ) {
262
+ validateOneOf ( key , 'format' , ObjectKeys ( inspect . colors ) ) ;
263
+ }
264
+ if ( skipColorize ) continue ;
265
+ left += escapeStyleCode ( formatCodes [ 0 ] ) ;
266
+ right = `${ escapeStyleCode ( formatCodes [ 1 ] ) } ${ right } ` ;
278
267
}
279
268
280
- return `${ escapeStyleCode ( formatCodes [ 0 ] ) } ${ text } ${ escapeStyleCode ( formatCodes [ 1 ] ) } ` ;
269
+ return skipColorize ? text : `${ left } ${ text } ${ right } ` ;
281
270
}
282
271
283
272
const months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' ,
0 commit comments