Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

@buzuli/json

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@buzuli/json - npm Package Compare versions

Comparing version
2.0.2
to
2.1.0
+63
bin/json.js
#!/usr/bin/env node
const yargs = require('yargs')
const buzJson = require('../lib/json')
;(async () => {
try {
const args = yargs
.option('indent', {
type: 'string',
desc: 'indentation characters or number of spaces',
alias: 'i'
})
.option('no-indent', {
type: 'boolean',
desc: 'disable indentation',
alias: ['I', 'ni']
})
.option('no-color', {
type: 'boolean',
desc: 'disable colorization',
alias: ['C', 'nc']
})
.parse()
await handleJson(args)
} catch (error) {
console.error('Fatal:', error)
process.exit(1)
}
})()
async function handleJson ({
indent,
noIndent,
noColor
}) {
let text = ''
indent = Number.parseInt(indent) || indent
process.stdin.on('data', data => {
text += data
})
process.stdin.on('error', error => {
console.error('Error while reading input', error)
process.exit(1)
})
process.stdin.once('end', () => {
try {
const formatted = buzJson(text, {
indent: noIndent ? false : indent,
color: !noColor
})
console.info(formatted)
} catch (error) {
console.error('Input is not valid JSON.', error)
process.exit(1)
}
})
}
+4
-1
{
"name": "@buzuli/json",
"version": "2.0.2",
"version": "2.1.0",
"description": "",
"main": "lib/json.js",
"bin": {
"json": "bin/json.js"
},
"scripts": {

@@ -7,0 +10,0 @@ "lint": "standard",

@@ -35,1 +35,25 @@ # json

- You can supply a string to replace the default indentation text (two space).
## CLI
There is a CLI utility paired with this module that is exposed as the command `json`.
This command has the same configuration options as the utility function.
Just pipe some data to it to format it with friendly colors.
```shell
cat package.json | json
```
Want a flat structure indented?
```shell
cat flat.json | json -C
```
Want an indented structure flattend?
```shell
cat indented.json | json -CI
```
+41
-24
const tap = require('tap')
const json = require('../lib/json')
const obj = {
A: [
{
a: 1,
b: 2
},
{
c: '3',
d: '4'
}
],
B: [
{
e: true,
f: false
},
{
g: null
}
],
C: [],
D: {},
'"E"': '"quoted"',
'\\F\\': '\\escaped\\',
'\\G\\': '\nnewlines\n',
'\\H\\': '"',
'\\I\\': '',
'some-thing': 'hyphenated'
}
tap.test('Should format an object', async assert => {
const obj = {
A: [
{
a: 1,
b: 2
},
{
c: '3',
d: '4'
}
],
B: [
{
e: true,
f: false
},
{
g: null,
h: undefined
}
]
}
assert.ok(typeof json(obj) === 'string')
assert.same(json({}), '{}')
assert.same(json({ id: 'jason' }, { indent: false, color: false }), '{"id":"jason"}')
})
tap.test('Should parse a string then format', async assert => {
tap.test('Should parse a json string then format', async assert => {
assert.ok(typeof json('{}') === 'string')
assert.same(json('{}'), '{}')
assert.same(obj, JSON.parse(json(JSON.stringify(obj), { color: false })))
})
tap.test('Should generate valid JSON in non-colorized mode', async assert => {
assert.same(obj, JSON.parse(json(obj, { color: false })))
assert.same(obj, JSON.parse(json(obj, { color: false, indent: false })))
assert.same(obj, JSON.parse(json(obj, { color: false, indent: 4 })))
})