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

fulcrum

v0.1.0

Published

Array pivot table

Downloads

4

Readme

Array Pivot

A utility to generate pivot tables from an array of objects:

var pivot = require('fulcrum');

var data = [{
	name: 'John',
	coin: '5ct',
	number: 30
},{
	name: 'John',
	coin: '5ct',
	number: 2
},{
	name: 'John',
	coin: '10ct',
	number: 3
},{
	name: 'Jane',
	coin: '10ct',
	number: 15
},{
	name: 'Jane',
	coin: '5ct',
	number: 6
}];

In this data model, John has 32 5ct and 3 10ct coins, Jane has 6 5ct and 15 10ct coins.

var res = pivot(data,'name','coin',pivot.sum('number'));

[{
    "name": "John",
    "5ct": 32,
    "10ct": 3
},{
    "name": "Jane",
    "5ct": 6,
    "10ct": 15
}]

API

pivot(array,rows,cols,reducer);
  • array: an array of objects

  • rows: a string for the field for rows

  • cols: a string for the field for columns

  • reducer (optional): an object describing how to calculate the cell value from the matched objects:

      {
      	callback: Function,
      	initialValue: Object
      }

    These are the same arguments as passed into Array.reduce: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce.

    If the reducer is omitted, an array of objects is returned in the cell

Built in reducers

  • pivot.sum(field): return the sum of the values in field field
  • pivot.count(): return the number of objects in the cell
  • pivot.average(field): return the average
  • pivot.min(field): return the minimum
  • pivot.max(field): return the maximum

For all these reducers, when no objects are present in the cell, null is returned, except for count()

Convenience attachments to Array.prototype

If you wish, you can attach a convenience method to Array.prototype:

var pivot = require('fulcrum').attach();
var res = data.pivot('name','coin',pivot.sum('number'));

It can also be detached:

pivot.detach();