From 97613eb3c7a5da352d05d4d3d7d2458f4d192e1d Mon Sep 17 00:00:00 2001 From: Phillip Nordwall Date: Tue, 20 Nov 2018 20:16:25 -0800 Subject: [PATCH 1/3] Adding test cases for JSON Beautify --- test/index.mjs | 1 + test/tests/operations/JSONBeautify.mjs | 111 +++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/tests/operations/JSONBeautify.mjs diff --git a/test/index.mjs b/test/index.mjs index 454982cc..ca92448f 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -50,6 +50,7 @@ import "./tests/operations/HaversineDistance"; import "./tests/operations/Hexdump"; import "./tests/operations/Image"; import "./tests/operations/Jump"; +import "./tests/operations/JSONBeautify"; import "./tests/operations/JWTDecode"; import "./tests/operations/JWTSign"; import "./tests/operations/JWTVerify"; diff --git a/test/tests/operations/JSONBeautify.mjs b/test/tests/operations/JSONBeautify.mjs new file mode 100644 index 00000000..20d06c05 --- /dev/null +++ b/test/tests/operations/JSONBeautify.mjs @@ -0,0 +1,111 @@ +/** + * JSONBeautify tests. + * + * @author Phillip Nordwall [Phillip.Nordwall@gmail.com] + * + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ +import TestRegister from "../../TestRegister"; + +TestRegister.addTests([ + { + name: "JSON Beautify: space, ''", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "JSON Beautify", + args: [" "], + }, + ], + }, + { + name: "JSON Beautify: space, number", + input: "42", + expectedOutput: "42", + recipeConfig: [ + { + op: "JSON Beautify", + args: [" "], + }, + ], + }, + { + name: "JSON Beautify: space, string", + input: "\"string\"", + expectedOutput: "\"string\"", + recipeConfig: [ + { + op: "JSON Beautify", + args: [" "], + }, + ], + }, + { + name: "JSON Beautify: space, boolean", + input: "false", + expectedOutput: "false", + recipeConfig: [ + { + op: "JSON Beautify", + args: [" "], + }, + ], + }, + { + name: "JSON Beautify: space, emptyList", + input: "[]", + expectedOutput: "[]", + recipeConfig: [ + { + op: "JSON Beautify", + args: [" "], + }, + ], + }, + { + name: "JSON Beautify: space, list", + input: "[2,1]", + expectedOutput: "[\n 2,\n 1\n]", + recipeConfig: [ + { + op: "JSON Beautify", + args: [" "], + }, + ], + }, + { + name: "JSON Beautify: tab, list", + input: "[2,1]", + expectedOutput: "[\n\t2,\n\t1\n]", + recipeConfig: [ + { + op: "JSON Beautify", + args: ["\t"], + }, + ], + }, + { + name: "JSON Beautify: space, object", + input: "{\"second\":2,\"first\":3}", + expectedOutput: "{\n \"second\": 2,\n \"first\": 3\n}", + recipeConfig: [ + { + op: "JSON Beautify", + args: [" "], + }, + ], + }, + { + 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"], + }, + ], + }, +]); From c16d13e2c9b18666e03d384f75d4ae8addddb36d Mon Sep 17 00:00:00 2001 From: Phillip Nordwall Date: Thu, 22 Nov 2018 11:07:48 -0800 Subject: [PATCH 2/3] Adding test cases for JSON Minify --- test/index.mjs | 1 + test/tests/operations/JSONMinify.mjs | 111 +++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/tests/operations/JSONMinify.mjs diff --git a/test/index.mjs b/test/index.mjs index ca92448f..9c11f6ae 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -51,6 +51,7 @@ import "./tests/operations/Hexdump"; import "./tests/operations/Image"; import "./tests/operations/Jump"; import "./tests/operations/JSONBeautify"; +import "./tests/operations/JSONMinify"; import "./tests/operations/JWTDecode"; import "./tests/operations/JWTSign"; import "./tests/operations/JWTVerify"; diff --git a/test/tests/operations/JSONMinify.mjs b/test/tests/operations/JSONMinify.mjs new file mode 100644 index 00000000..4bbccf8d --- /dev/null +++ b/test/tests/operations/JSONMinify.mjs @@ -0,0 +1,111 @@ +/** + * JSONMinify tests. + * + * @author Phillip Nordwall [Phillip.Nordwall@gmail.com] + * + * @copyright Crown Copyright 2017 + * @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: [], + }, + ], + }, +]); From c4c679021d365d3f89ca95d36ed681888d3cbe72 Mon Sep 17 00:00:00 2001 From: Phillip Nordwall Date: Tue, 20 Nov 2018 20:32:59 -0800 Subject: [PATCH 3/3] Adding Sort Object Keys, and tests for it. --- src/core/operations/JSONBeautify.mjs | 30 ++++++++++++++++++++++++++ test/tests/operations/JSONBeautify.mjs | 29 +++++++++++++++++-------- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/core/operations/JSONBeautify.mjs b/src/core/operations/JSONBeautify.mjs index 15fb7f58..83af32e0 100644 --- a/src/core/operations/JSONBeautify.mjs +++ b/src/core/operations/JSONBeautify.mjs @@ -1,5 +1,6 @@ /** * @author n1474335 [n1474335@gmail.com] + * @author Phillip Nordwall [phillip.nordwall@gmail.com] * @copyright Crown Copyright 2016 * @license Apache-2.0 */ @@ -28,6 +29,11 @@ class JSONBeautify extends Operation { "name": "Indent string", "type": "binaryShortString", "value": "\\t" + }, + { + "name": "Sort Object Keys", + "type": "boolean", + "value": false } ]; } @@ -39,10 +45,34 @@ class JSONBeautify extends Operation { */ run(input, args) { const indentStr = args[0]; + const sortBool = args[1]; 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 [phillip.nordwall@gmail.com] + * @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; diff --git a/test/tests/operations/JSONBeautify.mjs b/test/tests/operations/JSONBeautify.mjs index 20d06c05..30427317 100644 --- a/test/tests/operations/JSONBeautify.mjs +++ b/test/tests/operations/JSONBeautify.mjs @@ -16,7 +16,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: [" "], + args: [" ", false], }, ], }, @@ -27,7 +27,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: [" "], + args: [" ", false], }, ], }, @@ -38,7 +38,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: [" "], + args: [" ", false], }, ], }, @@ -49,7 +49,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: [" "], + args: [" ", false], }, ], }, @@ -60,7 +60,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: [" "], + args: [" ", false], }, ], }, @@ -71,7 +71,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: [" "], + args: [" ", false], }, ], }, @@ -82,7 +82,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: ["\t"], + args: ["\t", false], }, ], }, @@ -93,7 +93,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: [" "], + args: [" ", false], }, ], }, @@ -104,7 +104,18 @@ TestRegister.addTests([ recipeConfig: [ { op: "JSON Beautify", - args: ["\t"], + 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], }, ], },