Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
The fast-csv npm package is a comprehensive library for working with CSV data in Node.js. It provides functionalities for parsing CSV files and strings, formatting data to CSV, and transforming data during the parse and format process.
Parsing CSV
This feature allows you to parse CSV files or strings. The code sample demonstrates how to read a CSV file using a stream, parse it with headers, and log each row to the console.
const fs = require('fs');
const fastcsv = require('fast-csv');
fs.createReadStream('data.csv')
.pipe(fastcsv.parse({ headers: true }))
.on('data', row => console.log(row))
.on('end', () => console.log('CSV file successfully processed'));
Formatting Data to CSV
This feature allows you to format JavaScript data arrays or streams to CSV. The code sample shows how to take an array of objects and write it to a CSV file with headers.
const fs = require('fs');
const fastcsv = require('fast-csv');
const data = [{ id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Doe' }];
fastcsv
.write(data, { headers: true })
.pipe(fs.createWriteStream('out.csv'))
.on('finish', () => console.log('Write to out.csv successfully!'));
Transforming Data
This feature allows you to transform data during the parse or format process. The code sample demonstrates how to read a CSV file, transform each row by combining first and last names into a full name, and then write the transformed data to a new CSV file.
const fs = require('fs');
const fastcsv = require('fast-csv');
fs.createReadStream('data.csv')
.pipe(fastcsv.parse({ headers: true }))
.transform(row => ({ fullName: row.firstName + ' ' + row.lastName }))
.pipe(fastcsv.format({ headers: true }))
.pipe(fs.createWriteStream('out.csv'))
.on('finish', () => console.log('Transformed file successfully written.'));
PapaParse is a robust and powerful CSV parser for JavaScript with a similar feature set to fast-csv. It supports browser and server-side parsing, auto-detection of delimiters, and stream parsing. Compared to fast-csv, PapaParse has a stronger emphasis on browser-side parsing and provides a more user-friendly configuration for handling large files in the browser.
csv-parser is a Node.js module for parsing CSV data. It can handle large datasets and supports stream-based processing. While fast-csv offers both parsing and formatting capabilities, csv-parser focuses primarily on parsing CSV data and may be preferred for its simplicity when only parsing functionality is needed.
csv-writer is a CSV writing library for Node.js that provides functionality to serialize arrays and objects into CSV files. Unlike fast-csv, which offers both parsing and formatting, csv-writer is specialized for CSV output, making it a good choice if the primary requirement is to generate CSV files from data.
This is a library is aimed at providing fast CSV parsing. It accomplishes this by not handling some of the more complex edge cases such as multi line rows. However it does support escaped values, embedded commas, double and single quotes.
npm install fast-csv
To parse a file.
var csv = require("fast-csv");
csv("my.csv")
.on("data", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
})
.parse();
You may also parse a stream.
var stream = fs.createReadStream("my.csv");
csv(stream)
.on("data", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
})
.parse();
If you expect the first line your csv to headers you may pass a headers option in. Setting the headers option will cause change each row to an object rather than an array.
var stream = fs.createReadStream("my.csv");
csv(stream, {headers : true})
.on("data", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
})
.parse();
You may alternatively pass an array of header names which must match the order of each column in the csv, otherwise the data columns will not match.
var stream = fs.createReadStream("my.csv");
csv(stream, {headers : ["firstName", "lastName", "address"]})
.on("data", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
})
.parse();
If your data may include empty rows, the sort Excel might include at the end of the file for instance, you can ignore
these by including the ignoreEmpty
option.
Any rows consisting of nothing but empty strings and/or commas will be skipped, without emitting a 'data' or 'error' event.
var stream = fs.createReadStream("my.csv");
csv(stream, {ignoreEmpty: true})
.on("data", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
})
.parse();
You can validate each row in the csv by providing a validate handler. If a row is invalid then a data-invalid
event
will be emitted with the row and the index.
var stream = fs.createReadStream("my.csv");
csv(stream, {headers : true})
.validate(function(data){
return data.age < 50; //all persons must be under the age of 50
})
.on("data-invalid", function(data){
//do something with invalid row
})
.on("data", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
})
.parse();
You can transform data by providing in a transform function. What is returned from the transform function will be provided to validate and emitted as a row.
var stream = fs.createReadStream("my.csv");
csv(stream)
.transform(function(data){
return data.reverse(); //reverse each row.
})
.on("data", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
})
.parse();
##Namespaces
##Classes
MIT https://github.com/C2FO/fast-csv/raw/master/LICENSE
##Meta
git clone git://github.com/C2FO/fast-csv.git
FAQs
CSV parser and writer
The npm package fast-csv receives a total of 1,249,095 weekly downloads. As such, fast-csv popularity was classified as popular.
We found that fast-csv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.