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-streamy/lib

v1.0.7

Published

CSV Stream Library for Node.js.

Downloads

1

Readme

@csv-streamy/lib

npm version CI codecov License: MIT

CSV Streamy Lib - CSV Stream library for Node.js.

Install

npm i @csv-streamy/lib

Quick examples

Read and Write your CSV file

import { resolve } from 'path'
import { pipeline } from 'stream/promises'
import { createReadStream, createWriteStream } from 'fs'
import { createCsvParser, createCsvConverter, CsvRowData } from '@csv-streamy/lib'

// For example, it just converts all fields to uppercase letters.
async function processRow({ data }: CsvRowData): Promise<CsvRowData> {
  for (const [header, field] of Object.entries(data)) {
    data[header] = field.toUpperCase()
  }
  return Promise.resolve({ data })
}

async function run() {
  await pipeline(
    createReadStream(resolve(__dirname, 'input.csv'), { encoding: 'utf-8' }),
    createCsvParser({ hasHeaders: true, hasDoubleQuotes: true }),
    async function* (source) {
      for await (const row of source) {
        yield await processRow(row as CsvRowData)
      }
    },
    createCsvConverter({ hasHeaders: true, hasDoubleQuotes: true }),
    createWriteStream(resolve(__dirname, 'output.csv'), { encoding: 'utf-8' }),
  )
  console.log('Woo-hoo! Succeeded!!')
}

run().catch(console.error)

Read and Write your CSV file with stat

import { resolve } from 'path'
import { pipeline } from 'stream/promises'
import { createReadStream, createWriteStream } from 'fs'
import { createCsvParser, createCsvConverter, CsvRowData } from '@csv-streamy/lib'

// You can observe the number of row as `count` and the bytes of data as `amount` in `stat`.
// For example, it converts all fields to uppercase letters if `count` is even
// or it capitalizes all fields if `amount` is 200 bytes or more.
async function processRow({ data, stat }: CsvRowData): Promise<CsvRowData> {
  const { count, amount } = { ...stat }
  if (!!count && count % 2 === 0) {
    for (const [header, field] of Object.entries(data)) {
      data[header] = field.toUpperCase()
    }
  } else if (!!amount && amount >= 200) {
    for (const [header, field] of Object.entries(data)) {
      data[header] = field.charAt(0).toUpperCase() + field.slice(1)
    }
  }
  return Promise.resolve({ data })
}

async function run() {
  await pipeline(
    createReadStream(resolve(__dirname, 'input.csv'), { encoding: 'utf-8' }),
    createCsvParser({ hasHeaders: true, hasDoubleQuotes: true }),
    async function* (source) {
      for await (const row of source) {
        yield await processRow(row as CsvRowData)
      }
    },
    createCsvConverter({ hasHeaders: true, hasDoubleQuotes: true }),
    createWriteStream(resolve(__dirname, 'output.csv'), { encoding: 'utf-8' }),
  )
  console.log('Great! Perfect!!')
}

run().catch(console.error)

Using ES Modules (ESM)

If you want to use ES Modules, you can do it as follows.

import { resolve } from 'path'
import { dirname } from 'dirfilename'
import { pipeline } from 'stream/promises'
import { createReadStream, createWriteStream } from 'fs'
import { createCsvParser, createCsvConverter, CsvRowData } from '@csv-streamy/lib'

// Workaround to simply use `__dirname` because CommonJS variables are not available in ES modules.
const __dirname = dirname(import.meta.url)

async function processRow({ data }: CsvRowData): Promise<CsvRowData> {
  for (const [header, field] of Object.entries(data)) {
    data[header] = field.toUpperCase()
  }
  return Promise.resolve({ data })
}

async function run() {
  await pipeline(
    createReadStream(resolve(__dirname, 'input.csv'), { encoding: 'utf-8' }),
    createCsvParser({ hasHeaders: true, hasDoubleQuotes: true }),
    async function* (source) {
      for await (const row of source) {
        yield await processRow(row as CsvRowData)
      }
    },
    createCsvConverter({ hasHeaders: true, hasDoubleQuotes: true }),
    createWriteStream(resolve(__dirname, 'output.csv'), { encoding: 'utf-8' }),
  )
  console.log('Woo-hoo! Succeeded!!')
}

run().catch(console.error)

Using CommonJS

If you want to use CommonJS in just Node.js, you can do it as follows.

const { resolve } = require('path')
const { pipeline } = require('stream/promises')
const { createReadStream, createWriteStream } = require('fs')
const { createCsvParser, createCsvConverter } = require('@csv-streamy/lib')

async function processRow({ data }) {
  for (const [header, field] of Object.entries(data)) {
    data[header] = field.toUpperCase()
  }
  return Promise.resolve({ data })
}

async function run() {
  await pipeline(
    createReadStream(resolve(__dirname, 'input.csv'), { encoding: 'utf-8' }),
    createCsvParser({ hasHeaders: true, hasDoubleQuotes: true }),
    async function* (source) {
      for await (const row of source) {
        yield await processRow(row)
      }
    },
    createCsvConverter({ hasHeaders: true, hasDoubleQuotes: true }),
    createWriteStream(resolve(__dirname, 'output.csv'), { encoding: 'utf-8' })
  );
  console.log('Woo-hoo! Succeeded!!')
}

run().catch(console.error)

Usage

Parsing

You can parse your csv file, which can contain headers and enclose fields in double-quotes, to handy objects. Each object contains fields per row as data and statistics data as stat, which contains the number of row as count and the bytes of data as amount.

import { resolve } from 'path'
import { createReadStream } from 'fs'
import { createCsvParser } from '@csv-streamy/lib'

const reader = createReadStream(resolve(__dirname, 'input.csv'))
const parser = createCsvParser({ hasHeaders: true, hasDoubleQuotes: true })

reader
  .pipe(parser)
  .on('error', (error) => console.log(error))
  .on('data', (row) => console.log(row))
  .on('end', () => console.log('End'))
  • Input
"header[1]","header[2]","header[3]","header[4]","header[5]"
"item[1][1]","item[1][2]","item[1][3]","item[1][4]","item[1][5]"
"item[2][1]","item[2][2]","item[2][3]","item[2][4]","item[2][5]"
  • Output
{
  data: {
    'header[1]': 'item[1][1]',
    'header[2]': 'item[1][2]',
    'header[3]': 'item[1][3]',
    'header[4]': 'item[1][4]',
    'header[5]': 'item[1][5]'
  },
  stat: { count: 1, amount: 64 }
}
{
  data: {
    'header[1]': 'item[2][1]',
    'header[2]': 'item[2][2]',
    'header[3]': 'item[2][3]',
    'header[4]': 'item[2][4]',
    'header[5]': 'item[2][5]'
  },
  stat: { count: 2, amount: 128 }
}
End

Converting

You can convert your handy objects with data to csv format strings, which can contain headers and enclose fields in double-quotes, then create buffer stream.

import { createCsvConverter } from '@csv-streamy/lib'

const converter = createCsvConverter({ hasHeaders: true, hasDoubleQuotes: true })

converter.pipe(process.stdout).on('end', () => process.exit())

converter.write({ data: { 'header[1]': 'item[1][1]', 'header[2]': 'item[1][2]', 'header[3]': 'item[1][3]' } })
converter.write({ data: { 'header[1]': 'item[2][1]', 'header[2]': 'item[2][2]', 'header[3]': 'item[2][3]' } })
converter.write({ data: { 'header[1]': 'item[3][1]', 'header[2]': 'item[3][2]', 'header[3]': 'item[3][3]' } })
converter.end()
  • Output
"header[1]","header[2]","header[3]"
"item[1][1]","item[1][2]","item[1][3]"
"item[2][1]","item[2][2]","item[2][3]"
"item[3][1]","item[3][2]","item[3][3]"

Of course, you can export them to a file.

import { resolve } from 'path'
import { createWriteStream } from 'fs'
import { createCsvConverter } from '@csv-streamy/lib'

const converter = createCsvConverter({ hasHeaders: true, hasDoubleQuotes: true })
const writer = createWriteStream(resolve(__dirname, 'output.csv'))

converter.pipe(writer).on('end', () => writer.end())

converter.write({ data: { 'header[1]': 'item[1][1]', 'header[2]': 'item[1][2]', 'header[3]': 'item[1][3]' } })
converter.write({ data: { 'header[1]': 'item[2][1]', 'header[2]': 'item[2][2]', 'header[3]': 'item[2][3]' } })
converter.write({ data: { 'header[1]': 'item[3][1]', 'header[2]': 'item[3][2]', 'header[3]': 'item[3][3]' } })
converter.end()

Acceptable CSV format

This basically follows RFC4180 but additionally needs to meet the following rules to make a common csv file easier to use:

  • Fields must be Comma-Separated Values. (NOT Tab-Separated.)
  • Fields can be enclosed in double-quotes to contain line breaks, double quotes and commas. (BUT a file cannot mix enclosed fields and not-enclosed fields.)
  • A double-quote appearing inside a field must be escaped by preceding it with another double-quote or a backslash.
  • This doesn't check a MIME Type such as text/csv.