-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
37 lines (34 loc) · 1.17 KB
/
index.js
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
import chalk from 'chalk'
import { CID } from 'multiformats/cid'
const _pre = obj => {
if (Array.isArray(obj)) return '['
return '{'
}
const _post = obj => {
if (Array.isArray(obj)) return ']'
return '}'
}
function printify (obj, indent = 0, threshold = 80) {
const pre = Array.from({ length: indent }).map(() => ' ').join('')
const lines = []
for (const key in obj) {
const value = obj[key]
let line
if (Array.isArray(obj)) {
line = ''
} else {
line = chalk.green(key) + ': '
}
if (CID.asCID(value)) lines.push(line + 'CID(' + chalk.red(value.toString()) + ')')
else if (value instanceof Uint8Array) lines.push(line + 'Bytes(' + `{${chalk.green('size')}: ${chalk.red(value.length)}})`)
else if (typeof value === 'object' && value !== null) {
lines.push(line + printify(value, indent + 2, threshold - line.length))
} else {
lines.push(line + chalk.red(JSON.stringify(value)))
}
}
if (lines.join(', ').length < threshold) return _pre(obj) + ' ' + lines.join(', ') + ' ' + _post(obj)
const ipre = '\n' + pre + ' '
return _pre(obj) + ipre + lines.join(ipre) + '\n' + pre + _post(obj)
}
export default printify