fix: validate wrap line width

This commit is contained in:
Кирилл Ветров 2026-06-26 13:46:41 +03:00
parent 2329a0a0e5
commit b489bb3b98
2 changed files with 49 additions and 0 deletions

View File

@ -5,6 +5,9 @@
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
const MAX_LINE_WIDTH = 65536;
/**
* Wrap operation
@ -27,6 +30,9 @@ class Wrap extends Operation {
"name": "Line Width",
"type": "number",
"value": 64,
"min": 1,
"max": MAX_LINE_WIDTH,
"integer": true,
},
];
}
@ -39,6 +45,16 @@ class Wrap extends Operation {
run(input, args) {
if (!input) return ""; // Handle empty input
const lineWidth = args[0];
if (!Number.isInteger(lineWidth))
throw new OperationError("Line Width must be an integer.");
if (lineWidth < 1)
throw new OperationError("Line Width must be greater than or equal to 1.");
if (lineWidth > MAX_LINE_WIDTH)
throw new OperationError(`Line Width must be less than or equal to ${MAX_LINE_WIDTH}.`);
const regex = new RegExp(`.{1,${lineWidth}}`, "g");
return input.match(regex).join("\n");
}

View File

@ -40,5 +40,38 @@ TestRegister.addTests([
"args": [10]
},
],
},
{
name: "Wrap rejects negative line width",
input: "hi, how about you",
expectedOutput: "Line Width must be greater than or equal to 1.",
recipeConfig: [
{
"op": "Wrap",
"args": [-1]
},
],
},
{
name: "Wrap rejects floating-point line width",
input: "hi, how about you",
expectedOutput: "Line Width must be an integer.",
recipeConfig: [
{
"op": "Wrap",
"args": [1.1]
},
],
},
{
name: "Wrap rejects excessively large line width",
input: "hi, how about you",
expectedOutput: "Line Width must be less than or equal to 65536.",
recipeConfig: [
{
"op": "Wrap",
"args": [1.1761717177171882e+23]
},
],
}
]);