-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract generic message count calculator
Progress toward #6
- Loading branch information
Showing
4 changed files
with
106 additions
and
35 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// TODO better name? | ||
|
||
// statistics = { | ||
// a: 1, | ||
// b: 2, | ||
// c: 1 | ||
// }; | ||
// | ||
// mappings = { | ||
// true: 'men', | ||
// false: 'notMen', | ||
// else: 'unknown' | ||
// }; | ||
// | ||
// values = { | ||
// a: true, | ||
// b: false, | ||
// }; | ||
// | ||
// => | ||
// | ||
// { | ||
// men: 1, | ||
// notMen: 2, | ||
// unknown: 1 | ||
// } | ||
|
||
module.exports = function(statistics, values, mappings) { | ||
var userIDs = Object.keys(statistics); | ||
|
||
var emptyResult = {}; | ||
emptyResult[mappings.true] = 0; | ||
emptyResult[mappings.false] = 0; | ||
emptyResult[mappings.else] = 0; | ||
|
||
var counts = userIDs.reduce(function(counts, userID) { | ||
var value = values[userID]; | ||
var count = statistics[userID]; | ||
|
||
if (value) { | ||
counts[mappings.true] += count; | ||
} else if (value === false) { | ||
counts[mappings.false] += count; | ||
} else { | ||
counts[mappings.else] += count; | ||
} | ||
|
||
return counts; | ||
}, emptyResult); | ||
|
||
return counts; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env iojs --es_staging --use-strict | ||
|
||
var test = require('tape'); | ||
|
||
var trinaryGrouper = require('../../src/calculators/trinary-grouper'); | ||
|
||
test('trinaryGrouper groups counts by attribute value', function(t) { | ||
var statistics = { | ||
'Is tasty': 4, | ||
'Is also tasty': 3, | ||
'Not tasty': 2, | ||
'Tastiness unknown': 1 | ||
}; | ||
|
||
var isTasty = { | ||
'Is tasty': true, | ||
'Is also tasty': true, | ||
'Not tasty': false, | ||
'Tastiness unknown': undefined | ||
}; | ||
|
||
var mappings = { | ||
'true': 'tasties', | ||
'false': 'notTasties', | ||
'else': 'mysterious' | ||
}; | ||
|
||
var result = trinaryGrouper(statistics, isTasty, mappings); | ||
|
||
t.equal(result.tasties, 7, 'counts 7 from tasties'); | ||
t.equal(result.notTasties, 2, 'counts 2 from non-tasties'); | ||
t.equal(result.mysterious, 1, 'counts 1 from mysterious'); | ||
|
||
t.end(); | ||
}); |