From 5a01c710986db2dd774a836dc1194123366c8191 Mon Sep 17 00:00:00 2001 From: min23asdw <76154445+min23asdw@users.noreply.github.com> Date: Sat, 18 Jul 2026 17:10:40 +0700 Subject: [PATCH] fix: fromDecimal Auto delimiter now correctly parses multiple numbers (#2270) --- src/core/lib/Decimal.mjs | 10 ++--- src/core/operations/FromDecimal.mjs | 7 +++- tests/operations/tests/FromDecimal.mjs | 55 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/core/lib/Decimal.mjs b/src/core/lib/Decimal.mjs index a140fd4e..e0b36369 100644 --- a/src/core/lib/Decimal.mjs +++ b/src/core/lib/Decimal.mjs @@ -24,12 +24,12 @@ import Utils from "../Utils.mjs"; * fromDecimal("10:20:30", "Colon"); */ export function fromDecimal(data, delim="Auto") { - delim = Utils.charRep(delim); - const output = []; - let byteStr = data.split(delim); - if (byteStr[byteStr.length-1] === "") - byteStr = byteStr.slice(0, byteStr.length-1); + const delimRegex = delim === "Auto" ? /[^\d-]+/ : Utils.regexRep(delim); + let byteStr = data.split(delimRegex); + byteStr = byteStr.filter(str => str !== ""); + + const output = []; for (let i = 0; i < byteStr.length; i++) { output[i] = parseInt(byteStr[i], 10); } diff --git a/src/core/operations/FromDecimal.mjs b/src/core/operations/FromDecimal.mjs index f98931d6..16af47e9 100644 --- a/src/core/operations/FromDecimal.mjs +++ b/src/core/operations/FromDecimal.mjs @@ -8,6 +8,11 @@ import Operation from "../Operation.mjs"; import {DELIM_OPTIONS} from "../lib/Delim.mjs"; import {fromDecimal} from "../lib/Decimal.mjs"; +/** + * From Decimal delimiters, plus auto-detection. + */ +const FROM_DECIMAL_DELIM_OPTIONS = [...DELIM_OPTIONS, "Auto"]; + /** * From Decimal operation */ @@ -28,7 +33,7 @@ class FromDecimal extends Operation { { "name": "Delimiter", "type": "option", - "value": DELIM_OPTIONS + "value": FROM_DECIMAL_DELIM_OPTIONS }, { "name": "Support signed values", diff --git a/tests/operations/tests/FromDecimal.mjs b/tests/operations/tests/FromDecimal.mjs index dfc440ec..a8711840 100644 --- a/tests/operations/tests/FromDecimal.mjs +++ b/tests/operations/tests/FromDecimal.mjs @@ -30,4 +30,59 @@ TestRegister.addTests([ }, ], }, + { + name: "From Decimal with Auto delimiter (space)", + input: "72 101 108 108 111", + expectedOutput: "Hello", + recipeConfig: [ + { + op: "From Decimal", + args: ["Auto", false] + }, + ], + }, + { + name: "From Decimal with Auto delimiter (comma)", + input: "72,101,108,108,111", + expectedOutput: "Hello", + recipeConfig: [ + { + op: "From Decimal", + args: ["Auto", false] + }, + ], + }, + { + name: "From Decimal with Auto delimiter (mixed)", + input: "72, 101 : 108; 108\t111", + expectedOutput: "Hello", + recipeConfig: [ + { + op: "From Decimal", + args: ["Auto", false] + }, + ], + }, + { + name: "From Decimal with Auto delimiter (newline)", + input: "72\n101\n108\n108\n111", + expectedOutput: "Hello", + recipeConfig: [ + { + op: "From Decimal", + args: ["Auto", false] + }, + ], + }, + { + name: "From Decimal with Auto delimiter and signed values", + input: "-130 -140 -152 -151 115 33 0 -1", + expectedOutput: "~this!\u0000\u00ff", + recipeConfig: [ + { + op: "From Decimal", + args: ["Auto", true] + }, + ], + }, ]);