Convert Figma logo to code with AI

C2FO logofast-csv

CSV parser and formatter for node

1,638
208
1,638
49

Top Related Projects

Full featured CSV parser with simple api and tested against large datasets.

1,542

A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.

Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.

Quick Overview

The fast-csv library is a high-performance CSV parsing and formatting library for Node.js. It provides a simple and efficient way to read and write CSV data, with support for various options and customizations.

Pros

  • High Performance: The library is designed to be fast and efficient, making it suitable for processing large datasets.
  • Flexible Configuration: fast-csv offers a wide range of configuration options, allowing users to customize the parsing and formatting behavior to fit their specific needs.
  • Streaming Support: The library supports streaming, which enables processing of large CSV files without loading the entire dataset into memory.
  • Cross-Platform Compatibility: fast-csv works seamlessly across different platforms, including Windows, macOS, and Linux.

Cons

  • Limited Ecosystem: Compared to some other CSV libraries, fast-csv may have a smaller ecosystem of plugins and integrations.
  • Lack of Advanced Features: While the library is highly performant and configurable, it may not offer some more advanced features found in other CSV libraries, such as complex data transformations or data validation.
  • Dependency on Node.js: As a Node.js library, fast-csv is not directly usable in browser-based environments, which may limit its applicability in certain web development scenarios.
  • Potential Learning Curve: The library's flexibility and configuration options may present a steeper learning curve for some users, especially those new to working with CSV data in Node.js.

Code Examples

Reading a CSV file:

const fs = require('fs');
const csv = require('fast-csv');

fs.createReadStream('data.csv')
  .pipe(csv.parse({ headers: true }))
  .on('error', error => console.error(error))
  .on('data', row => console.log(row))
  .on('end', rowCount => console.log(`Parsed ${rowCount} rows`));

Writing a CSV file:

const csv = require('fast-csv');
const fs = require('fs');

const data = [
  { name: 'John Doe', age: 30, email: 'john.doe@example.com' },
  { name: 'Jane Smith', age: 25, email: 'jane.smith@example.com' },
];

fs.createWriteStream('output.csv')
  .pipe(csv.format({ headers: true }))
  .on('error', error => console.error(error))
  .on('data', row => console.log(row))
  .on('end', rowCount => console.log(`Wrote ${rowCount} rows`));

Parsing a CSV string:

const csv = require('fast-csv');

const csvString = 'name,age,email\nJohn Doe,30,john.doe@example.com\nJane Smith,25,jane.smith@example.com';

csv.parseString(csvString, { headers: true })
  .on('error', error => console.error(error))
  .on('data', row => console.log(row))
  .on('end', rowCount => console.log(`Parsed ${rowCount} rows`));

Formatting an array of objects as CSV:

const csv = require('fast-csv');

const data = [
  { name: 'John Doe', age: 30, email: 'john.doe@example.com' },
  { name: 'Jane Smith', age: 25, email: 'jane.smith@example.com' },
];

csv.format(data, { headers: true })
  .on('error', error => console.error(error))
  .on('data', row => console.log(row))
  .on('end', rowCount => console.log(`Formatted ${rowCount} rows`));

Getting Started

To get started with fast-csv, you can install the library using npm:

npm install fast-csv

Then, you can import the library and start using it in your Node.js project. The examples provided above demonstrate some of the basic usage patterns, such as reading and writing CSV data, parsing CSV strings, and formatting arrays of objects as CSV.

For more detailed information and advanced usage, please

Competitor Comparisons

Full featured CSV parser with simple api and tested against large datasets.

Pros of node-csv

  • More comprehensive CSV toolset with separate modules for parsing, generating, transforming, and stringifying
  • Supports both synchronous and asynchronous operations
  • Extensive documentation and examples

Cons of node-csv

  • Slightly more complex API due to its modular structure
  • May have a steeper learning curve for beginners
  • Potentially slower performance for simple CSV operations

Code Comparison

node-csv:

import { parse } from 'csv-parse/sync';
const records = parse(csvString, {
  columns: true,
  skip_empty_lines: true
});

fast-csv:

const csv = require('fast-csv');
csv.parseString(csvString, { headers: true })
  .on('data', (row) => {
    // Process each row
  });

Both libraries offer efficient CSV parsing capabilities, but node-csv provides a more modular approach with separate functions for different CSV operations. fast-csv, on the other hand, offers a simpler API that may be easier for beginners to grasp quickly. The choice between the two depends on the specific requirements of your project and your familiarity with CSV processing in Node.js.

1,542

A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.

Pros of CSV.js

  • Lightweight and simple to use, with a focus on browser compatibility
  • Supports both parsing and stringifying CSV data
  • Provides a synchronous API, which can be beneficial for certain use cases

Cons of CSV.js

  • Less feature-rich compared to fast-csv
  • Limited options for customization and advanced parsing scenarios
  • Smaller community and fewer updates

Code Comparison

CSV.js parsing example:

const csv = new CSV(csvString);
const records = csv.parse();

fast-csv parsing example:

const stream = fs.createReadStream('input.csv');
csv.parseStream(stream)
  .on('data', (row) => {
    // Process each row
  })
  .on('end', () => {
    console.log('Parsing finished');
  });

CSV.js is more straightforward for simple parsing tasks, while fast-csv offers a stream-based approach with more flexibility and options for handling large datasets. fast-csv provides a more robust set of features, including support for various parsing options, data transformation, and error handling. However, CSV.js may be preferable for lightweight browser-based applications or when a simpler API is desired. The choice between the two libraries depends on the specific requirements of your project, such as performance needs, environment constraints, and the complexity of CSV processing tasks.

Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.

Pros of node-csvtojson

  • Built-in support for various CSV parsing options and data transformations
  • Supports both synchronous and asynchronous parsing
  • Includes CLI tool for quick CSV to JSON conversion

Cons of node-csvtojson

  • Slightly slower performance for large files compared to fast-csv
  • Less actively maintained (fewer recent updates)

Code Comparison

node-csvtojson:

const csv = require('csvtojson');
csv()
  .fromFile('input.csv')
  .then((jsonObj) => {
    console.log(jsonObj);
  });

fast-csv:

const fs = require('fs');
const csv = require('fast-csv');

fs.createReadStream('input.csv')
  .pipe(csv.parse({ headers: true }))
  .on('data', (row) => {
    console.log(row);
  });

Both libraries offer straightforward ways to parse CSV files, but node-csvtojson provides a more concise API for file parsing and automatic conversion to JSON objects. fast-csv requires more manual setup but offers finer control over the parsing process and streaming capabilities.

node-csvtojson is better suited for quick conversions and projects requiring built-in data transformations, while fast-csv excels in performance-critical applications and scenarios requiring precise control over the parsing process.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

fast-csv Logo

npm version Build Status Coverage Status Known Vulnerabilities

Fast-csv

Fast-csv is library for parsing and formatting CSVs or any other delimited value file in node.

Features

  • CSV Formatting
  • CSV Parsing
  • Built using typescript.
  • Flexible formatting and parsing options, to fit almost any scenario.
  • Built with streams first to avoid creating large memory footprint when parsing large files.
  • Battle tested in production, parsing and formatting millions of records every day.

Install

See installation docs

Packages

  • fast-csv - One stop shop for all methods and options from @fast-csv/format and @fast-csv/parse. Get Started!
  • @fast-csv/parse - Parsing package, use this if you only need to parse files. Get Started!
  • @fast-csv/format - Formatting package, use this if you only need to format files. Get Started!

License

MIT https://github.com/C2FO/fast-csv/raw/master/LICENSE

Meta

NPM DownloadsLast 30 Days