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