Working with TSV in TypeScript
Tab-separated values (TSV) file handling with automatic format detection and high-performance data operations.
Installation
Section titled “Installation”npm install fairspecGetting Started
Section titled “Getting Started”The TSV format is handled by the CSV plugin, which provides:
loadCsvTable- Load TSV files into tablessaveCsvTable- Save tables to TSV filesCsvPlugin- Plugin for framework integration
For example:
import { loadCsvTable } from "fairspec"
const table = await loadCsvTable({ data: "table.tsv" })// the column types will be automatically inferredBasic Usage
Section titled “Basic Usage”Loading TSV Files
Section titled “Loading TSV Files”import { loadCsvTable } from "fairspec"
// Load a simple TSV fileconst table = await loadCsvTable({ data: "data.tsv" })
// Load with explicit formatconst table = await loadCsvTable({ data: "data.tsv", format: { name: "tsv", headerRows: [1] }})
// Load multiple TSV files (concatenated)const table = await loadCsvTable({ data: ["part1.tsv", "part2.tsv", "part3.tsv"]})Saving TSV Files
Section titled “Saving TSV Files”import { saveCsvTable } from "fairspec"
// Save with default optionsawait saveCsvTable(table, { path: "output.tsv", format: { name: "tsv" }})
// Save with line terminator optionawait saveCsvTable(table, { path: "output.tsv", format: { name: "tsv", lineTerminator: "\r\n" }})Advanced Features
Section titled “Advanced Features”Multi-Header Row Processing
Section titled “Multi-Header Row Processing”// TSV with multiple header rowsconst table = await loadCsvTable({ data: "multi-header.tsv", format: { name: "tsv", headerRows: [1, 2], headerJoin: "_" }})Comment Handling
Section titled “Comment Handling”// TSV with comment linesconst table = await loadCsvTable({ data: "with-comments.tsv", format: { name: "tsv", commentPrefix: "#", headerRows: [1] }})
// Or specify specific comment row numbersconst table = await loadCsvTable({ data: "with-comments.tsv", format: { name: "tsv", commentRows: [1, 2], headerRows: [3] }})Remote File Loading
Section titled “Remote File Loading”// Load from URLconst table = await loadCsvTable({ data: "https://example.com/data.tsv"})
// Load multiple remote filesconst table = await loadCsvTable({ data: [ "https://api.example.com/data-2023.tsv", "https://api.example.com/data-2024.tsv" ]})