Skip to content
Open
15 changes: 10 additions & 5 deletions src/core/lib/Decimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ 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);
let byteStr;
if (delim === "Auto") {
byteStr = data.split(/[^\d-]+/);
} else {
delim = Utils.charRep(delim);
byteStr = data.split(delim);
}
Comment thread
min23asdw marked this conversation as resolved.
Outdated

byteStr = byteStr.filter(str => str !== "");

const output = [];
for (let i = 0; i < byteStr.length; i++) {
output[i] = parseInt(byteStr[i], 10);
}
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]
},
],
},
]);
Loading