From ae0e9896350fe40b313a316d68e0b78ac52f5c91 Mon Sep 17 00:00:00 2001 From: min23asdw Date: Sun, 22 Mar 2026 02:00:17 +0700 Subject: [PATCH] fix: handle Auto delimiter correctly in fromDecimal When delim is "Auto", Utils.charRep("Auto") returns undefined, causing data.split(undefined) to return the entire string as a single element. This meant only the first number was parsed. Fix by splitting on a regex /[^\d-]+/ for Auto mode, consistent with how fromHex handles its Auto delimiter. Existing delimiter behavior is unchanged. Adds test cases for Auto delimiter with space, comma, and mixed separators. Fixes #2217 Ref: #2221 (closed due to unsigned CLA) --- src/core/lib/Decimal.mjs | 15 ++++++++---- tests/operations/tests/FromDecimal.mjs | 33 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/core/lib/Decimal.mjs b/src/core/lib/Decimal.mjs index a140fd4e..cf93257b 100644 --- a/src/core/lib/Decimal.mjs +++ b/src/core/lib/Decimal.mjs @@ -24,12 +24,17 @@ 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); + let byteStr; + if (delim === "Auto") { + byteStr = data.split(/[^\d-]+/); + } else { + delim = Utils.charRep(delim); + byteStr = data.split(delim); + } + byteStr = byteStr.filter(str => str !== ""); + + const output = []; for (let i = 0; i < byteStr.length; i++) { output[i] = parseInt(byteStr[i], 10); } diff --git a/tests/operations/tests/FromDecimal.mjs b/tests/operations/tests/FromDecimal.mjs index dfc440ec..b94e1cd8 100644 --- a/tests/operations/tests/FromDecimal.mjs +++ b/tests/operations/tests/FromDecimal.mjs @@ -30,4 +30,37 @@ 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] + }, + ], + }, ]);