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

@ricb/csv

v1.3.1

Published

Reads and writes data to and from flat files

Downloads

68

Readme

csv

Installation

Using npm

npm install @ricb/csv

Usage

This project provides functionality to write and read CSV files, as well as parse and serialize CSV data.

  • By default it will use "," as separator and "|" as array separator when writing.
  • By default it will try to detect data type for values when parsing.
  • It will try to detect value separator if not explicitly specified.

See CsvOptions for more info

Writing and Reading CSV

import { writeCsv, readCsv } from '@ricb/csv';

const data = [
    { id: 1, name: 'John Doe', born: new Date(), nicks: ['man', 'dude'] },
    { id: 2, name: 'Jane Doe', born: new Date('2000-01-01'), nicks: ['girl', 'woman'] }
];

const filePath = await writeCsv(data, 'test.csv'); 

const loadedData = await readCsv(filePath)
console.log(loadedData);

Parsing and Serializing CSV

import { fromCsv, toCsv } from '@ricb/csv'

const text = `Id,Name,Born,Colors,Joined,KidsAge
1,"Doe, John",2000,red|blue,2021-01-01,5|7
2,"Doe, Jane",2001,green|yellow,2021-01-02,3|4
3,"Mouse,Mickey",2002,black|white,2021-01-03,1|2
4,"Mick "The man" Jagger",1943,purple|orange,2021-01-04,8|9`;

const data = await fromCsv(text, { separator: ',' });
const csvText = toCsv(data, { separator: '\t' });
const data2 = await fromCsv(csvText, { separator: '\t' });

console.log(data2);

CsvOptions

CsvOptions is a type that allows you to customize how CSV data is processed. Here are the properties it can have:

  • fields: An array of strings specifying the order of the fields when writing CSV data. If not provided, the fields will be written in the order they appear in the data object.

  • separator: A string that specifies the character used to separate fields in the CSV data. If not provided, a comma (",") is used by default.

  • arraySeparator: A string that specifies the character used to separate array elements in the CSV data. If not provided, a pipe ("|") is used by default.

  • mapping: An object that maps field names to their types. This is used when reading CSV data to convert string values to their appropriate types. If not provided, all values are treated as strings.

  • detectTypes: A boolean that specifies whether to automatically detect and convert types when reading CSV data. If not provided, this is true by default.

const options: CsvOptions = {
    fields: ['id', 'name', 'born', 'nicks'],
    separator: ',',
    arraySeparator: '|',
    mapping: { id: { type: 'number' }, born: { type: 'date' } },
    detectTypes: true
};

const loadedData = await readCsv(filePath, options);