Merge 5366b0d0ccd4ba620da1e4375b1e30058660d760 into 4290ea753912378913b1f3f54e0fc5720afeda5d
This commit is contained in:
commit
0a608808c1
@ -19,7 +19,7 @@ class PadLines extends Operation {
|
|||||||
|
|
||||||
this.name = "Pad lines";
|
this.name = "Pad lines";
|
||||||
this.module = "Default";
|
this.module = "Default";
|
||||||
this.description = "Add the specified number of the specified character to the beginning or end of each line";
|
this.description = "Add the specified character to the start or end of each line. Padding can use either a fixed character count or a target final line length.";
|
||||||
this.inputType = "string";
|
this.inputType = "string";
|
||||||
this.outputType = "string";
|
this.outputType = "string";
|
||||||
this.args = [
|
this.args = [
|
||||||
@ -37,6 +37,11 @@ class PadLines extends Operation {
|
|||||||
"name": "Character",
|
"name": "Character",
|
||||||
"type": "binaryShortString",
|
"type": "binaryShortString",
|
||||||
"value": " "
|
"value": " "
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Mode",
|
||||||
|
"type": "option",
|
||||||
|
"value": ["Fixed Count", "Target Length"]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -47,22 +52,21 @@ class PadLines extends Operation {
|
|||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const [position, len, chr] = args,
|
const [position, len, chr, mode] = args,
|
||||||
lines = input.split("\n");
|
lines = input.split("\n");
|
||||||
let output = "",
|
|
||||||
i = 0;
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
const line = lines[i];
|
||||||
|
const targetLength = mode === "Fixed Count" ? line.length + len : len;
|
||||||
|
|
||||||
if (position === "Start") {
|
if (position === "Start") {
|
||||||
for (i = 0; i < lines.length; i++) {
|
lines[i] = line.padStart(targetLength, chr);
|
||||||
output += lines[i].padStart(lines[i].length+len, chr) + "\n";
|
|
||||||
}
|
|
||||||
} else if (position === "End") {
|
} else if (position === "End") {
|
||||||
for (i = 0; i < lines.length; i++) {
|
lines[i] = line.padEnd(targetLength, chr);
|
||||||
output += lines[i].padEnd(lines[i].length+len, chr) + "\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output.slice(0, output.length-1);
|
return lines.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
67
tests/operations/tests/PadLines.mjs
Normal file
67
tests/operations/tests/PadLines.mjs
Normal file
@ -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]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
Loading…
x
Reference in New Issue
Block a user