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-json-parser

v1.0.3

Published

csv to json & json to csv parser

Downloads

29

Readme

CsvToJson Parser

CsvToJson Parser is node based library converting CSV text into arrays / objects. It implements the Node.js stream API and es2018 aysnc generators iterables.

Usage

const { csvToJson,
        jsonToCsv,
        csvToJsonFromFile,
        csvToJsonFromUrl,
        jsonToCsvFromFile } 
        = require('csv-json-parser')

##Config Options

csvToJson

// const { outputMode = 'string', delimiter = ',', skipComments = false,  headers = false, , headersArray = [] } = config

| option | description | default | :------------- | :----------------- | :---------| |file|valid file path | |url | url implementing file protocol| |csvString| csv string| | outputMode | stream or string | stream|
| delimiter |,,:,#,@,!,\t,^,* | , | | skipComments |false or true | false | | headers|false or true |false| |headersArray|[array]| []

jsonToCsv

// const { outputMode = 'string', delimiter = ',', skipComments = false,  headers = false, , headersArray = [] } = config

| option | description | default | :------------- | :----------------- | :---------| |file|valid file path | |url | url implementing file protocol| |jsonString| json string| | outputMode | stream or string | stream|
| delimiter |,,:,#,@,!,\t,^,* | , | | newline: |"\n" or "\r\n"| "\n" | | headers|false or true |false| |quoteChar|' "| "|

##Examples

 const csvString = `policyID>statecode>county>eq_site_limit>hu_site_limit>fl_site_limit>fr_site_limit>tiv_2011>tiv_2012>eq_site_deductible>hu_site_deductible>fl_site_deductible>fr_site_deductible>point_latitude>point_longitude>line>construction>point_granularity
    223488>FL>CLAY COUNTY>328500>328500>328500>328500>328500>348374.25>0>16425>0>0>30.102217>-81.707146>Residential>Wood>1`

    // custom headers
    const headersArr = ["jkjl", "fr_site_limit", "tiv_2011", "tiv_2012", "fr_site", "tiv_20177", "tiv_201776", "1", "2", "3", "4", "5", "6", "7", "8", "policyID", "statecode", "county"]

    // comsuming using for csv string 
    const output = await csvToJson({ csvString, delimiter: '>', outputMode: 'string', headers: headersArr })
    console.log(output)

    // output as string 
    const string = await csvToJson({ path: 'small.csv', delimiter: ',', outputMode: 'string', headers: true, skipComments: true })

    const streamForAwait = await csvToJson({ path: 'sample.csv', delimiter: ',', outputMode: 'stream', headers: true })

    // comsuming using for await 
    for await (const line of streamForAwait) {
      // console.log("\n", line);
    }

    // consuming using streams
    const stream = await csvToJson({ path: 'sample.csv', delimiter: ',', outputMode: 'stream', headers: true })

    stream.on('data', (data) => {
      // console.log(data)
    })

    stream.on('end', () => {
      // console.log("stream end")
    })

Refer ./examples folder for more details

Test

npm run test

##ToDo

  • Should be able to handle errors, continue parsing even for ambiguous CSV's and return the errors for each row
  • Make your library Universal JavaScript (aka isomorphic JavaScript).
  • Support worker thread by passing a config. That is, the computation will not happen on main thread but on worker threads.
  • Auto detect the delimiter

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT