mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
Improved prettify when escape characters and add tests (#291)
This commit is contained in:
parent
8151e1d048
commit
3d63f3be4a
@ -4,3 +4,4 @@ dist/**/*
|
||||
coverage/**/*
|
||||
node_modules/**/*
|
||||
webpack/**/*
|
||||
**/__fixtures__/prettify/*
|
@ -0,0 +1 @@
|
||||
{"slash": "\\", "slashes": "\\\\", "quote": "\""}
|
@ -0,0 +1,6 @@
|
||||
{
|
||||
"slash": "\\",
|
||||
"slashes": "\\\\",
|
||||
"quote": "\""
|
||||
}
|
||||
|
12
app/common/__fixtures__/prettify/extra-whitespace-input.json
Normal file
12
app/common/__fixtures__/prettify/extra-whitespace-input.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
|
||||
"foo": "bar",
|
||||
"arr": [
|
||||
1,
|
||||
2,
|
||||
|
||||
3,
|
||||
4
|
||||
|
||||
]
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"foo": "bar",
|
||||
"arr": [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
]
|
||||
}
|
@ -0,0 +1 @@
|
||||
{"foo":"bar","arr":[1,2,3,4],"object":{"foo":{},"empty":[]}}
|
@ -0,0 +1,13 @@
|
||||
{
|
||||
"foo": "bar",
|
||||
"arr": [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
],
|
||||
"object": {
|
||||
"foo": {},
|
||||
"empty": []
|
||||
}
|
||||
}
|
6
app/common/__fixtures__/prettify/precision-input.json
Normal file
6
app/common/__fixtures__/prettify/precision-input.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"float": 1.24815739284710478594274018450372432784024,
|
||||
"round": 1.000000,
|
||||
"invalid": foo,
|
||||
"exponent": 1E5
|
||||
}
|
6
app/common/__fixtures__/prettify/precision-output.json
Normal file
6
app/common/__fixtures__/prettify/precision-output.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"float": 1.24815739284710478594274018450372432784024,
|
||||
"round": 1.000000,
|
||||
"invalid": foo,
|
||||
"exponent": 1E5
|
||||
}
|
1
app/common/__fixtures__/prettify/root-array-input.json
Normal file
1
app/common/__fixtures__/prettify/root-array-input.json
Normal file
@ -0,0 +1 @@
|
||||
[1,2,3]
|
6
app/common/__fixtures__/prettify/root-array-output.json
Normal file
6
app/common/__fixtures__/prettify/root-array-output.json
Normal file
@ -0,0 +1,6 @@
|
||||
[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
|
1
app/common/__fixtures__/prettify/root-string-input.json
Normal file
1
app/common/__fixtures__/prettify/root-string-input.json
Normal file
@ -0,0 +1 @@
|
||||
"Hello World!"
|
1
app/common/__fixtures__/prettify/root-string-output.json
Normal file
1
app/common/__fixtures__/prettify/root-string-output.json
Normal file
@ -0,0 +1 @@
|
||||
"Hello World!"
|
@ -0,0 +1 @@
|
||||
{"foo": "bar", "array": [1,2,3,4,],"last": "comma",}
|
10
app/common/__fixtures__/prettify/trailing-comma-output.json
Normal file
10
app/common/__fixtures__/prettify/trailing-comma-output.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"foo": "bar",
|
||||
"array": [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
],
|
||||
"last": "comma",
|
||||
}
|
@ -0,0 +1 @@
|
||||
{foo: bar, bar: [1,2,3,number?]}
|
@ -0,0 +1,10 @@
|
||||
{
|
||||
foo: bar,
|
||||
bar: [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
number?
|
||||
]
|
||||
}
|
||||
|
23
app/common/__tests__/prettify.test.js
Normal file
23
app/common/__tests__/prettify.test.js
Normal 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);
|
||||
});
|
||||
}
|
||||
});
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user