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

underscore.nest

v0.1.1

Published

Underscore.Nest is an extenstion for converting flat data into nested tree structures

Downloads

40

Readme

Underscore.Nest

Underscore.Nest is an extenstion for converting flat data into nested tree structures. It works in Node.js and is AMD compatible.

For example, if your data looks like this:

var data = [
    { c1 : "A", c2 : "B", c3 : "C", v : 10 },
    { c1 : "A", c2 : "C", c3 : "D", v : 10 },
    { c1 : "B", c2 : "B", c3 : "C", v : 10 }
  ];

And you want it to look like this:

{
  children : [
    { name : 'A', index : 0, children : [
      { c1 : "A", c2 : "B", c3 : "C", v : 10 },
      { c1 : "A", c2 : "C", c3 : "D", v : 10 }
    ]},
    { name : 'B', index : 1, children : [
      { c1 : "B", c2 : "B", c3 : "C", v : 10 }
    ]}
  ]
}

You can accomplish that by using underscore.nest like so:

  _.nest(data, "c1");

Node.js

npm install underscore.nest

Get your favorite underscore flavor:

// get your requirements in order.
var u = require('underscore');
u.nst = require('underscore.nest');

// run your nesting
u.nst.nest(data, 'c1');

API:

Basic API:

_.nest(data, columnsToReduceBy, reduceFunction);
  • data - An array of objects
  • columnsToReduceBy - Underscore.nest can infinitly nest your data. You can either nest by a single field by passing the name of that field, or an array of fields.
  • reduceFunction - Optional. If you want to reduce your resulting children into a single value result, pass a function that takes an array of objects and returns a single value.

Note that every child grouping will recieve an index property that will mark its order in the heirarchy.

Examples:

Nest With Reduce:

If you pass in a reduce function, instead of having a children property set, you will have a value property set that will be the result of the reduce function call.


var data = [
  { c1 : "A", c2 : "B", c3 : "C", v : 10 },
  { c1 : "A", c2 : "C", c3 : "D", v : 10 },
  { c1 : "B", c2 : "B", c3 : "C", v : 10 }
];

_.nest(data, "c1", _.sum); // _.sum is from underscore.math

Results in:

{
  children : [
    { name : 'A', index : 0, value : 20 },
    { name : 'B', index : 1, value : 10 }
  ]
};

Multi Level Nesting

If you pass an array of properties instead of a single string for the second argument, nest will create a nesting based on each of those arguments like so:

var data = [
  { c1 : "A", c2 : "B", c3 : "C", v : 10 },
  { c1 : "A", c2 : "C", c3 : "D", v : 10 },
  { c1 : "B", c2 : "B", c3 : "C", v : 10 }
];

_.nest(data, ["c1", "c2"]);

Results in:

{ children :
  [
    { name : "A",
      children : [
        { name : "B",
          children :[
            { c1 : "A", c2 : "B", c3 : "C", v :10 }
          ],
          index : 0},
        { name : "C", 
          children :[
            { c1 : "A", c2 : "C", c3 : "D", v :10 }
          ],
          index : 1
        }
      ],
      index : 0 },{
      name : "B",
      index : 1,
      children : [
        { name : "B",
          index : 0
          children : [
            { c1 : "B", c2 : "B", c3 : "C", v : 10 }
          ]
      }]
    }
  ]
};

Questions?

Contact @ireneros on twitter, iros on IRC email me.