From a27637888792eada3db2ac7625cc10cad3f1f0a9 Mon Sep 17 00:00:00 2001 From: Chris van Marle Date: Fri, 12 Oct 2018 10:00:09 +0200 Subject: [PATCH 1/3] Enable parsing of negative decimals #176 --- src/core/operations/FromDecimal.mjs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/core/operations/FromDecimal.mjs b/src/core/operations/FromDecimal.mjs index f3c117ff..3376eebd 100644 --- a/src/core/operations/FromDecimal.mjs +++ b/src/core/operations/FromDecimal.mjs @@ -29,6 +29,11 @@ class FromDecimal extends Operation { "name": "Delimiter", "type": "option", "value": DELIM_OPTIONS + }, + { + "name": "Convert negatives", + "type": "boolean", + "value": false } ]; this.patterns = [ @@ -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; } } From 3f0af9cdea617c6a3113b7507e35c8affa6dfb53 Mon Sep 17 00:00:00 2001 From: Chris van Marle Date: Mon, 22 Oct 2018 17:51:26 +0800 Subject: [PATCH 2/3] Add tests for From Decimal --- test/index.mjs | 3 ++- test/tests/operations/FromDecimal.mjs | 33 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/tests/operations/FromDecimal.mjs diff --git a/test/index.mjs b/test/index.mjs index 9bb93a60..93dfecf6 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -41,7 +41,8 @@ import "./tests/operations/ConditionalJump"; import "./tests/operations/Crypt"; import "./tests/operations/DateTime"; 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"; diff --git a/test/tests/operations/FromDecimal.mjs b/test/tests/operations/FromDecimal.mjs new file mode 100644 index 00000000..95078cdb --- /dev/null +++ b/test/tests/operations/FromDecimal.mjs @@ -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] + }, + ], + }, +]); From 91fc2c28dca5e3d4e217ada20f45d1439c52d4bf Mon Sep 17 00:00:00 2001 From: n1474335 Date: Wed, 7 Nov 2018 14:39:33 +0000 Subject: [PATCH 3/3] Added signed feature to 'To Decimal' --- src/core/operations/FromDecimal.mjs | 14 ++++----- src/core/operations/ToDecimal.mjs | 11 ++++++- test/tests/operations/FromDecimal.mjs | 44 +++++++++++++-------------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/core/operations/FromDecimal.mjs b/src/core/operations/FromDecimal.mjs index 3376eebd..1d0f272c 100644 --- a/src/core/operations/FromDecimal.mjs +++ b/src/core/operations/FromDecimal.mjs @@ -31,7 +31,7 @@ class FromDecimal extends Operation { "value": DELIM_OPTIONS }, { - "name": "Convert negatives", + "name": "Support signed values", "type": "boolean", "value": false } @@ -40,32 +40,32 @@ class FromDecimal extends Operation { { 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] }, ]; } diff --git a/src/core/operations/ToDecimal.mjs b/src/core/operations/ToDecimal.mjs index cad8dc18..6df51ae2 100644 --- a/src/core/operations/ToDecimal.mjs +++ b/src/core/operations/ToDecimal.mjs @@ -30,6 +30,11 @@ class ToDecimal extends Operation { "name": "Delimiter", "type": "option", "value": DELIM_OPTIONS + }, + { + "name": "Support signed values", + "type": "boolean", + "value": false } ]; } @@ -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); } diff --git a/test/tests/operations/FromDecimal.mjs b/test/tests/operations/FromDecimal.mjs index 95078cdb..b73292a8 100644 --- a/test/tests/operations/FromDecimal.mjs +++ b/test/tests/operations/FromDecimal.mjs @@ -8,26 +8,26 @@ 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] - }, - ], - }, + { + 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] + }, + ], + }, ]);