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)
This commit is contained in:
parent
78d40eab60
commit
ae0e989635
@ -24,12 +24,17 @@ 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);
|
byteStr = data.split(/[^\d-]+/);
|
||||||
if (byteStr[byteStr.length-1] === "")
|
} else {
|
||||||
byteStr = byteStr.slice(0, byteStr.length-1);
|
delim = Utils.charRep(delim);
|
||||||
|
byteStr = data.split(delim);
|
||||||
|
}
|
||||||
|
|
||||||
|
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