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

oh-csv

v1.0.2

Published

Simple and parametrable CSV parser/encoder.

Downloads

1,018

Readme

OH - CSV

Simple and highly configurable CSV/TSV parser and encoder.

NPM version Build status Dependency Status devDependency Status Coverage Status Code Climate

Usage

var csv = require('oh-csv');

Parsing CSV

var parser = new csv.Parser({
  sep: ',',
  linesep: ['\n', '\r', '\r\n'],
  quote: '"',
  esc: '\\'
});

parser.on('readable', function() {
  var row;
  while(row = parser.read()) {
    console.log(row);
  }
});

parser.write('1,Nicolas Froidure,[email protected]');
// [1, 'Nicolas Froidure', '[email protected]']

parser.end();

Alternatively, you can specify fields to map them to an object properties:

var parser = new csv.Parser({
  fields: ['id', 'name', 'email'], // fields are required for this mode
  sep: ',',
  linesep: ['\n', '\r', '\r\n'],
  quote: '"',
  esc: '\\'
});

parser.on('readable', function() {
  var row;
  while(row = parser.read()) {
    console.log(row);
  }
});

parser.write('1,Nicolas Froidure,[email protected]');
// {
//  id: 1,
//  name: 'Nicolas Froidure',
//  email: '[email protected]'
// }

parser.end();

Encoding to CSV

var encoder = new csv.Encoder({
  fields: ['id', 'name', 'email']
});

encoder.pipe(process.stdout);

// Array form
encoder.write([1, 'Nicolas Froidure', '[email protected]']);
// '1,Nicolas Froidure,[email protected]'

// Object form (you need to specify fields)
encoder.write({
  id:1,
  email: '[email protected]',
  name: 'Nicolas Froidure'
});
// '1,Nicolas Froidure,[email protected]'

Transforming CSV on the fly

No library needed, DIY !

var fs = require('fs');
var parser = new csv.Parser();
var encoder = new csv.Encoder();

var Transform = require('stream').Transform;
var transformer = new Transform({objectMode: true});
transformer._transform = function(row, unused, cb) {
  row[name] = row[name].toLowerCase();
  this.push(row);
  cb();
};

fs.createReadStream('mycsv.csv')
  .pipe(parser)
  .pipe(transformer)
  .pipe(encoder)
  .pipe(fs.createWriteStream('mycsv.new.csv'));

Excel compatible CSV

We've added a simple wrapper to get a CSV stream compatible with Excel for both OSX and Windows from a csv.encoder instance:

  var encoder = new csv.Encoder(csv.tsvOpts);
csv.wrapForExcel(encoder)
  .pipe(fs.createWriteStream('excel.csv'));

Predefined options

There are some CSV and TSV predefined objects in order to allow you to choose your format in a simpler manner.

csv.csvOpts

CSV (Comma-Separated Values) as it's commonly found.

csv.tsvOpts

TSV (Tabulation-Separated Values)

csv.csvRFCOpts

The RFC 4180 CSV format.

API

csv.Parser(options:Object)

Create a new CSV Parser transform stream with options as defined in the options section.

csv.Encoder(options:Object)

Create a new CSV Encoder transform stream with options as defined in the options section.

Options

The options object is meant to be usable either with the Parser and the Encoder.

options.sep:Array

Default: [',']

The strings used for separating values. The first string is used to encode CSV. Separators can have several chars (useless thus essential).

options.linesep:Array

Default: ['\r\n', '\r', '\n']

The strings used for separating lines. The first string is used to encode CSV.

options.quote:Array

Default: ['"']

The strings used for quoting values. The first string is used to encode CSV.

options.toQuote:Array

Default: An array containing options.sep, options.linesep strings.

If a field contains any occurrence of the given strings, it must be quoted.

options.esc:Array

Default: ['\\']

The strings used for escaping special chars. The first string is used to encode CSV.

options.toEsc:Array

Default: If options.esc is empty, an empty array. If options.quote is empty, an array containing options.sep, options.linesep, options.quote and options.esc strings, otherwise, an array containing options.quote and options.esc.

The strings that must be escaped.