From b489bb3b980ac05a57e2b902de9dfe022014dddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=92=D0=B5=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=B2?= Date: Fri, 26 Jun 2026 13:46:41 +0300 Subject: [PATCH] fix: validate wrap line width --- src/core/operations/Wrap.mjs | 16 ++++++++++++++++ tests/operations/tests/Wrap.mjs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/core/operations/Wrap.mjs b/src/core/operations/Wrap.mjs index c6e57f88..303ea9e2 100644 --- a/src/core/operations/Wrap.mjs +++ b/src/core/operations/Wrap.mjs @@ -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"); } diff --git a/tests/operations/tests/Wrap.mjs b/tests/operations/tests/Wrap.mjs index 8d7c9a51..2373d070 100644 --- a/tests/operations/tests/Wrap.mjs +++ b/tests/operations/tests/Wrap.mjs @@ -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] + }, + ], } ]);