npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

csv-toolkit

v3.0.0

Published

Parse and write CSV files efficiently via an intuitive API

Downloads

4

Readme

CSVToolkit

Parse and write CSV files efficiently with an intuitive API.

Installation

$ npm install csv-toolkit

Quick Start

Parsing CSV files

Let's say you have a CSV file, data.csv, with this structure:

Name,Ranking,Height
Jason,110,1.65
Derek,55,1.45
Anne,63,1.34

Parsing the CSV file:

const CSVToolkit = require("csv-toolkit")

CSVToolkit.parseFile("./data.csv"))
          .then(data => console.log("Player #0 rank:", data[0].Ranking))

Writing CSV files

CSVToolkit enables you to convert arrays of JavaScript objects into CSV files with a reasonable degree of flexibility.

const CSVToolkit = require("csv-toolkit")

const myData = [
  {
    name: "Will",
    language: "JavaScript",
    "Favorite Color": "Teal"
  },
  {
    name: "Derek",
    language: "English",
    "Favorite Color": "Blue"
  }
]

// write our data to disk as a CSV
CSVToolkit.serializeAndWrite("./ouput.csv", {
    fields: [ "language", "name" ]
}).then((csvString) => console.log("CSV saved! Great job!"))

This would output the following CSV:

language,name
JavaScript,Will
English,Derek

Documentation

CSVToolkit.parse(string,options?)

Parameters

string a CSV string from which data will be extracted,

options is an (optional) object which allows you to specify the delimiting strings:

{
  fieldDelimiter: (String, this string seperates values in the CSV; Default: ","),
  rowDelimiter: (String, this string seperates rows in the CSV; Default: "\n"),
  headerDelimiter: (String, this string seperates header/column names in the CSV; Default: ",")
}

Note: if you specify fieldDelimiter this value will also be used for the headerDelimiter if the headerDelimiter has not been specified.

Returns: an array of row objects.

CSVToolkit.parseFile(path,options?)

path path from which the CSV will be read,

options is an (optional) object which allows you to specify the delimiting strings:

{
  fieldDelimiter: (String, this string seperates values in the CSV; Default: ','),
  rowDelimiter: (String, this string seperates rows in the CSV; Default: '\n'),
  headerDelimiter: (String, this string seperates header/column names in the CSV; Default: ','),
}

Note: if you specify fieldDelimiter this value will also be used for the headerDelimiter if the headerDelimiter has not been specified.

Returns: a promise which resolves to an array of row objects.

CSVToolkit.serialize(data,options?)

data the object to be converted,

options an (optional) object used to control the string output:

{
  prepend: (String, prepend this string to the output),
  fields: (String (comma seperated) or Array, a list of fields, order is preserved),
  
  fieldDelimiter: (String, this string seperates values in the CSV; Default: ","),
  rowDelimiter: (String, this string seperates rows in the CSV; Default: "\n"),
  headerDelimiter: (String, this string seperates header/column names in the CSV; Default: ",")
}

Note: if you specify fieldDelimiter this value will also be used for the headerDelimiter if the headerDelimiter has not been specified.

Returns: a csv string, or an Error if serialization fails.

CSVToolkit.serializeAndWrite(data,outputPath,options?)

data the array of rows to be converted and written to CSV

outputPath path to which the CSV will be written

options an (optional) object used to control the CSV output file:

{
  prepend: (String, prepend this string to the CSV file),
  fields:  (A comma-delimited String, or an Array. A list of fields, order is preserved),
  
  fieldDelimiter: (String, this string seperates values in the CSV; Default: ","),
  rowDelimiter: (String, this string seperates rows in the CSV; Default: "\n"),
  headerDelimiter: (String, this string seperates header/column names in the CSV; Default: ",")
}

Note: if you specify fieldDelimiter this value will also be used for the headerDelimiter if the headerDelimiter has not been specified!

Returns: a promise which resolves after the file has been written, with the csv string.

Bug Reports, Improvements

Please open a new issue on this package's GitHub Repository Issue Page.

License

This package uses the ISC License.

If this package saved you time, money, or effort, please consider adding somewhere Thanks to Will Brickner for CSVToolkit. I worked hard on it and gave it to you for free.

DISCLAIMER

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.