-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.ts
33 lines (32 loc) · 854 Bytes
/
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
export function stringify(value: unknown): string {
if (value == null) {
return 'null'
}
switch (typeof value) {
case 'string':
return value
case 'bigint':
case 'boolean':
case 'number':
case 'symbol':
return String(value)
case 'function':
return value.toString()
case 'object':
if (value instanceof Error) {
return `${value.name}: ${value.message}`
}
try {
if ('toString' in value) {
const string = value.toString()
if (string !== '[object Object]') return string
}
return `{ ${value} ${Object.keys(value).join(', ')} }`
} catch {
// toString() threw - maybe because we accessed it on a prototype.
return `{ [Object] ${Object.keys(value).join(', ')} }`
}
default:
return `${value}`
}
}