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 that provides CSV parsing and formatting.
NOTE As of v0.2.0 fast-csv
supports multi-line values.
npm install fast-csv
All methods accept the following options
headers=false
: Ste to true if you expect the first line of your CSV
to contain headers, alternatly you can specify an array of headers to use.ignoreEmpty=false
: If you wish to ignore empty rows.delimiter=','
: If your data uses an alternate delimiter such as ;
or \t
.
delimiter
you may only pass in a single character delimeterquote='"'
: The character to use to escape values that contain a delimeter.escape='"'
: The character to use when escaping a value that is quoted
and contains a quote
character.
i.e
: 'First,"Name"' => '"First,""name"""'trim=false
: If you want to trim all values parsed set to true.rtrim=false
: If you want to right trim all values parsed set to true.ltrim=false
: If you want to left trim all values parsed set to true.events
parse-error
: Emitted if there was an error parsing a row.
record
: Emitted when a record is parsed.
data-invalid
: Emitted if there was invalid row encounted, only emitted if the validate
function is used.
data
: Emitted with the stringified
version of a record.
([options])
If you use fast-csv
as a function it returns a transform stream that can be piped into.
var stream = fs.createReadStream("my.csv");
var csvStream = csv()
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
stream.pipe(csvStream);
`.fromPath(path[, options])
This method parses a file from the specified path.
var csv = require("fast-csv");
csv
.fromPath("my.csv")
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
`.fromString(string[, options])
This method parses a string
var csv = require("fast-csv");
var CSV_STRING = 'a,b\n' +
'a1,b1\n' +
'a2,b2\n';
csv
.fromPath(CSV_STRING, {headers: true})
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
`.fromStream(stream[, options])
This accepted a readable stream to parse data from.
var stream = fs.createReadStream("my.csv");
csv()
.fromStream(stream)
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
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()
.fromStream(stream, {headers : true})
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
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
.fromStream(stream, {headers : ["firstName", "lastName", "address"]})
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
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
.fromStream(stream, {ignoreEmpty: true})
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
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(
.fromStream(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("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
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
.fromStream(stream)
.transform(function(data){
return data.reverse(); //reverse each row.
})
.on("record", function(data){
console.log(data):
})
.on("end", function(){
console.log("done");
});
fast-csv
also allows to you to create create a CSV
from data.
Formatting accepts the same options as parsing. * Writing Data
Each of the following methods accept an array of values to be written, however each value must be an array
of array
s or object
s.
write(arr[, options])
Create a readable stream to read data from.
var ws = fs.createWritableStream("my.csv");
csv
.write([
["a", "b"],
["a1", "b1"],
["a2", "b2"]
], {headers: true})
.pipe(ws);
var ws = fs.createWritableStream("my.csv");
csv
.write([
{a: "a1", b: "b1"},
{a: "a2", b: "b2"}
], {headers: true})
.pipe(ws);
writeToStream(stream,arr[, options])
Write an array of values to a WritableStream
csv
.writeToStream(fs.createWritableStream("my.csv"), [
["a", "b"],
["a1", "b1"],
["a2", "b2"]
], {headers: true});
csv
.writeToStream(fs.createWritableStream("my.csv"), [
{a: "a1", b: "b1"},
{a: "a2", b: "b2"}
], {headers: true})
.pipe(ws);
writeToPath(arr[, options])
Write an array of values to the specified path
csv
.writeToPath("my.csv", [
["a", "b"],
["a1", "b1"],
["a2", "b2"]
], {headers: true})
.on("finish", function(){
console.log("done!");
});
csv
.writeToStream("my.csv", [
{a: "a1", b: "b1"},
{a: "a2", b: "b2"}
], {headers: true})
.on("finish", function(){
console.log("done!");
});
writeToString(arr[, options])
csv.writeToString([
["a", "b"],
["a1", "b1"],
["a2", "b2"]
], {headers: true}); //"a,b\na1,b1\na2,b2\n"
csv.writeToString([
{a: "a1", b: "b1"},
{a: "a2", b: "b2"}
], {headers: true}); //"a,b\na1,b1\na2,b2\n"
Parsing 20000 records AVG over 3 runs
fast-csv: 198.67ms
csv: 525.33ms
Parsing 50000 records AVG over 3 runs
fast-csv: 441.33ms
csv: 1291ms
Parsing 100000 records AVG over 3 runs
fast-csv: 866ms
csv: 2773.33ms
Parsing 1000000 records AVG over 3 runs
fast-csv: 8562.67ms
csv: 30030.67ms
MIT https://github.com/C2FO/fast-csv/raw/master/LICENSE
##Meta
git clone git://github.com/C2FO/fast-csv.git
##Namespaces
##Classes
v0.2.0
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.