Skip to content

Sheethuahua

Type-safe CSV and Google Sheets Parser

For TypeScript and JavaScript

Quick Start 🪄

1. Adopt our little doggo

bash
npm i sheethuahua

2. Define the schema

IDNameEmail AddressPhone Number
1Samoyedsamo123@doggo0800000000
2Shibashibainu@doggo
ts
import { Column, Object, asNumber, asString } from 'sheethuahua';

const schema = Object({
	id: Column('ID', asNumber()),
	name: Column('Name', asString()),
	contact: Object({
		email: Column('Email Address', asString()),
		phone: Column('Phone Number', asString().optional()),
	}),
});

3. Parse the data

ts
import { parseCsv, fetchCsv, Spreadsheet } from 'sheethuahua';

// const output: {
//     id: number;
//     name: string;
//     contact: {
//         email: string;
//         phone?: string | undefined;
//     };
// }[]
const output = parseCsv('some,csv,string', schema);
// or from URL
const output = await fetchCsv('https://url-to-csv', schema);
// or from Google Sheets
const output = await Spreadsheet('google-sheets-id').get('Sheet1', schema);

console.log(output);
json
[
	{
		"id": 1,
		"name": "Samoyed",
		"contact": { "email": "samo123@doggo", "phone": "0800000000" }
	},
	{
		"id": 2,
		"name": "Shiba",
		"contact": { "email": "shibainu@doggo" }
	}
]

4. Format back to CSV if you need

ts
import { formatToCsv } from 'sheethuahua';

const csvString = formatToCsv(output, schema);

console.log(csvString);
csv
ID,Name,Email Address,Phone Number
1,Samoyed,samo123@doggo,0800000000
2,Shiba,shibainu@doggo,

Released under the MIT License.