var reductio = require('reductio_json'),
crossfilter = require('crossfilter');
var items = [
{category: 'mine,yours,theirs', items: 4},
{category: 'mine', items: 13},
{category: 'yours,theirs', items: 22}
];
var cf = crossfilter(items);
var dimension = cf.dimension(function(d){return d.category;});
var reducerConfig = {
groupAll: 'category',
sum: 'items'
};
var group = reductio_json(reducerConfig)(dimension.groupAll());
console.log(group.all());
// [{"key":"mine","value":{"sum":17}},{"key":"theirs","value":{"sum":26}},{"key":"yours","value":{"sum":26}}]
Last active
October 1, 2015 17:03
-
-
Save nordfjord/a1f19118976c9dcee43e to your computer and use it in GitHub Desktop.
Reductio JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reductio = require 'reductio' | |
_ = require 'lodash' | |
makeTemplate = (config)-> | |
if !type.call(config) == '[object String]' | |
return ()-> 'unknown' | |
if ~config.indexOf '<%' | |
templ = _.template config | |
tpl = (d)-> templ d:d | |
else | |
tpl = (d)-> d[config] | |
tpl | |
reductio_json = (config)-> | |
reducer = reductio() | |
if config.values | |
for k,v of values | |
as_reductio.value reducer, k, v | |
for k,v of config | |
as_reductio[k](reducer,v) if k != 'values' | |
reducer | |
reductio_json.value = (reducer, val, config)-> | |
red = reducer.value val | |
for k,v of config | |
as_reductio[k](red, v) | |
reductio_json.groupAll = (reducer, config)-> | |
tpl = makeTemplate config | |
reducer.groupAll (d)-> (tpl(d) || '').split(',') | |
base = ['count', 'avg', 'sum'] | |
for name in base | |
reductio_json[name] = (reducer, config)-> | |
if type.call(config) == '[object String]' | |
if !~config.indexOf('<%') | |
reducer[name](config) | |
else | |
tpl = makeTemplate config | |
reducer[name](tpl) | |
else | |
reducer[name](config) | |
module.exports = reductio_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment