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

jstocsv

v0.1.2

Published

micro csv writer

Downloads

1

Readme

Build Status License NPM Downloads Known Vulnerabilities Coverage Status

JStoCSV

A small, versatile, dependency free module to convert an array of JavaScript objects to CSV.

  • Handles basic nesting, e.g. field.nested.inAnother or myArray[4]
  • Support alternative delimiters (e.g. TAB delimted) and end of lines.
  • Powerful user function option for "tricky stuff".

Usage

const labelledFields = [
  { path: "name.given", label: "First Name" },  // example of nested data
  { path: "name.surname", label: "Last Name" }
  { path: "birth_day", label: "Date of Birth" },
...
];

data = [
 { name: { given: "John", surname: "Doe" }, birth_day: "06 March 2003" },
  ...
]

const jstocsv = new JStoCSV(labelledFields);
jstocsv.stream(data, someWriteableStream); // or
let result = jstocsv.generateString(data);

Output / Result
First Name,Last Name,Date of Birth
John,Doe, 06 March 2003
...

API

JStoCSV(fields, options) constructor

  • fields is usually an array of {}

    • path: a string, route to get the data, e.g. "name" or "name.first"
    • label: label for header line (defaults to path)
    • fn: optional user function to apply afterwards (usually null, see Notes at end)
  • fields may be an array of strings

    • if so, they are used for the path (and the header label)
    • fn option is unavailable

If fields is not provided, JStoCSV will use all keys from the first data value.

  • options
    • delimiter defaults to ',', you can use '\t' etc.
    • eol defaults to '\n'
    • quoteEmpties put quotes around all empty fields
    • quoteAll put quotes around all fields
    • noHeaderLine don't write out a header line

generateLines(data)

Creates an array of Strings, one per line of the CSV file.

  • data is an array of objects.

generateString(data, appendEOL)

Joins the lines with the eol character.

  • data is an array of objects.
  • appendEOL at end (default is false)

Both of those require that all the CSV file fit in memory. If your data is huge, consider

stream(data, writeStream)

  • data is an array of objects.
  • writeStream note: caller must end() or close() etc...

For the ultimate in control

reduce(data, reducer, acc)

  • data is an array of objects
  • reducer(acc, line) called for each line in the csv file
  • acc the initial accumulator

For convenience, you can use (see tests)

  • _stringReducer();
  • _streamReducer()

Notes, Todos, and Caveats

Supplying a user function fn in fields

This module is short because it just does the basics for output. If you need to "do something very special", supply a user function fn to fields. For each value, it will be called with

fn(value, datum, index, array)

  • value is the value within datum (as determined by path)
  • datum is the entire object in the data array
  • index is the 0-based index of the object within the data array
  • array is the full data array

For example, in the unit test "userfn", we generate an artificial column based on the difference between successive values in the field .number, via

fn: function(val, datum, index, array) {
  return index ? val-array[index-1].number : val;
}

Caveats

  • The _quoteValue() method is pretty basic. Pathological data may break it.
  • a new field, .splitpath, will be added to each of the fields you pass in.

Alternatives. Most are larger.

json-csv

  • works very similarly, even has a user function (caller "filter") but less versatile

simple-csv-writer

  • works with arrays of arrays (not objects)

csv-writer

  • looks good but more complex.

fast-csv

  • looks powerful
  • the headers are confusing