-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.ts
26 lines (26 loc) · 899 Bytes
/
lines.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { chunkIfs } from "./chunkIfs";
import { flatMaps } from "./flatMaps";
import { maps } from "./maps";
import { throughs } from "./throughs";
type LinesOptions = { EOL?: "KEEP" | "LF" | "CRLF" | "NONE" };
/** split string stream into lines stream, handy to concat LLM's tokens stream into line by line stream or split a long string by lines */
export const lines: {
(opts?: LinesOptions): TransformStream<string, string>;
} = ({ EOL = "KEEP" }: LinesOptions = {}) => {
const CRLFMap = {
KEEP: "$1",
LF: "\n",
CRLF: "\r\n",
NONE: "",
};
return throughs<string, string>((r) =>
r
.pipeThrough(flatMaps((s: string) => s.split(/(?<=\n)/g)))
.pipeThrough(
chunkIfs((ch: string) => ch.indexOf("\n") === -1, { inclusive: true }),
)
.pipeThrough(
maps((chunks) => chunks.join("").replace(/(\r?\n?)$/, CRLFMap[EOL])),
),
);
};