csv-it
Simple and useful tools to work with CSV files.
Features
- Read all the rows from a file or row by row
- Write rows into a file or row by row
- The flexibility when creating the files: the directory structure is created (aka
mkdirp
) - Promises, callbacks and streams
🚀
☁️ Installation
# Using npm
npm install --save csv-it
# Using yarn
yarn add csv-it
📋 Example
const CsvIt = require("csv-it");
// Create a stream into a directory which is going to be created
const testStream = CsvIt.writeAsync(`${__dirname}/csv/some/directory/first.csv`)
testStream.write({ A: 42 , B: 33 })
testStream.write({ A: 7 , B: -1 })
testStream.end()
// Write the rows which are already available
const SECOND_PATH = `${__dirname}/csv/some/directory/second.csv`
CsvIt.write(SECOND_PATH, [
{ A: 42 , B: 33 }
, { A: 7 , B: -1 }
]).then(() => {
console.log("Done")
return CsvIt.read(SECOND_PATH)
}).then(res => {
console.log(res)
})
❓ Get Help
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues.
🐛 - For direct and quick help, you can use Codementor.
🚀
📝 Documentation
read(file, opts, cb, onData, transformFn)
Read a CSV file.
Params
-
String
file
: The file path of the CSV file. -
Object
opts
: An object containing the following fields: -
Function
cb
: The callback function. -
Function
onData
: The on data handler. If provided the rows will not be cached in memory. -
Function
transformFn
: The transform handler. Use it for async operations.
Return
- Promise A promise resolving to the csv content.
write(file, rows, opts, cb)
Write the rows in the CSV file.
Params
-
String
file
: The file path of the CSV file. -
Array
rows
: The rows to write in the CSV file. -
Object
opts
: An object containing the following fields:-
headers
(Array|Boolean): The list of headers or enable the headers. - other fields passed to the
fast-csv
library
-
-
Function
cb
: The callback function.
Return
- Promise A promise resolving to the csv content.
writeAsync(file, rows, opts, cb)
Create a CSV file stream which you can use to write rows manually.
Params
-
String
file
: The file path of the CSV file. -
Array
rows
: The rows to write in the CSV file. -
Object
opts
: An object containing the following fields:-
headers
(Array|Boolean): The list of headers or enable the headers. -
append
(Boolean): Wheater to append the data in the CSV file or override it (default: override) - other fields passed to the
fast-csv
library
-
-
Function
cb
: The callback function.
Return
- Stream The CSV stream you can write to. Note you have to end it.
😋 How to contribute
Have an idea? Found a bug? See how to contribute.
💖 Support my projects
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
-
Starring and sharing the projects you like
🚀 -
—I love books! I will remember you after years if you buy me one.
😁 📖 -
—You can make one-time donations via PayPal. I'll probably buy a
coffeetea.🍵 -
—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
-
Bitcoin—You can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks!
💫 Where is this library used?
If you are using this library in one of your projects, add it in this list.
spreadsheet-parser