-
Notifications
You must be signed in to change notification settings - Fork 0
/
_article-time-calculator.js
79 lines (65 loc) · 2.9 KB
/
_article-time-calculator.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
//var ReadingGuesstimator = require('./article-time-calculator');
var instance = new ReadingGuesstimator({debug: true});
function constructDomTest(id, amt){
function populateElemWithNumberOfWords(elem, num){
var str = '';
let i = 0;
for(; i < num; i++){
str += ' derp';
}
elem.textContent = str;
}
let dom = document.body;
let testP = document.createElement('p');
testP.id = id;
populateElemWithNumberOfWords(testP, amt);
dom.appendChild(testP);
}
constructDomTest('testparagraph', 200);
console.assert(instance.guessTimeToReadSelector('#testparagraph') === "1 minute 0 seconds");
constructDomTest('otherp', 500);
console.assert(instance.guessTimeToReadSelector('#otherp') === "2 minutes 30 seconds");
constructDomTest('anotherp', 550);
console.assert(instance.guessTimeToReadSelector('#anotherp') === "2 minutes 45 seconds");
console.assert(instance.guessBasedOnWordCount(200) === "1 minute 0 seconds");
console.assert(instance.guessBasedOnWordCount(300) === "1 minute 30 seconds");
console.assert(instance.guessBasedOnWordCount(400) === "2 minutes 0 seconds");
console.assert(instance.guessBasedOnWordCount(0) === "0 minutes 0 seconds");
console.assert(instance.guessBasedOnWordCount(404) === "2 minutes 1 second");
console.assert(instance.guessBasedOnWordCount(10) === "0 minutes 3 seconds");
console.assert(instance.guessBasedOnWordCount(11) === "0 minutes 3 seconds");
console.assert(instance.guessBasedOnWordCount(12) === "0 minutes 4 seconds");
instance.wordsPerMinute = 60;
console.assert(instance.guessBasedOnWordCount(49) === "0 minutes 49 seconds");
let rg = new ReadingGuesstimator({
wordsPerMinute : 400 // because fast readers.
, nameForMinute : 'minuto'
, nameForSecond : 'segundo'
});
console.assert( rg.guessBasedOnWordCount(600) === "1 minuto 30 segundos");
rg = new ReadingGuesstimator({
pluralizationInterceptor: function(word, amount){
return (amount > 1 || amount === 0) ? word + 'z' : word;
}
});
console.assert( rg.guessBasedOnWordCount(402) === '2 minutez 1 second');
rg = new ReadingGuesstimator({
formatResultInterceptor: function(minutes, nameForMinutes, seconds, nameForSeconds ){
return 'roughly ' + minutes + ' ' + nameForMinutes + ' and ' + seconds + ' ' + nameForSeconds + " if you read at an average speed." ;
}
});
console.assert( rg.guessBasedOnWordCount(402) === 'roughly 2 minutes and 1 second if you read at an average speed.');
// logs: roughly 2 minutes and 1 second if you read at an average speed.
rg = new ReadingGuesstimator({
wordsPerMinute: 400
, nameForMinute: 'm'
, nameForSecond: 's'
, pluralizationInterceptor: function(word){
return word;
}
, formatResultInterceptor: function(min, nameM, sec, nameS){
return "You wasted: " + min + nameM + " and " + sec + nameS + " of your life reading this crap."
}
});
console.assert( rg.guessBasedOnWordCount(404) === "You wasted: 1m and 1s of your life reading this crap.");