fix: fromDecimal Auto delimiter now correctly handles multiple numbers
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"
This commit is contained in:
parent
cd7dafdf53
commit
3878744e97
@ -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);
|
||||
}
|
||||
|
||||
@ -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]
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user