Type-safe CSV and Google Sheets parser for TypeScript and JavaScript
Using TypeBox, d3-dsv, Web Fetch API (and Tempo for date transformer) under the hood, Sheethuahua should be supported by every modern browsers and back-end runtime.
npm i sheethuahua
ID | Name | Email Address | Phone Number |
---|---|---|---|
1 | Samoyed | samo123@doggo | 0800000000 |
2 | Shiba | shibainu@doggo |
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()),
}),
});
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);
[
{
"id": 1,
"name": "Samoyed",
"contact": { "email": "samo123@doggo", "phone": "0800000000" }
},
{
"id": 2,
"name": "Shiba",
"contact": { "email": "shibainu@doggo" }
}
]
import { formatToCsv } from 'sheethuahua';
const csvString = formatToCsv(output, schema);
console.log(csvString);
ID,Name,Email Address,Phone Number
1,Samoyed,samo123@doggo,0800000000
2,Shiba,shibainu@doggo,
Released under the MIT License - Copyright © 2024-present Punch Up