-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPoll.js
43 lines (31 loc) · 857 Bytes
/
Poll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const logger = require('../../lib/logger');
class Poll {
constructor() {
this.inMemVotes = new Map();
}
vote(choice) {
let oldval = this.inMemVotes.get(choice);
if (!oldval) {
oldval = 0;
}
let newval = oldval + 1;
this.inMemVotes.set(choice, newval);
logger.info(`Voted for [${choice}], which now has a total of [${newval}] votes`);
}
getResults() {
let items = [];
this.inMemVotes.forEach((v, k) => {
items.push({ Shortcode: k, Votes: v });
});
let totalVotes = items.reduce((prev, curr) => {
return prev + curr.Votes;
}, 0);
items.sort((a, b) => {
return b.Votes - a.Votes;
});
logger.info(`Returning results with [${items.length}] total entries based on [${totalVotes}] total votes.`);
return items;
}
}
module.exports = Poll;