This commit is contained in:
Thomas 2025-06-07 23:59:25 +00:00 committed by GitHub
commit 8224d8454c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 124 additions and 0 deletions

View file

@ -299,6 +299,7 @@
"From Case Insensitive Regex", "From Case Insensitive Regex",
"Add line numbers", "Add line numbers",
"Remove line numbers", "Remove line numbers",
"Line Break",
"Get All Casings", "Get All Casings",
"To Table", "To Table",
"Reverse", "Reverse",

View file

@ -0,0 +1,79 @@
/**
* @author ThomasNotTom
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
/**
* Line Break operation
*/
class LineBreak extends Operation {
/**
* LineBreak constructor
*/
constructor() {
super();
this.name = "Line Break";
this.module = "Default";
this.description = "Breaks the input text every <code>n</code> characters.";
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [
{
"name": "Line break width",
"type": "number",
"value": 16,
"min": 1
}, {
"name": "Remove leading whitespace",
"type": "boolean",
"value": false
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const lines = [];
const lineWidth = args[0];
const removeLeading = args[1];
const msg = Utils.arrayBufferToStr(input, false);
let i = 0;
while (i < msg.length) {
let slice = msg.slice(i, i + lineWidth);
let leadingWhitespace = 0;
if (removeLeading) {
const match = slice.match(/^\s*/);
leadingWhitespace = match ? match[0].length : 0;
slice = slice.trimStart();
}
slice = msg.slice(i, i + lineWidth + leadingWhitespace);
if (removeLeading) {
slice = slice.trimStart();
}
i += lineWidth + leadingWhitespace;
if (slice.length === 0) continue;
lines.push(slice);
}
return lines.join("\n");
}
}
export default LineBreak;

View file

@ -100,6 +100,7 @@ import "./tests/JWTDecode.mjs";
import "./tests/JWTSign.mjs"; import "./tests/JWTSign.mjs";
import "./tests/JWTVerify.mjs"; import "./tests/JWTVerify.mjs";
import "./tests/LevenshteinDistance.mjs"; import "./tests/LevenshteinDistance.mjs";
import "./tests/LineBreak.mjs";
import "./tests/Lorenz.mjs"; import "./tests/Lorenz.mjs";
import "./tests/LS47.mjs"; import "./tests/LS47.mjs";
import "./tests/LuhnChecksum.mjs"; import "./tests/LuhnChecksum.mjs";

View file

@ -0,0 +1,43 @@
/**
* @author ThomasNotTom
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
// Check line remains unbroken
name: "Line Break: No break",
input: "Hello, world!",
expectedOutput: "Hello, world!",
recipeConfig: [
{
"op": "Line Break",
"args": [32, false]
}
]
}, {
// Check line breaks
name: "Line Break: With break",
input: "Hello, world!",
expectedOutput: "Hello,\n world\n!",
recipeConfig: [
{
"op": "Line Break",
"args": [6, false]
}
]
}, {
// Check line breaks and leading whitespace is removed.
name: "Line Break: With break and no leading whitespace",
input: "Hello, world!",
expectedOutput: "Hello,\nworld!",
recipeConfig: [
{
"op": "Line Break",
"args": [6, true]
}
]
}
]);