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

v2.0.1

Published

Library to convert CSV files to JSON objects, with support for nested arrays and objects

Downloads

4

Readme

Instructions

The first uncommented line will return with

Data Types

Strings

By default, everything is a string. For example, the header person.name will coerce all values under that column to string types.

Numbers

If a header ends in #, it will be converted to a number. For example, person.age# will be converted to a number.

Arrays

If a config path ends in [], it will be treated as an array. For example, person.parents[].age# says that the parents property is an array, within which exist objects with the property age; and age is a number.

Boolean Values

If a header ends in ?, it will be converted to a number. For example, person.licensed? will be converted to a boolean. Acceptable values are true/false, y/n and 1/0.

Comments

If a cell starts with a single #, for example # hello world it will be ignored (will be treated as an empty cell) If the first cell of a row starts with ##, for example ## hello world, the entire row will be ignored. If a header cell is commented out, that column is skipped over on each row.

Nested properties

Simple nested properties can be defined in the header as follows:

person.parent.name, person.parent.age

if person.parent does not exist, it will be created.

Primitive properties in Arrays

Sometimes you want to create an array of primitives (strings or integers) using the library. For example, you might want to create an object like the following:

[{
    integers: [1,2,3,4,5],
    strings: ["one","two","three","four","five"],
    boolean: [true,false,true,false,true,false]
}]

To do this, use the pipe character | as a delimiter and define your headers as follows:

integers#[],strings[],boolean?[]
1|2|3|4|5,one|two|three|four|five,true|false|1|0|y|n

Keep in mind that mixed data types in the same array are not supported.

Defining Keys In CSV

Often, you may wish to define a key string in CSV format. To do so, you would use {key}

The following file:

{key1},{key1}.month,{key2},{key2}.month
shipment_1,jan,return_1,feb
shipment_2,feb
shipment_3,mar,return2,mar

Will translate to the following object:

[
    { "shipment_1":"jan","return_1":"feb"},
    { "shipment_2":"feb"},
    { "shipment_3":"mar","return2":"mar"}
]

Creating objects instead of arrays

The best way to create objects instead of arrays is to use the method described in Defining Keys in CSV. After creating such an array, you can run the following code to flatten the array into an object. You are encouraged to write your own methods for this, since it is impossible to cover all cases for creating objects instead of arrays inside a library.

var csvObjects = require('csv-objects');
var csvString = exampleFileAbove;

csvObjects.parse(csvString, function(err, arrayData) {
    let objectData = {};
    arrayData.forEach((obj) => {
        let key = Object.keys(obj)[0]; // get the key
        if (objectData[key]) throw Error("Key [" + key + "] was defined twice!");
        objectData[key] = obj[key]; // set the data value against the key.
    });
});