-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_file_line_by_line.ts
48 lines (45 loc) · 1.29 KB
/
read_file_line_by_line.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { create } from "../src/index.js";
const dropTarget = document.getElementById("drop-target")!;
const preview = document.getElementById("preview")!;
const readline = async function* (
readable: ReadableStream<string>
): AsyncGenerator<string> {
const reader = readable.getReader();
try {
let previous = "";
while (true) {
const { done, value } = await reader.read();
if (done) return;
const lines = value.split(/\r?\n/);
if (lines.length > 1) {
yield previous + lines[0];
previous = "";
}
for (let i = 1; i < lines.length - 1; i++) {
yield lines[i];
}
previous = lines.at(-1)!;
}
} finally {
reader.releaseLock();
}
};
create(dropTarget, {
onDrop: async (files) => {
const frag = document.createDocumentFragment();
for (const [name, lines] of files) {
const title = document.createElement("h1");
title.textContent = name;
const list = document.createElement("ol");
frag.append(title, list);
for await (const line of lines) {
const li = document.createElement("li");
li.textContent = line;
list.append(li);
}
}
preview.replaceChildren(frag);
},
parse: (file: File) =>
readline(file.stream().pipeThrough(new TextDecoderStream())),
});