-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtally.js
52 lines (43 loc) · 1.38 KB
/
tally.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
44
45
46
47
48
49
50
51
52
var buffer =window.getSelection?window.getSelection().toString():document.selection.createRange().text;
var lines = buffer.split("\n");
var total_points = 0;
var tally = [{name: "Remove", points: 0}];
function updateTally(person, value) {
console.log("Adding " + value + " to " + person);
for (j=0;j<tally.length;j++) {
if (tally[j].name.localeCompare(person) == 0) {
tally[j].points += parseInt(value);
console.log(tally[j].name + " is now " + tally[j].points);
break;
} else if (j == (tally.length-1)) {
tally.push({name: person, points: value});
break;
}
}
}
for (i=0;i<lines.length;i++) {
console.log(lines[i]);
var pattern;
// if line has "Completed by", do the per person tally calculation
if (lines[i].indexOf("Completed by") != -1) {
pattern = /\[(\d+)\].*\(Completed by (.+) on \d.*\)/; // h/t to Arun for giving me this regex during our interview
} else {
pattern = /\[(\d+)\].*/;
}
var match = lines[i].match(pattern);
if (match && match != "null") {
value = parseInt(match[1]);
if (match[2] && match[2] != "null") {
person = match[2];
updateTally(person, value);
}
} else {
continue;
}
total_points += value;
}
var output = "Total Points: " + total_points + "\n\n";
for (i=1;i<tally.length;i++) {
output += tally[i].name + ": " + tally[i].points + "\n";
}
alert(output);