Improved prettify when escape characters and add tests (#291)

This commit is contained in:
Gregory Schier 2017-06-07 14:51:41 -07:00 committed by GitHub
parent 8151e1d048
commit 3d63f3be4a
19 changed files with 126 additions and 6 deletions

View File

@ -4,3 +4,4 @@ dist/**/*
coverage/**/*
node_modules/**/*
webpack/**/*
**/__fixtures__/prettify/*

View File

@ -0,0 +1 @@
{"slash": "\\", "slashes": "\\\\", "quote": "\""}

View File

@ -0,0 +1,6 @@
{
"slash": "\\",
"slashes": "\\\\",
"quote": "\""
}

View File

@ -0,0 +1,12 @@
{
"foo": "bar",
"arr": [
1,
2,
3,
4
]
}

View File

@ -0,0 +1,9 @@
{
"foo": "bar",
"arr": [
1,
2,
3,
4
]
}

View File

@ -0,0 +1 @@
{"foo":"bar","arr":[1,2,3,4],"object":{"foo":{},"empty":[]}}

View File

@ -0,0 +1,13 @@
{
"foo": "bar",
"arr": [
1,
2,
3,
4
],
"object": {
"foo": {},
"empty": []
}
}

View File

@ -0,0 +1,6 @@
{
"float": 1.24815739284710478594274018450372432784024,
"round": 1.000000,
"invalid": foo,
"exponent": 1E5
}

View File

@ -0,0 +1,6 @@
{
"float": 1.24815739284710478594274018450372432784024,
"round": 1.000000,
"invalid": foo,
"exponent": 1E5
}

View File

@ -0,0 +1 @@
[1,2,3]

View File

@ -0,0 +1,6 @@
[
1,
2,
3
]

View File

@ -0,0 +1 @@
"Hello World!"

View File

@ -0,0 +1 @@
"Hello World!"

View File

@ -0,0 +1 @@
{"foo": "bar", "array": [1,2,3,4,],"last": "comma",}

View File

@ -0,0 +1,10 @@
{
"foo": "bar",
"array": [
1,
2,
3,
4,
],
"last": "comma",
}

View File

@ -0,0 +1 @@
{foo: bar, bar: [1,2,3,number?]}

View File

@ -0,0 +1,10 @@
{
foo: bar,
bar: [
1,
2,
3,
number?
]
}

View File

@ -0,0 +1,23 @@
import {prettifyJson} from '../prettify';
import fs from 'fs';
import path from 'path';
describe('prettify()', () => {
const basePath = path.join(__dirname, '../__fixtures__/prettify');
const files = fs.readdirSync(basePath);
for (const file of files) {
if (!file.match(/-input\.json$/)) {
continue;
}
const slug = file.replace(/-input\.json$/, '');
const name = slug.replace(/-/g, ' ');
it(`handles ${name}`, () => {
const input = fs.readFileSync(path.join(basePath, `${slug}-input.json`), 'utf8').trim();
const output = fs.readFileSync(path.join(basePath, `${slug}-output.json`), 'utf8').trim();
const result = prettifyJson(input, ' ');
expect(result).toBe(output);
});
}
});

View File

@ -7,7 +7,7 @@
* @param indentChars
* @returns {string}
*/
export function prettifyJson (json, indentChars) {
export function prettifyJson (json, indentChars = '\t') {
// Convert the unicode. To correctly mimic JSON.stringify(JSON.parse(json), null, indentChars)
// we need to convert all escaped unicode characters to proper unicode characters.
try {
@ -19,10 +19,11 @@ export function prettifyJson (json, indentChars) {
let i = 0;
let il = json.length;
let tab = (typeof indentChars !== 'undefined') ? indentChars : ' ';
let tab = indentChars;
let newJson = '';
let indentLevel = 0;
let inString = false;
let isEscaped = false;
let currentChar = null;
let previousChar = null;
let nextChar = null;
@ -32,7 +33,18 @@ export function prettifyJson (json, indentChars) {
previousChar = json.charAt(i - 1);
nextChar = json.charAt(i + 1);
// Handle the escaped case
if (isEscaped) {
isEscaped = false;
newJson += currentChar;
continue;
}
switch (currentChar) {
case '\\':
isEscaped = !isEscaped;
newJson += currentChar;
break;
case '{':
if (!inString && nextChar !== '}') {
newJson += currentChar + '\n' + _repeatString(tab, indentLevel + 1);
@ -87,9 +99,7 @@ export function prettifyJson (json, indentChars) {
}
break;
case '"':
if (previousChar !== '\\') {
inString = !inString;
}
inString = !inString;
newJson += currentChar;
break;
case '\r':
@ -101,7 +111,8 @@ export function prettifyJson (json, indentChars) {
}
}
return newJson;
// Remove lines that only contain whitespace
return newJson.replace(/^\s*\n/gm, '');
}
function _repeatString (s, count) {