From 1a1ea896df86d705be5709c61c181cd06e218aee Mon Sep 17 00:00:00 2001 From: Oliver Mitchell Date: Wed, 6 May 2026 00:24:22 +0930 Subject: [PATCH] feat PadLines: added "Mode" argument - Added "Mode" argument to the Pad Lines operation. - Added "Fixed Count" mode. Mimics original behaviour, fixed number of characters added to the string. - Added "Target Length" mode. Adds padding characters in order to reach a certain length. - Modified Pad lines implementation to use join rather than slice for final result --- src/core/operations/PadLines.mjs | 25 +++++++---- tests/operations/tests/PadLines.mjs | 67 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 tests/operations/tests/PadLines.mjs diff --git a/src/core/operations/PadLines.mjs b/src/core/operations/PadLines.mjs index c1464cce..5d49f338 100644 --- a/src/core/operations/PadLines.mjs +++ b/src/core/operations/PadLines.mjs @@ -19,6 +19,7 @@ class PadLines extends Operation { this.name = "Pad lines"; this.module = "Default"; + this.description = "Add the specified character to the beginning or end of each line. Operation modes for padding length are either fixed or target line length." this.description = "Add the specified number of the specified character to the beginning or end of each line"; this.inputType = "string"; this.outputType = "string"; @@ -37,6 +38,11 @@ class PadLines extends Operation { "name": "Character", "type": "binaryShortString", "value": " " + }, + { + "name": "Mode", + "type": "option", + "value": ["Fixed Count", "Target Length"] } ]; } @@ -47,22 +53,23 @@ class PadLines extends Operation { * @returns {string} */ run(input, args) { - const [position, len, chr] = args, + const [position, len, chr, mode] = args, lines = input.split("\n"); let output = "", i = 0; - if (position === "Start") { - for (i = 0; i < lines.length; i++) { - output += lines[i].padStart(lines[i].length+len, chr) + "\n"; - } - } else if (position === "End") { - for (i = 0; i < lines.length; i++) { - output += lines[i].padEnd(lines[i].length+len, chr) + "\n"; + for (let i = 0; i < lines.length; i++) { + let line = lines[i]; + let targetLength = mode == "Fixed Count" ? line.length + len : len; + + if (position === "Start") { + lines[i] = line.padStart(targetLength, chr); + } else if (position === "End") { + lines[i] = line.padEnd(targetLength, chr); } } - return output.slice(0, output.length-1); + return lines.join('\n'); } } diff --git a/tests/operations/tests/PadLines.mjs b/tests/operations/tests/PadLines.mjs new file mode 100644 index 00000000..3293f1e3 --- /dev/null +++ b/tests/operations/tests/PadLines.mjs @@ -0,0 +1,67 @@ +/** + * Pad Lines tests. + * + * @author oliver-mitchell [oliver@polymerlabs.dev] + * + * @copyright Crown Copyright 2026 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Can pad Lines adds the specified number of characters to the start.", + input: "ABCD\nEF", + expectedOutput: `--ABCD\n--EF`, + recipeConfig: [ + { + op: "Pad Lines", + args: ["Start", 2, "-", "Fixed Count"], // [Position, Length, Character, Mode] + }, + ], + }, + { + name: "Can pad lines adds the specified number of characters to the start.", + input: "ABCD\nEF", + expectedOutput: `--ABCD\n--EF`, + recipeConfig: [ + { + op: "Pad Lines", + args: ["Start", 2, "-", "Fixed Count"], // [Position, Length, Character, Mode] + }, + ], + }, + { + name: "Can pad lines adds the specified number of characters to the end.", + input: "ABCD\nEF", + expectedOutput: `ABCD--\nEF--`, + recipeConfig: [ + { + op: "Pad Lines", + args: ["End", 2, "-", "Fixed Count"], // [Position, Length, Character, Mode] + }, + ], + }, + { + name: "Can pad lines with enough characters to the start in target length mode.", + input: "ABCD\nEF", + expectedOutput: `------ABCD\n--------EF`, + recipeConfig: [ + { + op: "Pad Lines", + args: ["Start", 10, "-", "Target Length"], // [Position, Length, Character, Mode] + }, + ], + }, + { + name: "Can pad lines with enough characters to the end in target length mode.", + input: "ABCD\nEF", + expectedOutput: `ABCD------\nEF--------`, + recipeConfig: [ + { + op: "Pad Lines", + args: ["End", 10, "-", "Target Length"], // [Position, Length, Character, Mode] + }, + ], + }, +]);