-
Notifications
You must be signed in to change notification settings - Fork 25
/
print.js
40 lines (38 loc) · 1.32 KB
/
print.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
38
39
40
const $ = require('programmatic')
const util = require('util')
function print (value, annotate, annotation, depth, matched) {
switch (typeof value) {
case 'object':
if (Array.isArray(value)) {
if (annotate.length == 0) {
return util.inspect(value, { depth: null })
}
} else {
const properties = []
for (const property in value) {
if (depth != 0 || annotate[0] == property) {
if (annotate.length == 1 && matched && property == annotate[0]) {
properties.push(`// ${annotation}`)
}
const printed = print(value[property], annotate.slice(1), annotation, depth + 1, annotate[0] == property)
const object = {}
object[property] = null
const name = /^{\s+([^:]+)/.exec(util.inspect(object))
properties.push(`${name[1]}: ${printed}`)
}
}
return $(`
{
`, properties.join('\n'), `
}
`)
}
break
case 'number':
return String(value)
break
}
}
module.exports = function (value, annotate, annotation) {
return print(value, annotate, annotation, 0, true)
}