From b7a5424c13b9300bf1d7aa688b0afce9a720c45b Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Tue, 28 May 2024 20:32:26 +0530 Subject: [PATCH] SimpleChat:DU: Add NewLines helper class To work with an array of new lines. Allow adding, appending, shifting, ... --- .../server/public_simplechat/datautils.mjs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/examples/server/public_simplechat/datautils.mjs b/examples/server/public_simplechat/datautils.mjs index 865f47888..fc04598e8 100644 --- a/examples/server/public_simplechat/datautils.mjs +++ b/examples/server/public_simplechat/datautils.mjs @@ -199,3 +199,53 @@ export function trim_garbage_at_end(sIn) { } return sCur; } + + +/** + * NewLines array helper + */ +export class NewLines { + + constructor() { + /** @type {string[]} */ + this.lines = []; + } + + /** + * Extracts lines from the passed string and inturn either + * append to a previous partial line or add a new line. + * @param {string} sLines + */ + add_append(sLines) { + let aLines = sLines.split("\n"); + let lCnt = 0; + for(let line of aLines) { + lCnt += 1; + // Add back newline removed if any during split + if (lCnt < aLines.length) { + line += "\n"; + } else { + if (sLines.endsWith("\n")) { + line += "\n"; + } + } + // Append if required + if (lCnt == 1) { + let lastLine = this.lines[this.lines.length-1]; + if (lastLine != undefined) { + if (!lastLine.endsWith("\n")) { + this.lines[this.lines.length-1] += line; + continue; + } + } + } + // Add new line + this.lines.push(line); + } + } + + shift() { + return this.lines.shift(); + } + +}