/
display.ts
58 lines (50 loc) · 1.57 KB
/
display.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
// eslint-disable-next-line unicorn/prefer-node-protocol
import * as util from 'util'
// @ts-expect-error doesn't have types
import loupeImport from 'loupe'
interface LoupeOptions {
truncateThreshold?: number
}
const loupe = (typeof loupeImport.default === 'function' ? loupeImport.default : loupeImport)
export function format(...args: any[]) {
return util.format(...args)
}
export function utilInspect(item: unknown, options?: util.InspectOptions) {
return util.inspect(item, options)
}
// chai utils
export function loupeInspect(obj: unknown, options: LoupeOptions = {}): string {
return loupe(obj, {
depth: 2,
truncate: options.truncateThreshold === 0
? Infinity
: (options.truncateThreshold ?? 40),
})
}
export function objDisplay(obj: unknown, options: LoupeOptions = {}): string {
const truncateThreshold = options.truncateThreshold ?? 40
const str = loupeInspect(obj, options)
const type = Object.prototype.toString.call(obj)
if (truncateThreshold && str.length >= truncateThreshold) {
if (type === '[object Function]') {
const fn = obj as () => void
return (!fn.name || fn.name === '')
? '[Function]'
: `[Function: ${fn.name}]`
}
else if (type === '[object Array]') {
return `[ Array(${(obj as []).length}) ]`
}
else if (type === '[object Object]') {
const keys = Object.keys(obj as {})
const kstr = keys.length > 2
? `${keys.splice(0, 2).join(', ')}, ...`
: keys.join(', ')
return `{ Object (${kstr}) }`
}
else {
return str
}
}
return str
}