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

@imnull/csv-reader

v1.1.0

Published

```js const { CSVReader } = require('./dist')

Downloads

3

Readme

CSVReader

const { CSVReader } = require('./dist')

const reader = new CSVReader({
    head: [
        {
            name: 'id',
            parse: Number,
            index: 0,
        },
        'hash',
        'type',
        'prefix',
        {
            name: 'datetime',
            parse(s) {
                return new Date(s)
            }
        },
    ],
    /** Give then progress of scaning file */
    // callback: (progress) => {
    //     console.log(`percent ${Math.floor(progress * 1e6) / 1e4}%`)
    // },
    /** Filter the rows you want */
    // filter: (row) => {
    //     return row.datetime.getSeconds() === 31
    // },
})

reader.parse(file).then(res => {
    console.log(res)
})

parse

Conver then CSV data to a key-value object by the options config.

TReaderOptions.head

type TReaderOptions = {
  //...
  head: (string | { [k in keyof THead]?: THead[k] })[]
  //...
}

It's a array of string or a optional THead.

If string, it will be the name of the column at index of itself.

If THead, it has defined the column name, column index, and data parser.

type THead = {
    name: string;
    parse(raw: string): any;
    index: number;
}

matrix

It just convert the plain text to a string[][] format. For example, if you what to insert the data into some database table, a array of row values is enough.

const { CSVReader } = require('@imnull/csv-reader')

console.time('parse')
reader.matrix('some local file').then(rows => {
    console.log(rows)
})

So you got rows just like:

[ 'HeartRate', '1678076194000', '1678076194992', '0', '6', '0', '' ],
[ 'HeartRate', '1678076195000', '1678076195992', '0', '6', '0', '' ],
[ 'HeartRate', '1678076196000', '1678076196992', '0', '6', '0', '' ],
[ 'HeartRate', '1678076197000', '1678076197992', '0', '6', '0', '' ],
[ 'HeartRate', '1678076198000', '1678076198992', '0', '6', '0', '' ],
[ 'HeartRate', '1678076199000', '1678076199992', '0', '6', '0', '' ],
...

And then

rows.forEach(cols => {
  db.exec('INSERT INTO SOME_TABLE(c1,c2,c3,c4)VALUES(?,?,?,?)', cols)
})

Performance

Use CSVReader.matrix:

[demo.cvs * 100] parsed
  - 100,000 rows
  - 5 columns
  - 11,082,295 bytes

608.049ms

----------------

[demo.cvs * 1000] parsed
  - 1,000,000 rows
  - 5 columns
  - 111,822,896 bytes

6.009s

----------------

[demo2.cvs]
  - 86,280 rows
  - 7 columns
  - 3,955,613 bytes

560.668ms

----------------

[demo3.cvs]
  - 1,438 rows
  - 2 columns
  - 106,337,864 bytes

268.806ms

----------------

[demo3.cvs * n]
  - 23,008 rows
  - 2 columns
  - 1,701,405,824 bytes

3.119s

Version

1.1.0

Add methods CSVReader.matrix and CSVReader.matrixSync.

1.0.1

fix some bug