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

js-csvparser

v1.5.0

Published

Fast and feature rich CSV parser with great auto detection for line ending and delimiter.

Downloads

49

Readme

js-csvparser (Javascript CSV Parser)

NPM version Build Status

Fast and feature rich CSV parser with great auto detection for line ending and delimiter. It can be used as node module or in the browser. It is completely independent from other javascript libraries but should work side-by-side with any other library.

###Usage

// A simple echo program:
const csvparse = require('js-csvparser');

console.log(csvparse('h1,h2,h3\r\n1,2,3\r\n4,5,6'));

If you don't want to use default options, pass in an options object as second parameter.

// A simple echo program:
let defaultOptions = {
    delimiter: 'auto',
    lineEnding: 'auto',
    comment: '#',
    convertToTypes: {
        convert: false,
        decimalDelimiter: 'auto',
        dateFormat: 'yyyy-mm-dd'
    },
    skipEmptyLines: false,
    maxRows: 0,
    maxColumns: {
        numberOfColumns: -1,
        cutRemaining: false
    },
    header: 'auto'
};

console.log(csvparse('h1,h2,h3\r\n1,2,3\r\n4,5,6', defaultOptions));

###Node.js Installation

npm install js-csvparser

Current version should work with all Node.js versions, at least with version 4 and above.

###Features

  • Easy to use
  • Great auto detection for line ending and delimiter
  • Can handle header rows with simple auto detection
  • Configurable delimiter, line ending and comment character
  • Can handle empty lines, even at the beginning
  • Ignores lines starting with comment character
  • No external dependencies
  • Very fast (each character will be accessed only once, despite the usual access for the output data)
  • Can convert numbers, booleans and dates to their types

###Options

You can set different options for parsing the data.

let defaultOptions = {
    // Specify the delimiter for each cell. Leave blank to auto-detect.
    // If specified, it must be a string of length 1.
    delimiter: 'auto',
    
    // Specify the line ending character. Leave blank to auto-detect. 
    // If specified, it must be one of  \r, \n, or \r\n.
    lineEnding: 'auto',
    
    // Specify the escape character. Lines starting with this string 
    // will be skipped. If specified, it must be a string of length 1.
    comment: '#', 
    
    convertToTypes: {
        // If true, numeric, boolean and date data will be converted
        // to their type instead of remaining strings.
        convert: false,
        
        // Specify the decimal delimiter for converting numeric data.
        // Leave blank to auto-detect. If specified, it must be '.' or ','.
        decimalDelimiter: 'auto',
        
        // Specify the date format using one of the following tokens:
        // token| description       | example
        // yyyy | 4 or 2 digit year | 2014
        // yy 	| 2 digit year      |   14
        // m mm | month number      | 1..12
        // d dd	| day number        | 1..31
        // H HH | 24 hour time      | 0..23
        // M MM	| Minutes           | 0..59
        // S SS | Seconds           | 0..59
        dateFormat: 'yyyy-mm-dd'
    },

    // If true, lines that are completely empty will be skipped. An empty 
    // line is defined to be one which evaluates to empty string.
    skipEmptyLines: false,
    
    // Limit the number of parsed lines, 0 means no limit (e.g. parse the 
    // whole file).
    maxRows: 0,
     
    maxColumns: {
        // Limit the number of parsed columns, -1 means no limit (e.g. parse 
        // all columns).
        numberOfColumns: -1,
        
        // If there is a column limit, specify how to procced with the 
        // remaining date of the row: If true, the last column of the output 
        // will contain the remaining data of the parsed row, otherwise the 
        // remaining data of the row will be omitted.
        cutRemaining: false
    },

    // One of 'auto', true (meaning one header row), false (no header row) or
    // any non negative number (meaning the number of header rows). In the case 
    // of 'auto', js-csvparser is try its best to detect the number of header rows.
    header: 'auto'
};

###References

Several other good csvparser with slightly different features were used as guideline:

This small javascript component uses or is based on other javascript projects and code snippets:

Licence

MIT