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

pivot-table-js

v1.3.1

Published

A lightweight module that takes an array of objects and produces an array of objects back based on one or more aggregate function per column. Emulating excel pivot tables

Downloads

81

Readme

build code-size min-size types node npm

pivot-table-js

A lightweight module that takes an array of objects and produces an array of objects back based on one or more aggregate function per column. Emulating excel pivot tables.

pivot-table-js can calculate different aggregate functions on sets of values. The results can be optionally renamed.

Install

Using npm:

$ npm install pivot-table-js

Using yarn:

$ yarn add pivot-table-js

Example

import { Pivot } from 'pivot-table-js'


const data = [
  {
    domain: 'duckduckgo.com',
    path: '/search',
    traffic: 15000,
    trustFlow: 30
  },
  {
    domain: 'duckduckgo.com',
    path: '/images',
    traffic: 8000,
    trustFlow: 20
  },
  {
    domain: 'google.com',
    path: '/search',
    traffic: 20000,
    trustFlow: 42
  },
  {
    domain: 'google.com',
    path: '/images',
    traffic: 10000,
    trustFlow: 38
  }
]


const index = 'domain'

const aggFunc =   {
  domain: 'count', 
  traffic: ['sum', 'mean'], 
  trustFlow: 'mean' 
}

const rename = ['Domain', 'Frequency', 'Traffic Sum', 'Traffic Average', 'TF Average']

const pivotTable = Pivot(data, index, aggFunc, rename)

console.log(pivotTable)

Will output:

[{
  Domain: 'duckduckgo.com',
  'Frequency': 2,
  'Traffic Sum': 23000,
  'Traffic Average': 11500,
  'TF Average': 25
},
{
  Domain: 'google.com',
  'Frequency': 2,
  'Traffic Sum': 30000,
  'Traffic Average': 15000,
  'TF Average': 40
},
{
  Domain: 'Grand Total',
  'Frequency': 4,
  'Traffic Sum': 53000,
  'Traffic Average': 13250,
  'TF Average': 32.5
}]

| Domain | Frequency | Traffic Sum | Traffic Average | Average TF | | -------------- | --------- | ----------- | --------------- | ---------- | | duckduckgo.com | 2 | 23000 | 11500 | 25 | | google.com | 2 | 30000 | 15000 | 40 | | Grand Total | 4 | 53000 | 13250 | 32.5 |

Updates

New feature allows for multiple funcions on the same column, just enclose the type of funcions in an array

const aggFunc =   {
  domain: 'count', 
  traffic: ['sum', 'mean'], 
  trustFlow: 'mean' 
}

Available aggregate functions

| Function | Definition | | :----------- | :------------------------------------------------------------------ | | count | Calculates the count of all values in a set | | counta | Calculates the count of all values in a set including empty strings | | count-unique | Calculates the count of all unique values in a set | | sum | Calculates the sum of values. | | mean | Calculates the average in a set of values — not rounded | | median | Calculates the median in a set of values — not rounded | | mode | Calculates the mode in a set of values | | min | Minimum gets the minimum value in a set of values | | max | Maximun gets the maximum value in a set of values |

Usage

Pivot(data, index, values [,rename])

  • data <Array<Object>> Prepared Array of objects to pivot against.
  • index <string> The index row to use as pivot.
  • values <Object> Aggregate functions
    • [column: <string>]: <Array<string>> | string Use array for more than one option on the same column
  • rename <Array<string>> Optionally rename the output columns, the order is important.
  • returns: <Array<Object>>