Skip to content

Commit

Permalink
Extract generic participant 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 6518141 commit d1b4998
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/calculators/gender-participant-count.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
var trinaryCounter = require('./trinary-counter');

class GenderParticipantCountReportGenerator {
constructor(userMessages, userIsMan) {
this.userMessages = userMessages;
this.userIsMan = userIsMan;
}

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

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

if (isMan) {
counts.men += 1;
} else if (isMan === false) {
counts.notMen += 1;
} else {
counts.unknown += 1;
return trinaryCounter(
this.userMessages,
this.userIsMan,
{
'true': 'men',
'false': 'notMen',
'else': 'unknown'
}

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

return counts;
);
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/calculators/trinary-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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];

if (value) {
counts[mappings.true] += 1;
} else if (value === false) {
counts[mappings.false] += 1;
} else {
counts[mappings.else] += 1;
}

return counts;
}, emptyResult);

return counts;
};
35 changes: 35 additions & 0 deletions test/calculators/trinary-counter.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 trinaryCounter = require('../../src/calculators/trinary-counter');

test('trinaryCounter counts by attribute value', function(t) {
var statistics = {
'Is Purple': 4,
'Is Also Purple': 3,
'Is Yellow': 2,
'Colour unknown': 1
};

var isPurple = {
'Is Purple': true,
'Is Also Purple': true,
'Is Yellow': false,
'Colour unknown': undefined
};

var mappings = {
'true': 'purples',
'false': 'notPurples',
'else': 'hmm'
};

var result = trinaryCounter(statistics, isPurple, mappings);

t.equal(result.purples, 2, 'counts 2 purples');
t.equal(result.notPurples, 1, 'counts 1 not-purple');
t.equal(result.hmm, 1, 'counts 1 unknown');

t.end();
});

0 comments on commit d1b4998

Please sign in to comment.