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

csvin

v1.0.1

Published

๐Ÿ“š Tiny, over-opinionated Node.js utility that synchronously loads a CSV file into a JS object (e.g. for scripting)

Downloads

8

Readme

csvin

๐Ÿ—‚ Tiny, over-opinionated Node.js utility that synchronously loads a CSV file into a JS object (e.g. for scripting)

What is this? Why is this?

Sometimes I want to write a one-off Node.js script that does something with a CSV file (e.g. reformat a data file for human readability) and I wish I could just do require('data.csv') like I would with a JSON file. This is basically just two lines of code, using fs and d3-dsv that does this for me, because I never remember how to read and parse files properly. ๐Ÿคญ

This is all synchronous and in many cases you'll want to do some more nuanced parsing, but if you want to do a little thing in a hot sec, ๐Ÿคทโ€โ™€๏ธ.

Installation

npm install csvin, or similar. Intended to be used in Node.js and imported with require.

Lil' example

data.csv

animal,count
cat,3
bird,8

index.js

const csvin = require('csvin');

const data = csvin('data.csv');
// [
//   { animal: 'cat', count: '3' },
//   { animal: 'bird', count: '8' },
//   columns: [ 'animal', 'count' ]
// ]

data.forEach((row) => {
  console.log(`There are ${row.count} ${row.animal}s.`);
});
// Prints:
// There are 3 cats.
// There are 8 birds.

// If you must do some parsing ...
const betterData = csvin('data.csv', (row) => {
  return { species: row.animal.toUpperCase(), multitude: +row.count };
});
// [
//   { species: 'CAT', multitude: 3 },
//   { species: 'BIRD', multitude: 8 },
//   columns: [ 'animal', 'count' ] // The original column names
// ]

API Reference

csvin(csvPath[, rowParser])

Given the path to a CSV file, csvPath, synchronously read it and return an array of objects whose keys are the column headers and values are row values of the CSV file (as strings). The array also has a columns attribute, which is the array of column headers in the order they appear in the CSV file.

This assumes your file is UTF-8 encoded and has a header row.

You can optionally provide a rowParser, which should be a function applied to each row (in the format described above), which maps it to a new value. (If it returns null or undefined, that row is omitted from the output.)

For more details, see the API of the dsv.parse method of d3-dsv, because this is basically just that, except we're reading in a file as well to make life easy for lazy people like me. ๐Ÿ˜Ž ๐Ÿ˜•

๐ŸŽ‰

Feel free to submit issues or pull requests with whatever feedback you may have! It's not supposed to be so much of a thing, though, you know. It may be just as fruitful and pleasing to gaze at a tree or a bird. ๐ŸŒณ