/
joolaio.string.js
151 lines (127 loc) · 4.23 KB
/
joolaio.string.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
/**
* joola.io
*
* Copyright Joola Smart Solutions, Ltd. <info@joo.la>
*
* Licensed under GNU General Public License 3.0 or later.
* Some rights reserved. See LICENSE, AUTHORS.
*
* @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
*/
joolaio.provide('joolaio.string');
/**
* Repeats a string n times.
* @param {string} string The string to repeat.
* @param {number} length The number of times to repeat.
* @return {string} A string containing {@code length} repetitions of
* {@code string}.
*/
joolaio.string.repeat = function (string, length) {
return new Array(length + 1).join(string);
};
/**
* Pads number to given length and optionally rounds it to a given precision.
* For example:
* <pre>padNumber(1.25, 2, 3) -> '01.250'
* padNumber(1.25, 2) -> '01.25'
* padNumber(1.25, 2, 1) -> '01.3'
* padNumber(1.25, 0) -> '1.25'</pre>
*
* @param {number} num The number to pad.
* @param {number} length The desired length.
* @param {number=} opt_precision The desired precision.
* @param {string=} opt_symbol The symbol to use for padding, default is 0.
* @return {string} {@code num} as a string with the given options.
*/
joolaio.string.padNumber = function (num, length, opt_precision, opt_padsymbol) {
var s = joolaio.isDef(opt_precision) ? num.toFixed(opt_precision) : String(num);
var index = s.indexOf('.');
if (index == -1) {
index = s.length;
}
if (opt_padsymbol)
return joolaio.string.repeat(opt_padsymbol, Math.max(0, length - index)) + s;
else
return joolaio.string.repeat('0', Math.max(0, length - index)) + s;
};
joolaio.string.formatNumber = function (number, decimalplaces, commas) {
number = parseFloat(number);
if (decimalplaces > -1) {
number = joolaio.string.roundNumber(number, decimalplaces);
}
if (commas == true) {
number += '';
x = number.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
number = x1 + x2;
}
return number;
};
joolaio.string.roundNumber = function (number, decimal_points) {
if (!decimal_points) return Math.round(number);
if (number == 0) {
var decimals = "";
for (var i = 0; i < decimal_points; i++) decimals += "0";
return "0." + decimals;
}
if (number > -1 && number < 1) {
return number.toFixed(2);
}
var exponent = Math.pow(10, decimal_points);
var num = Math.round((number * exponent)).toString();
num = num.slice(0, -1 * decimal_points) + "." + num.slice(-1 * decimal_points);
if (num < 1 && num > 0)
num = '0' + num;
else if (num > -1 && num < 0)
num = '-0' + num.replace('-', '');
return num;
};
joolaio.string.shortenNumber = function (n) {
if ('number' !== typeof n) n = Number(n);
var sgn = n < 0 ? '-' : ''
, suffixes = ['k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
, overflow = Math.pow(10, suffixes.length * 3 + 3)
, suffix, digits;
n = Math.abs(Math.round(n));
if (n < 1000) return sgn + n;
if (n >= 1e100) return sgn + 'many';
if (n >= overflow) return (sgn + n).replace(/(\.\d*)?e\+?/i, 'e'); // 1e24
do {
n = Math.floor(n);
suffix = suffixes.shift();
digits = n % 1e6;
n = n / 1000;
if (n >= 1000) continue; // 1M onwards: get them in the next iteration
/*if (n >= 10 && n < 1000 // 10k ... 999k
|| (n < 10 && (digits % 1000) < 100) // Xk (X000 ... X099)
)
return sgn + Math.floor(n) + suffix;*/
return (sgn + n).replace(/(\.\d\d).*/, '$1') + suffix; // #.#k
} while (suffixes.length)
return sgn + 'many';
}
joolaio.string.intToTime = function (TimeInSeconds) {
var negative = false;
if (TimeInSeconds < 0) {
negative = true;
TimeInSeconds = Math.abs(TimeInSeconds);
}
var sHours = (Math.round(((TimeInSeconds / 60.0) / 60) - 0.5, 0)).toString();
var sMinutes = (Math.round((TimeInSeconds / 60.0) - 0.5, 0) % 60).toString();
var sSeconds = parseInt((TimeInSeconds % 60)).toString();
if (sHours.length == 1)
sHours = '0' + sHours;
if (sMinutes.length == 1)
sMinutes = '0' + sMinutes;
if (sSeconds.length == 1)
sSeconds = '0' + sSeconds;
if (!negative)
return sHours + ':' + sMinutes + ':' + sSeconds;
else
return '-' + sHours + ':' + sMinutes + ':' + sSeconds;
}