@buzuli/json
Advanced tools
Comparing version 2.0.2 to 2.1.0
{ | ||
"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 | ||
``` |
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 }))) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7363
6
210
59