-
Notifications
You must be signed in to change notification settings - Fork 42
/
learn-keyboard.js
90 lines (84 loc) · 3.2 KB
/
learn-keyboard.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
86
87
88
89
90
// Wrap everything in a function to avoid polluting the global namespace.
(function() {
let E = {};
TypeJig.WordSets.LearnKeyboard = E;
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
*/
function shuffle(a) {
for (var i=a.length-1; i>=1; i--) {
var j = Math.floor(Math.random() * (i+1));
var a_i=a[i]; a[i]=a[j]; a[j]=a_i;
}
return a;
}
function IndividualChords(chords, intro, line_length) {
const reversed = [...chords].reverse()
const shuffled = shuffle([...chords])
if(intro === false) intro = []
else {
intro = [].concat(chords, chords, reversed, reversed)
intro[2*chords.length] = '\n'+intro[2*chords.length]
for(const ch of shuffled) {
for(let i=0; i<3; ++i) intro.push((i===0?'\n':'')+ch)
}
}
return (function() {
if(this.i < this.intro.length) {
return this.intro[this.i++]
} else {
const N = line_length || 2*this.chords.length
const i = this.i - this.intro.length
++this.i
const r = i%N === 0 && this.i>1 ? '\n' : ''
const c = Math.floor(this.chords.length * Math.random())
return r + this.chords[c]
}
}).bind({chords: chords, intro: intro, i: 0})
}
function ChordCombos(groups) {
return (function() {
const nl = this.i%8 === 0 && this.i>0 ? '\n' : ''
++this.i
let chord = nl
for(const g of groups) {
chord += g[Math.floor(g.length * Math.random())]
}
return chord
}).bind({i: 0})
}
E["Left hand, bottom row"] = IndividualChords(['S','K','W','R'])
E["Right hand, bottom row"] = IndividualChords(['-S','-G','-B','-R'])
E["Left hand, top row"] = IndividualChords(['S','T','P','H'])
E["Right hand, top row"] = IndividualChords(['-F','-P','-L','-T'])
E["Right hand, full bottom row"] = IndividualChords(['-Z','-S','-G','-B','-R'])
E["Right hand, full top row"] = IndividualChords(['-F','-P','-L','-T','-D'])
E["Vowels"] = IndividualChords(['A','O','E','U'])
// E['"Long" vowels'] = IndividualChords(['AEU','OEU','AOE','AOU','AOEU'])
// E["Diphthongs and Disambiguators"] = IndividualChords(['AO','OE','AE','OU','AU'])
E["Left hand"] = IndividualChords(['S','T','K','P','W','H','R'],false,20)
E["Right hand"] = IndividualChords(['-F','-R','-P','-B','-L','-G','-T','-S','-D','-Z'],false,20)
E["All keys"] = IndividualChords(['S','T','K','P','W','H','R','A','O','E','U','-F','-R','-P','-B','-L','-G','-T','-S','-D','-Z'],false,20)
E["Left + Right"] = ChordCombos([
['S','T','K','P','W','H','R'],
['-F','-R','-P','-B','-L','-G','-T','-S','-D','-Z']
], false)
E["Left + Vowel"] = ChordCombos([
['S','T','K','P','W','H','R'],
['A','O','E','U']
], false)
E["Vowel + Right"] = ChordCombos([
['A','O','E','U'],
['F','R','P','B','L','G','T','S','D','Z']
], false)
E["Left + Vowel + Right"] = ChordCombos([
['S','T','K','P','W','H','R'],
['A','O','E','U'],
['F','R','P','B','L','G','T','S','D','Z']
], false)
E["Columns: D, B, L, -N"] = IndividualChords(['D-','B-','L-','-N'])
E["Rows (2-key): F, M, Q, -M, -K"] = IndividualChords(['F-', 'M-', 'Q-', '-M', '-K'])
E["Rows: N, Y, J, C, V"] = IndividualChords(['N-', 'Y-', 'J-', 'C-', 'V-'], 100)
E["Other chords: G, X, Z, -J"] = IndividualChords(['G-', 'X-', 'Z-', '-J'], 100)
})(); // Execute the code in the wrapper function.