@@ -56,57 +56,6 @@ export const field = <T>(name: string, value: T): Field<T> => {
5656 return new Field ( name , value ) ;
5757} ;
5858
59- /**
60- * Hashes a string.
61- */
62- const djb2 = ( str : string ) : number => {
63- let hash = 5381 ;
64- for ( let i = 0 ; i < str . length ; i ++ ) {
65- hash = ( ( hash << 5 ) + hash ) + str . charCodeAt ( i ) ; /* hash * 33 + c */
66- }
67-
68- return hash ;
69- } ;
70-
71- /**
72- * Convert rgb to hex.
73- */
74- const rgbToHex = ( r : number , g : number , b : number ) : string => {
75- const integer = ( ( Math . round ( r ) & 0xFF ) << 16 )
76- + ( ( Math . round ( g ) & 0xFF ) << 8 )
77- + ( Math . round ( b ) & 0xFF ) ;
78-
79- const str = integer . toString ( 16 ) ;
80-
81- return "#" + "000000" . substring ( str . length ) + str ;
82- } ;
83-
84- /**
85- * Convert fully-formed hex to rgb.
86- */
87- const hexToRgb = ( hex : string ) : [ number , number , number ] => {
88- const integer = parseInt ( hex . substr ( 1 ) , 16 ) ;
89-
90- return [
91- ( integer >> 16 ) & 0xFF ,
92- ( integer >> 8 ) & 0xFF ,
93- integer & 0xFF ,
94- ] ;
95- } ;
96-
97- /**
98- * Generates a deterministic color from a string using hashing.
99- */
100- const hashStringToColor = ( str : string ) : string => {
101- const hash = djb2 ( str ) ;
102-
103- return rgbToHex (
104- ( hash & 0xFF0000 ) >> 16 ,
105- ( hash & 0x00FF00 ) >> 8 ,
106- hash & 0x0000FF ,
107- ) ;
108- } ;
109-
11059/**
11160 * This formats & builds text for logging.
11261 * It should only be used to build one log item at a time since it stores the
@@ -207,7 +156,7 @@ export class BrowserFormatter extends Formatter {
207156 */
208157export class ServerFormatter extends Formatter {
209158 public tag ( name : string , color : string ) : void {
210- const [ r , g , b ] = hexToRgb ( color ) ;
159+ const [ r , g , b ] = this . hexToRgb ( color ) ;
211160 while ( name . length < 5 ) {
212161 name += " " ;
213162 }
@@ -220,7 +169,7 @@ export class ServerFormatter extends Formatter {
220169 this . format += "\u001B[1m" ;
221170 }
222171 if ( color ) {
223- const [ r , g , b ] = hexToRgb ( color ) ;
172+ const [ r , g , b ] = this . hexToRgb ( color ) ;
224173 this . format += `\u001B[38;2;${ r } ;${ g } ;${ b } m` ;
225174 }
226175 this . format += this . getType ( arg ) ;
@@ -241,6 +190,19 @@ export class ServerFormatter extends Formatter {
241190 this . args . push ( JSON . stringify ( obj ) ) ;
242191 console . log ( ...this . flush ( ) ) ; // tslint:disable-line no-console
243192 }
193+
194+ /**
195+ * Convert fully-formed hex to rgb.
196+ */
197+ private hexToRgb ( hex : string ) : [ number , number , number ] {
198+ const integer = parseInt ( hex . substr ( 1 ) , 16 ) ;
199+
200+ return [
201+ ( integer >> 16 ) & 0xFF ,
202+ ( integer >> 8 ) & 0xFF ,
203+ integer & 0xFF ,
204+ ] ;
205+ }
244206}
245207
246208/**
@@ -258,7 +220,7 @@ export class Logger {
258220 private readonly defaultFields ?: FieldArray ,
259221 ) {
260222 if ( name ) {
261- this . nameColor = hashStringToColor ( name ) ;
223+ this . nameColor = this . hashStringToColor ( name ) ;
262224 }
263225 const envLevel = typeof global !== "undefined" && typeof global . process !== "undefined" ? global . process . env . LOG_LEVEL : process . env . LOG_LEVEL ;
264226 if ( envLevel ) {
@@ -401,7 +363,7 @@ export class Logger {
401363 const green = expPer < 1 ? max : min ;
402364 const red = expPer >= 1 ? max : min ;
403365 this . _formatter . push ( ` ${ time . identifier } =` , "#3794ff" ) ;
404- this . _formatter . push ( `${ diff } ms` , rgbToHex ( red > 0 ? red : 0 , green > 0 ? green : 0 , 0 ) ) ;
366+ this . _formatter . push ( `${ diff } ms` , this . rgbToHex ( red > 0 ? red : 0 , green > 0 ? green : 0 , 0 ) ) ;
405367 } ) ;
406368 }
407369
@@ -413,6 +375,44 @@ export class Logger {
413375 }
414376 // tslint:enable no-console
415377 }
378+
379+ /**
380+ * Hashes a string.
381+ */
382+ private djb2 ( str : string ) : number {
383+ let hash = 5381 ;
384+ for ( let i = 0 ; i < str . length ; i ++ ) {
385+ hash = ( ( hash << 5 ) + hash ) + str . charCodeAt ( i ) ; /* hash * 33 + c */
386+ }
387+
388+ return hash ;
389+ }
390+
391+ /**
392+ * Convert rgb to hex.
393+ */
394+ private rgbToHex ( r : number , g : number , b : number ) : string {
395+ const integer = ( ( Math . round ( r ) & 0xFF ) << 16 )
396+ + ( ( Math . round ( g ) & 0xFF ) << 8 )
397+ + ( Math . round ( b ) & 0xFF ) ;
398+
399+ const str = integer . toString ( 16 ) ;
400+
401+ return "#" + "000000" . substring ( str . length ) + str ;
402+ }
403+
404+ /**
405+ * Generates a deterministic color from a string using hashing.
406+ */
407+ private hashStringToColor ( str : string ) : string {
408+ const hash = this . djb2 ( str ) ;
409+
410+ return this . rgbToHex (
411+ ( hash & 0xFF0000 ) >> 16 ,
412+ ( hash & 0x00FF00 ) >> 8 ,
413+ hash & 0x0000FF ,
414+ ) ;
415+ }
416416}
417417
418418export const logger = new Logger (
0 commit comments