Skip to content
Permalink
Browse files

Add lossless json

  • Loading branch information
antonmedv committed Dec 12, 2019
1 parent 98d886d commit 7426016d6b498f48868b066c6ec9b94fff9f59a9
Showing with 25 additions and 13 deletions.
  1. +2 −1 index.js
  2. +4 −3 package.json
  3. +8 −4 print.js
  4. +1 −0 stream.js
  5. +10 −5 test.js
@@ -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
15 test.js
@@ -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')
})

0 comments on commit 7426016

Please sign in to comment.
You can’t perform that action at this time.