Skip to content
Open
17 changes: 12 additions & 5 deletions src/core/lib/Decimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import Utils from "../Utils.mjs";
import {DELIM_OPTIONS} from "./Delim.mjs";


/**
Expand All @@ -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"];
5 changes: 2 additions & 3 deletions src/core/operations/FromDecimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +27,7 @@ class FromDecimal extends Operation {
{
"name": "Delimiter",
"type": "option",
"value": DELIM_OPTIONS
"value": FROM_DECIMAL_DELIM_OPTIONS
},
{
"name": "Support signed values",
Expand Down
55 changes: 55 additions & 0 deletions tests/operations/tests/FromDecimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
},
],
},
]);