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

simply-parse-csv

v1.2.2

Published

Simply parse a .csv file into a javascript object with or without designated headers.

Downloads

56

Readme

Simply Parse CSV

NPM

Usage

Simply pass a csv string into simply-parse-csv, and it will return an array of javascript objects containing the data from each row.

Example Input/Output:

Input (with headers):

"id","name","birthday"
1,"Jeff","1983-04-21"
2,"Micah","1995-11-05"

Output:

[
  {
    id: 1,
    name: "Jeff",
    birthday: "1983-04-21"
  },
  {
    id: 2,
    name: "Micah",
    birthday: "1995-11-05"
  }
]

Usage Examples

In its simplest and intended form, you can run the parseCSV function seen below using the default options (see the Available Options section) by simply passing a standard .csv string with a header row: parseCSV(csvData);.

What follows are examples of slightly more involved use cases.

Node.js ES6 Example:

const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const parseCsv = require('simply-parse-csv');

async function main () {
  const csvPath = './path/to/file.csv';

  let csvData = await readFile(csvPath, 'utf8'); // Get csv string from file

  let options = { // Set options
    trim = true
  }
  let parsed = await parseCsv(csvData, options);

  console.log(util.inspect(parsed));
}

main();

Node.js ES6 Example with defined Headers:

const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const parseCsv = require('simply-parse-csv');

async function main () {
  const csvPath = './path/to/file.csv';

  let csvData = await readFile(csvPath, 'utf8'); // Get csv string from file

  const headers = ['id', 'name', 'birthday'];

  let options = { // Set options
    trim = true,
    containsHeaders: false,
    headers: headers
  }
  let parsed = await parseCsv(csvData, options);

  console.log(util.inspect(parsed));
}

main();

Node.js Promise.then Example:

const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const parseCsv = require('simply-parse-csv');

async function main () {
  const csvPath = './path/to/file.csv';
  
  readFile(csvPath, 'utf8') // Read in CSV string
  .then(csvData => {

    let options = { // Set Options
      trim = true
    }

    return parseCsv(csvData, options); // Pass CSV string and options into parseCsv Function
  }).then(parsed => {
    console.log(util.inspect(parsed)); 
    /* Do something */  
  })
}

main();

Available Options

  • containsHeaders:
    • Accepts: boolean
    • Default: true
    • If true, read the headers contained in the first row of the csv file.
    • If false, either use headers supplied in the headers option or use default headers; column1, column2, ...
  • headers:
    • Accepts: array of strings
    • Default: null
    • If defined, then associate columns to this header array, in order
  • trim:
    • Accepts: boolean
    • Default: false
    • Choose whether to use the str.trim() function on the csv data before returning.
  • parseAllAsString
    • Accepts: boolean
    • Default: false
    • Choose whether to return everything in the CSV as a string.

Data Types and Quoted Strings

  • This module automatically parses datatypes; "1.0" remains a string, 1.0 becomes 1 as an integer, 1.2 becomes 1.2 as a floating point (unless options.parseAllAsString === true).
  • This module should work just as well for .csv data without quote encapsulated strings as for .csv data with properly quoted (single or double) strings.