Skip to content

Parser

Parser validate and decode the input CSV from the given data source and the schema. If validation has failed, the error will be thrown.

Let's use this schema as an example:

ts
import { Column, Object, asNumber, asString } from 'sheethuahua';

const schema = Object({
	id: Column('ID', asNumber()),
	name: Column('Name', asString()),
});

parseCsv

parseCsv() use CSV string as the data source.

ts
import { parseCsv } from 'sheethuahua';

const input = 'ID,Name\n1,A\n2,B\n';

// const output: {
//     id: number;
//     name: string;
// }[]
const output = parseCsv(input, schema);

fetchCsv

fetchCsv() use URL to the CSV file as the data source.

ts
import { fetchCsv } from 'sheethuahua';

// const output: {
//     id: number;
//     name: string;
// }[]
const output = await fetchCsv('https://url-to/data.csv', schema);

Spreadsheets

Spreadsheets() use Google Sheets as a data source. Providing Sheets ID and call .get() to parse the data from specific sheet's name and corresponded schema.

TIP

Sheets ID can be found from the Google Sheets URL: https://docs.google.com/spreadsheets/d/{sheetsId}/

ts
import { fetchCsv } from 'sheethuahua';

const sheets = Spreadsheet('google-sheets-id');

// const output: {
//     id: number;
//     name: string;
// }[]
const output = await sheets.get('SheetName', schema);

WARNING

  1. The sheets must be publicly accessible (At least anyone with the link can view).
  2. Google Sheets has a very low rate limit. It should be used with Static Site Generation (SSG), cache, or both.

Released under the MIT License.