From 5cf7f823e615beddf3a09caadb39d7cef6c5b45f Mon Sep 17 00:00:00 2001 From: Niall Date: Fri, 23 Aug 2024 21:09:42 +0000 Subject: [PATCH 1/3] Adding operation to insert a delimiter at a given interval --- src/core/config/Categories.json | 3 +- src/core/operations/InsertDelimiter.mjs | 54 ++++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/InsertDelimiter.mjs | 65 ++++++++++++++++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/InsertDelimiter.mjs create mode 100644 tests/operations/tests/InsertDelimiter.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index bebdd6a5e2..2345280f9d 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -320,7 +320,8 @@ "Unescape string", "Pseudo-Random Number Generator", "Sleep", - "File Tree" + "File Tree", + "Insert Delimiter" ] }, { diff --git a/src/core/operations/InsertDelimiter.mjs b/src/core/operations/InsertDelimiter.mjs new file mode 100644 index 0000000000..4279316074 --- /dev/null +++ b/src/core/operations/InsertDelimiter.mjs @@ -0,0 +1,54 @@ +/** + * @author 0xff1ce [github.com/0xff1ce] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * InsertDelimiter operation + */ +class InsertDelimiter extends Operation { + + /** + * InsertDelimiter constructor + */ + constructor() { + super(); + this.name = "Insert Delimiter"; + this.module = "Default"; + this.description = "Inserts a given delimiter at regular intervals within the input string."; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Interval", + "type": "number", + "value": 8, // Default interval is 8 + }, + { + "name": "Delimiter", + "type": "string", + "value": " ", // Default delimiter is a space + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + if (!input) return "" + + if (isNaN(args[0]) || args[0] <= 0) { + return "Invalid interval: must be a positive integer."; + } + + return input.match(new RegExp(`.{1,${parseInt(args[0])}}`, 'g')).join(args[1]); + } +} + +export default InsertDelimiter; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 40ce7a2ee6..7c2ce20dbe 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -83,6 +83,7 @@ import "./tests/Hexdump.mjs"; import "./tests/HKDF.mjs"; import "./tests/Image.mjs"; import "./tests/IndexOfCoincidence.mjs"; +import "./tests/InsertDelimiter.mjs"; import "./tests/JA3Fingerprint.mjs"; import "./tests/JA4.mjs"; import "./tests/JA3SFingerprint.mjs"; diff --git a/tests/operations/tests/InsertDelimiter.mjs b/tests/operations/tests/InsertDelimiter.mjs new file mode 100644 index 0000000000..e86fa76aca --- /dev/null +++ b/tests/operations/tests/InsertDelimiter.mjs @@ -0,0 +1,65 @@ +/** + * @author 0xff1ce [github.com/0xff1ce] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Insert space every 8 characters", + input: "010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001", + expectedOutput: "01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 00100001", + recipeConfig: [ + { + "op": "Insert Delimiter", + "args": [8, " "] + }, + ], + }, + { + name: "Insert newline every 4 characters", + input: "ABCDEFGHIJKL", + expectedOutput: "ABCD\nEFGH\nIJKL", + recipeConfig: [ + { + "op": "Insert Delimiter", + "args": [4, "\n"] + }, + ], + }, + { + name: "Insert hyphen every 3 characters", + input: "1234567890", + expectedOutput: "123-456-789-0", + recipeConfig: [ + { + "op": "Insert Delimiter", + "args": [3, "-"] + }, + ], + }, + { + name: "Use a float as delimiter", + input: "1234567890", + expectedOutput: "123-456-789-0", + recipeConfig: [ + { + "op": "Insert Delimiter", + "args": [3.4, "-"] + }, + ], + }, + { + name: "Handle empty input gracefully", + input: "", + expectedOutput: "", + recipeConfig: [ + { + "op": "Insert Delimiter", + "args": [8, " "] + }, + ], + } +]); From 10865d8a2492db0f0edc823812299f46e9dadc5e Mon Sep 17 00:00:00 2001 From: Niall Date: Fri, 23 Aug 2024 21:10:03 +0000 Subject: [PATCH 2/3] Patch: string formatting --- src/core/operations/InsertDelimiter.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/operations/InsertDelimiter.mjs b/src/core/operations/InsertDelimiter.mjs index 4279316074..a02ed361d3 100644 --- a/src/core/operations/InsertDelimiter.mjs +++ b/src/core/operations/InsertDelimiter.mjs @@ -47,7 +47,7 @@ class InsertDelimiter extends Operation { return "Invalid interval: must be a positive integer."; } - return input.match(new RegExp(`.{1,${parseInt(args[0])}}`, 'g')).join(args[1]); + return input.match(new RegExp(`.{1,${parseInt(args[0])}}`, "g")).join(args[1]); } } From 0e78f8416de8e8cc30f3f7a08709d33e8576107b Mon Sep 17 00:00:00 2001 From: Niall Date: Fri, 23 Aug 2024 21:17:34 +0000 Subject: [PATCH 3/3] Patch: Linter fixes --- src/core/operations/InsertDelimiter.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/operations/InsertDelimiter.mjs b/src/core/operations/InsertDelimiter.mjs index a02ed361d3..5274cbea62 100644 --- a/src/core/operations/InsertDelimiter.mjs +++ b/src/core/operations/InsertDelimiter.mjs @@ -41,13 +41,13 @@ class InsertDelimiter extends Operation { * @returns {string} */ run(input, args) { - if (!input) return "" + if (!input) return ""; if (isNaN(args[0]) || args[0] <= 0) { return "Invalid interval: must be a positive integer."; } - return input.match(new RegExp(`.{1,${parseInt(args[0])}}`, "g")).join(args[1]); + return input.match(new RegExp(`.{1,${parseInt(args[0], 10)}}`, "g")).join(args[1]); } }