-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
151 lines (117 loc) · 3.29 KB
/
app.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
document.addEventListener('DOMContentLoaded', () => {
// Organized dictionary.
// [baseName, teensName, tensPrefix]
let numbersDictionary = {
0: ['zero', 'ten'],
1: ['one', 'eleven'],
2: ['two', 'twelve', 'twen'],
3: ['three', 'thirteen', 'thir'],
4: ['four', null, 'for'],
5: ['five', 'fifteen', 'fif'],
6: ['six'],
7: ['seven'],
8: ['eight', 'eighteen', 'eigh'],
9: ['nine']
}
// Main parsing toolbox magic object.
// Could also have been a class, but why bother...?
let parser = {
teenifyNext: false,
hundrifyNext: false,
thousifyNext: false,
overThousifyNext: false,
// Units. Will also handle teens if present.
0: (n) => {
let response = '';
let and = this.hundrifyNext ? ' and ' : '';
this.hundrifyNext = false;
if (this.teenifyNext) {
response = numbersDictionary[n][1] ?
`${numbersDictionary[n][1]}` :
` ${numbersDictionary[n][0]}teen`;
this.teenifyNext = false;
} else if (n != '0') {
response = numbersDictionary[n][0];
} else
and = '';
return ` ${and}${response}`;
},
// Tens.
1: (n) => {
let response = '';
let and = this.hundrifyNext ? ' and ' : '';
if (n == '1') {
this.teenifyNext = true;
this.hundrifyNext = false;
} else if (n != '0') {
response = ` ${numbersDictionary[n][2] ? numbersDictionary[n][2] : numbersDictionary[n][0]}ty`;
this.hundrifyNext = false;
} else
and = '';
return `${and}${response}`;
},
// Hundreds.
2: (n) => {
response = '';
this.hundrifyNext = this.thousifyNext || n !='0';
if (n != '0') {
response = `${this.thousifyNext ? ', ' : ' '}${numbersDictionary[n][0]} hundred`;
}
this.thousifyNext = false;
return `${response}`
},
// Thousands.
3: (n) => {
let response = '';
let and = this.hundrifyNext ? ' and ' : '';
this.hundrifyNext = false;
if (this.teenifyNext)
response = parser[0](n)
else if (n != '0') {
response = numbersDictionary[n][0];
} else
and = '';
this.thousifyNext = true;
return ` ${and}${response} thousand`;
},
// Tens of thousands.
4: (n) => {
return parser[1](n);
},
// Hundreds of thousands.
5: (n) => {
return parser[2](n);
}
}
// Main DOM elements.
let formElement = document.querySelector('#input');
let responseElement = document.querySelector('.response');
formElement.focus();
// Main event. Will fire on <enter>.
formElement.addEventListener('change', () => {
responseElement.innerHTML = formElement.value ? onFormChanged(formElement.value) : ' ';
})
function onFormChanged(value) {
value = cleanInput(value);
formElement.value = value;
let split = value.split('');
let splitProcessed = split.map((n, index, arr) => {
return isNumber(n) && parser[arr.length - index - 1] ? parser[arr.length - index - 1](n) : '';
});
return cleanOutput(splitProcessed.join(''));
}
// Utils
function cleanInput(input) {
return input.replace(/[^0-9]|^0+/g, '');
}
function cleanOutput(output) {
let response = capitalize(output.replace(/^ +| +$/g, ''));
return response == '' ? '"Zero"' : `"${response}"`;
}
function isNumber(input) {
return input.match(/[0-9]/) ? true : false
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
}, false);