init; collecting some of my commonly used utils
This commit is contained in:
28
pkg/csv.ts
Normal file
28
pkg/csv.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Options, parse, Parser } from "csv-parse";
|
||||
import { z } from "zod";
|
||||
|
||||
export async function readCSVToType<
|
||||
TData extends Record<string, unknown>
|
||||
>(
|
||||
text: string,
|
||||
validator: z.ZodType<TData>,
|
||||
options?: Options
|
||||
) {
|
||||
options ??= {
|
||||
columns: true,
|
||||
ignore_last_delimiters: true,
|
||||
skip_empty_lines: true,
|
||||
relax_column_count: true
|
||||
}
|
||||
|
||||
const parser = parse(text, options);
|
||||
const records: TData[] = [];
|
||||
|
||||
for await (const record of parser as (Parser & AsyncIterable<never>)) {
|
||||
records.push(
|
||||
validator.parse(record)
|
||||
)
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
Reference in New Issue
Block a user