-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.ts
36 lines (32 loc) · 846 Bytes
/
read.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
import type { JSONData } from "./types.d.ts";
import { parseNdjson } from "./parse.ts";
/**
* Reads a ndjson file and returns a promise that resolves to its content
* parsed inside an array.
*
* **Usage**
*
* ```typescript
* import { readNdjson } from 'https://deno.land/x/[email protected]/mod.ts';
*
* const parsed = await readNdjson("<filePath_here>");
* ```
* @see https://github.com/ndjson/ndjson-spec
*/
export async function readNdjson<T extends JSONData[]>(
filePath: string,
): Promise<T> {
const output = [] as unknown as T;
const file = await Deno.open(filePath);
try {
for await (const parsed of parseNdjson(file)) {
output.push(parsed);
}
} catch (readError) {
readError.message = `${filePath}: ${readError.message}`;
throw readError;
} finally {
file.close();
}
return output;
}