Skip to content

Commit 5531ca9

Browse files
committed
manage helpers
1 parent 48b0510 commit 5531ca9

2 files changed

Lines changed: 30 additions & 43 deletions

File tree

src/debug.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export const debug = (namespace?: string, elapsedTime = true) => {
2323

2424
return (...rest: any[]): void => {
2525
// check if debug env exist in both environments
26-
if (!showLog(isBrowser ? localStorage.getItem("debug") : process.env.DEBUG))
26+
if (
27+
!showLog(isBrowser() ? localStorage.getItem("debug") : process.env.DEBUG)
28+
)
2729
return
2830

2931
// Calculate elapsed time for each namespace to avoid global state & Cleanup if needed
@@ -47,7 +49,7 @@ export const debug = (namespace?: string, elapsedTime = true) => {
4749
/**
4850
* Browser environment
4951
*/
50-
if (isBrowser) {
52+
if (isBrowser()) {
5153
const colorStyle = `color: rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]});`
5254
const args = []
5355

src/helpers.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,58 @@
11
/**
22
* Check if is browser env
33
*/
4-
export const isBrowser: boolean = !!(
5-
typeof window != "undefined" && window.document
6-
)
4+
export function isBrowser(): boolean {
5+
return typeof window !== "undefined"
6+
}
77

88
/**
99
* Transform string to RGB
1010
* @param str
1111
*/
1212
export function stringToRgb(str: string): [number, number, number] {
1313
if (!str) return [128, 128, 128]
14-
14+
1515
// Add a salt to make numbers at the end produce different colors
16-
const salt = 'x7f2q9';
17-
const stringToHash = str + salt;
18-
16+
const salt = "x7f2q9"
17+
const stringToHash = str + salt
18+
1919
let hash = 0
2020
for (let i = 0; i < stringToHash.length; i++) {
2121
let character = stringToHash.charCodeAt(i)
2222
hash = (hash << 5) - hash + character
2323
hash = Math.abs(hash & hash)
2424
}
25-
25+
2626
// Create more variance in the RGB values
2727
const r = (hash & 0xff0000) >> 16
2828
const g = ((hash >> 3) & 0x00ff00) >> 8
29-
const b = ((hash >> 6) & 0x0000ff)
30-
29+
const b = (hash >> 6) & 0x0000ff
30+
3131
return [r, g, b]
3232
}
3333

3434
/**
3535
* ansi RGB
3636
*/
37+
// Wraper for ansi 256 code
38+
const _wrapAnsi256 = (code) => `\u001B[${38};5;${code}m`
39+
// Convert RGB color to ansi 256
40+
const _rgbToAnsi256 = (red: number, green: number, blue: number): number => {
41+
if (red === green && green === blue) {
42+
if (red < 8) return 16
43+
if (red > 248) return 231
44+
return Math.round(((red - 8) / 247) * 24) + 232
45+
}
46+
return (
47+
16 +
48+
36 * Math.round((red / 255) * 5) +
49+
6 * Math.round((green / 255) * 5) +
50+
Math.round((blue / 255) * 5)
51+
)
52+
}
3753
export function ansiRgb(r: number, g: number, b: number) {
3854
return function (str: string): string {
3955
const _close = "\u001B[39m"
40-
41-
/**
42-
* Wrapper for ansi 256 code
43-
* @param code
44-
*/
45-
const _wrapAnsi256 = (code) => `\u001B[${38};5;${code}m`
46-
47-
/**
48-
* Convert RGB color to ansi 256
49-
* @param red
50-
* @param green
51-
* @param blue
52-
*/
53-
const _rgbToAnsi256 = (
54-
red: number,
55-
green: number,
56-
blue: number
57-
): number => {
58-
if (red === green && green === blue) {
59-
if (red < 8) return 16
60-
if (red > 248) return 231
61-
return Math.round(((red - 8) / 247) * 24) + 232
62-
}
63-
return (
64-
16 +
65-
36 * Math.round((red / 255) * 5) +
66-
6 * Math.round((green / 255) * 5) +
67-
Math.round((blue / 255) * 5)
68-
)
69-
}
70-
7156
return _wrapAnsi256(_rgbToAnsi256(r, g, b)) + str + _close
7257
}
7358
}

0 commit comments

Comments
 (0)