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-hero

v0.0.4

Published

CSV Hero will save your day and parse all the CSV files you have

Downloads

26

Readme

CSV Hero - The powerful CSV parser

CSV Hero

CSV Hero will save your day and handle all the CSV files you have to parse! CSV Hero can parse your csv files even when no one else can.

Features

Here is what CSV Hero can do for you:

  • Parse CSV with an easy to use api
  • Handle different input sources
  • Automatically detect line breaks, delimiter and quote style
  • Handle badly escaped quotes
  • Work in the background as Worker
  • Detect numbers and boolean values
  • Bring no dependencies
  • Try it out

Installation

Install with NPM

npm i csv-hero

or include the compiled file

<script src="https://cdn.rawgit.com/marius-wieschollek/csv-hero/master/dist/csv-hero.web.js"></script>

or build from source

git clone https://github.com/marius-wieschollek/csv-hero.git
cd csv-hero
npm install
npm run build

Usage

Using CSV Hero is absolutely easy:

// Just parse a csv string
CsvHero.parse('Easy,as,123');

// Parse a user file
let SomeFile = document.getElementById('some-file').files[0];
CsvHero.parse(SomeFile);

// Use some custom options
CsvHero.parse('Easy,as,123', {worker:true});

CSV Hero uses Promises:

CsvHero.parse('Easy,as,123')
    .then(console.log)
    .catch(console.error);

Parser Options

| Option | Type | Default | Description | | --- | --- | --- | --- | | delimiter | string | auto | The character used to separate the columns. If the value is auto, CSV Hero will try to find the best match. | | newLine | string | auto | The new line character. If the value is auto. CSV Hero will try to find the best match and default to \n if he can't. | | quotes | string | auto | The quote character used to encapsulate fields. If the value is auto, CSV Hero will try to find the best match. | | escape | string | auto | The escape character used to escape the quote character inside a quoted field. If the value is auto, CSV Hero will use the same as the quote character. | | comment | string | - | The comment character. If a line is prefixed with this character, it will be ignored. Usually empty to disable quotes. | | encoding | string | UTF-8 | If you pass a file to CSV Hero, you can specify the encoding. | | strictSpaces | boolean | true | Defines how CSV Hero handles spaces before the first and after the last quote in a field. See Strict Spaces Option for details. | | strictQuotes | boolean | true | Defines how CSV Hero handles quotes. If it is disabled, will try to detect badly escaped quotes. See Strict Quotes Option for details. | | strictEndingQuotes | boolean | false | Enable or disable the detection of badly escaped quotes at the end of a field if strictQuotes is disabled. See Strict Ending Quotes Option for details. | | strictRows | boolean | false | If this option is enabled, CSV Hero will guarantee a minimum row size and report an error for rows that have too many columns. | | rowSize | number | -1 | Sets the deisred row size for strictRows. If the value is -1, the size of the first row will be used as reference. The detection will ignore skipHeader. | | maxRows | number | -1 | If set to any value except -1, CSV Hero will parse after the given amount of rows were parsed. skipEmptyRows, skipEmptyFieldRows and skipHeader will be respected. | | skipHeader | boolean | false | Skip the first row which is usually used as header. skipEmptyRows and skipEmptyFieldRows will be respected. | | skipEmptyRows | boolean | false | Skip rows with no values at all. | | skipEmptyFieldRows | boolean | false | Skips rows with only empty fields. See trimFields if you want to include fields with only spaces too. | | trimFields | boolean | false | Trim spaces at the beginning and end of a value. This is a post processing filter and hass no effect on strictSpaces. | | castTypes | boolean | false | Detect numbers and booleans and convert them to their native type. | | mapFields | boolean | false | Map colums to a field name instead of creating an array with the values. | | fieldMapping | array | [] | The property names for mapFields. If it is left empty, the header row will be used. Extra fields will be named field_<index>. | | worker | boolean | false | Run all jobs as a WebWorker. Workers run in the background and will prevent your site from becoming unresponsive. | | workerUrl | string|null | null | If needed you may set the worker url here (URL to the CSV Hero script). If you embedded CSV Hero as standalone script you may not need this. | | ignoreErrors | boolean | false | Ignore if errors occur and always trigger the success function. |

Strict Spaces Option

By default, CSV Hero will enforce standards compliant behaviour and consider spaces at the beginning and at the end of a column as part of the content. In some cases that may not be wanted. See the following example:

Column 1,Column 2,Column 3
"Value 1" , "Value 2" , "Value 3"

With strictSpaces enabled, an error may be logged and the result will look like this:

| Column 1 | Column 2 | Column 3 | | --- | --- | --- | | Value 1" , "Value 2" , "Value 3 | | |

With strictSpaces disabled, the result will look like this:

| Column 1 | Column 2 | Column 3 | | --- | --- | --- | | Value 1 | Value 2 | Value 3 |

With strictSpaces disabled, CSV Hero will ignore the spaces around the values in the second line as long as it finds a delimiter at some point

Strict Quotes Option

By default, CSV Hero will enforce standards compliant behaviour and report an error if an unescaped quote character is found, but no delimiter follows. Sometimes you may have to parse csv files where quotes were not escaped properly. Take the content below as example:

Column 1,Column 2
"Oops, some bad "Quotes"","What could go wrong?"

With strictQuotes enabled, an error will be reported and the result will look like this:

| Column 1 | Column 2 | | --- | --- | | Oops, some bad "Quotes"!,"What could go wrong? | |

With strictQuotes disabled, the result will look like this:

| Column 1 | Column 2 | | --- | --- | | Oops, some bad "Quotes" | What could go wrong? |

Strict Ending Quotes

With strictQuotes disabled, CSV Hero will try to determine if a quote is used as field start, content or in combination with the delimiter as field end. However especially the field endings are very hard to detect, and it may happen that CSV Hero detects the quote + delimiter combination inside a field. See this example:

JSON
"{"well":"that","sucks":"now"}"
"{""well"":""that"",""sucks"":""now""}"

The commas in the json string will look exactly like a field delimiter and therefore counted as one. even the correctly encoded third line will be an issue now:

| JSON | | | --- | --- | | {"well":"that | sucks":"now"} | | {"well":"that" | "sucks":"now"} |

Enabling strictEndingQuotes will disable the check for bad quotes at the end of a field and therefore guarantee that proper csv parses correctly:

| JSON | | | --- | --- | | {"well":"that | sucks":"now"} | | {"well":"that","sucks":"now"} |

However, bad quotes at the end will no longer be detected. Sometimes even a hero can't save you from CSV hell.