forked from denosaurs/debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.ts
67 lines (63 loc) · 1.44 KB
/
colors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { colors } from "./deps.ts";
// @ts-ignore: lib
const isBrowser = typeof Deno === "undefined" &&
// @ts-ignore: lib
typeof process === "undefined" && typeof window !== "undefined" &&
// @ts-ignore: lib
typeof document !== "undefined";
export type ColorFunction = (message: string) => string[];
export const colorFunctions: ColorFunction[] = [
(v) => {
if (isBrowser) {
return [`%c${v}`, "color: red"];
} else {
return [colors.red(v)];
}
},
(v) => {
if (isBrowser) {
return [`%c${v}`, "color: green"];
} else {
return [colors.green(v)];
}
},
(v) => {
if (isBrowser) {
return [`%c${v}`, "color: yellow"];
} else {
return [colors.yellow(v)];
}
},
(v) => {
if (isBrowser) {
return [`%c${v}`, "color: blue"];
} else {
return [colors.blue(v)];
}
},
(v) => {
if (isBrowser) {
return [`%c${v}`, "color: magenta"];
} else {
return [colors.magenta(v)];
}
},
(v) => {
if (isBrowser) {
return [`%c${v}`, "color: cyan"];
} else {
return [colors.cyan(v)];
}
},
];
function hashCode(s: string): number {
let h = 0;
const l = s.length;
let i = 0;
if (l > 0) while (i < l) h = ((h << 5) - h + s.charCodeAt(i++)) | 0;
return h;
}
export function generateColor(message: string): ColorFunction {
const hash = Math.abs(hashCode(message));
return colorFunctions[hash % colorFunctions.length];
}