-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (30 loc) · 1017 Bytes
/
index.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
'use strict';
const optionalText = text => {
if (typeof text === 'number' || text === undefined) {
text = '';
}
return text || 'min to read';
};
module.exports = (text, altTextOrWpm, wpm) => {
if (typeof text !== 'string') {
throw new TypeError(`Expected an String in the first argument, got ${typeof text}`);
}
if (altTextOrWpm !== undefined) {
if (typeof altTextOrWpm !== 'string' && typeof altTextOrWpm !== 'number') {
throw new TypeError(`Expected an String/Number in the second argument, got ${typeof altTextOrWpm}`);
}
}
if (wpm !== undefined && typeof wpm !== 'number') {
throw new TypeError(`Expected a Number in the third argument, got ${typeof wpm}`,
);
}
let wordsPerMin = 265;
if (typeof altTextOrWpm === 'number') {
wordsPerMin = altTextOrWpm;
}
if (wpm !== undefined && typeof wpm === 'number') {
wordsPerMin = wpm;
}
const timeTaken = Math.round(text.split(' ').length / wordsPerMin);
return `${timeTaken || 'less than a'} ${optionalText(altTextOrWpm)}`;
};