-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (22 loc) · 861 Bytes
/
index.js
File metadata and controls
26 lines (22 loc) · 861 Bytes
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
(function(root) {
/**
* [exports description]
* @param {string} text The text for which you want to estimate the reading time.
* @param {number} [wpm] The preferred words per minute. Default is 180.
* @param {number} [minimumDelay] Specifies a minimum delay for the timer. Default is 2000.
* @return {number} Number of seconds required for the user to focus attention and read the text.
*/
root.readingTime = function(text, wpm, minimumDelay) {
if (minimumDelay === undefined) {
minimumDelay = 2000;
}
if (!text || !text.length) {
return minimumDelay;
}
if (wpm === undefined) {
wpm = 180;
}
return Math.max(2000, Math.round(text.replace('.,;\'\"', ' ').match(/\S+/g).length * 60000 / wpm, 2));
};
//pass either module.exports or window as the root object
})(typeof module !== 'undefined' ? module.exports : window);