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

objects2csv

v1.1.0

Published

A simple, small library to convert JavaScript objects into a csv string

Downloads

65

Readme

objects2csv

A simple, small library to convert JavaScript objects into a csv string.

npm version

Features

  • Small (~5kB unpacked)
  • No dependencies
  • Uses modern JavaScript (ES7)
  • Automatic field detection
  • Default value for missing fields
  • Custom & automatic quotation (can be disabled)
  • Custom csv separator, line separator and escape sequence
  • Excludable fields

Installation

npm install objects2csv

Usage

const convert = require('objects2csv');

const data = [
    {type: "car", model: "BMW", year: 2019, maxPassengers: 5, doors: 5},
    {type: "car", model: "VW", year: 2003, doors: 3},
    {type: "bus", model: "Mercedes", year: 2019, maxPassengers: 50},
]

const csv = convert(data);

console.log(csv);

Output:

"type";"model";"year";"maxPassengers";"doors"
"car";"BMW";"2019";"5";"5"
"car";"VW";"2003";" ";"3"
"bus";"Mercedes";"2019";"50";" "

This will use the default values for all options. In order to customize the generated csv, the following options exist:

// These options represent the default values
const options = {
    header: true, // If false, the header won't be included in the converted string
    unavailable: " ", // If the value does not exist, this is the string that will be used instead
    separator: ";", // This is the sequence that will be used as a separator (between each value)
    quote: "\"", // This is the sequence that will be used to quote the values, for example: "myValue"
    escape: "\\", // This is the sequence that will be used to escape a sequence in the value, if it is equal to the quote, for example: "And then he said \"Hello\""
    lineSeparator: "\n", // This is the sequence that will be used at the end of a line (after each object)
    excludeKeys: [] // If you don't want a field to be in the final csv string, exclude it here. For example the ccnumber and cvv field: ["ccnumber", "cvv"]
}

Usage of the custom options:

const csv = convert(data, { excludeKeys: ["maxPassengers", "doors"], quote: null }); // quote: "" can also be used

Output:

type;model;year
car;BMW;2019
car;VW;2003
bus;Mercedes;2019