|
|
|
@ -3,18 +3,19 @@ const indent = require('indent-string')
|
|
|
|
|
const config = require('./config')
|
|
|
|
|
|
|
|
|
|
function print(input, options = {}) {
|
|
|
|
|
const {expanded, highlight} = options
|
|
|
|
|
const {expanded, highlight, currentPath} = options
|
|
|
|
|
const index = new Map()
|
|
|
|
|
let row = 0
|
|
|
|
|
|
|
|
|
|
function format(text, style) {
|
|
|
|
|
function format(text, style, path) {
|
|
|
|
|
if (!highlight) {
|
|
|
|
|
return style(text)
|
|
|
|
|
}
|
|
|
|
|
const highlightStyle = (currentPath === path) ? config.highlightCurrent : config.highlight
|
|
|
|
|
return text
|
|
|
|
|
.replace(highlight, s => '<fx>' + s + '<fx>')
|
|
|
|
|
.split(/<fx>/g)
|
|
|
|
|
.map((s, i) => i % 2 !== 0 ? config.highlight(s) : style(s))
|
|
|
|
|
.map((s, i) => i % 2 !== 0 ? highlightStyle(s) : style(s))
|
|
|
|
|
.join('')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -31,20 +32,20 @@ function print(input, options = {}) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (v === null) {
|
|
|
|
|
return format('null', config.null)
|
|
|
|
|
return format('null', config.null, path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof v === 'number' && Number.isFinite(v)) {
|
|
|
|
|
return format(v.toString(), config.number)
|
|
|
|
|
return format(v.toString(), config.number, path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof v === 'boolean') {
|
|
|
|
|
return format(v.toString(), config.boolean)
|
|
|
|
|
return format(v.toString(), config.boolean, path)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof v === 'string') {
|
|
|
|
|
return format(JSON.stringify(v), config.string)
|
|
|
|
|
return format(JSON.stringify(v), config.string, path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(v)) {
|
|
|
|
@ -82,7 +83,7 @@ function print(input, options = {}) {
|
|
|
|
|
output += eol()
|
|
|
|
|
let i = 0
|
|
|
|
|
for (let [key, value] of entries) {
|
|
|
|
|
const part = format(JSON.stringify(key), config.key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
|
|
|
|
|
const part = format(JSON.stringify(key), config.key, path + '.' + key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
|
|
|
|
|
output += indent(part, config.space)
|
|
|
|
|
output += i++ < len - 1 ? config.comma(',') : ''
|
|
|
|
|
output += eol()
|
|
|
|
|