From 81e2b5f9ac1b0b6fcfbcaaa278b3ceb500ec1add Mon Sep 17 00:00:00 2001 From: Thomas <31802793+ThomasNotTom@users.noreply.github.com> Date: Fri, 6 Jun 2025 12:36:36 +0100 Subject: [PATCH] Create run implementation --- src/core/operations/LineBreak.mjs | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/core/operations/LineBreak.mjs b/src/core/operations/LineBreak.mjs index 0825150e..d2dd8cfe 100644 --- a/src/core/operations/LineBreak.mjs +++ b/src/core/operations/LineBreak.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import Utils from "../Utils.mjs"; /** @@ -36,6 +37,43 @@ class LineBreak extends Operation { } ]; } + + /** + * @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;