csv-it

2.0.0 • Public • Published

csv-it

Support me on Patreon Buy me a book PayPal Ask me anything Version Downloads Get help on Codementor

Buy Me A Coffee

Simple and useful tools to work with CSV files.

Features

  1. Read all the rows from a file or row by row
  2. Write rows into a file or row by row
  3. The flexibility when creating the files: the directory structure is created (aka mkdirp)
  4. 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:

  1. Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
  2. For bug reports and feature requests, open issues. 🐛
  3. 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 🚀

  • Buy me a book—I love books! I will remember you after years if you buy me one. 😁 📖

  • PayPal—You can make one-time donations via PayPal. I'll probably buy a coffee tea. 🍵

  • Support me on Patreon—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

📜 License

MIT © Ionică Bizău

Package Sidebar

Install

npm i csv-it

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

14.7 kB

Total Files

4

Last publish

Collaborators

  • ionicabizau