-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.deno.ts
33 lines (28 loc) · 944 Bytes
/
utils.deno.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
export async function readFile(filePath: string): Promise<string[]> {
const fileReader = await Deno.open(filePath);
const lines: string[] = [];
const decoder = new TextDecoder("utf-8");
const buffer = new Uint8Array(1024); // Adjust buffer size as needed
let remaining = "";
try {
while (true) {
const bytesRead = await fileReader.read(buffer);
if (bytesRead === null) break;
// Decode the chunk and combine with remaining
const chunk = decoder.decode(buffer.subarray(0, bytesRead));
const combined = remaining + chunk;
// Split by newline and process lines
const parts = combined.split("\n");
// Save the incomplete part for the next iteration
remaining = parts.pop() || "";
lines.push(...parts);
}
// Add the final leftover line, if any
if (remaining) {
lines.push(remaining);
}
} finally {
fileReader.close();
}
return lines;
}