fx/index.js

114 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-10-26 10:59:46 +00:00
#!/usr/bin/env node
2018-03-13 03:28:40 +00:00
'use strict'
2018-06-26 14:59:04 +00:00
const pretty = require('@medv/prettyjson')
2018-01-25 17:30:05 +00:00
2018-09-14 17:00:36 +00:00
const usage = `
2018-01-25 17:30:05 +00:00
Usage
$ fx [code ...]
Examples
2018-01-26 02:35:58 +00:00
$ echo '{"key": "value"}' | fx 'x => x.key'
2018-08-30 15:09:12 +00:00
value
2018-01-25 17:30:05 +00:00
2018-01-26 02:35:58 +00:00
$ echo '[1,2,3]' | fx 'this.map(x => x * 2)'
2018-01-25 17:30:05 +00:00
[2, 4, 6]
2018-01-26 02:35:58 +00:00
$ echo '{"items": ["one", "two"]}' | fx 'this.items' 'this[1]'
2018-08-30 15:09:12 +00:00
two
2018-01-25 17:30:05 +00:00
2018-01-26 02:35:58 +00:00
$ echo '{"count": 0}' | fx '{...this, count: 1}'
2018-01-25 17:30:05 +00:00
{"count": 1}
2018-08-30 15:09:12 +00:00
$ echo '{"foo": 1, "bar": 2}' | fx ?
["foo", "bar"]
$ echo '{"key": "value"}' | fx .key
value
2018-11-02 18:24:56 +00:00
2018-09-14 17:00:36 +00:00
`
2018-01-25 17:30:05 +00:00
2018-09-14 17:00:36 +00:00
function main(input) {
2018-11-02 18:24:56 +00:00
const {stdout, stderr} = process
2018-09-14 17:00:36 +00:00
if (input === '') {
2018-11-02 18:24:56 +00:00
stderr.write(usage)
2018-09-14 17:00:36 +00:00
process.exit(2)
2018-01-25 17:30:05 +00:00
}
2018-09-14 17:00:36 +00:00
const json = JSON.parse(input)
2018-11-02 18:24:56 +00:00
const args = process.argv.slice(2)
if (args.length === 0 && stdout.isTTY) {
require('./fx')(json)
return
}
const result = args.reduce(reduce, json)
2018-01-25 17:30:05 +00:00
if (typeof result === 'undefined') {
2018-11-02 18:24:56 +00:00
stderr.write('undefined\n')
2018-08-30 15:09:12 +00:00
} else if (typeof result === 'string') {
console.log(result)
2018-11-02 18:24:56 +00:00
} else if (stdout.isTTY) {
2018-06-26 14:59:04 +00:00
console.log(pretty(result))
2018-01-25 17:30:05 +00:00
} else {
2018-03-13 03:28:40 +00:00
console.log(JSON.stringify(result, null, 2))
2018-01-25 17:30:05 +00:00
}
}
function reduce(json, code) {
if (/^\w+\s*=>/.test(code)) {
const fx = eval(code)
return fx(json)
2018-08-30 15:09:12 +00:00
}
if (/yield/.test(code)) {
2018-01-25 17:30:05 +00:00
const fx = eval(`
function fn() {
const gen = (function*(){
${code.replace(/\\\n/g, '')}
}).call(this)
return [...gen]
}; fn
`)
return fx.call(json)
2018-08-30 15:09:12 +00:00
}
if (/^\?$/.test(code)) {
2018-03-19 17:15:05 +00:00
return Object.keys(json)
2018-08-30 15:09:12 +00:00
}
if (/^\./.test(code)) {
const fx = eval(`function fn() { return ${code === '.' ? 'this' : 'this' + code} }; fn`)
2018-01-25 17:30:05 +00:00
return fx.call(json)
}
2018-08-30 15:09:12 +00:00
const fx = eval(`function fn() { return ${code} }; fn`)
return fx.call(json)
2018-01-25 17:30:05 +00:00
}
2018-11-02 18:24:56 +00:00
function run() {
const stdin = process.stdin
stdin.setEncoding('utf8')
2018-09-14 17:00:36 +00:00
2018-11-02 18:24:56 +00:00
let buff = ''
2018-09-14 17:00:36 +00:00
2018-11-02 18:24:56 +00:00
if (stdin.isTTY) {
main('')
}
2018-09-14 17:00:36 +00:00
2018-11-02 18:24:56 +00:00
stdin.on('readable', () => {
let chunk
2018-09-14 17:00:36 +00:00
2018-11-02 18:24:56 +00:00
while ((chunk = stdin.read())) {
buff += chunk
}
})
stdin.on('end', () => {
main(buff)
})
}
2018-09-14 17:00:36 +00:00
2018-11-02 18:24:56 +00:00
run()