Skip to content

Commit

Permalink
Merge branch 'qistoph-FromNegs'
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Nov 7, 2018
2 parents e1b456c + 91fc2c2 commit ab3a73f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
23 changes: 16 additions & 7 deletions src/core/operations/FromDecimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,43 @@ class FromDecimal extends Operation {
"name": "Delimiter",
"type": "option",
"value": DELIM_OPTIONS
},
{
"name": "Support signed values",
"type": "boolean",
"value": false
}
];
this.patterns = [
{
match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?: (?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
flags: "",
args: ["Space"]
args: ["Space", false]
},
{
match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:,(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
flags: "",
args: ["Comma"]
args: ["Comma", false]
},
{
match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:;(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
flags: "",
args: ["Semi-colon"]
args: ["Semi-colon", false]
},
{
match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?::(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
flags: "",
args: ["Colon"]
args: ["Colon", false]
},
{
match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:\\n(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
flags: "",
args: ["Line feed"]
args: ["Line feed", false]
},
{
match: "^(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5])(?:\\r\\n(?:\\d{1,2}|1\\d{2}|2[0-4]\\d|25[0-5]))*$",
flags: "",
args: ["CRLF"]
args: ["CRLF", false]
},
];
}
Expand All @@ -71,7 +76,11 @@ class FromDecimal extends Operation {
* @returns {byteArray}
*/
run(input, args) {
return fromDecimal(input, args[0]);
let data = fromDecimal(input, args[0]);
if (args[1]) { // Convert negatives
data = data.map(v => v < 0 ? 0xFF + v + 1 : v);
}
return data;
}

}
Expand Down
11 changes: 10 additions & 1 deletion src/core/operations/ToDecimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class ToDecimal extends Operation {
"name": "Delimiter",
"type": "option",
"value": DELIM_OPTIONS
},
{
"name": "Support signed values",
"type": "boolean",
"value": false
}
];
}
Expand All @@ -40,7 +45,11 @@ class ToDecimal extends Operation {
* @returns {string}
*/
run(input, args) {
const delim = Utils.charRep(args[0]);
const delim = Utils.charRep(args[0]),
signed = args[1];
if (signed) {
input = input.map(v => v > 0x7F ? v - 0xFF - 1 : v);
}
return input.join(delim);
}

Expand Down
3 changes: 2 additions & 1 deletion test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import "./tests/operations/Crypt";
import "./tests/operations/DateTime";
import "./tests/operations/ExtractEmailAddresses";
import "./tests/operations/Fork";
import "./tests/operations/FromGeohash.mjs";
import "./tests/operations/FromDecimal";
import "./tests/operations/FromGeohash";
import "./tests/operations/Hash";
import "./tests/operations/HaversineDistance";
import "./tests/operations/Hexdump";
Expand Down
33 changes: 33 additions & 0 deletions test/tests/operations/FromDecimal.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* From Decimal tests
*
* @author qistoph
* @copyright Crown Copyright 2018
* @licence Apache-2.0
*/
import TestRegister from "../../TestRegister";

TestRegister.addTests([
{
name: "From Decimal",
input: "83 97 109 112 108 101 32 84 101 120 116",
expectedOutput: "Sample Text",
recipeConfig: [
{
op: "From Decimal",
args: ["Space", false]
},
],
},
{
name: "From Decimal with negatives",
input: "-130,-140,-152,-151,115,33,0,-1",
expectedOutput: "~this!\u0000\u00ff",
recipeConfig: [
{
op: "From Decimal",
args: ["Comma", true]
},
],
},
]);

0 comments on commit ab3a73f

Please sign in to comment.