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

redugator

v0.1.0

Published

A library for creating reducers for crossfilter

Downloads

143

Readme

Redugator (Reducer + Aggregator)

This library helps you create crossfilter reduce functions.

Installation

npm i -S redugator
# Or
yarn add redugator

Motivation

The best helpers available for crossfilter at the moment are hard to extend. They rely on complicated internals in order to do reductions. This library is structured to make sure you can extend it and those extensions will be just as natural as a first class reducer from the library.

Take for example the sum reducer:

export function sum(valueAccessor, key = 'sum') {
  return {
    reduceAdd(p, v) {
      p[key] += valueAccessor(v)
      return p
    },
    reduceRemove(p, v) {
      p[key] -= valueAccessor(v)
      return p
    },
    reduceInitial(p = {}) {
      p[key] = 0
      return p
    },
  }
}

that's all there is to it.

Because we treat reducers as composable pieces we can implement a count reducer in terms of a sum reducer.

const count = (key = 'count') => sum(() => 1, key)

So how do we compose these reducers together?

Reducer

This library exposes a helper for composing reducers together

Reducer.concat(reducer1, reducer2)

const reducer1 = sum(x => x.count)
const reducer2 = max(x => x.count)
const combined = Reducer.concat(reducer1, reducer2)
;[{ count: 1 }, { count: 2 }].reduce(
  combined.reduceAdd,
  combined.reduceInitial()
)
// { sum: 3, max: 2 }

Reducer.concatAll(...reducers)

const reducer = Reducer.concatAll(
  sum(x => x.count, 'count'),
  sum(x => x.sum),
  max(x => x.count),
  average()
)
;[
  { count: 2, sum: 4 },
  { count: 4, sum: 5 },
].reduce(reducer.reduceAdd, reducer.reduceInitial())
// { count: 6, sum: 9, avg: 1.33, max: 4}

Reducer.reduceGroup(reducer, group)

Helper to apply a reducer to a crossfilter group

const reducer = sum(x => x.count)
const dim = cf.dimension(x => x.type)
const group = Reducer.reduceGroup(reducer, dim.group())
group.all()

Reducers

sum(valueAccessor)

Sum the values

// default property is sum
const reducer = sum(x => x.sum)
;[{ sum: 1 }, { sum: 1 }].reduce(reducer.reduceAdd, reducer.reduceInitial())
// {sum : 2}

max(valueAccessor)

const reducer = max(x => x.sum)
;[{ sum: 1 }, { sum: 2 }, { sum: 3 }].reduce(
  reducer.reduceAdd,
  reducer.reduceInitial()
)
// { max: 3 }

average(countKey, sumKey)

const reducer = Reducer.concatAll(
  sum(x => x.sum),
  sum(() => 1, 'count'),
  // these are the defaults
  average('count', 'sum')
)
;[{ sum: 1 }, { sum: 2 }, { sum: 3 }].reduce(
  reducer.reduceAdd,
  reducer.reduceInitial()
)
// { sum: 6, count: 3, avg: 2 }

valueList(valueAccessor)

used internally by max to maintain a list of seen values in ascending order

const reducer = valueList(x => x.sum)
;[{ sum: 2 }, { sum: 1 }, { sum: 3 }].reduce(
  reducer.reduceAdd,
  reducer.reduceInitial()
)
// { valueList: [1, 2, 3] }

groupBy(keyAccessor, innerReducer)

allows you to group by a key

const reduceSum = sum(x => x.sum)
const reducer = groupBy(x => x.type, reduceSum)
;[
  { sum: 1, type: 'T1' },
  { sum: 2, type: 'T1' },
  { sum: 5, type: 'T2' },
].reduce(reducer.reduceAdd, reducer.reduceInitial())
// { T1: { sum: 3 }, T2: { sum: 5 } }

splitBy(keyAccessor, innerReducer)

Useful if you want to create a timeseries

const reduceSum = sum(x => x.sum)
const reducer = splitBy(x => x.timestamp, reduceSum)
;[
  ({ timestamp: 1, sum: 1 },
  { timestamp: 2, sum: 2 },
  { timestamp: 1, sum: 1 }),
].reduce(reducer.reduceAdd, reducer.reduceInitial())
// [[1, {sum: 2}], [2, {sum: 2}]]