1.0.10 • Published 1 year ago
@bhozza/tabula v1.0.10
Tabula
Your lightweight CSV parser & validator
Installation
npm install @bhozza/tabula
Usage
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,alice@test.com\nBob,bob@test.com";
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;
}