Skip to content

Commit

Permalink
Add opening to first chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
holzmaster committed Jul 14, 2024
1 parent 55431e2 commit 1fd6712
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/service/chunking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1fd6712

Please sign in to comment.