diff --git a/src/core/lib/Decimal.mjs b/src/core/lib/Decimal.mjs index a140fd4e6d..3d81695eda 100644 --- a/src/core/lib/Decimal.mjs +++ b/src/core/lib/Decimal.mjs @@ -7,6 +7,7 @@ */ import Utils from "../Utils.mjs"; +import {DELIM_OPTIONS} from "./Delim.mjs"; /** @@ -24,14 +25,20 @@ 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); + const delimRegex = delim === "Auto" ? /[^\d-]+/ : Utils.regexRep(delim); + let byteStr = data.split(delimRegex); + + byteStr = byteStr.filter(str => str !== ""); + const output = []; for (let i = 0; i < byteStr.length; i++) { output[i] = parseInt(byteStr[i], 10); } return output; } + + +/** + * From Decimal delimiters. + */ +export const FROM_DECIMAL_DELIM_OPTIONS = [...DELIM_OPTIONS, "Auto"]; diff --git a/src/core/operations/FromDecimal.mjs b/src/core/operations/FromDecimal.mjs index f98931d625..83353074a6 100644 --- a/src/core/operations/FromDecimal.mjs +++ b/src/core/operations/FromDecimal.mjs @@ -5,8 +5,7 @@ */ import Operation from "../Operation.mjs"; -import {DELIM_OPTIONS} from "../lib/Delim.mjs"; -import {fromDecimal} from "../lib/Decimal.mjs"; +import {fromDecimal, FROM_DECIMAL_DELIM_OPTIONS} from "../lib/Decimal.mjs"; /** * From Decimal operation @@ -28,7 +27,7 @@ class FromDecimal extends Operation { { "name": "Delimiter", "type": "option", - "value": DELIM_OPTIONS + "value": FROM_DECIMAL_DELIM_OPTIONS }, { "name": "Support signed values", diff --git a/tests/operations/tests/FromDecimal.mjs b/tests/operations/tests/FromDecimal.mjs index dfc440ec53..a8711840fc 100644 --- a/tests/operations/tests/FromDecimal.mjs +++ b/tests/operations/tests/FromDecimal.mjs @@ -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] + }, + ], + }, ]);