summaryrefslogtreecommitdiffhomepage
path: root/packages/cli/src/ndjson.ts
blob: 57093c1ba96cad2a02987be686dcbdf6c22c9e68 (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 };
}