Last active
April 3, 2016 11:06
-
-
Save a-x-/7de7844a3605640881e96b7cc9fb1fee to your computer and use it in GitHub Desktop.
summarize active and total browsers by its names
This file contains hidden or 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
| 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