-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
267 lines (223 loc) · 6.5 KB
/
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
(function (root, factory) {
/* istanbul ignore else */
if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
// AMD, registers as an anonymous module.
define(factory);
} else {
// Browser global.
root.numbered = factory();
}
})(this, function () {
var NUMBER_MAP = {
'.': 'point',
'-': 'negative',
0: 'zero',
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight',
9: 'nine',
10: 'ten',
11: 'eleven',
12: 'twelve',
13: 'thirteen',
14: 'fourteen',
15: 'fifteen',
16: 'sixteen',
17: 'seventeen',
18: 'eighteen',
19: 'nineteen',
20: 'twenty',
30: 'thirty',
40: 'forty',
50: 'fifty',
60: 'sixty',
70: 'seventy',
80: 'eighty',
90: 'ninety'
};
// http://en.wikipedia.org/wiki/English_numerals#Cardinal_numbers
var CARDINAL_MAP = {
2: 'hundred',
3: 'thousand',
6: 'million',
9: 'billion',
12: 'trillion',
15: 'quadrillion',
18: 'quintillion',
21: 'sextillion',
24: 'septillion',
27: 'octillion',
30: 'nonillion',
33: 'decillion',
36: 'undecillion',
39: 'duodecillion',
42: 'tredecillion',
45: 'quattuordecillion',
48: 'quindecillion',
51: 'sexdecillion',
54: 'septendecillion',
57: 'octodecillion',
60: 'novemdecillion',
63: 'vigintillion',
100: 'googol',
303: 'centillion'
};
// Make a hash of words back to their numeric value.
var WORD_MAP = {
nil: 0,
naught: 0,
period: '.',
decimal: '.'
};
Object.keys(NUMBER_MAP).forEach(function (num) {
WORD_MAP[NUMBER_MAP[num]] = isNaN(+num) ? num : +num;
});
Object.keys(CARDINAL_MAP).forEach(function (num) {
WORD_MAP[CARDINAL_MAP[num]] = isNaN(+num) ? num : Math.pow(10, +num);
});
/**
* Returns the number of significant figures for the number.
*
* @param {number} num
* @return {number}
*/
function intervals (num) {
var match = String(num).match(/e\+(\d+)/);
if (match) return match[1];
return String(num).length - 1;
}
/**
* Calculate the value of the current stack.
*
* @param {Array} stack
* @param {number} largest
*/
function totalStack (stack, largest) {
var total = stack.reduceRight(function (prev, num, index) {
if (num > stack[index + 1]) {
return prev * num;
}
return prev + num;
}, 0);
return total * largest;
}
/**
* Accepts both a string and number type, and return the opposite.
*
* @param {string|number} num
* @return {string|number}
*/
function numbered (num) {
if (typeof num === 'string') return numbered.parse(num);
if (typeof num === 'number') return numbered.stringify(num);
throw new Error('Numbered can only parse strings or stringify numbers');
}
/**
* Turn a number into a string representation.
*
* @param {number} num
* @return {string}
*/
numbered.stringify = function (value) {
var num = Number(value);
var floor = Math.floor(num);
// If the number is in the numbers object, we quickly return.
if (NUMBER_MAP[num]) return NUMBER_MAP[num];
// If the number is a negative value.
if (num < 0) return NUMBER_MAP['-'] + ' ' + numbered.stringify(-num);
// Check if we have decimals.
if (floor !== num) {
var words = [numbered.stringify(floor), NUMBER_MAP['.']];
var chars = String(num).split('.').pop();
for (var i = 0; i < chars.length; i++) {
words.push(numbered.stringify(+chars[i]));
}
return words.join(' ');
}
var interval = intervals(num);
// It's below one hundred, but greater than nine.
if (interval === 1) {
return NUMBER_MAP[Math.floor(num / 10) * 10] + '-' + numbered.stringify(Math.floor(num % 10));
}
var sentence = [];
// Simple check to find the closest full number helper.
while (!CARDINAL_MAP[interval]) interval -= 1;
if (CARDINAL_MAP[interval]) {
var remaining = Math.floor(num % Math.pow(10, interval));
sentence.push(numbered.stringify(Math.floor(num / Math.pow(10, interval))));
sentence.push(CARDINAL_MAP[interval] + (remaining > 99 ? ',' : ''));
if (remaining) {
if (remaining < 100) sentence.push('and');
sentence.push(numbered.stringify(remaining));
}
}
return sentence.join(' ');
};
/**
* Turns a string representation of a number into a number type
* @param {string} num
* @return {number}
*/
numbered.parse = function (num) {
var modifier = 1;
var largest = 0;
var largestInterval = 0;
var zeros = 0; // Track leading zeros in a decimal.
var stack = [];
var total = num.split(/\W+/g)
.map(function (word) {
var num = word.toLowerCase();
return WORD_MAP[num] !== undefined ? WORD_MAP[num] : num;
})
.filter(function (num) {
if (num === '-') modifier = -1;
if (num === '.') return true; // Decimal points are a special case.
return typeof num === 'number';
})
.reduceRight(function (memo, num) {
var interval = intervals(num);
// Check the interval is smaller than the largest one, then create a stack.
if (typeof num === 'number' && interval < largestInterval) {
stack.push(num);
if (stack.length === 1) return memo - largest;
return memo;
}
memo += totalStack(stack, largest);
stack = []; // Reset the stack for more computations.
// If the number is a decimal, transform everything we have worked with.
if (num === '.') {
var decimals = zeros + String(memo).length;
zeros = 0;
largest = 0;
largestInterval = 0;
return memo * Math.pow(10, -decimals);
}
// Buffer encountered zeros.
if (num === 0) {
zeros += 1;
return memo;
}
// Shove the number on the front if the intervals match and the number whole.
if (memo >= 1 && interval === largestInterval) {
var output = '';
while (zeros > 0) {
zeros -= 1;
output += '0';
}
return Number(String(num) + output + String(memo));
}
largest = num;
largestInterval = intervals(largest);
return (memo + num) * Math.pow(10, zeros);
}, 0);
return modifier * (total + totalStack(stack, largest));
};
return numbered;
});