فهرست منبع

Adding Sort Object Keys, and tests for it.

Phillip Nordwall 6 سال پیش
والد
کامیت
c4c679021d
2فایلهای تغییر یافته به همراه50 افزوده شده و 9 حذف شده
  1. 30 0
      src/core/operations/JSONBeautify.mjs
  2. 20 9
      test/tests/operations/JSONBeautify.mjs

+ 30 - 0
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;

+ 20 - 9
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],
             },
         ],
     },