From ce07f48eabdba68124472343324f54913650f62c Mon Sep 17 00:00:00 2001 From: Niall Date: Thu, 22 Aug 2024 18:24:53 +0000 Subject: [PATCH 1/4] Adding character wrap operation and test --- src/core/config/Categories.json | 3 ++- src/core/operations/Wrap.mjs | 35 ++++++++++++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/Wrap.mjs | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/Wrap.mjs create mode 100644 tests/operations/tests/Wrap.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index bebdd6a5e..c23c1fec2 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", + "Wrap" ] }, { diff --git a/src/core/operations/Wrap.mjs b/src/core/operations/Wrap.mjs new file mode 100644 index 000000000..b3261dfeb --- /dev/null +++ b/src/core/operations/Wrap.mjs @@ -0,0 +1,35 @@ +/** + * @author 0xff1ce [github.com/0xff1ce] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +class Wrap extends Operation { + constructor() { + super(); + + this.name = "Wrap"; + this.module = "Default"; + this.description = "Wraps the input text at a specified number of characters per line."; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + "name": "Line Width", + "type": "number", + "value": 64, + }, + ]; + } + + run(input, args) { + if (!input) return ""; // Handle empty input + const lineWidth = args[0]; + const regex = new RegExp(`.{1,${lineWidth}}`, 'g'); + return input.match(regex).join("\n"); + } +} + +export default Wrap; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 40ce7a2ee..22afb198b 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -151,6 +151,7 @@ import "./tests/TranslateDateTimeFormat.mjs"; import "./tests/Typex.mjs"; import "./tests/UnescapeString.mjs"; import "./tests/Unicode.mjs"; +import "./tests/Wrap.mjs"; import "./tests/YARA.mjs"; import "./tests/ParseCSR.mjs"; import "./tests/XXTEA.mjs"; diff --git a/tests/operations/tests/Wrap.mjs b/tests/operations/tests/Wrap.mjs new file mode 100644 index 000000000..f0dada29b --- /dev/null +++ b/tests/operations/tests/Wrap.mjs @@ -0,0 +1,44 @@ +/** + * @author 0xff1ce [github.com/0xff1ce] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + // Add tests specific to the Wrap operation + { + name: "Wrap text at 64 characters", + input: "A".repeat(128), // Generate an input string of 128 'A' characters + expectedOutput: "A".repeat(64) + "\n" + "A".repeat(64), // Expected output with a line break after 64 characters + recipeConfig: [ + { + "op": "Wrap", + "args": [64] + }, + ], + }, + { + name: "Wrap text at 32 characters", + input: "B".repeat(96), // Generate an input string of 96 'B' characters + expectedOutput: "B".repeat(32) + "\n" + "B".repeat(32) + "\n" + "B".repeat(32), // Expected output with line breaks after every 32 characters + recipeConfig: [ + { + "op": "Wrap", + "args": [32] + }, + ], + }, + { + name: "Wrap text at 10 characters", + input: "1234567890".repeat(10), // Generate an input string by repeating '1234567890' + expectedOutput: Array(10).fill("1234567890").join("\n"), // Expected output with line breaks every 10 characters + recipeConfig: [ + { + "op": "Wrap", + "args": [10] + }, + ], + } +]); \ No newline at end of file From 19d0b7e9152aecb5ea0b168e28643e43177df5e5 Mon Sep 17 00:00:00 2001 From: Niall Date: Thu, 22 Aug 2024 18:28:55 +0000 Subject: [PATCH 2/4] Formatting patch: newline at end of file --- tests/operations/tests/Wrap.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/operations/tests/Wrap.mjs b/tests/operations/tests/Wrap.mjs index f0dada29b..8d7c9a517 100644 --- a/tests/operations/tests/Wrap.mjs +++ b/tests/operations/tests/Wrap.mjs @@ -41,4 +41,4 @@ TestRegister.addTests([ }, ], } -]); \ No newline at end of file +]); From b0d806cd36739ca780f45f29be0e7918affcc03d Mon Sep 17 00:00:00 2001 From: Niall Date: Thu, 22 Aug 2024 18:34:05 +0000 Subject: [PATCH 3/4] Formatting patch: Linter test fixes --- src/core/operations/Wrap.mjs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/core/operations/Wrap.mjs b/src/core/operations/Wrap.mjs index b3261dfeb..9ae538d2c 100644 --- a/src/core/operations/Wrap.mjs +++ b/src/core/operations/Wrap.mjs @@ -6,6 +6,9 @@ import Operation from "../Operation.mjs"; +/** + * Wrap operation + */ class Wrap extends Operation { constructor() { super(); @@ -24,10 +27,15 @@ class Wrap extends Operation { ]; } + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ run(input, args) { if (!input) return ""; // Handle empty input const lineWidth = args[0]; - const regex = new RegExp(`.{1,${lineWidth}}`, 'g'); + const regex = new RegExp(`.{1,${lineWidth}}`, "g"); return input.match(regex).join("\n"); } } From 6d4b641227a7bf758ca12512099127cd878b81e8 Mon Sep 17 00:00:00 2001 From: Niall Date: Thu, 22 Aug 2024 18:35:57 +0000 Subject: [PATCH 4/4] Formatting patch: Linter test fixes --- src/core/operations/Wrap.mjs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/operations/Wrap.mjs b/src/core/operations/Wrap.mjs index 9ae538d2c..c6e57f885 100644 --- a/src/core/operations/Wrap.mjs +++ b/src/core/operations/Wrap.mjs @@ -10,6 +10,10 @@ import Operation from "../Operation.mjs"; * Wrap operation */ class Wrap extends Operation { + + /** + * Wrap constructor + */ constructor() { super();