Skip to content

Commit

Permalink
Extract generic message count calculator
Browse files Browse the repository at this point in the history
Progress toward #6
  • Loading branch information
backspace committed Feb 12, 2015
1 parent a14f12a commit 6518141
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 35 deletions.
27 changes: 10 additions & 17 deletions src/calculators/gender-message-count.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
var trinaryGrouper = require('./trinary-grouper');

class GenderMessageCountStatisticsGenerator{
constructor(statistics, userIsMan) {
this.statistics = statistics;
this.userIsMan = userIsMan;
}

generate() {
var userIDs = Object.keys(this.statistics);

var counts = userIDs.reduce(function(counts, userID) {
var isMan = this.userIsMan[userID];
var count = this.statistics[userID];

if (isMan) {
counts.men += count;
} else if (isMan === false) {
counts.notMen += count;
} else {
counts.unknown += count;
return trinaryGrouper(
this.statistics,
this.userIsMan,
{
'true': 'men',
'false': 'notMen',
'else': 'unknown'
}

return counts;
}.bind(this), {men: 0, notMen: 0, unknown: 0});

return counts;
);
}
}

Expand Down
27 changes: 9 additions & 18 deletions src/calculators/race-message-count.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// FIXME extract a generalised solution from this and GenderMessageCount
var trinaryGrouper = require('./trinary-grouper');

class RaceMessageCount {
constructor(statistics, userIsPersonOfColour) {
Expand All @@ -7,24 +7,15 @@ class RaceMessageCount {
}

generate() {
var userIDs = Object.keys(this.statistics);

var counts = userIDs.reduce(function(counts, userID) {
var isPersonOfColour = this.userIsPersonOfColour[userID];
var count = this.statistics[userID];

if (isPersonOfColour) {
counts.peopleOfColour += count;
} else if (isPersonOfColour === false) {
counts.whitePeople += count;
} else {
counts.unknown += count;
return trinaryGrouper(
this.statistics,
this.userIsPersonOfColour,
{
'true': 'peopleOfColour',
'false': 'whitePeople',
'else': 'unknown'
}

return counts;
}.bind(this), {peopleOfColour: 0, whitePeople: 0, unknown: 0});

return counts;
);
}
}

Expand Down
52 changes: 52 additions & 0 deletions src/calculators/trinary-grouper.js
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;
};
35 changes: 35 additions & 0 deletions test/calculators/trinary-grouper.js
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();
});

0 comments on commit 6518141

Please sign in to comment.