@webalternatif/js-core 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -3
- package/.babelrc +0 -12
- package/.idea/inspectionProfiles/Project_Default.xml +0 -12
- package/.idea/js-core.iml +0 -8
- package/.idea/modules.xml +0 -8
- package/.idea/php.xml +0 -20
- package/.idea/vcs.xml +0 -6
- package/i18n/agenda/en.js +0 -73
- package/i18n/agenda/fr.js +0 -73
- package/i18n/agenda/index.js +0 -2
- package/i18n/ajaxform/en.js +0 -5
- package/i18n/ajaxform/fr.js +0 -5
- package/i18n/ajaxform/index.js +0 -2
- package/i18n/ajaxupload/en.js +0 -12
- package/i18n/ajaxupload/fr.js +0 -12
- package/i18n/ajaxupload/index.js +0 -2
- package/i18n/autocomplete/en.js +0 -3
- package/i18n/autocomplete/fr.js +0 -3
- package/i18n/autocomplete/index.js +0 -2
- package/i18n/confirm/en.js +0 -5
- package/i18n/confirm/fr.js +0 -5
- package/i18n/confirm/index.js +0 -2
- package/i18n/core/en.js +0 -4
- package/i18n/core/fr.js +0 -4
- package/i18n/core/index.js +0 -2
- package/i18n/datagrid/en.js +0 -8
- package/i18n/datagrid/fr.js +0 -8
- package/i18n/datagrid/index.js +0 -2
- package/i18n/date/en.js +0 -51
- package/i18n/date/fr.js +0 -51
- package/i18n/date/index.js +0 -2
- package/i18n/datetimepicker/en.js +0 -30
- package/i18n/datetimepicker/fr.js +0 -30
- package/i18n/datetimepicker/index.js +0 -2
- package/i18n/dialog/en.js +0 -3
- package/i18n/dialog/fr.js +0 -3
- package/i18n/dialog/index.js +0 -2
- package/i18n/fulldayeventagenda/en.js +0 -73
- package/i18n/fulldayeventagenda/fr.js +0 -73
- package/i18n/fulldayeventagenda/index.js +0 -2
- package/i18n/index.d.ts +0 -4
- package/i18n/index.js +0 -15
- package/i18n/richtexteditor/en.js +0 -58
- package/i18n/richtexteditor/fr.js +0 -58
- package/i18n/richtexteditor/index.js +0 -2
- package/i18n/select/en.js +0 -3
- package/i18n/select/fr.js +0 -3
- package/i18n/select/index.js +0 -2
- package/i18n/timepicker/en.js +0 -3
- package/i18n/timepicker/fr.js +0 -3
- package/i18n/timepicker/index.js +0 -2
- package/i18n/useragenda/en.js +0 -74
- package/i18n/useragenda/fr.js +0 -73
- package/i18n/useragenda/index.js +0 -2
- package/jest.config.js +0 -14
- package/src/array.js +0 -124
- package/src/dom.js +0 -569
- package/src/eventDispatcher.js +0 -118
- package/src/i18n.js +0 -55
- package/src/index.js +0 -33
- package/src/is.js +0 -89
- package/src/math.js +0 -109
- package/src/random.js +0 -40
- package/src/string.js +0 -576
- package/src/stringPrototype.js +0 -15
- package/src/traversal.js +0 -134
- package/src/utils.js +0 -130
- package/tests/array.test.js +0 -326
- package/tests/dom.test.js +0 -239
- package/tests/eventdispatcher.test.js +0 -177
- package/tests/i18n.test.js +0 -132
- package/tests/index.test.js +0 -29
- package/tests/is.test.js +0 -354
- package/tests/math.test.js +0 -221
- package/tests/random.test.js +0 -72
- package/tests/string.test.js +0 -1106
- package/tests/traversal.test.js +0 -517
- package/tests/utils.test.js +0 -371
- package/tsconfig.json +0 -16
- package/webpack.config.cjs +0 -31
package/src/string.js
DELETED
|
@@ -1,576 +0,0 @@
|
|
|
1
|
-
import {isArray, isFloat, isInteger, isObject, isPlainObject, isString, isUndefined} from "./is.js";
|
|
2
|
-
import {dec2hex, hex2dec, round} from "./math.js";
|
|
3
|
-
import {inArray} from "./array.js";
|
|
4
|
-
import {each, foreach, map} from "./traversal.js";
|
|
5
|
-
import {translate} from "./i18n.js";
|
|
6
|
-
|
|
7
|
-
export const trim = function (str, char = '\\s') {
|
|
8
|
-
return ltrim(rtrim(str, char), char);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const ltrim = function (str, char = '\\s') {
|
|
12
|
-
return str.replace(new RegExp(`^${char}+`, 'g'), '');
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const rtrim = function (str, char = '\\s') {
|
|
16
|
-
return str.replace(new RegExp(`${char}+$`, 'g'), '');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const stripMultipleSpaces = function(str) {
|
|
20
|
-
return str.trim().replace(/ +/g, ' ');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const noAccent = function(str) {
|
|
24
|
-
return str
|
|
25
|
-
.replace(/[àäâ]/g, 'a')
|
|
26
|
-
.replace(/[èéêë]/g, 'e')
|
|
27
|
-
.replace(/[îïí]/g, 'i')
|
|
28
|
-
.replace(/[öô]/g, 'o')
|
|
29
|
-
.replace(/[üù]/g, 'u')
|
|
30
|
-
.replace(/ç/g, 'c')
|
|
31
|
-
.replace(/ÿ/g, 'y')
|
|
32
|
-
.replace(/[ÀÄÂ]/g, 'A')
|
|
33
|
-
.replace(/[ÈÉÊË]/g, 'E')
|
|
34
|
-
.replace(/[ÎÏÍ]/g, 'I')
|
|
35
|
-
.replace(/[ÖÔ]/g, 'O')
|
|
36
|
-
.replace(/[ÜÙ]/g, 'U')
|
|
37
|
-
.replace(/Ç/g, 'C')
|
|
38
|
-
.replace(/Ÿ/g, 'Y');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export const br2nl = function(str) {
|
|
42
|
-
return str.split(/<br\s*\/*>/).join('\n');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export const nl2br = function(str) {
|
|
46
|
-
return str.split('\n').join('<br>');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const ucfirst = function(str) {
|
|
50
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export const lcfirst = function(str) {
|
|
54
|
-
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export const insertTag = function(str, tag, position = 0, length = 0) {
|
|
58
|
-
let startTag = `<${tag}>`
|
|
59
|
-
let endTag = `</${tag}>`
|
|
60
|
-
|
|
61
|
-
if (['br', 'hr', 'img', 'link', 'input'].includes(tag)) {
|
|
62
|
-
startTag = `<${tag}/>`;
|
|
63
|
-
endTag = '';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return str.slice(0, position) + startTag + str.slice(position, position + length) + endTag + str.slice(position + length);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export const substringIndex = function(str, delimiter, index)
|
|
70
|
-
{
|
|
71
|
-
let input = str + '',
|
|
72
|
-
arr = input.split(delimiter);
|
|
73
|
-
|
|
74
|
-
if (index > 0) {
|
|
75
|
-
arr.splice(index, arr.length - index);
|
|
76
|
-
} else if (index < 0) {
|
|
77
|
-
arr.splice(0, arr.length + index);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return arr.join(delimiter);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export const insert = (str, ins, n) => {
|
|
84
|
-
if (n >= str.length) {
|
|
85
|
-
return str;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return [...str].reduce((newStr, char, index) => {
|
|
89
|
-
if (index > 0 && index % n === 0) {
|
|
90
|
-
return newStr + ins + char;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return newStr + char;
|
|
94
|
-
}, '');
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export const reverse = function(str) {
|
|
98
|
-
let res = [];
|
|
99
|
-
|
|
100
|
-
for (let i = 0; i < str.length; i++) {
|
|
101
|
-
res.unshift(str[i]);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return res.join('');
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export const thousandSeparator = function(value, separator = '.', pointDecimal = '.') {
|
|
108
|
-
if (isUndefined(value) || null === value) {
|
|
109
|
-
return value;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
value = (value + '').replace(',', '.');
|
|
113
|
-
|
|
114
|
-
if (Math.abs(value) >= 1000) {
|
|
115
|
-
let intval = Math[value >= 1000 ? 'floor' : 'ceil'](value) + '';
|
|
116
|
-
let newval = reverse(insert(reverse(intval), reverse(separator), 3));
|
|
117
|
-
|
|
118
|
-
return value.indexOf('.') > 0 ? newval + pointDecimal + substringIndex(value, '.' ,-1) : newval;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return (value + '').replace('.', pointDecimal);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export const numberFormat = function(number, decimals = 2, forceCentimes = false, thousandSep = '', pointDecimal = '.') {
|
|
125
|
-
number = number ? number + '' : '0';
|
|
126
|
-
number = round(parseFloat(number.replace(',', '.')), decimals) + '';
|
|
127
|
-
|
|
128
|
-
if (decimals === 0) {
|
|
129
|
-
return thousandSeparator(number, thousandSep, pointDecimal);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const pos = number.lastIndexOf('.');
|
|
133
|
-
|
|
134
|
-
if (-1 === pos) {
|
|
135
|
-
if (true === forceCentimes) {
|
|
136
|
-
number += pointDecimal + repeat('0', decimals);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return thousandSeparator(number, thousandSep, pointDecimal);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const digits = number.slice(pos + 1);
|
|
143
|
-
const nbDigits = digits.length;
|
|
144
|
-
if (decimals > nbDigits) {
|
|
145
|
-
return thousandSeparator(number + '0'.repeat(decimals - nbDigits), thousandSep, pointDecimal);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return thousandSeparator(number.slice(0, pos + 1 + decimals), thousandSep, pointDecimal);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export const toPrice = numberFormat;
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Pads a string to a specified length with a specified string and padding type
|
|
155
|
-
*
|
|
156
|
-
* @param {string} str
|
|
157
|
-
* @param {number} pad_length
|
|
158
|
-
* @param {string} [pad_str]
|
|
159
|
-
* @param {string} [pad_type]
|
|
160
|
-
* @returns {string}
|
|
161
|
-
*/
|
|
162
|
-
export const pad = function(str, pad_length, pad_str = ' ', pad_type = 'left') {
|
|
163
|
-
if (isUndefined(pad_length) || str.length >= pad_length || !inArray(pad_type, ['left', 'right'])) {
|
|
164
|
-
return str;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (pad_type === 'left') {
|
|
168
|
-
return pad_str.repeat(Math.ceil(pad_length / pad_str.length)).slice(0, pad_length - str.length) + str;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return str + pad_str.repeat(Math.ceil(pad_length / pad_str.length)).slice(0, pad_length - str.length);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Converts RGB values to a hexadecimal color string.
|
|
176
|
-
*
|
|
177
|
-
* @param {number|number[]} r - Either an array representing a RGB color (e.g. [255, 0, 0]) or the red component as a
|
|
178
|
-
* number (0-255).
|
|
179
|
-
* @param {number} [g] - The green component (0-255). Required if `r` is a number.
|
|
180
|
-
* @param {number} [b] - The blue component (0-255). Required if `r` is a number.
|
|
181
|
-
* @returns {string} The hexadecimal color string (e.g., 'FF0000').
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* // Using separate RGB components
|
|
185
|
-
* rgb2hex(255, 0, 0); // Returns 'FF0000'
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
* // Using an array of RGB components
|
|
189
|
-
* rgb2hex([255, 0, 0]); // Returns 'FF0000'
|
|
190
|
-
*/
|
|
191
|
-
export const rgb2hex = function(r, g, b) {
|
|
192
|
-
if (isArray(r)) {
|
|
193
|
-
return rgb2hex(...r);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (!isInteger(r) || !isInteger(g) || !isInteger(b)) return '';
|
|
197
|
-
|
|
198
|
-
return [
|
|
199
|
-
pad(dec2hex(parseInt(r)), 2, '0').toUpperCase(),
|
|
200
|
-
pad(dec2hex(parseInt(g)), 2, '0').toUpperCase(),
|
|
201
|
-
pad(dec2hex(parseInt(b)), 2, '0').toUpperCase(),
|
|
202
|
-
].join('')
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export const rgbtohex = rgb2hex
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Converts a hexadecimal color to RGB values.
|
|
209
|
-
*
|
|
210
|
-
* @param {string} hex - The hexadecimal value (e.g #FF0000)
|
|
211
|
-
* @returns {number[]} The RGB color array (e.g [255, 0, 0]).
|
|
212
|
-
*/
|
|
213
|
-
export const hex2rgb = function(hex) {
|
|
214
|
-
if (!isString(hex) || !hex.length) return [];
|
|
215
|
-
|
|
216
|
-
hex = hex.slice(-6).toUpperCase();
|
|
217
|
-
|
|
218
|
-
if (hex.length < 6) {
|
|
219
|
-
hex = map(hex.slice(-3), (i, h) => h + '' + h).join('');
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
for (let i = 0; i < hex.length; i++) {
|
|
223
|
-
if (-1 === '0123456789ABCDEF'.indexOf(hex[i])) {
|
|
224
|
-
return [];
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return map(insert(hex, ',', 2).split(','), (i, h) => hex2dec(h))
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export const hextorgb = hex2rgb
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Parses a URL string into its components.
|
|
235
|
-
*
|
|
236
|
-
* @param {string} str - The URL string to be parsed.
|
|
237
|
-
* @returns {Object} An object containing the parsed components of the URL.
|
|
238
|
-
*
|
|
239
|
-
* @property {string} [scheme] - The scheme (protocol) of the URL (e.g., `http`, `https`).
|
|
240
|
-
* @property {string} [authority] - The authority part of the URL.
|
|
241
|
-
* @property {string} [userInfo] - The user information (e.g., `user:pass`).
|
|
242
|
-
* @property {string} [user] - The username from the user information.
|
|
243
|
-
* @property {string} [pass] - The password from the user information.
|
|
244
|
-
* @property {string} [host] - The host of the URL (e.g., `example.com`).
|
|
245
|
-
* @property {string} [port] - The port of the URL (e.g., `8080`).
|
|
246
|
-
* @property {string} [relative] - The relative URL.
|
|
247
|
-
* @property {string} [path] - The path of the URL (e.g., `/path/to/resource`).
|
|
248
|
-
* @property {string} [directory] - The directory of the URL path.
|
|
249
|
-
* @property {string} [file] - The file name from the path, if applicable.
|
|
250
|
-
* @property {string} [query] - The query string (e.g., `key=value&key2=value2`).
|
|
251
|
-
* @property {string} [fragment] - The fragment (hash) of the URL (e.g., `#section`).
|
|
252
|
-
*/
|
|
253
|
-
export const parse_url = function(str) {
|
|
254
|
-
const
|
|
255
|
-
key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'fragment'],
|
|
256
|
-
parser = /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/;
|
|
257
|
-
|
|
258
|
-
const m = parser.exec(str);
|
|
259
|
-
let uri = {}, i = 14;
|
|
260
|
-
|
|
261
|
-
while (i--) {
|
|
262
|
-
if (m[i]) {
|
|
263
|
-
uri[key[i]] = m[i];
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
delete uri.source;
|
|
268
|
-
return uri;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Adds or updates one or more query parameters to a given URL.
|
|
273
|
-
*
|
|
274
|
-
* @param {string} url - The URL to which the parameters should be added.
|
|
275
|
-
* @param {string|Object} param - The key of the parameter to add, or an object containing multiple key-value pairs to add.
|
|
276
|
-
* @param {string|null} [value=null] - The value of the parameter to add. Ignored if `param` is an object.
|
|
277
|
-
* @returns {string} The updated URL with the new query parameters.
|
|
278
|
-
*
|
|
279
|
-
* @example <caption>Add a single parameter to a URL without a query string</caption>
|
|
280
|
-
* addUrlParam('https://example.com', 'key', 'value');
|
|
281
|
-
* // Returns: 'https://example.com?key=value'
|
|
282
|
-
*
|
|
283
|
-
* @example <caption>Add multiple parameters to a URL</caption>
|
|
284
|
-
* addUrlParam('https://example.com', { key1: 'value1', key2: 'value2' });
|
|
285
|
-
* // Returns: 'https://example.com?key1=value1&key2=value2'
|
|
286
|
-
*/
|
|
287
|
-
export const addUrlParam = function(url, param, value = null) {
|
|
288
|
-
if (isPlainObject(param)) {
|
|
289
|
-
each(param, (key, val) => {
|
|
290
|
-
url = addUrlParam(url, key, val);
|
|
291
|
-
});
|
|
292
|
-
} else {
|
|
293
|
-
let parseUrl = parse_url(url), pos, hash = "";
|
|
294
|
-
|
|
295
|
-
if ((pos = url.indexOf("#")) > -1) {
|
|
296
|
-
hash = url.slice(pos);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (hash) {
|
|
300
|
-
url = url.slice(0, url.lastIndexOf(hash));
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
if (!parseUrl.query) {
|
|
304
|
-
url += "?" + param + "=" + value + hash;
|
|
305
|
-
} else {
|
|
306
|
-
const params = parseUrl.query.split('&');
|
|
307
|
-
let param_exists = false;
|
|
308
|
-
|
|
309
|
-
for (let i = 0; i < params.length; i++) {
|
|
310
|
-
if (params[i].indexOf(param + "=") > -1) {
|
|
311
|
-
param_exists = true;
|
|
312
|
-
params[i] = param + "=" + value;
|
|
313
|
-
break;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (false === param_exists) {
|
|
318
|
-
params.push(param + "=" + value);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (parseUrl.scheme && parseUrl.host) {
|
|
322
|
-
url = parseUrl.scheme + '://' + parseUrl.host + (parseUrl.path || '') + "?" + params.join("&") + hash;
|
|
323
|
-
} else {
|
|
324
|
-
url = (parseUrl.host || '') + parseUrl.path + "?" + params.join("&") + hash;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return url;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
export const decodeHtml = function(str) {
|
|
333
|
-
if (!isString(str))
|
|
334
|
-
return '';
|
|
335
|
-
|
|
336
|
-
return str
|
|
337
|
-
.replace(/&/g, "&")
|
|
338
|
-
.replace(/</g, "<")
|
|
339
|
-
.replace(/>/g, ">")
|
|
340
|
-
.replace(/"/g, '"')
|
|
341
|
-
.replace(/'/g, "'");
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
export const htmlquotes = function(str) {
|
|
345
|
-
if (!isString(str))
|
|
346
|
-
return '';
|
|
347
|
-
|
|
348
|
-
return str
|
|
349
|
-
.replace(/"/g, """)
|
|
350
|
-
.replace(/'/g, "'");
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
export const htmlsimplequotes = function(str) {
|
|
354
|
-
if (!isString(str))
|
|
355
|
-
return '';
|
|
356
|
-
|
|
357
|
-
return str.replace(/'/g, "'");
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
export const repeat = function(str, n) {
|
|
361
|
-
if (!isString(str) || !isFloat(n))
|
|
362
|
-
return '';
|
|
363
|
-
|
|
364
|
-
return new Array(Math.floor(n) + 1).join(str);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
export const stripTags = function(str, tag) {
|
|
368
|
-
if (isString(tag)) {
|
|
369
|
-
const rStripTags = new RegExp(`<${tag}[^>]*>(.*?)</${tag}>|<${tag}[^>]*\/>`, 'ig');
|
|
370
|
-
|
|
371
|
-
while (rStripTags.test(str))
|
|
372
|
-
str = str.replace(rStripTags, '$1');
|
|
373
|
-
|
|
374
|
-
return str;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
return str.replace(/(<([^>]+)>)/ig,"");
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
export const toUrl = function(str) {
|
|
381
|
-
return trim(noAccent(str).toLowerCase()
|
|
382
|
-
.replace(/[^a-z0-9]/g,'-')
|
|
383
|
-
.replace(/-{2,}/g,'-'),
|
|
384
|
-
'-')
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* @see http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
|
|
389
|
-
*/
|
|
390
|
-
export const escapeRegex = function(str) {
|
|
391
|
-
return str
|
|
392
|
-
.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
|
|
393
|
-
.replace(/[\n\t]/g, " ");
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
export const camelCase = function(str) {
|
|
397
|
-
foreach('-_', (char) => {
|
|
398
|
-
str = trim(str, char).replace(new RegExp(`${char}+`, 'g'), char);
|
|
399
|
-
})
|
|
400
|
-
|
|
401
|
-
str = trim(str).replace(/\s+/g, '-');
|
|
402
|
-
|
|
403
|
-
return str.toLowerCase().replace(/[_-](\w)/g, (match, p1) => p1.toUpperCase());
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
export const format = function(str, ...args) {
|
|
407
|
-
if (args.length) {
|
|
408
|
-
each(args, (i, arg) => {
|
|
409
|
-
if (isString(arg)) {
|
|
410
|
-
const o = {};
|
|
411
|
-
o[i] = arg;
|
|
412
|
-
arg = o;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
each(arg, (placeholder, replacement) => {
|
|
416
|
-
str = str.replace(new RegExp('\\{' + placeholder + '\\}', 'gm'), match =>
|
|
417
|
-
isUndefined(replacement) ? match : replacement)
|
|
418
|
-
})
|
|
419
|
-
})
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return str;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
export const f = format
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* @see https://stackoverflow.com/questions/7627000/javascript-convert-string-to-safe-class-name-for-css
|
|
429
|
-
*/
|
|
430
|
-
export const toCssClassName = function(str) {
|
|
431
|
-
return str.replace(/[^a-z0-9_-]/ig, s => {
|
|
432
|
-
const c = s.charCodeAt(0);
|
|
433
|
-
if (c === 32) return '-';
|
|
434
|
-
return '__' + ('000' + c.toString(16)).slice(-4);
|
|
435
|
-
})
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
export const hilite = function(str, req, tag = 'strong')
|
|
439
|
-
{
|
|
440
|
-
str = decodeHtml(str);
|
|
441
|
-
let str_folded = noAccent(str).toLowerCase().replace(/[\[\]]+/g, '');
|
|
442
|
-
let q_folded, re, hilite_hints = '';
|
|
443
|
-
|
|
444
|
-
if (!isArray(req)) {
|
|
445
|
-
req = [req];
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
each(req, (i, q) => {
|
|
449
|
-
if (q.length) {
|
|
450
|
-
q = decodeHtml(q);
|
|
451
|
-
q_folded = noAccent(q).toLowerCase().replace(/[\[\]]+/g, '');
|
|
452
|
-
|
|
453
|
-
re = new RegExp(escapeRegex(q_folded), 'g');
|
|
454
|
-
hilite_hints = str_folded.replace(re, `[${q_folded}]`);
|
|
455
|
-
|
|
456
|
-
str_folded = hilite_hints;
|
|
457
|
-
}
|
|
458
|
-
})
|
|
459
|
-
|
|
460
|
-
if (!hilite_hints.length) {
|
|
461
|
-
return str;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
let spos = 0;
|
|
465
|
-
let highlighted = '';
|
|
466
|
-
let dirHook = 'end';
|
|
467
|
-
|
|
468
|
-
each(hilite_hints, (i, hint) => {
|
|
469
|
-
const c = str.charAt(spos);
|
|
470
|
-
|
|
471
|
-
if (hint === '[' && dirHook === 'end') {
|
|
472
|
-
highlighted += `<${tag}>`;
|
|
473
|
-
dirHook = 'start';
|
|
474
|
-
} else if (hint === ']' && dirHook === 'start') {
|
|
475
|
-
highlighted += `</${tag}>`;
|
|
476
|
-
dirHook = 'end';
|
|
477
|
-
} else {
|
|
478
|
-
spos += 1;
|
|
479
|
-
highlighted += c;
|
|
480
|
-
}
|
|
481
|
-
})
|
|
482
|
-
|
|
483
|
-
return highlighted
|
|
484
|
-
.replace(/</g, '<')
|
|
485
|
-
.replace(/>/g, '>')
|
|
486
|
-
.replace(new RegExp(`<${tag}>`, 'g'), `<${tag}>`)
|
|
487
|
-
.replace(new RegExp(`</${tag}>`, 'g'), `</${tag}>`)
|
|
488
|
-
.replace(new RegExp('<br>', 'g'), '<br>');
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
export const formatSize = function(bytes, decimalPoint = ',')
|
|
492
|
-
{
|
|
493
|
-
let i = -1, decimals = 0;
|
|
494
|
-
|
|
495
|
-
do {
|
|
496
|
-
bytes /= 1024;
|
|
497
|
-
i++;
|
|
498
|
-
} while (bytes > 999);
|
|
499
|
-
|
|
500
|
-
if (!isInteger(bytes)) {
|
|
501
|
-
decimals = 1;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
const units = map(['k', 'M', 'G', 'T', 'P', 'E'], (i, prefix) => {
|
|
505
|
-
return prefix + translate('unitByte');
|
|
506
|
-
})
|
|
507
|
-
|
|
508
|
-
return numberFormat(Math.max(bytes, 0), decimals, true, '', decimalPoint) + ' ' + units[i];
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
export const compareMixAlphaDigits = (a, b) => {
|
|
512
|
-
if (a === b) return 0;
|
|
513
|
-
|
|
514
|
-
if (isInteger(a) && isInteger(b)) {
|
|
515
|
-
return Math.sign(a - b);
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
let startEq = '';
|
|
519
|
-
|
|
520
|
-
for (let i = 0; i < Math.min(a.length, b.length); i++) {
|
|
521
|
-
if (a.charAt(i) === b.charAt(i) && !isInteger(a)) {
|
|
522
|
-
startEq += a.charAt(i);
|
|
523
|
-
} else {
|
|
524
|
-
break;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
a = a.slice(startEq.length);
|
|
529
|
-
b = b.slice(startEq.length);
|
|
530
|
-
|
|
531
|
-
let nbA = '';
|
|
532
|
-
let idxDigitA = null;
|
|
533
|
-
|
|
534
|
-
each(a, (i, c) => {
|
|
535
|
-
if (!nbA) {
|
|
536
|
-
idxDigitA = i;
|
|
537
|
-
if (c >= '0' && c <= '9') {
|
|
538
|
-
nbA += c;
|
|
539
|
-
}
|
|
540
|
-
} else {
|
|
541
|
-
if (c >= '0' && c <= '9') {
|
|
542
|
-
nbA += c;
|
|
543
|
-
return true;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
return false;
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
let nbB = '';
|
|
551
|
-
let idxDigitB = null;
|
|
552
|
-
|
|
553
|
-
each(b, (i, c) => {
|
|
554
|
-
if (!nbB) {
|
|
555
|
-
idxDigitB = i;
|
|
556
|
-
if (c >= '0' && c <= '9') {
|
|
557
|
-
nbB += c;
|
|
558
|
-
}
|
|
559
|
-
} else {
|
|
560
|
-
if (c >= '0' && c <= '9') {
|
|
561
|
-
nbB += c;
|
|
562
|
-
return true;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
return false;
|
|
566
|
-
}
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
if (nbA.length && nbB.length && idxDigitA === idxDigitB) {
|
|
570
|
-
if (a.substring(0, idxDigitA) === b.substring(0, idxDigitB)) {
|
|
571
|
-
return Math.sign(nbA - nbB);
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
return a > b ? 1 : -1;
|
|
576
|
-
}
|
package/src/stringPrototype.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {each} from "./traversal.js";
|
|
2
|
-
import * as stringFunctions from "./string.js";
|
|
3
|
-
|
|
4
|
-
each(Object.keys(stringFunctions), (i, name) => {
|
|
5
|
-
const f = stringFunctions[name], p = String.prototype;
|
|
6
|
-
|
|
7
|
-
const origSF = p[name];
|
|
8
|
-
p[name] = function (...args) {
|
|
9
|
-
if (origSF && args.length === origSF.length) {
|
|
10
|
-
return origSF.apply(this, args);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return f(this, ...args);
|
|
14
|
-
}
|
|
15
|
-
})
|