-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexpeak.js
227 lines (211 loc) · 7.96 KB
/
hexpeak.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* DOM element selectors */
var elem = {
countSet: ":radio[name=counting]",
numField: "[data-radix]",
hexField: "[data-radix='16']:first",
trans: "#translation",
playBtn: "#playback"
};
/* numbers are mapped to these words */
var dict = {
css: ["ones", "texes", "hundreks", "thouseks"]
,
zero: {card: "zero", ord: "zeroth"}
,
card: ["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "draze", "eptwin", "fim"]
,
ord: ["first", "second", "third", "fourth", "fifth", "sixth", "seventh",
"eighth", "ninth", "tenth", "leventh", "twelfth", "drazeth", "eptwinth", "fimth"]
,
teeks: ["oneteek", "twenteek", "thirteek", "fourteek", "fifteek", "sixteek", "sevteek",
"eighteek", "nineteek", "tenteek", "levteek", "twelfteek", "drazeteek", "epteek", "fimteek", "umpteek"]
,
texes: ["tex", "twentek", "thirtek", "fourtek", "fiftek", "sixtek", "sevtek",
"eightek", "ninetek", "tentek", "levtek", "twelftek", "drazetek", "eptek", "fimtek"]
,
0x2: "hundrek"
,
0x3: "thousek"
,
0x4: "millek"
,
0x8: "billek"
,
0xC: "trillek"
,
0x10: "quadrillek"
,
maxHexDigits: 13 // limitation of parseInt?
};
/* range of random numbers */
var rand = { min: 0x10, max: 0xFFFFF };
/** initialize the fields to a zero */
function zeroFields() {
updateNumFields(0);
}
/** initialize the fields to a random value */
function randFields(min, max) {
min = min || rand.min;
max = max || rand.max;
var randVal = Math.floor(Math.random() * (max-min) + min);
updateNumFields(randVal);
}
/** parse the given textfield object by its own radix and then update TFs */
function parseNumField( TFobj ) {
var num = parseInt(TFobj.text(), TFobj.attr( "data-radix" ));
num = isNaN(num) ? 0 : num;
updateNumFields(num, TFobj );
}
/** synchronize all fields according to their respective bases */
function updateNumFields( num ) {
var hexString = num.toString(0x10);
var numHexDigits = hexString.length;
if ( numHexDigits > dict.maxHexDigits ) {
console.warn("digits in 0x%s exceeds maximum (%d > %d)",
hexString.toUpperCase(), numHexDigits, dict.maxHexDigits);
num = parseInt(hexString.substr(-dict.maxHexDigits), 0x10);
}
elem.numField.each(function() {
var text = num.toString($(this).attr( "data-radix" )).toUpperCase();
if ( $(this).get(0).id == "hex" ) {
location.hash = text;
/* $(this).text("");
for (var place = 0; place < text.length; place++ ) {
$(this).prepend( $("<span></span>").text(text.charAt(text.length-place-1)).addClass(dict.css[place%4]) );
}
} else*/
}
$(this).text(text);
});
updateTranslation();
}
/** respond to changing counting type */
function registerChangeCount() {
elem.countSet.change(function() {
updateTranslation($(this));
})
}
/** translate the number and update the text-out element */
function updateTranslation() {
var numText = elem.hexField.text();
var countMode = elem.countSet.filter( ":checked" ).get(0).id; // cardinal or ordinal
elem.trans.text( "" ); // clear the translation box
elem.hexField.text( "" ); // clear the hex field
if ( parseInt( numText, 0x10) === 0 ) {
// zero is a special case; create a <span class="ones"> for it
elem.trans.prepend( $("<span></span>").addClass(dict.css[0]).text(dict.zero[countMode]) );
elem.hexField.prepend( $("<span></span>").addClass(dict.css[0]).text("0") );
return;
}
var numLen = numText.length, zeroString = "";
for (var place = 0; place < numLen; place++) {
var digit, hasTeek, newWord, newDigits;
/* predetermine the digits for every block of 4, and whether there's a teek */
if ( place%4 === 0 ) {
digit = [];
for (var i = 0; i < 4; i++) {
digit[i] = parseInt( numText.charAt(numLen-(place+i+1)), 0x10 ) || 0;
}
// the teeks are 0x11 < num < 0xFF
hasTeek = (digit[1] === 1 && digit[0] > 0);
}
if ( digit[place%4] === 0 && (place === 0 || place%4 != 0) ) {
zeroString += "0";
continue; // zeroes contribute to digits but not pronunciation
}
switch ( place%4 ) {
case ( 0 ): // ones digit within block of 4
newWord = hasTeek ? dict.teeks : dict[countMode];
newWord = newWord[digit[0]-1] || "";
if (dict[place]) { // add space and block word, for blocks >= milleks
newWord += " " + dict[place];
if (parseInt(numText.substr(-place), 0x10) > 0xFF) { // add comma if more words >= 0x100
newWord += ",";
}
}
break;
case ( 1 ): // texes digit within block of 4
if ( hasTeek ) // teeks are processed as the ones digit
continue;
newWord = dict["texes"][digit[1]-1];
break;
case ( 2 ): // hundreks digit within block of 4
newWord = dict["card"][digit[2]-1] + " " + dict[place%4];
break;
case ( 3 ): // thouseks digit within block of 4
newWord = dict["card"][digit[3]-1] + " " + dict[place%4];
if ( digit[2] > 0 ) // add comma if hundreks > 0
newWord += ",";
break;
default:
newWord = "undefined"
}
if ( countMode === "ord" ) {
if ( place > 0 || hasTeek ) {
newWord += "th"; // ordination suffix is unique only for ones digit, "-th" otherwise
}
countMode = "card"; // ordination suffix can be used no more than once
}
newDigits = (hasTeek && place%4 === 0 ? "1" : "") + numText.charAt( numLen-(place+1) ) + zeroString;
zeroString = "";
// prepend a <span>, with place-determined class, containing the new word and a space
elem.trans.prepend( $("<span></span>").addClass(dict.css[place%4]).text(newWord + " ").fadeIn( ) );
elem.hexField.prepend( $("<span></span>").addClass(dict.css[place%4]).text(newDigits) );
}
}
/** initialize and return the speech object */
function initSpeech() {
var speech = new SpeechSynthesisUtterance();
speech.onstart = function() {
//console.log("started speaking:\n", speech.text);
};
speech.onend = function() {
//console.log("done speaking.");
elem.playBtn.button("option", {icons: {
primary: "ui-icon-play"
}});
};
speech.start = function() {
speech.text = elem.trans.text();
speechSynthesis.speak(speech);
elem.playBtn.button("option", {icons: {
primary: "ui-icon-stop"
}});
};
speech.stop = function() {
speechSynthesis.cancel();
elem.playBtn.button("option", {icons: {
primary: "ui-icon-play"
}});
};
return speech;
}
/** onload function */
$(document).ready(function() {
$( "[contenteditable]" ).keypress(function(event) {
var char = event.which;
if (char == 13) { // manual update on enter key
parseNumField($(this));
event.preventDefault();
return;
}
char = String.fromCharCode(char);
char = parseInt( char, $(this).attr( "data-radix" ));
if (isNaN(char)) { // invalid numeral for this radix
event.preventDefault();
}
}).blur(function() {
parseNumField($(this));
});
/* make jQuery objects out of the selectors given */
$.each(elem, function( key, selector ) {
elem[key] = $( selector );
});
//console.log("hash: %s", location.hash);
if (!location.hash)
randFields();
else
updateNumFields(parseInt(location.hash.substr(1), 0x10));
registerChangeCount();
});