-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount.js
345 lines (313 loc) · 7.21 KB
/
count.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! count.js
//! version : 0.1.8
//! authors : Charney Kaye
//! license : MIT
//! https://charneykaye.com
(function (undefined) {
var count,
VERSION = '0.1.8',
words = {
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'
},
zeros = [
{
min: 2,
max: 2,
name: 'hundred'
},
{
min: 3,
max: 5,
name: 'thousand'
},
{
min: 6,
max: 8,
name: 'million'
},
{
min: 9,
max: 11,
name: 'billion'
},
{
min: 12,
max: 14,
name: 'trillion'
}
],
i,
oldGlobalCount,
hasOwnProperty = Object.prototype.hasOwnProperty,
globalScope = typeof global !== 'undefined' ? global : this,
hasModule = (typeof module !== 'undefined' && module.exports);
/**
* @param {Number} num
* @returns {Array} words representing the number
*/
function wordsForNumber(num) {
var _n = num // number to work with
, _p // power of ten
, _m // minimum (calculator)
, _i // iterator
, _out = []; // output
if (_n === 0) {
_out.push(words[0]);
} else {
while (_n > 0) {
//
// from 1 to 19 e.g. "seventeen" (done)
if (_n < 20) {
_out.push(words[_n]);
_n = 0;
}
// from 20 to 99 e.g. "eighty" (leave <10)
else if (_n < 100) {
_p = Math.floor(_n / 10);
_out.push(words[_p * 10]);
_n -= _p * 10;
}
// iterate through the possible x10 digits and reference zeros{} language dynamically
else {
for (_i in zeros) {
if (_n < Math.pow(10, zeros[_i].max + 1)) {
_m = Math.pow(10, zeros[_i].min);
_p = Math.floor(_n / _m);
_out = _out.concat(wordsForNumber(_p));
_out.push(zeros[_i].name);
_n -= _p * _m;
break;
}
}
}
}
}
return _out;
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function defaultParsingFlags() {
// We need to deep clone this object,
// and es5 standard is not very helpful.
return {
empty: false,
nullInput: false,
userInvalidated: false
};
}
function printMsg(msg) {
if (count.suppressDeprecationWarnings === false &&
typeof console !== 'undefined' && console.warn) {
console.warn('Deprecation warning: ' + msg);
}
}
function deprecate(msg, fn) {
var firstTime = true;
return extend(function () {
if (firstTime) {
printMsg(msg);
firstTime = false;
}
return fn.apply(this, arguments);
}, fn);
}
/**
* Constructor
* @constructs Count
* @param config
*/
function Count(config) {
copyConfig(this, config);
}
/**
* Copy configuration to target object
* @param to
* @param from
* @returns {*}
*/
function copyConfig(to, from) {
if (typeof from._isACountObject !== 'undefined') {
to._isACountObject = from._isACountObject;
}
if (typeof from._i !== 'undefined') {
to._i = from._i;
}
if (typeof from._pf !== 'undefined') {
to._pf = from._pf;
}
return to;
}
/**
* Simple object extension
* @param a
* @param b
* @returns {*}
*/
function extend(a, b) {
for (var i in b) {
if (hasOwnProp(b, i)) {
a[i] = b[i];
}
}
if (hasOwnProp(b, 'toString')) {
a.toString = b.toString;
}
if (hasOwnProp(b, 'valueOf')) {
a.valueOf = b.valueOf;
}
return a;
}
/**
* Top-level: Make a Count object
* @param config
* @returns {*}
*/
function makeCount(config) {
var input = config._i;
if (config._i === null) {
return count.invalid({nullInput: true});
}
if (typeof input !== 'number') {
config._i = Number(input);
}
return new Count(config);
}
/**
* Top-level: Make this accessible
* @param input
* @returns {*}
*/
count = function (input) {
var c;
// object construction must be done this way.
// https://github.com/charneykaye/count/issues/1423
c = {};
c._isACountObject = true;
c._i = input;
c._pf = defaultParsingFlags();
return makeCount(c);
};
count.suppressDeprecationWarnings = false;
count.createFromInputFallback = deprecate(
'count construction falls back to js Date. This is ' +
'discouraged and will be removed in upcoming major ' +
'release. Please refer to ' +
'https://github.com/charneykaye/count/issues/1407 for more info.',
function (config) {
config._d = new Date(config._i);
}
);
// version number
count.version = VERSION;
// compare count object
count.isCount = function (obj) {
return obj instanceof Count ||
(obj != null && hasOwnProp(obj, '_isACountObject'));
};
count.invalid = function (flags) {
var m = count(0);
if (flags != null) {
extend(m._pf, flags);
} else {
m._pf.userInvalidated = true;
}
return m;
};
/**
* Count Prototype
*/
extend(count.fn = Count.prototype, {
/**
* Create a camel case block out of the input.
* @returns {string}
*/
camel: function () {
var out = '',
words = this.words();
for (i = 0; i < words.length; i++) {
out += capitalize(words[i]);
}
return out;
},
/**
* Get the sequence of words that spells out the count
*/
words: function () {
return wordsForNumber(this._i);
},
clone: function () {
return count(this);
},
valueOf: function () {
return +this._i;
},
parsingFlags: function () {
return extend({}, this._pf);
}
});
/**
* Exposing Count
*/
function hasOwnProp(a, b) {
return hasOwnProperty.call(a, b);
}
function makeGlobal(shouldDeprecate) {
/*global ender:false */
if (typeof ender !== 'undefined') {
return;
}
oldGlobalCount = globalScope.count;
if (shouldDeprecate) {
globalScope.count = deprecate(
'Accessing Count through the global scope is ' +
'deprecated, and will be removed in an upcoming ' +
'release.',
count);
} else {
globalScope.count = count;
}
}
// CommonJS module is defined
if (hasModule) {
module.exports = count;
} else if (typeof define === 'function' && define.amd) {
define('count', function (require, exports, module) {
if (module.config && module.config() && module.config().noGlobal === true) {
// release the global variable
globalScope.count = oldGlobalCount;
}
return count;
});
makeGlobal(true);
} else {
makeGlobal();
}
}).call(this);