-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.ts
141 lines (132 loc) · 2.08 KB
/
utils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { env } = Deno;
import { camelCase } from "https://raw.githubusercontent.com/denolib/camelcase/master/mod.ts";
// We assume the terminal supports colors
export const colors = [
20,
21,
26,
27,
32,
33,
38,
39,
40,
41,
42,
43,
44,
45,
56,
57,
62,
63,
68,
69,
74,
75,
76,
77,
78,
79,
80,
81,
92,
93,
98,
99,
112,
113,
128,
129,
134,
135,
148,
149,
160,
161,
162,
163,
164,
165,
166,
167,
168,
169,
170,
171,
172,
173,
178,
179,
184,
185,
196,
197,
198,
199,
200,
201,
202,
203,
204,
205,
206,
207,
208,
209,
214,
215,
220,
221,
];
/**
* Selects a color for a debug namespace
*/
export function selectColor(namespace: string): number {
let hash = 0;
for (let i = 0; i < namespace.length; i++) {
hash = (hash << 5) - hash + namespace.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return colors[Math.abs(hash) % colors.length];
}
export function getInspectOpts(): Deno.InspectOptions {
const currentEnv = env.toObject();
const inspectOpts: Deno.InspectOptions = Object.keys(currentEnv)
.filter((key) => /^debug_/i.test(key))
.reduce((obj: { [key: string]: number | boolean | null }, key) => {
const prop = camelCase(key.slice(6));
let envVar: string = currentEnv[key];
let val: boolean | number | null;
if (/^(yes|on|true|enabled)$/i.test(envVar)) {
val = true;
} else if (/^(no|off|false|disabled)$/i.test(envVar)) {
val = false;
} else if (envVar === "null") {
val = null;
} else {
val = Number(envVar);
}
obj[prop] = val;
return obj;
}, {});
return inspectOpts;
}
/**
* Coerce `val`.
*/
export function coerce(val: any): any {
if (val instanceof Error) {
return val.stack || val.message;
}
return val;
}
/**
* Convert regexp to namespace
*/
export function regexpToNamespace(regexp: RegExp): string {
return regexp
.toString()
.substring(2, regexp.toString().length - 2)
.replace(/\.\*\?$/, "*");
}