From c4c679021d365d3f89ca95d36ed681888d3cbe72 Mon Sep 17 00:00:00 2001 From: Phillip Nordwall Date: Tue, 20 Nov 2018 20:32:59 -0800 Subject: [PATCH] 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 15fb7f5..83af32e 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 20d06c0..3042731 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], }, ], },