Add lossless json

pull/123/head
Anton Medvedev 5 years ago
parent 98d886da20
commit 7426016d6b

@ -3,6 +3,7 @@
const os = require('os')
const fs = require('fs')
const path = require('path')
const JSON = require('lossless-json')
const std = require('./std')
try {
@ -82,7 +83,7 @@ function handle(input) {
return
}
input = fs.readFileSync(args[0])
input = fs.readFileSync(args[0]).toString('utf8')
filename = path.basename(args[0])
global.FX_FILENAME = filename
args.shift()

@ -47,13 +47,14 @@
},
"dependencies": {
"@medv/blessed": "^2.0.0",
"chalk": "^2.4.2",
"chalk": "^3.0.0",
"indent-string": "^4.0.0",
"string-width": "^4.1.0"
"lossless-json": "^1.0.3",
"string-width": "^4.2.0"
},
"devDependencies": {
"ava": "^2.4.0",
"pkg": "^4.4.0",
"pkg": "^4.4.1",
"release-it": "^12.4.3"
}
}

@ -26,7 +26,7 @@ function print(input, options = {}) {
// Code for highlighting parts become cumbersome.
// Maybe we should refactor this part.
const highlightStyle = (currentPath === path) ? config.highlightCurrent : config.highlight
const formatStyle = (v, style) => format(JSON.stringify(v), style, highlightStyle, highlight)
const formatStyle = (v, style) => format(v, style, highlightStyle, highlight)
const formatText = (v, style, path) => {
const highlightStyle = (currentPath === path) ? config.highlightCurrent : config.highlight
return format(v, style, highlightStyle, highlight, JSON.stringify)
@ -42,15 +42,19 @@ function print(input, options = {}) {
}
if (v === null) {
return formatStyle(v, config.null)
return formatStyle(JSON.stringify(v), config.null)
}
if (typeof v === 'number' && Number.isFinite(v)) {
return formatStyle(v, config.number)
return formatStyle(JSON.stringify(v), config.number)
}
if (typeof v === 'object' && v.isLosslessNumber) {
return formatStyle(v.toString(), config.number)
}
if (typeof v === 'boolean') {
return formatStyle(v, config.boolean)
return formatStyle(JSON.stringify(v), config.boolean)
}

@ -1,4 +1,5 @@
'use strict'
const JSON = require('lossless-json')
function apply(cb, input) {
let json

@ -14,17 +14,17 @@ test('pass', t => {
test('anon func', t => {
const r = fx({"key": "value"}, "'function (x) { return x.key }'")
t.deepEqual(r, 'value\n')
t.is(r, 'value\n')
})
test('arrow func', t => {
const r = fx({"key": "value"}, "'x => x.key'")
t.deepEqual(r, 'value\n')
t.is(r, 'value\n')
})
test('arrow func ()', t => {
const r = fx({"key": "value"}, "'(x) => x.key'")
t.deepEqual(r, 'value\n')
t.is(r, 'value\n')
})
test('this bind', t => {
@ -34,12 +34,12 @@ test('this bind', t => {
test('chain', t => {
const r = fx({"items": ["foo", "bar"]}, "'this.items' '.' 'x => x[1]'")
t.deepEqual(r, 'bar\n')
t.is(r, 'bar\n')
})
test('file argument', t => {
const r = execSync(`node index.js package.json .name`).toString('utf8')
t.deepEqual(r, 'fx\n')
t.is(r, 'fx\n')
})
test('stream', t => {
@ -68,3 +68,8 @@ test('stream', t => {
reader.read()
}
})
test('lossless number', t => {
const r = execSync(`echo '{"long": 123456789012345678901}' | node index.js .long`).toString('utf8')
t.is(r, '123456789012345678901\n')
})

Loading…
Cancel
Save