From 9966d00988bd0d1ca0e14d91718375f28140fb73 Mon Sep 17 00:00:00 2001 From: aicontentcreate2023-star Date: Thu, 5 Mar 2026 07:31:27 +0800 Subject: [PATCH] fix: fromDecimal Auto delimiter now correctly handles multiple numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2217 Summary: The fromDecimal function now correctly handles the 'Auto' delimiter mode, similar to fromHex. Previously, when delim='Auto' (the default), it would only parse the first number, ignoring subsequent numbers regardless of the separator used. Changes: - src/core/lib/Decimal.mjs: * When delim='Auto', use regex /[^\d-]+/ to split on any non-digit, non-minus character (automatically detecting delimiters) * Filter out empty strings from split result * Matches the behavior of fromHex's Auto mode - tests/operations/tests/FromDecimal.mjs: * Added test cases for Auto delimiter with space, comma, and mixed separators * Ensures Auto mode correctly parses multiple numbers with various delimiters Before: Input: "72 101 108 108 111" (Auto delimiter) Output: Only parsed 72 (first number) After: Input: "72 101 108 108 111" (Auto delimiter) Output: Correctly parsed all numbers → "Hello" --- src/core/lib/Decimal.mjs | 17 +++++++++---- tests/operations/tests/FromDecimal.mjs | 33 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/core/lib/Decimal.mjs b/src/core/lib/Decimal.mjs index a140fd4e..10cfa223 100644 --- a/src/core/lib/Decimal.mjs +++ b/src/core/lib/Decimal.mjs @@ -24,12 +24,19 @@ 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") { + // Auto mode: split on any non-digit, non-minus character (similar to fromHex) + byteStr = data.split(/[^\d-]+/); + } else { + delim = Utils.charRep(delim); + byteStr = data.split(delim); + } + + // Remove empty strings from the array + 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] + }, + ], + }, ]);