### Installation Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Install the bun-csv package using Bun. ```bash bun add @nds-stack/bun-csv ``` -------------------------------- ### Real-World Example Source: https://github.com/nds-stack/bun-csv/blob/main/README.md A comprehensive example demonstrating parsing a CSV file, filtering and transforming data, and writing it back as a new CSV file, including streaming from a URL. ```typescript import { parse, stringify, parseStream } from "@nds-stack/bun-csv"; import { write } from "bun"; // Parse downloaded CSV const file = Bun.file("./users.csv"); const text = await file.text(); const users = parse(text, { cast: true }); // Filter + transform const activeUsers = users.data.filter(u => u.active === true); const transformed = activeUsers.map(u => ({ name: u.name, email: u.email, age: u.age, })); // Write back as CSV const output = stringify(transformed); await write("./active-users.csv", output); // Or stream directly from URL const response = await fetch("https://example.com/report.csv"); const text = await response.text(); const data = parse(text); console.log(`Parsed ${data.rows} rows`); ``` -------------------------------- ### Multi-Instance / Cross-Boundary Usage Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Example demonstrating stateless parser usage across multiple instances or workers. ```typescript // Worker 1 const result1 = parse(csv1); // Worker 2 (same import) const result2 = parse(csv2); // Streaming in separate processes for await (const chunk of parseStream(file)) { ... } ``` -------------------------------- ### Streaming Large File Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Demonstrates parsing a large file efficiently using streams, processing data in chunks. ```typescript import { parseStream } from "@nds-stack/bun-csv"; const file = Bun.file("./massive.csv"); for await (const chunk of parseStream(file)) { await processBatch(chunk.data); } ``` -------------------------------- ### Extending Parser Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Shows how to extend the parser functionality by creating a custom parsing function with added validation. ```typescript import { parse } from "@nds-stack/bun-csv"; function parseWithValidation(csv: string) { const result = parse(csv); if (result.rows === 0) throw new Error("Empty CSV"); return result; } ``` -------------------------------- ### Headerless Mode Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Parses CSV data when there is no header row, assigning default column names. ```typescript const result = parse(csv, { hasHeader: false }); // headers: ["column0", "column1", ...] ``` -------------------------------- ### Custom Delimiter (TSV) Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Parses and stringifies data using a custom delimiter, specifically tab for TSV. ```typescript import { parse, stringify } from "@nds-stack/bun-csv"; const tsv = parse(data, { delimiter: "\t" }); const output = stringify(tsv.data as Record[], { delimiter: "\t" }); ``` -------------------------------- ### ParseResult Interface Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Interface for the result of the parse function. ```typescript interface ParseResult> { data: T[] headers: string[] rows: number } ``` -------------------------------- ### Auto-Type Casting Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Enables automatic type casting for values during parsing, converting strings to their appropriate types. ```typescript const result = parse(csv, { cast: true }); // "30" → 30, "true" → true, "false" → false ``` -------------------------------- ### StreamChunk Interface Source: https://github.com/nds-stack/bun-csv/blob/main/README.md Interface for chunks yielded by parseStream. ```typescript interface StreamChunk> { data: T[] chunkIndex: number byteOffset: number } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.