-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'PhillipNordwall-JSON_Object_Sort'
- Loading branch information
Showing
4 changed files
with
267 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/** | ||
* @author n1474335 [[email protected]] | ||
* @author Phillip Nordwall [[email protected]] | ||
* @copyright Crown Copyright 2016 | ||
* @license Apache-2.0 | ||
*/ | ||
|
@@ -27,7 +28,12 @@ class JSONBeautify extends Operation { | |
{ | ||
"name": "Indent string", | ||
"type": "binaryShortString", | ||
"value": "\\t" | ||
"value": " " | ||
}, | ||
{ | ||
"name": "Sort Object Keys", | ||
"type": "boolean", | ||
"value": false | ||
} | ||
]; | ||
} | ||
|
@@ -38,11 +44,35 @@ class JSONBeautify extends Operation { | |
* @returns {string} | ||
*/ | ||
run(input, args) { | ||
const indentStr = args[0]; | ||
const [indentStr, sortBool] = args; | ||
|
||
if (!input) return ""; | ||
if (sortBool) { | ||
input = JSON.stringify(JSONBeautify._sort(JSON.parse(input))); | ||
} | ||
return vkbeautify.json(input, indentStr); | ||
} | ||
|
||
|
||
/** | ||
* Sort JSON representation of an object | ||
* | ||
* @author Phillip Nordwall [[email protected]] | ||
* @private | ||
* @param {object} o | ||
* @returns {object} | ||
*/ | ||
static _sort(o) { | ||
if (Array.isArray(o)) { | ||
return o.map(JSONBeautify._sort); | ||
} else if ("[object Object]" === Object.prototype.toString.call(o)) { | ||
return Object.keys(o).sort().reduce(function(a, k) { | ||
a[k] = JSONBeautify._sort(o[k]); | ||
return a; | ||
}, {}); | ||
} | ||
return o; | ||
} | ||
} | ||
|
||
export default JSONBeautify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/** | ||
* JSONBeautify tests. | ||
* | ||
* @author Phillip Nordwall [[email protected]] | ||
* | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
import TestRegister from "../../TestRegister"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "JSON Beautify: space, ''", | ||
input: "", | ||
expectedOutput: "", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: [" ", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: space, number", | ||
input: "42", | ||
expectedOutput: "42", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: [" ", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: space, string", | ||
input: "\"string\"", | ||
expectedOutput: "\"string\"", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: [" ", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: space, boolean", | ||
input: "false", | ||
expectedOutput: "false", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: [" ", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: space, emptyList", | ||
input: "[]", | ||
expectedOutput: "[]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: [" ", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: space, list", | ||
input: "[2,1]", | ||
expectedOutput: "[\n 2,\n 1\n]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: [" ", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: tab, list", | ||
input: "[2,1]", | ||
expectedOutput: "[\n\t2,\n\t1\n]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: ["\t", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: space, object", | ||
input: "{\"second\":2,\"first\":3}", | ||
expectedOutput: "{\n \"second\": 2,\n \"first\": 3\n}", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: [" ", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: tab, nested", | ||
input: "[2,{\"second\":2,\"first\":3,\"beginning\":{\"j\":\"3\",\"i\":[2,3,false]}},1,2,3]", | ||
expectedOutput: "[\n\t2,\n\t{\n\t\t\"second\": 2,\n\t\t\"first\": 3,\n\t\t\"beginning\": {\n\t\t\t\"j\": \"3\",\n\t\t\t\"i\": [\n\t\t\t\t2,\n\t\t\t\t3,\n\t\t\t\tfalse\n\t\t\t]\n\t\t}\n\t},\n\t1,\n\t2,\n\t3\n]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: ["\t", false], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Beautify: tab, nested, sorted", | ||
input: "[2,{\"second\":2,\"first\":3,\"beginning\":{\"j\":\"3\",\"i\":[2,3,false]}},1,2,3]", | ||
expectedOutput: "[\n\t2,\n\t{\n\t\t\"beginning\": {\n\t\t\t\"i\": [\n\t\t\t\t2,\n\t\t\t\t3,\n\t\t\t\tfalse\n\t\t\t],\n\t\t\t\"j\": \"3\"\n\t\t},\n\t\t\"first\": 3,\n\t\t\"second\": 2\n\t},\n\t1,\n\t2,\n\t3\n]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Beautify", | ||
args: ["\t", true], | ||
}, | ||
], | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* JSONMinify tests. | ||
* | ||
* @author Phillip Nordwall [[email protected]] | ||
* | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
import TestRegister from "../../TestRegister"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "JSON Minify: ''", | ||
input: "", | ||
expectedOutput: "", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: number", | ||
input: "42", | ||
expectedOutput: "42", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: number", | ||
input: "4.2", | ||
expectedOutput: "4.2", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: string", | ||
input: "\"string\"", | ||
expectedOutput: "\"string\"", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: boolean", | ||
input: "false", | ||
expectedOutput: "false", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: emptyList", | ||
input: "[\n \n \t]", | ||
expectedOutput: "[]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: list", | ||
input: "[2,\n \t1]", | ||
expectedOutput: "[2,1]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: object", | ||
input: "{\n \"second\": 2,\n \"first\": 3\n}", | ||
expectedOutput: "{\"second\":2,\"first\":3}", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
name: "JSON Minify: tab, nested", | ||
input: "[\n\t2,\n\t{\n\t\t\"second\": 2,\n\t\t\"first\": 3,\n\t\t\"beginning\": {\n\t\t\t\"j\": \"3\",\n\t\t\t\"i\": [\n\t\t\t\t2,\n\t\t\t\t3,\n\t\t\t\tfalse\n\t\t\t]\n\t\t}\n\t},\n\t1,\n\t2,\n\t3\n]", | ||
expectedOutput: "[2,{\"second\":2,\"first\":3,\"beginning\":{\"j\":\"3\",\"i\":[2,3,false]}},1,2,3]", | ||
recipeConfig: [ | ||
{ | ||
op: "JSON Minify", | ||
args: [], | ||
}, | ||
], | ||
}, | ||
]); |