Skip to content

Instantly share code, notes, and snippets.

@a-x-
Last active April 3, 2016 11:06
Show Gist options
  • Select an option

  • Save a-x-/7de7844a3605640881e96b7cc9fb1fee to your computer and use it in GitHub Desktop.

Select an option

Save a-x-/7de7844a3605640881e96b7cc9fb1fee to your computer and use it in GitHub Desktop.
summarize active and total browsers by its names
var summary = hubs => _(hubs)
.groupBy('browserName')
.mapValues((data, bro) =>
_(data).mapTuple(['active', 'total']).sumTuple().value()
)
.value();
var hubs = [{
host: 'hub01',
browserName: 'firefox',
active: 3,
total: 10
}, {
host: 'hub02',
browserName: 'chrome',
active: 2,
total: 5
}, {
host: 'hub03',
browserName: 'firefox',
active: 0,
total: 10
}];
_.mixin({
mapTuple: (arr, props) => {
return _.map(arr, item => {
return _.pick(item, props);
})
},
sumTuple: collection => {
let init = _.mapValues(collection[0], _.constant(0));
return _.reduce(collection, (sum, tuple) => {
_.forEach(tuple, (item, key) => sum[key] += item);
return sum;
}, init);
}
});
summary(hubs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment