forked from Archives/fx
parent
851427d8c8
commit
f110142160
@ -0,0 +1,15 @@
|
||||
'use strict'
|
||||
const chalk = require('chalk')
|
||||
const noop = x => x
|
||||
|
||||
module.exports = {
|
||||
space: global.FX_STYLE_SPACE || 2,
|
||||
null: global.FX_STYLE_NULL || chalk.grey.bold,
|
||||
number: global.FX_STYLE_NUMBER || chalk.cyan.bold,
|
||||
boolean: global.FX_STYLE_BOOLEAN || chalk.yellow.bold,
|
||||
string: global.FX_STYLE_STRING || chalk.green.bold,
|
||||
key: global.FX_STYLE_KEY || chalk.blue.bold,
|
||||
bracket: global.FX_STYLE_BRACKET || noop,
|
||||
comma: global.FX_STYLE_COMMA || noop,
|
||||
colon: global.FX_STYLE_COLON || noop,
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
'use strict'
|
||||
const indent = require('indent-string')
|
||||
const config = require('./config')
|
||||
|
||||
function print(input, expanded = null) {
|
||||
const index = new Map()
|
||||
let row = 0
|
||||
|
||||
function doPrint(v, path = '') {
|
||||
index.set(row, path)
|
||||
|
||||
const eol = () => {
|
||||
row++
|
||||
return '\n'
|
||||
}
|
||||
|
||||
if (typeof v === 'undefined') {
|
||||
return void 0
|
||||
}
|
||||
|
||||
if (v === null) {
|
||||
return config.null(v)
|
||||
}
|
||||
|
||||
if (typeof v === 'number' && Number.isFinite(v)) {
|
||||
return config.number(v)
|
||||
}
|
||||
|
||||
if (typeof v === 'boolean') {
|
||||
return config.boolean(v)
|
||||
|
||||
}
|
||||
|
||||
if (typeof v === 'string') {
|
||||
return config.string(JSON.stringify(v))
|
||||
}
|
||||
|
||||
if (Array.isArray(v)) {
|
||||
if (expanded && !expanded.has(path)) {
|
||||
return config.bracket('[') + '\u2026' + config.bracket(']')
|
||||
}
|
||||
|
||||
let output = config.bracket('[') + eol()
|
||||
|
||||
const len = v.length
|
||||
let i = 0
|
||||
|
||||
for (let item of v) {
|
||||
const value = typeof item === 'undefined' ? null : item // JSON.stringify compatibility
|
||||
output += indent(doPrint(value, path + '[' + i + ']'))
|
||||
output += i++ < len - 1 ? config.comma(',') : ''
|
||||
output += eol()
|
||||
}
|
||||
|
||||
return output + config.bracket(']')
|
||||
}
|
||||
|
||||
if (typeof v === 'object' && v.constructor === Object) {
|
||||
if (expanded && !expanded.has(path)) {
|
||||
return config.bracket('{') + '\u2026' + config.bracket('}')
|
||||
}
|
||||
|
||||
let output = config.bracket('{') + eol()
|
||||
|
||||
const entries = Object.entries(v)
|
||||
.filter(([key, value]) => typeof value !== 'undefined') // JSON.stringify compatibility
|
||||
const len = entries.length
|
||||
|
||||
let i = 0
|
||||
for (let [key, value] of entries) {
|
||||
const part = config.key(JSON.stringify(key)) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
|
||||
output += indent(part, config.space)
|
||||
output += i++ < len - 1 ? config.comma(',') : ''
|
||||
output += eol()
|
||||
}
|
||||
|
||||
return output + config.bracket('}')
|
||||
}
|
||||
|
||||
return v.toString()
|
||||
}
|
||||
|
||||
return [doPrint(input), index]
|
||||
}
|
||||
|
||||
module.exports = print
|
@ -0,0 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
function reduce(json, code) {
|
||||
if (/^\w+\s*=>/.test(code)) {
|
||||
const fx = eval(code)
|
||||
return fx(json)
|
||||
}
|
||||
|
||||
if (/yield/.test(code)) {
|
||||
const fx = eval(`
|
||||
function fn() {
|
||||
const gen = (function*(){
|
||||
${code.replace(/\\\n/g, '')}
|
||||
}).call(this)
|
||||
return [...gen]
|
||||
}; fn
|
||||
`)
|
||||
return fx.call(json)
|
||||
}
|
||||
|
||||
if ('?' === code) {
|
||||
return Object.keys(json)
|
||||
}
|
||||
|
||||
if (/^\./.test(code)) {
|
||||
const fx = eval(`function fn() { return ${code === '.' ? 'this' : 'this' + code} }; fn`)
|
||||
return fx.call(json)
|
||||
}
|
||||
|
||||
const fx = eval(`function fn() { return ${code} }; fn`)
|
||||
return fx.call(json)
|
||||
}
|
||||
|
||||
module.exports = reduce
|
Loading…
Reference in New Issue