Merge ea10625a0994a272b146ebda21bc998bb064599b into 61deeb81653677b6f04fc01e1e8c4edd2104ca59

This commit is contained in:
Thomas 2026-06-09 04:13:45 +08:00 committed by GitHub
commit 41040fa0fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 124 additions and 0 deletions

View File

@ -314,6 +314,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

@ -109,6 +109,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]
}
]
}
]);