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");
|
* fromDecimal("10:20:30", "Colon");
|
||||||
*/
|
*/
|
||||||
export function fromDecimal(data, delim="Auto") {
|
export function fromDecimal(data, delim="Auto") {
|
||||||
delim = Utils.charRep(delim);
|
let byteStr;
|
||||||
const output = [];
|
if (delim === "Auto") {
|
||||||
let byteStr = data.split(delim);
|
// Auto mode: split on any non-digit, non-minus character (similar to fromHex)
|
||||||
if (byteStr[byteStr.length-1] === "")
|
byteStr = data.split(/[^\d-]+/);
|
||||||
byteStr = byteStr.slice(0, byteStr.length-1);
|
} 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++) {
|
for (let i = 0; i < byteStr.length; i++) {
|
||||||
output[i] = parseInt(byteStr[i], 10);
|
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