Forráskód Böngészése

Update hexadecimal sort after review

- Use Array.map instead of for-loop
- Add test case
Chris van Marle 6 éve
szülő
commit
ba24e12454
2 módosított fájl, 20 hozzáadás és 13 törlés
  1. 9 13
      src/core/operations/Sort.mjs
  2. 11 0
      test/tests/operations/SeqUtils.mjs

+ 9 - 13
src/core/operations/Sort.mjs

@@ -143,22 +143,18 @@ class Sort extends Operation {
      * @returns {number}
      */
     static _hexadecimalSort(a, b) {
-        const a_ = a.split(/([^\da-f]+)/i),
+        let a_ = a.split(/([^\da-f]+)/i),
             b_ = b.split(/([^\da-f]+)/i);
 
-        for (let i = 0; i < a_.length; ++i) {
-            const t = parseInt(a_[i], 16);
-            if (!isNaN(t)) {
-                a_[i] = t;
-            }
-        }
+        a_ = a_.map(v => {
+            const t = parseInt(v, 16);
+            return isNaN(t) ? v : t;
+        });
 
-        for (let i = 0; i < b_.length; ++i) {
-            const t = parseInt(b_[i], 16);
-            if (!isNaN(t)) {
-                b_[i] = t;
-            }
-        }
+        b_ = b_.map(v => {
+            const t = parseInt(v, 16);
+            return isNaN(t) ? v : t;
+        });
 
         for (let i = 0; i < a_.length && i < b.length; ++i) {
             if (isNaN(a_[i]) && !isNaN(b_[i])) return 1; // Numbers after non-numbers

+ 11 - 0
test/tests/operations/SeqUtils.mjs

@@ -30,4 +30,15 @@ TestRegister.addTests([
             }
         ],
     },
+    {
+        name: "SeqUtils - Hexadecimal sort",
+        input: "06,08,0a,0d,0f,1,10,11,12,13,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f,2,3,4,5,7,9,b,c,e",
+        expectedOutput: "1,2,3,4,5,06,7,08,9,0a,b,c,0d,e,0f,10,11,12,13,14,15,16,17,18,19,1a,1b,1c,1d,1e,1f",
+        recipeConfig: [
+            {
+                "op": "Sort",
+                "args": ["Comma", false, "Numeric (hexadecimal)"]
+            }
+        ],
+    },
 ]);