Passed
Push — master ( a3ee4a...501c3e )
by Night
01:05
created

numberFuncs.toInt   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 15
rs 8.6666
1
/** global: UB */
2
3
4
UB.Float_MaxValue = 3.40282e+038; /// 038 or 056? .. it was originally 038F
5
UB.Float_MinValue = -3.40282e+038;
6
UB.roman1s = ["", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"];
7
UB.roman10s = ["", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"];
8
UB.roman100s = ["", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"];
9
UB.roman1000s = ["", "m", "mm", "mmm", "mmmm", "", "", "", "", ""];
10
UB.roman = [UB.roman1s, UB.roman10s, UB.roman100s, UB.roman1000s];
11
UB.letterLegal = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", "MM", "NN", "OO", "PP", "QQ", "RR", "SS", "TT", "UU", "VV", "WW", "XX", "YY", "ZZ", "AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ", "KKK", "LLL", "MMM", "NNN", "OOO", "PPP", "QQQ", "RRR", "SSS", "TTT", "UUU", "VVV", "WWW", "XXX", "YYY", "ZZZ", "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLL", "MMMM", "NNNN", "OOOO", "PPPP", "QQQQ", "RRRR", "SSSS", "TTTT", "UUUU", "VVVV", "WWWW", "XXXX", "YYYY", "ZZZZ", "AAAAA", "BBBBB", "CCCCC", "DDDDD", "EEEEE", "FFFFF", "GGGGG", "HHHHH", "IIIII", "JJJJJ", "KKKKK", "LLLLL", "MMMMM", "NNNNN", "OOOOO", "PPPPP", "QQQQQ", "RRRRR", "SSSSS", "TTTTT", "UUUUU", "VVVVV", "WWWWW", "XXXXX", "YYYYY", "ZZZZZ"];
12
UB.letterExcel = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM", "BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ", "CA", "CB", "CC", "CD", "CE", "CF", "CG", "CH", "CI", "CJ", "CK", "CL", "CM", "CN", "CO", "CP", "CQ", "CR", "CS", "CT", "CU", "CV", "CW", "CX", "CY", "CZ", "DA", "DB", "DC", "DD", "DE", "DF", "DG", "DH", "DI", "DJ", "DK", "DL", "DM", "DN", "DO", "DP", "DQ", "DR", "DS", "DT", "DU", "DV", "DW", "DX", "DY", "DZ"];
13
UB.decZeros = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
14
UB.hexChars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
15
UB.toDegrees = (180 / Math.PI);
16
UB.toRadians = (Math.PI / 180);
17
18
UB.minOf3 = function(num1, num2, num3){
19
	if (num1 < num2) {
20
		if (num1 < num3) {
21
			return num1;
22
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
23
			return num3;
24
		}
25
	}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
26
		if (num2 < num3) {
27
			return num2;
28
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
29
			return num3;
30
		}
31
	}
32
};
33
UB.maxOf3 = function(num1, num2, num3){
34
	if (num1 > num2) {
35
		if (num1 > num3) {
36
			return num1;
37
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
38
			return num3;
39
		}
40
	}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
41
		if (num2 > num3) {
42
			return num2;
43
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
44
			return num3;
45
		}
46
	}
47
};
48
49
UB.minOf4 = function(num1, num2, num3, num4){
50
	if (num1 < num2) {
51
		
52
		// 1, 3, 4
53
		if (num1 < num3) {
54
			
55
			// 1, 4
56
			if (num1 < num4) {
57
				return num1;
58
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
59
				return num4;
60
			}
61
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
62
			
63
			// 3, 4
64
			if (num3 < num4) {
65
				return num3;
66
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
67
				return num4;
68
			}
69
		}
70
		
71
	}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
72
		
73
		// 2, 3, 4
74
		if (num2 < num3) {
75
			
76
			// 2, 4
77
			if (num2 < num4) {
78
				return num2;
79
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
80
				return num4;
81
			}
82
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
83
			
84
			// 3, 4
85
			if (num3 < num4) {
86
				return num3;
87
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
88
				return num4;
89
			}
90
		}
91
	}
92
};
93
UB.maxOf4 = function(num1, num2, num3, num4){
94
	if (num1 > num2) {
95
		
96
		// 1, 3, 4
97
		if (num1 > num3) {
98
			
99
			// 1, 4
100
			if (num1 > num4) {
101
				return num1;
102
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
103
				return num4;
104
			}
105
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
106
			
107
			// 3, 4
108
			if (num3 > num4) {
109
				return num3;
110
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
111
				return num4;
112
			}
113
		}
114
		
115
	}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
116
		
117
		// 2, 3, 4
118
		if (num2 > num3) {
119
			
120
			// 2, 4
121
			if (num2 > num4) {
122
				return num2;
123
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
124
				return num4;
125
			}
126
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
127
			
128
			// 3, 4
129
			if (num3 > num4) {
130
				return num3;
131
			}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
132
				return num4;
133
			}
134
		}
135
	}
136
};
137
138
139
var stringFuncs = {
140
	
141
	/** CONVERT LEGAL LETTER TO NUMBER .. A = 1, B = 2, C = 3  */
142
	legalNumeralToNumber: function(s, zeroBased){
143
		return UB.letterLegal.indexOf(s.toUpperCase()) + (zeroBased ? 0 : 1);
144
	},
145
	/** CONVERT EXCEL COLUMN ID TO NUMBER .. A = 1, B = 2, C = 3  */
146
	excelColumnToNumber: function(s, zeroBased = false){
147
		return UB.letterExcel.indexOf(s.toUpperCase()) + (zeroBased ? 0 : 1);
148
	},
149
	
150
	
151
	none:null
152
};
153
154
// register funcs
155
UB.registerFuncs(String.prototype, stringFuncs);
156
157
158
159
var numberFuncs = {
160
	
161
	isOdd: function(val){
162
		return (val % 2) != 0;
1 ignored issue
show
Best Practice introduced by
Comparing val % 2 to 0 using the != operator is not safe. Consider using !== instead.
Loading history...
163
	},
164
	isEven: function(val){
165
		return (val % 2) == 0;
1 ignored issue
show
Best Practice introduced by
Comparing val % 2 to 0 using the == operator is not safe. Consider using === instead.
Loading history...
166
	},
167
	isPositive: function(val){
168
		return (val > 0);
169
	},
170
	isNegative: function(val){
171
		return (val < 0);
172
	},
173 View Code Duplication
	relativeTo: function(v1, v2, type){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
174
		// type .. 0=any, 1=pos, 2=neg
175
		var v = v2 - v1;
176
		if (type == 1) {
1 ignored issue
show
Best Practice introduced by
Comparing type to 1 using the == operator is not safe. Consider using === instead.
Loading history...
177
			return v > 0 ? v : -v;
178
		}else if (type == 2) {
179
			return v > 0 ? -v : v;
180
		}
181
		return v;
182
	},
183
	abs: function(v, negative = false){
184
		if (negative){
185
			return v > 0 ? -v : v;
186
		}
187
		return v > 0 ? v : -v;
188
	},
189
	acos: function(v, degrees = false){
190
		if (degrees) {
191
			return Math.acos(v) * UB.toDegrees;
192
		}
193
		return Math.acos(v);
194
	},
195
	asin: function(v, degrees = false){
196
		if (degrees) {
197
			return Math.asin(v) * UB.toDegrees;
198
		}
199
		return Math.asin(v);
200
	},
201
	atan: function(v, degrees = false){
202
		if (degrees) {
203
			return Math.atan(v) * UB.toDegrees;
204
		}
205
		return Math.atan(v);
206
	},
207
	atan2: function(y, x, degrees = false){
208
		if (degrees) {
209
			return Math.atan2(y, x) * UB.toDegrees;
210
		}
211
		return Math.atan2(y, x);
212
	},
213
	exp: function(v){
214
		return Math.exp(v);
215
	},
216
	log: function(v){
217
		return Math.log(v);
218
	},
219
	pow: function(base, power){
220
		return Math.pow(base, power)
221
	},
222
	sin: function(angle, degrees = false){
223
		if (degrees) {
224
			return Math.sin(angle * UB.toRadians);
225
		}
226
		return Math.sin(angle);
227
	},
228
	cos: function(angle, degrees = false){
229
		if (degrees) {
230
			return Math.cos(angle * UB.toRadians);
231
		}
232
		return Math.cos(angle);
233
	},
234
	tan: function(angle, degrees = false){
235
		if (degrees) {
236
			return Math.tan(angle * UB.toRadians);
237
		}
238
		return Math.tan(angle);
239
	},
240
	sqrt: function(v){
241
		return Math.sqrt(v);
242
	},
243
	compare: function(v1, v2){
244
		if (v1 < v2) {
245
			return -1;
246
		}
247
		if (v1 > v2) {
248
			return 1;
249
		}
250
		return 0;
251
	},
252
	positive: function(val){
253
		return val > 0 ? val : -val;
254
	},
255
	negative: function(val){
256
		return val > 0 ? -val : val;
257
	},
258
	
259
	exists: function(v, zeroOK = false){
260
		if (zeroOK) {
261
			return (v != null && v == v);
1 ignored issue
show
Best Practice introduced by
Comparing v to null using the != operator is not safe. Consider using !== instead.
Loading history...
262
		}
263
		return v != null && v == v && v != 0;
264
	},
265
	
266
	
267
	min: function(num1, num2){
268
		if (num1 < num2) {
269
			return num1;
270
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
271
			return num2;
272
		}
273
	},
274
	max: function(num1, num2){
275
		if (num1 > num2) {
276
			return num1;
277
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
278
			return num2;
279
		}
280
	},
281
	atMost: function(num, limit){
282
		return num > limit ? limit : num;
283
	},
284
	atLeast: function(num, limit){
285
		return num < limit ? limit : num;
286
	},
287
	
288
	
289
	isNear: function(val, target, near = 1){
290
		return val >= (target - near) && val <= (target + near);
291
	},
292
	
293
294
	/** Render a percentage (25%) using 2 values */
295
	percentageToString: function(total, current, decimals = 0){
296
		return ((current / total) * 100).roundToString(decimals) + "%";
297
	},
298
	
299
300
	// CONVERT TO ABBREVIATION .. 1.5K
301
	toAbbreviation: function(num, decimals = 1){
302
		if (num < 0) {
303
			return "-" + (-num).toAbbreviation; // -5.2K, -5.2M ..
304
		}
305
		if (num < 1000) {
306
			return String(num); // 520
307
		}
308
		if (num < 1000000) {
309
			return (num / 1000).roundToDigits(decimals) + 'K'; // 5.2K
310
		}
311
		if (num < 1000000000) {
312
			return (num / 1000000).roundToDigits(decimals) + 'M'; // 5.2M
313
		}
314
		return (num / 1000000000).roundToDigits(decimals) + 'G'; // 5.2B
315
	},
316
	
317
	
318
	// CONVERT TO ROMAN
319
	toRomanNumeral: function(n){
320
		if (n < 1) {
321
			n = 1;
322
		}
323
		
324
		// exit quickly if single digit
325
		if (n <= 9) {
326
			return UB.roman1s[n];
327
		}
328
		
329
		// create roman number if longer
330
		var str = n.toString();
331
		var output = [];
332
		for (var n = 0, nl = str.length;n<nl;n++){
333
			var series = ((nl - 1) - n);
334
			var charIndex = int(str.charAt(n));
335
			output.push(UB.roman[series][charIndex]);
336
		}
337
		return output.join("");
338
	},
339
	
340
	/** CONVERT TO LEGAL LETTER .. 1 = A, 2 = B, 3 = C .. AA, BB, CC */
341
	toLegalNumeral: function(n){
342
		if (n < 1) {
343
			n = 1;
344
		}
345
		return UB.letterLegal[n];
346
	},
347
	// CONVERT TO NORMAL LETTER .. A, B, C .. AB, AB, AC .. BA, BB, BC
348
	toLetterNumeral: function(n){
349
		if (n <= 26) {
350
			return UB.letterLegal[n];
351
		}
352
		return "?";
353
	},
354
	/** CONVERT TO EXCEL COLUMN ID .. 1 = A, 2 = B, 3 = C .. AA, AB, AC */
355
	toExcelColumn: function(n, zeroBased = false){
356
		var min = zeroBased?0:1;
357
		if (n < min) {
358
			n = min;
359
		}
360
		return UB.letterExcel[zeroBased ? n : n-1];
361
	},
362
	
363
	// CONVERT TO ORDINAL FORMAT .. 1st, 2nd, 3rd
364
	toOrdinal: function(i){
365
		var j = i % 10;
366
		var k = i % 100;
367
		if (j == 1 && k != 11) {
1 ignored issue
show
Best Practice introduced by
Comparing j to 1 using the == operator is not safe. Consider using === instead.
Loading history...
368
			return i + "st";
369
		}
370
		if (j == 2 && k != 12) {
371
			return i + "nd";
372
		}
373
		if (j == 3 && k != 13) {
374
			return i + "rd";
375
		}
376
		return i + "th";
377
	},
378
	
379
	
380
	// OR
381
	or: function(n, returnVal, ifNull = 0){
382
		if (n == ifNull) {
383
			return returnVal;
384
		}
385
		return n;
386
	},
387
	
388
	
389
	/** Calc padding for a given length */
390
	padding: function(length, multipleOf){
391
		return (multipleOf - (length % multipleOf));
392
	},
393
	
394
	multipleOf: function(value, multipleOf){
395
		return value + (multipleOf - (value % multipleOf));
396
	},
397
	
398
	
399
	// FROM C#
400
	
401
	distance: function(n1, n2){
402
		return Math.abs(n1 - n2);
403
	},
404
	
405
	/** Calc x % of the given value */
406
	percentage: function(val100Percent, percentWanted){
407
		if (percentWanted == 100) {
408
			return val100Percent;
409
		}
410
		return val100Percent * (percentWanted / 100.0);
411
	},
412
	
413
	/** Calc the % of otherVal in relation to val100Percent */
414
	percentOf: function(val100Percent, otherVal){
415
		return (otherVal / val100Percent) * 100.0;
416
	},
417
	
418
	/** Calc the difference between 2 numbers in %
419
	//  ... 100, 50 shows -50% difference
420
	//  ... 100, 150 shows 50% difference */
421
	percentDifference: function(val100Percent, otherVal){
422
		return ((otherVal - val100Percent) / val100Percent) * 100.0;
423
	},
424
	
425
	/** calc overlap of 2 numeric ranges */
426
	overlap: function(value1Start, value1End, value2Start, value2End){
427
		
428
		// swap if reversed
429
		if (value1Start > value1End) {
430
			var temp = value1Start;
431
			value1Start = value1End;
432
			value1End = temp;
433
		}
434
		if (value2Start > value2End) {
435
			temp = value2Start;
436
			value2Start = value2End;
437
			value2End = temp;
438
		}
439
		
440
		// exit if no overlap
441
		if (value1End < value2Start || value2End < value1Start) {
442
			return 0;
443
		}
444
		
445
		// return overlap of 2 ranges
446
		if (value1Start >= value2Start && value1End <= value2End) {
447
			return (value1End - value1Start);
448
		} else if (value2Start >= value1Start && value2End <= value1End) {
449
			return (value2End - value2Start);
450
		} else if (value1Start < value2Start && value1End <= value2End) {
451
			return (value1End - value2Start);
452
		} else if (value2Start < value1Start && value2End <= value1End) {
453
			return (value2End - value1Start);
454
		}
455
		return 0;
456
	},
457
	
458
	isConsequetive: function(value, value2){
459
		return value2 == (value - 1) || value2 == (value + 1);
460
	},
461
	
462
	/** Returns the number halfway between the 2 given numbers
463
		Value1 can be greater than Value2. */
464
	between: function(value1, value2){
465
		if (value1 > value2) {
466
			return value2 + ((value1 - value2) / 2);
467
		}
468
		return value1 + ((value2 - value1) / 2);
469
	},
470
	
471
	/** Returns the number halfway between the 2 given numbers
472
		Value1 can be greater than Value2. */
473
	middle: function(value1, value2){
474
		if (value1 > value2) {
475
			return value2 + ((value1 - value2) / 2);
476
		}
477
		return value1 + ((value2 - value1) / 2);
478
	},
479
	
480
	calcScale: function(value, parentValue){
481
		if (parentValue == 0 && value == 0) {
2 ignored issues
show
Best Practice introduced by
Comparing value to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing parentValue to 0 using the == operator is not safe. Consider using === instead.
Loading history...
482
			return 0;
483
		}
484
		return (value / parentValue);
485
	},
486
	calcMultiplierTo: function(fromValue, toValue){
487
		return CalcScale(toValue, fromValue);
488
	},
489
	
490
	ensure: function(value, defaultVal){
491
		if (value == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing value to 0 using the == operator is not safe. Consider using === instead.
Loading history...
492
			return defaultVal;
493
		}
494
		return value;
495
	},
496
	
497
	snapToHigher: function(val, snapBy){
498
		return Math.ceil(val / snapBy) * snapBy;
499
	},
500
	snapToLower: function(val, snapBy){
501
		return Math.floor(val / snapBy) * snapBy;
502
	},
503
	snapToNearest: function(val, snapBy){
504
		return Math.round(val / snapBy) * snapBy;
505
	},
506
	
507
	toInt: function(num, round){
508
		if (!round || round == "truncate") {
509
			return parseInt(num);
510
		}
511
		if (round == "round") {
512
			return Math.round(num);
513
		}
514
		if (round == "floor") {
515
			return Math.floor(num);
516
		}
517
		if (round == "ceiling") {
518
			return Math.ceil(num);
519
		}
520
		return 0;
521
	},
522
	
523
	/**
524
	 * Formats a Number adding commas and the specified decimal digits.
525
	 * Supports the million/billion and lakh/crore system.
526
	 * 
527
	 * @param	commas					3 = million, 2 = lakh
528
	 * @param	decimals				-1 = keep exact decimals as number, 0 = no decimals, 1+ = specific decimals
529
	 * @param	alwaysShowDecimal		add decimal even if decimal part is 0
530
	 */
531
	formatNumber: function(num, commas, decimals = -1, alwaysShowDecimal = false){
532
		
533
		// test if number is negative
534
		var neg = false;
535
		if (num < 0) {
536
			neg = true;
537
			num = -num;
538
		}
539
		// get the left number
540
		var numStr = String(num);
541
		var hasDot = numStr.indexOf(".")>-1;
542
		if (hasDot) {
543
			var parts = numStr.split(".");
544
			var whole = parts[0];
545
			var decimal = parts[1];
546
		}else{
547
			whole = numStr;
548
			decimal = null;
549
		}
550
		
551
		// if at least 4 digits
552
		if (whole.length > 3) {
553
			
554
			// extract 3 digits first 
555
			var wholeRight3 = whole.substr(whole.length - 3, 3);
556
			whole = whole.substr(0, whole.length - 3);			
557
			
558
			// add million / crore seperators
559
			if (whole.length < commas) {
560
				whole = whole + "," + wholeRight3;
561
			}else {
562
				var thou = "";
563
				var rem = whole.substr(0, int(whole.length % commas));
564
				var l = ((int(whole.length / commas)*commas) - commas) + (whole.length % commas);
565
				for (var c = 0;l >= 0;l -= commas, c++){
0 ignored issues
show
Unused Code introduced by
The loop variable c is initialized by the loop but not used in the test. Consider using another type of loop if this is the intended behavior.
Loading history...
566
					thou = whole.substr(l, commas) + (c == 0 ? "" : ",") + thou;
1 ignored issue
show
Best Practice introduced by
Comparing c to 0 using the == operator is not safe. Consider using === instead.
Loading history...
567
				}
568
				whole = (rem == "" ? "" : rem + ",") + thou + "," + wholeRight3;
569
			}
570
		}
571
		
572
		// return with or without dot
573
		var result;
574
		if (hasDot) {
575
			if (decimals > 0) {
576
				//  4.200
577
				result = whole + "." + decimal.substr(0, decimals).padRight("0", decimals);
578
			}else if (decimals == 0) {
1 ignored issue
show
Best Practice introduced by
Comparing decimals to 0 using the == operator is not safe. Consider using === instead.
Loading history...
579
				//  4
580
				result = whole;
581
			}else if (decimals == -1) {
582
				//  4.2
583
				result = whole + "." + decimal;
584
			}
585
			
586
		}else if (alwaysShowDecimal && decimals > 0) {
587
			
588
			//  4.0000
589
			result = whole + "." + decimals.repeat("0");
590
		}else {
591
			
592
			//  4
593
			result = whole;
594
		}
595
		return (neg ? "-" + result : result);
0 ignored issues
show
Bug introduced by
The variable result does not seem to be initialized in case decimals == -1 on line 581 is false. Are you sure this can never be the case?
Loading history...
596
	},
597
	
598
	ensureNotNaN: function(val, instead = 0){
599
		if (val != val) { /// fast isNaN check
600
			return instead;
601
		}else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
602
			return val;
603
		}
604
	},
605
	
606
	isNaN: function(val){
607
		return val != val || isNaN(val);
608
	},
609
	
610
	isAny: function(needle, haystacks, asInt = false, fractionalDigits = -1){
611
		
612
		// return true if any haystack equals the needle
613
		for (var s = 0, sl = haystacks.length;s<sl;s++){
614
			var num = haystacks[s];
615
			
616
			// simplify value for comparison
617
			if (asInt) {
618
				num = int(num);
619
			}else if(fractionalDigits != -1) {
620
				num = num.roundToDigits(fractionalDigits);
621
			}
622
			
623
			if (needle == num) {
624
				return true;
625
			}
626
		}
627
		return false;
628
	},
629
	
630
	scale: function(input, inputMin, inputMax, outputMin, outputMax, outputLimit = true, outputFlip = false){
631
		
632
		// ensure input within limits
633
		if (input < inputMin) {
634
			input = inputMin;
635
		}
636
		if (input > inputMax) {
637
			input = inputMax;
638
		}
639
		
640
		// convert input to %
641
		var percent = ((1*(input - inputMin)) / (inputMax - inputMin));
642
		
643
		// convert % to output
644
		var output = (percent*(outputMax - outputMin)) + outputMin;
645
		
646
		// ensure output within limits
647
		if (outputLimit){
648
			if (output < outputMin) {
649
				output = outputMin;
650
			}
651
			if (output > outputMax) {
652
				output = outputMax;
653
			}
654
		}
655
		
656
		// flip output
657
		if (outputFlip) {
658
			output = ((outputMax - outputMin) - (output - outputMin)) + outputMin;
659
		}
660
		
661
		// return scaled, limit-checked output
662
		return output;
663
	},
664
	smartScale: function(input, inputMin, inputMax, outputMin, outputMax){
665
		if (inputMin > inputMax) {
666
			if (outputMin > outputMax) {
667
				return ScaleTo(FlipWithin(input, inputMin, inputMax), inputMax, inputMin, outputMax, outputMin, true, true);
668
			} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
669
				return ScaleTo(FlipWithin(input, inputMin, inputMax), inputMax, inputMin, outputMin, outputMax);
670
			}
671
		} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
672
			if (outputMin > outputMax) {
673
				return ScaleTo(input, inputMin, inputMax, outputMax, outputMin, true, true);
674
			} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
675
				return ScaleTo(input, inputMin, inputMax, outputMin, outputMax);
676
			}
677
		}
678
	},
679
	
680
	flipWithin: function(input, min, max){
681
		if (max < min) {
682
			return ((min - max) - (input - max)) + min;
683
		}
684
		return ((max - min) - (input - min)) + min;
685
	},
686
	
687
	limitToDigits: function(input, digits){
688
		return parseFloat(input.toString().substr(0, digits));
689
	},
690 View Code Duplication
	limit: function(input, min, max){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
691
		
692
		// ensure ok
693
		if (input != input) { /// fast isNaN check
694
			return min;
695
		}
696
		
697
		// flip limits if in reverse order
698
		if (min > max) {
699
			var temp = max;
700
			max = min;
701
			min = temp;
702
		}
703
		
704
		// ensure within limits
705
		if (input < min) {
706
			return min;
707
		}
708
		if (input > max) {
709
			return max;
710
		}
711
		return input;
712
	},
713
	limitToArray: function(pointer, array, extraMax = 0, extraMin = 0){
714
		if (pointer <= -extraMin) {
715
			return -extraMin;
716
		}
717
		var last = (array.length + extraMax) - 1;
718
		if (last == -1) {
719
			return -extraMin;
720
		}
721
		if (pointer > last) {
722
			return last;
723
		}
724
		return pointer
725
	},
726
	
727
	wrap: function(value, min, max){
728
		value -= min;
729
		var diff = (max - min) + 1;
730
		while (value < 0) {
731
			value += diff;
732
		}
733
		return min + (value % diff);
734
	},
735 View Code Duplication
	wrap2: function(input, min, max){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
736
		
737
		// flip limits if in reverse order
738
		if (min > max) {
739
			var temp = max;
740
			max = min;
741
			min = temp;
742
		}
743
		
744
		// ensure wrapped to limits
745
		var diff = (max - min) + 1;
746
		while (input < min) {
747
			input += diff;
748
		}
749
		while (input > max) {
750
			input -= diff;
751
		}
752
		return input;
753
	},
754
	wrapToMax: function(value, max){
755
		var diff = max + 1;
756
		while (value < 0) {
757
			value += diff;
758
		}
759
		return value % diff;
760
	},
761
	
762
	/** Identical to modulo operator, except negative values are also supported, and brought into positive range.
763
		Value will never reach limit, just like the modulo operator.
764
		Simpler than WrapTo which takes min and max. */
765
	modulo: function(input, limit){
766
		
767
		// ensure wrapped to limits
768
		while (input < 0) {
769
			input += limit;
770
		}
771
		while (input >= limit) {
772
			input -= limit;
773
		}
774
		return input;
775
	},
776
	
777
	roundToDigits: function(input, decimals = 4){
778
		
779
		if (decimals <= 0) {
780
			return Math.round(input);
781
		}
782
		
783
		// Returns a number rounded to specified number of decimals
784
		var multiplier = Math.pow(10, decimals);
785
		return Math.round(input*multiplier)/multiplier;
786
	},
787
	roundToString: function(input, decimals){
788
		
789
		// Returns a number rounded to specified number of decimals
790
		// Add zeros to always return those many decimals
791
		var num = RoundTo(input, decimals).toString();
792
		if (decimals <= 0) {
793
			return num;
794
		}
795
		var add0 = 0;
796
		if (num.lastIndexOf(".") == -1){
797
			num = num+".";
798
			add0 = decimals;
799
		}else{
800
			add0 = decimals - ((num.length - num.lastIndexOf(".")) - 1);
801
		}
802
		while(add0 > 0){
803
			num = num+"0";
804
			add0 --;
805
		}
806
		return num;
807
	},
808
	
809
	
810
	// CHECKS
811
	/** Checks if the value is within min and max (supporting inclusive/exclusive modes) */
812
	isWithin: function(num, min, max, inclusive = true){
813
		if (inclusive) {
814
			if (min > max) {
815
				return (num >= max && num <= min);
816
			}
817
			return (num >= min && num <= max);
818
		} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
819
			if (min > max) {
820
				return (num > max && num < min);
821
			}
822
			return (num > min && num < max);
823
		}
824
	},
825
	isWithinArray: function(slot, arr){
826
		
827
		// no, if array empty / index negative / index more than last slot
828
		var len = arr.length;
829
		if (len == 0 || slot < 0 || slot >= len) {
1 ignored issue
show
Best Practice introduced by
Comparing len to 0 using the == operator is not safe. Consider using === instead.
Loading history...
830
			return false;
831
		}
832
		
833
		// else yes
834
		return true;
835
	},
836
	/** Checks if the value is > min and < max */
837
	isBetween: function(num, min, max){
838
		return (num > min && num < max);
839
	},
840
	isOutside: function(num, min, max, inclusive = true){
841
		if (inclusive) {
842
			return num <= min || num >= max;
843
		}
844
		return num < min || num > max;
845
	},
846
	isFractional: function(num){
847
		return int(num) != num;
848
	},
849
	isInteger: function(num){
850
		return int(num) == num;
851
	},
852
	
853
	
854
	// HEX
855
	/** DO NOT USE THIS FOR `uint`s! */
856
	toHex: function(num, digits = 6, withHash = true){
857
		var zeros = ("0000000000000000000000000000000000").substr(0, digits - 1);
858
		return ((withHash ? "#" : "") + (zeros + num.toString(16).toUpperCase()).substr( -digits));
859
	},
860
	/** USE THIS FOR `uint`s! */
861
	toHexUInt: function(num, digits = 8, withHash = true){
862
		var zeros = ("0000000000000000000000000000000000").substr(0, digits - 1);
863
		return ((withHash ? "#" : "") + (zeros + num.toString(16).toUpperCase()).substr( -digits));
864
	},
865
	hexToDecimal: function(hex, onlyDigits = 0){
866
		hex = hex.toUpperCase();
867
		
868
		// remove prefix
869
		if (hex.indexOf("0X") == 0){
1 ignored issue
show
Best Practice introduced by
Comparing hex.indexOf("0X") to 0 using the == operator is not safe. Consider using === instead.
Loading history...
870
			hex = hex.substr(2);
871
		}else if (hex.charAt(0) == "#"){
872
			hex = hex.substr(1);
873
		}
874
		
875
		if (onlyDigits) {
876
			hex = hex.substr(0, onlyDigits);
877
		}
878
		return int("0x" + hex);
879
	},
880
	
881
	incrementWithin: function(num, min, max){
882
		num++;
883
		if (num > max) {
884
			return min;
885
		}
886
		return num;
887
	},
888
	decrementWithin: function(num, min, max){
889
		num--;
890
		if (num < min) {
891
			return max;
892
		}
893
		return num;
894
	},
895
	
896
	
897
	// BINARY
898
	binaryToDecimal: function(bin){
899
		bin = bin.toUpperCase();
900
		
901
		// remove prefix
902
		if (bin.indexOf("0B") == 0){
1 ignored issue
show
Best Practice introduced by
Comparing bin.indexOf("0B") to 0 using the == operator is not safe. Consider using === instead.
Loading history...
903
			bin = bin.substr(2);
904
		}
905
		
906
		var byte = 0;
907
		for (var i = 0, il = bin.length, il2 = bin.length - 1;i<il;i++){
908
			byte += uint(bin.charAt(il2 - i)) * Math.pow(2,i);
909
		}
910
		return byte;
911
	},
912
	
913
	decimalToBinary: function(num, digits = 8){
914
		var zeros = UB.decZeros.substr(0, digits - 1);
915
		return (zeros + num.toString(2)).substr( -digits);
916
	}, 
917
	decimalToBinary2: function(byte, digits = 8){
918
		var bin = '';
919
		for (var i = 0;i<digits;i++){
920
			bin += String((byte & (0x80 >> i)) >> (7 - i));
921
		}
922
		return bin;
923
	},
924
	
925
	
926
	
927
	// MISC MATH
928
	pad: function(number, length, addToEnd = false, padChar = '0'){
929
		
930
		// num to text
931
		var result = String(number);
932
		
933
		// exit quickly if already reached target len
934
		if (result.length >= length) {
935
			return result;
936
		}
937
		
938
		
939
		// pad
940
		if (addToEnd) {
941
			while (result.length < length){
942
				result = result + padChar;
943
			}
944
		}else {
945
			while (result.length < length){
946
				result = padChar + result;
947
			}
948
		}
949
		
950
		
951
		return result;
952
	},
953
	interpolate: function(min, max, progress){
954
		// progress = (0 to 1)
955
		/*return( (1 - progress) * min + progress * max ); <---- WHAT IS THIS??? */ 
956
		return min + ((max - min) * progress);
957
	},
958
	hypot: function(a, b){
959
		// sqrt(a^2 + b^2) without under/overflow.
960
		
961
		var r;
962
		var aAbs = (a > 0 ? a : -a);
963
		var bAbs = (b > 0 ? b : -b);
964
		if (aAbs > bAbs) {
965
			r = b/a;
966
			r = aAbs*Math.sqrt(1+r*r);
967
		} else if (b != 0) {
1 ignored issue
show
Best Practice introduced by
Comparing b to 0 using the != operator is not safe. Consider using !== instead.
Loading history...
968
			r = a/b;
969
			r = bAbs*Math.sqrt(1+r*r);
970
		} else {
971
			r = 0.0;
972
		}
973
		return r;
974
	},
975
	apow: function(pow, value){
976
		// input:	1,2,4,8,16,32,64
977
		// output:	1,2,3,4,5,6,7
978
		if (value == 1 || value == -1) return value;
1 ignored issue
show
Best Practice introduced by
Comparing value to 1 using the == operator is not safe. Consider using === instead.
Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
979
		var count = 0;
980
		while (value != 0) {
981
			//remainder = value % pow;
982
			value = value / pow;
983
			count++;
984
		}
985
		return count;
986
	},
987
	invert: function(value){
988
		// input:	0.1		0.5		1 	2 		8
989
		// output:	10		2		1	0.5		0.2
990
		return 1 / value;
991
	},
992
	powAbs: function(x, exponent){
993
	// This function always returns a positive value that conforms to the expected Exponent curve.
994
	// Math.pow: If x is negative, and exponent is not an integer, returns NaN.
995
		if( x >= 0 ) {
996
			return Math.pow( x, exponent );
997
		} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
998
			return Math.pow(-x, 1/exponent);
999
		}
1000
	},
1001
	
1002
	// count digits in number
1003
	getWhole: function(val){
1004
		
1005
		// -10.235 has 2 whole digits
1006
		return int(val);
1007
	},
1008
	setWhole: function(val, newval){
1009
		return newval + GetFractional(val);
1010
	},
1011
	getFractional: function(val){
1012
		
1013
		// -10.235 has 3 fractional digits
1014
		var s = (val < 0 ? -val : val).toString();
1015
		var i = s.indexOf(".");
1016
		return (i == -1) ? int(val) : int(s.substr(i + 1));
1017
	},
1018
	setFractional: function(val, fraction){
1019
		return Number((parseInt(val).toString()) + "." + fraction.toString());
1020
	},
1021
	
1022
	
1023
	
1024
	// distance between 2 lat/lon coords
1025
	latlonDistance: function(latitude1, longitude1, latitude2, longitude2){
1026
		
1027
		// all lat/lons are floating point numbers in degrees
1028
		
1029
		var theta = longitude1 - longitude2;
1030
		var miles = (Math.sin(latitude1*UB.toRadians)*Math.sin(latitude2*UB.toRadians)) + (Math.cos(latitude1*UB.toRadians)*Math.cos(latitude2*UB.toRadians)*Math.cos(theta*UB.toRadians));
1031
		miles = Math.acos(miles);
1032
		miles = (miles * UB.toDegrees);
1033
		
1034
		miles = miles * 60 * 1.1515;
1035
		var feet = miles*5280;
1036
		var yards = feet / 3;
1037
		var kilometers = miles*1.609344;
1038
		var meters = kilometers*1000;
1039
		return [miles, feet, yards, kilometers, meters];
1040
	},
1041
	
1042
	
1043
	
1044
	// SLOT INDEX
1045
	minIndex: function(index1, index2){
1046
		if (index1 <= -1) {
1047
			return index2;
1048
		}
1049
		if (index2 <= -1) {
1050
			return index1;
1051
		}
1052
		if (index1 < index2) {
1053
			return index1;
1054
		} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1055
			return index2;
1056
		}
1057
	},
1058
	maxIndex: function(index1, index2){
1059
		if (index1 <= -1) {
1060
			return index2;
1061
		}
1062
		if (index2 <= -1) {
1063
			return index1;
1064
		}
1065
		if (index1 > index2) {
1066
			return index1;
1067
		} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
1068
			return index2;
1069
		}
1070
	},
1071
	
1072
	
1073
	
1074
	none:null
1075
};
1076
1077
// register funcs
1078
UB.registerFuncs(Number.prototype, numberFuncs);
1079
1080