-
Notifications
You must be signed in to change notification settings - Fork 42
/
word-drill.js
85 lines (76 loc) · 2.2 KB
/
word-drill.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
80
81
82
83
84
85
// Word-based drills
// =================
// Takes parameters:
//
// - `drill`: look up in TypeJig.WordSets. Can have multiple
// drills which will be merged together.
//
// - `timeLimit`: floating-point minutes.
//
// - `type`:
// - not present: drill words once in order (normal text).
// - `randomly`: drill words in random order until `timeLimit`.
// - `shuffled`: drill words once in a random order.
function wordDrill(params) {
var words = getDrillWords(params.drill, +params.count || 0);
if(!words.length) return;
var name = words.name;
var timeLimit = 0;
var first = +params.first || 0;
var count = +params.count || words.length;
var choose = +params.choose || count;
if(first !== 0 || count !== words.length) {
words = words.slice(first, first+count);
name += ' ' + first + ' to ' + (first+count);
}
if(choose < count) {
shuffle(words)
words = words.slice(0, choose)
count = choose
}
if(params.type === 'randomly') {
timeLimit = Math.round(60 * params.timeLimit);
name = timeString(params.timeLimit) + ' of Random ' + name;
}
var randomly = (params.type === 'randomly');
if(params.type === 'shuffled') {
name = 'Randomized ' + name;
shuffleTail(words, words.length);
}
exercise = new TypeJig.Exercise(words, timeLimit, randomly, params.select);
exercise.name = name;
return exercise;
}
function getDrillWords(drills, count) {
if(!count) count = 1000;
if(!Array.isArray(drills)) drills = [drills];
var name = ''; words = [];
for(let i=0; i<drills.length; ++i) {
var w = TypeJig.WordSets[drills[i]]
if(typeof w === 'function') {
const generateWord = w;
const n = Math.floor(count*(i+1)/drills.length) - Math.floor(count*i/drills.length);
w = []; for(let j=0; j<n; ++j) w.push(generateWord());
}
if(w) {
var last = (i === drills.length-1);
name = nameAnd(name, last, drills[i]);
words = words.concat(w);
}
}
words.name = name;
return words;
}
function nameAnd(name, last, clause) {
if(name.length) {
name += ', ';
if(last) name += 'and ';
}
return name + clause;
}
function timeString(minutes) {
var seconds = Math.round(60 * (minutes % 1));
if(seconds < 10) seconds = '0' + seconds;
minutes = Math.floor(minutes);
return minutes + ':' + seconds;
}