@@ -129,21 +129,24 @@ function getHexStyleCache() {
129129 return hexStyleCache ;
130130}
131131
132+ function codesToStyle ( codes ) {
133+ const openNum = codes [ 0 ] ;
134+ return {
135+ __proto__ : null ,
136+ openSeq : kEscape + openNum + kEscapeEnd ,
137+ closeSeq : kEscape + codes [ 1 ] + kEscapeEnd ,
138+ keepClose : openNum === kDimCode || openNum === kBoldCode ,
139+ } ;
140+ }
141+
132142function getStyleCache ( ) {
133143 if ( styleCache === undefined ) {
134144 styleCache = { __proto__ : null } ;
135145 const colors = inspect . colors ;
136146 for ( const key of ObjectGetOwnPropertyNames ( colors ) ) {
137147 const codes = colors [ key ] ;
138148 if ( codes ) {
139- const openNum = codes [ 0 ] ;
140- const closeNum = codes [ 1 ] ;
141- styleCache [ key ] = {
142- __proto__ : null ,
143- openSeq : kEscape + openNum + kEscapeEnd ,
144- closeSeq : kEscape + closeNum + kEscapeEnd ,
145- keepClose : openNum === kDimCode || openNum === kBoldCode ,
146- } ;
149+ styleCache [ key ] = codesToStyle ( codes ) ;
147150 }
148151 }
149152 }
@@ -241,10 +244,10 @@ function rgbToAnsi24Bit(r, g, b) {
241244 */
242245function styleText ( format , text , options ) {
243246 const validateStream = options ?. validateStream ?? true ;
244- const cache = getStyleCache ( ) ;
245247
246248 // Fast path: single format string with validateStream=false
247249 if ( ! validateStream && typeof format === 'string' && typeof text === 'string' ) {
250+ const cache = getStyleCache ( ) ;
248251 if ( format === 'none' ) return text ;
249252 const style = cache [ format ] ;
250253 if ( style !== undefined ) {
@@ -284,6 +287,7 @@ function styleText(format, text, options) {
284287 }
285288
286289 const formatArray = ArrayIsArray ( format ) ? format : [ format ] ;
290+ const colors = inspect . colors ;
287291
288292 let openCodes = '' ;
289293 let closeCodes = '' ;
@@ -293,30 +297,27 @@ function styleText(format, text, options) {
293297 if ( key === 'none' ) continue ;
294298
295299 if ( typeof key === 'string' && key [ 0 ] === '#' ) {
296- let hexStyle = getHexStyleCache ( ) . get ( key ) ;
297- if ( hexStyle === undefined ) {
298- if ( RegExpPrototypeExec ( hexColorRegExp , key ) === null ) {
299- throw new ERR_INVALID_ARG_VALUE ( 'format' , key ,
300- 'must be a valid hex color (#RGB or #RRGGBB)' ) ;
301- }
302- if ( skipColorize ) continue ;
303- hexStyle = getHexStyle ( key ) ;
304- } else if ( skipColorize ) {
305- continue ;
300+ if ( RegExpPrototypeExec ( hexColorRegExp , key ) === null ) {
301+ throw new ERR_INVALID_ARG_VALUE ( 'format' , key ,
302+ 'must be a valid hex color (#RGB or #RRGGBB)' ) ;
306303 }
307- openCodes += hexStyle . openSeq ;
308- closeCodes = hexStyle . closeSeq + closeCodes ;
309- processedText = replaceCloseCode ( processedText , hexStyle . closeSeq , hexStyle . openSeq , false ) ;
304+ if ( skipColorize ) continue ;
305+ const { 0 : r , 1 : g , 2 : b } = hexToRgb ( key ) ;
306+ const hexOpenSeq = kEscape + rgbToAnsi24Bit ( r , g , b ) + kEscapeEnd ;
307+ openCodes += hexOpenSeq ;
308+ closeCodes = kHexCloseSeq + closeCodes ;
309+ processedText = replaceCloseCode ( processedText , kHexCloseSeq , hexOpenSeq , false ) ;
310310 continue ;
311311 }
312312
313- const style = cache [ key ] ;
314- if ( style === undefined ) {
313+ const codes = colors [ key ] ;
314+ if ( ! codes ) {
315315 validateOneOf ( key , 'format' , ObjectGetOwnPropertyNames ( inspect . colors ) ) ;
316316 }
317- openCodes += style . openSeq ;
318- closeCodes = style . closeSeq + closeCodes ;
319- processedText = replaceCloseCode ( processedText , style . closeSeq , style . openSeq , style . keepClose ) ;
317+ const { openSeq, closeSeq, keepClose } = codesToStyle ( codes ) ;
318+ openCodes += openSeq ;
319+ closeCodes = closeSeq + closeCodes ;
320+ processedText = replaceCloseCode ( processedText , closeSeq , openSeq , keepClose ) ;
320321 }
321322
322323 if ( skipColorize ) return text ;
0 commit comments