From 1fd6712a743a1bc0a230ed652ef0d5f51766a05f Mon Sep 17 00:00:00 2001 From: holzmaster Date: Sun, 14 Jul 2024 14:04:36 +0200 Subject: [PATCH] Add opening to first chunk --- src/service/chunking.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/service/chunking.ts b/src/service/chunking.ts index 8bbd55bd..87d27029 100644 --- a/src/service/chunking.ts +++ b/src/service/chunking.ts @@ -7,16 +7,16 @@ export function splitInChunks( lines: readonly string[], { charLimitPerChunk = 2000, chunkOpeningLine, chunkClosingLine }: SplitOptions, ): string[] { - let charsInChunk = 0; - let currentChunk: string[] = []; - const chunks = [currentChunk]; - const open = chunkOpeningLine ?? ""; const close = chunkClosingLine ?? ""; // we need + 1 for line ending if there is an opening const chunkOverhead = (open.length ? open.length + 1 : 0) + (close.length ? close.length + 1 : 0); + let currentChunk = open ? [open] : []; + let charsInChunk = currentChunk.length; + const chunks = [currentChunk]; + for (const line of lines) { const appendedChars = line.length + 1; // + 1 for line ending if (charsInChunk + appendedChars + chunkOverhead > charLimitPerChunk) {