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-arrays

v1.0.1

Published

Node.js library to convert a set of arrays into csv.

Downloads

31

Readme

csv-arrays

csv-arrays is a Node.js library to convert a set of arrays into a csv formatted document. There specialty of this implementation is that it is designed to handle the input data as arrays of columns. In addition to this csv-arrays aims to be a well-documented, simple and easy-to-use csv conversion library for Node.js.

For example, if you have measured the temperature of your office every day and have stored the dates in an array and the temperatures of those dates in an another array like this:

["2021-10-01", "2021-10-02", "2021-10-03", "2021-10-04"]
[20, 23, 19, 24]

You could save the data into a csv-formatted document using csv-arrays like this:

import fs from "fs";
import util from "util";
import stream from "stream";
import {CsvStream} from "csv-arrays";

const pipeline = util.promisify(stream.pipeline);

const dates: string[] = [];
const temperatures: number[] = [];

async function main(): Promise<void>{
    await pipeline(
        new CsvStream([dates, temperatures]),
        fs.createWriteStream("temperatures.csv")
    );
}

main();

This produces a temperatures.csv file containing the temperature data.

2021-10-01,20
2021-10-02,23
2021-10-03,19
2021-10-04,24

Usage

For full API documentation, see https://mik546.github.io/csv-arrays/.

csv-arrays exposes two modes of usage. You can create a readable stream by creating an instance of the CsvStream class. This way the result data doesn't need to be stored in memory and can instead be streamed directly to for example a file. The other way to use csv-arrays is to use the generateCsv function that takes the data and synchronously generates and returns the csv-document. Behind the scenes generateCsv is just a utility function feeding the data into a CsvStream and reading all of the output into memory.

Both the CsvStream constructor and generateCsv function accept the same two parameters. The first parameter is an array containing arrays of data. Each of the inner arrays becomes a column in the csv-document. All of the inner arrays must have the same length. The individual data items inside the inner arrays can by default have any type except a function or an object. By using a custom cell value converter function however any types of data can be used.

The second argument is the options object that can be used to define additional options for generating the csv document. All options are optional.

|Option |Default |Description | |---------------------|----------|------------| |columnSeparator |"," |The separator used between columns. To output tab-separated csv for example, set this otpion to "\t". | |rowSeparator |\n |The separator used between rows. If you want to use the output file on Widows, set this to \n\r for Windows style newlines. | |headerStyle |FIRST_ROW |Sets the style of header (first) row used in the csv document. See the HeaderStyle enum for options. | |customHeader |null |An array containing title strings for a custom header row. If set, must have the same length as the outer data array. For this to affect the output, you must set headerStyle to CUSTOM. | |cellValueConverter | |Set a custom function that is used to convert all cell data items into strings. If none is set, the default converter is used. The default converter does not support functions or objects. |