summaryrefslogtreecommitdiffhomepage
path: root/packages/cli/src/ndjson.ts
blob: 590235431b117b6b07ff489564e097da1d6a44a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * Pure NDJSON line splitter — zero I/O.
 *
 * Splits a buffer on newlines, keeping an incomplete trailing line in `rest`.
 * Does NOT parse JSON — that is the caller's job.
 */

export interface SplitResult {
  readonly lines: readonly string[];
  readonly rest: string;
}

export function splitNdjsonLines(buffer: string): SplitResult {
  const parts = buffer.split("\n");
  const rest = parts[parts.length - 1] ?? "";
  const lines = parts.slice(0, -1).filter((l) => l.length > 0);
  return { lines, rest };
}