@bhozza/tabula
v1.0.12
Published
Package to parse CSV with validation and types
Readme
Tabula
Your lightweight CSV parser & validator
Installation
npm install @bhozza/tabulaUsage
Basic usage
import { tabula } from "@bhozza/tabula";
const csv = "name,age\nAlice,30\nBob,25";
const parsed = tabula.parse(csv);
console.log(parsed);
// [
// ["name", "age"],
// ["Alice", "30"],
// ["Bob", "25"],
// ]Validation and parsing
import { tabula, StringSchema, NumberSchema } from "@bhozza/tabula";
const csv = "name,age\nAlice,30\nBob,25";
const parsed = tabula.parse(csv, {
schema: [StringSchema, NumberSchema] as const,
header: true,
});
console.log(parsed);
// [
// ["Alice", 30],
// ["Bob", 25],
// ]Custom schema
import { tabula, type SchemaType, StringSchema } from "@bhozza/tabula";
class EmailSchema implements SchemaType<string> {
parse(value: string): string {
if (!value.includes("@")) {
throw new Error("Invalid email");
}
return value;
}
}
const csv = "name,email\nAlice,[email protected]\nBob,[email protected]";
const parsed = tabula.parse(csv, {
schema: [StringSchema, EmailSchema] as const,
header: true,
});Configuration object
interface Config {
schema?: Schema;
header?: boolean;
separator?: string;
quote?: string;
newline?: string;
}