fix: fromDecimal Auto delimiter now correctly parses multiple numbers (#2270)

This commit is contained in:
min23asdw 2026-07-18 17:10:40 +07:00 committed by GitHub
parent c7898130e7
commit 5a01c71098
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 6 deletions

View File

@ -24,12 +24,12 @@ 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); const delimRegex = delim === "Auto" ? /[^\d-]+/ : Utils.regexRep(delim);
const output = []; let byteStr = data.split(delimRegex);
let byteStr = data.split(delim);
if (byteStr[byteStr.length-1] === "")
byteStr = byteStr.slice(0, byteStr.length-1);
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);
} }

View File

@ -8,6 +8,11 @@ import Operation from "../Operation.mjs";
import {DELIM_OPTIONS} from "../lib/Delim.mjs"; import {DELIM_OPTIONS} from "../lib/Delim.mjs";
import {fromDecimal} from "../lib/Decimal.mjs"; import {fromDecimal} from "../lib/Decimal.mjs";
/**
* From Decimal delimiters, plus auto-detection.
*/
const FROM_DECIMAL_DELIM_OPTIONS = [...DELIM_OPTIONS, "Auto"];
/** /**
* From Decimal operation * From Decimal operation
*/ */
@ -28,7 +33,7 @@ class FromDecimal extends Operation {
{ {
"name": "Delimiter", "name": "Delimiter",
"type": "option", "type": "option",
"value": DELIM_OPTIONS "value": FROM_DECIMAL_DELIM_OPTIONS
}, },
{ {
"name": "Support signed values", "name": "Support signed values",

View File

@ -30,4 +30,59 @@ 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]
},
],
},
{
name: "From Decimal with Auto delimiter (newline)",
input: "72\n101\n108\n108\n111",
expectedOutput: "Hello",
recipeConfig: [
{
op: "From Decimal",
args: ["Auto", false]
},
],
},
{
name: "From Decimal with Auto delimiter and signed values",
input: "-130 -140 -152 -151 115 33 0 -1",
expectedOutput: "~this!\u0000\u00ff",
recipeConfig: [
{
op: "From Decimal",
args: ["Auto", true]
},
],
},
]); ]);