1.0.10 • Published 1 year ago

@bhozza/tabula v1.0.10

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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;
}
1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago