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

chunk-csv

v1.0.2

Published

Splits a CSV read stream into smaller chunks.

Downloads

116

Readme

chunk-csv

Coverage Status

Splits a CSV read stream into multiple write streams or strings. This library was forked from csv-split-stream, an extra method was added and the previous code was updated to support async functions within the callback functions. The original library hasn't been updated since 2017, so I decided to revive it. Feel free to submit a PR or issue containing any bug fixes or feature requests.

Install

npm install chunk-csv

Usage

Using write streams

  1. Split a local CSV file into multiple CSV files (10000 lines each, excluding the header row):
const chunkCsv = require('chunk-csv');

return chunkCsv.splitStream(
  fs.createReadStream('input.csv'),
  {
    lineLimit: 10000
  },
  (index) => fs.createWriteStream(`output-${index}.csv`)
)
.then(csvSplitResponse => {
  console.log('csv split succeeded!', csvSplitResponse);
  // outputs: {
  //  "totalChunks": 350,
  //  "options": {
  //    "delimiter": "\n",
  //    "lineLimit": "10000"
  //  }
  // }
}).catch(csvSplitError => {
  console.log('csv split failed!, csvSplitError);
});
  1. Download a large CSV file via HTTP, split it into chunks of 10000 lines and upload each of them to s3:
const http           = require('http'),
const chunkCsv = require('csv-split-stream');
const AWS            = require('aws-sdk'),
const s3Stream       = require('s3-upload-stream')(new AWS.S3());

function downloadAndSplit(callback) {
  http.get({...}, downloadStream => {
    chunkCsv.splitStream(
      downloadStream,
      {
        lineLimit: 10000
      },
      (index) => s3Stream.upload({
        Bucket: 'testBucket',
        Key: `output-${index}.csv`
      })
    )
    .then(csvSplitResponse => {
      console.log('csv split succeeded!', csvSplitResponse);
      callback(...);
    }).catch(csvSplitError => {
      console.log('csv split failed!', csvSplitError);
      callback(...);
    });
  });    
}

Using strings

  1. Split a local CSV file into multiple strings (10000 lines each, excluding the header row):
const chunkCsv = require('chunk-csv');

chunkCsv.split(
  fs.createReadStream('input.csv'),
  {
    lineLimit: 10000
  },
  async (chunk, index) => {
      const data = await neatCsv(chunk);
      console.log("Processed Chunk", index);
      console.log(data);
  }
)
.then(csvSplitResponse => {
  console.log('csv split succeeded!', csvSplitResponse);
  // outputs: {
  //  "totalChunks": 350,
  //  "options": {
  //    "delimiter": "\n",
  //    "lineLimit": "10000"
  //  }
  // }
}).catch(csvSplitError => {
  console.log('csv split failed!', csvSplitError);
});

Methods

splitStream(readable, options, callback(index)) The splitStream method splits a CSV readable stream into multiple write streams and takes 3 arguments.

  1. readable is a readable stream from your csv file.
  2. options object: delimiter (defaults to "\n"), lineLimit number of lines per chunk.
  3. callback(index) - for every chunk of csv this callback function will be executed. A stream that is writable must be returned from this callback (can be a write or read/write stream). The chunks of csv data will be piped to this write stream. The index argument is given which specifies the chunk number being processed.

split(readable, options, callback(chunk, index)) The split method splits a CSV readable stream into multiple, smaller strings.

  1. readable is a readable stream from your csv file.
  2. options object: delimiter (defaults to "\n"), lineLimit number of lines per chunk.
  3. callback(chunk, index) - for every chunk of csv this callback function will be executed. The chunk argument will be the raw csv data with the specified number of lines and index specifies the chunk number being processed.

Notes

This module will use the first row as a header, so make sure your CSV has a header row. Currently working on a solution to support a "no headers" option.