@webalternatif/js-core 1.1.0 → 1.1.2

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.
@@ -1,51 +1,36 @@
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') {
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
+ export const trim = function (str, char = '\\s') {
13
7
  return ltrim(rtrim(str, char), char);
14
8
  };
15
- exports.trim = trim;
16
- const ltrim = function (str, char = '\\s') {
9
+ export const ltrim = function (str, char = '\\s') {
17
10
  return str.replace(new RegExp(`^${char}+`, 'g'), '');
18
11
  };
19
- exports.ltrim = ltrim;
20
- const rtrim = function (str, char = '\\s') {
12
+ export const rtrim = function (str, char = '\\s') {
21
13
  return str.replace(new RegExp(`${char}+$`, 'g'), '');
22
14
  };
23
- exports.rtrim = rtrim;
24
- const stripMultipleSpaces = function (str) {
15
+ export const stripMultipleSpaces = function (str) {
25
16
  return str.trim().replace(/ +/g, ' ');
26
17
  };
27
- exports.stripMultipleSpaces = stripMultipleSpaces;
28
- const noAccent = function (str) {
18
+ export const noAccent = function (str) {
29
19
  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
20
  };
31
- exports.noAccent = noAccent;
32
- const br2nl = function (str) {
21
+ export const br2nl = function (str) {
33
22
  return str.split(/<br\s*\/*>/).join('\n');
34
23
  };
35
- exports.br2nl = br2nl;
36
- const nl2br = function (str) {
24
+ export const nl2br = function (str) {
37
25
  return str.split('\n').join('<br>');
38
26
  };
39
- exports.nl2br = nl2br;
40
- const ucfirst = function (str) {
27
+ export const ucfirst = function (str) {
41
28
  return str.charAt(0).toUpperCase() + str.slice(1);
42
29
  };
43
- exports.ucfirst = ucfirst;
44
- const lcfirst = function (str) {
30
+ export const lcfirst = function (str) {
45
31
  return str.charAt(0).toLowerCase() + str.slice(1);
46
32
  };
47
- exports.lcfirst = lcfirst;
48
- const insertTag = function (str, tag, position = 0, length = 0) {
33
+ export const insertTag = function (str, tag, position = 0, length = 0) {
49
34
  let startTag = `<${tag}>`;
50
35
  let endTag = `</${tag}>`;
51
36
  if (['br', 'hr', 'img', 'link', 'input'].includes(tag)) {
@@ -54,8 +39,7 @@ const insertTag = function (str, tag, position = 0, length = 0) {
54
39
  }
55
40
  return str.slice(0, position) + startTag + str.slice(position, position + length) + endTag + str.slice(position + length);
56
41
  };
57
- exports.insertTag = insertTag;
58
- const substringIndex = function (str, delimiter, index) {
42
+ export const substringIndex = function (str, delimiter, index) {
59
43
  let input = str + '',
60
44
  arr = input.split(delimiter);
61
45
  if (index > 0) {
@@ -65,8 +49,7 @@ const substringIndex = function (str, delimiter, index) {
65
49
  }
66
50
  return arr.join(delimiter);
67
51
  };
68
- exports.substringIndex = substringIndex;
69
- const insert = (str, ins, n) => {
52
+ export const insert = (str, ins, n) => {
70
53
  if (n >= str.length) {
71
54
  return str;
72
55
  }
@@ -77,17 +60,15 @@ const insert = (str, ins, n) => {
77
60
  return newStr + char;
78
61
  }, '');
79
62
  };
80
- exports.insert = insert;
81
- const reverse = function (str) {
63
+ export const reverse = function (str) {
82
64
  let res = [];
83
65
  for (let i = 0; i < str.length; i++) {
84
66
  res.unshift(str[i]);
85
67
  }
86
68
  return res.join('');
87
69
  };
88
- exports.reverse = reverse;
89
- const thousandSeparator = function (value, separator = '.', pointDecimal = '.') {
90
- if ((0, _is.isUndefined)(value) || null === value) {
70
+ export const thousandSeparator = function (value, separator = '.', pointDecimal = '.') {
71
+ if (isUndefined(value) || null === value) {
91
72
  return value;
92
73
  }
93
74
  value = (value + '').replace(',', '.');
@@ -98,10 +79,9 @@ const thousandSeparator = function (value, separator = '.', pointDecimal = '.')
98
79
  }
99
80
  return (value + '').replace('.', pointDecimal);
100
81
  };
101
- exports.thousandSeparator = thousandSeparator;
102
- const numberFormat = function (number, decimals = 2, forceCentimes = false, thousandSep = '', pointDecimal = '.') {
82
+ export const numberFormat = function (number, decimals = 2, forceCentimes = false, thousandSep = '', pointDecimal = '.') {
103
83
  number = number ? number + '' : '0';
104
- number = (0, _math.round)(parseFloat(number.replace(',', '.')), decimals) + '';
84
+ number = round(parseFloat(number.replace(',', '.')), decimals) + '';
105
85
  if (decimals === 0) {
106
86
  return thousandSeparator(number, thousandSep, pointDecimal);
107
87
  }
@@ -119,8 +99,7 @@ const numberFormat = function (number, decimals = 2, forceCentimes = false, thou
119
99
  }
120
100
  return thousandSeparator(number.slice(0, pos + 1 + decimals), thousandSep, pointDecimal);
121
101
  };
122
- exports.numberFormat = numberFormat;
123
- const toPrice = exports.toPrice = numberFormat;
102
+ export const toPrice = numberFormat;
124
103
 
125
104
  /**
126
105
  * Pads a string to a specified length with a specified string and padding type
@@ -131,8 +110,8 @@ const toPrice = exports.toPrice = numberFormat;
131
110
  * @param {string} [pad_type]
132
111
  * @returns {string}
133
112
  */
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'])) {
113
+ export const pad = function (str, pad_length, pad_str = ' ', pad_type = 'left') {
114
+ if (isUndefined(pad_length) || str.length >= pad_length || !inArray(pad_type, ['left', 'right'])) {
136
115
  return str;
137
116
  }
138
117
  if (pad_type === 'left') {
@@ -158,16 +137,14 @@ const pad = function (str, pad_length, pad_str = ' ', pad_type = 'left') {
158
137
  * // Using an array of RGB components
159
138
  * rgb2hex([255, 0, 0]); // Returns 'FF0000'
160
139
  */
161
- exports.pad = pad;
162
- const rgb2hex = function (r, g, b) {
163
- if ((0, _is.isArray)(r)) {
140
+ export const rgb2hex = function (r, g, b) {
141
+ if (isArray(r)) {
164
142
  return rgb2hex(...r);
165
143
  }
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('');
144
+ if (!isInteger(r) || !isInteger(g) || !isInteger(b)) return '';
145
+ return [pad(dec2hex(parseInt(r)), 2, '0').toUpperCase(), pad(dec2hex(parseInt(g)), 2, '0').toUpperCase(), pad(dec2hex(parseInt(b)), 2, '0').toUpperCase()].join('');
168
146
  };
169
- exports.rgb2hex = rgb2hex;
170
- const rgbtohex = exports.rgbtohex = rgb2hex;
147
+ export const rgbtohex = rgb2hex;
171
148
 
172
149
  /**
173
150
  * Converts a hexadecimal color to RGB values.
@@ -175,21 +152,20 @@ const rgbtohex = exports.rgbtohex = rgb2hex;
175
152
  * @param {string} hex - The hexadecimal value (e.g #FF0000)
176
153
  * @returns {number[]} The RGB color array (e.g [255, 0, 0]).
177
154
  */
178
- const hex2rgb = function (hex) {
179
- if (!(0, _is.isString)(hex) || !hex.length) return [];
155
+ export const hex2rgb = function (hex) {
156
+ if (!isString(hex) || !hex.length) return [];
180
157
  hex = hex.slice(-6).toUpperCase();
181
158
  if (hex.length < 6) {
182
- hex = (0, _traversal.map)(hex.slice(-3), (i, h) => h + '' + h).join('');
159
+ hex = map(hex.slice(-3), (i, h) => h + '' + h).join('');
183
160
  }
184
161
  for (let i = 0; i < hex.length; i++) {
185
162
  if (-1 === '0123456789ABCDEF'.indexOf(hex[i])) {
186
163
  return [];
187
164
  }
188
165
  }
189
- return (0, _traversal.map)(insert(hex, ',', 2).split(','), (i, h) => (0, _math.hex2dec)(h));
166
+ return map(insert(hex, ',', 2).split(','), (i, h) => hex2dec(h));
190
167
  };
191
- exports.hex2rgb = hex2rgb;
192
- const hextorgb = exports.hextorgb = hex2rgb;
168
+ export const hextorgb = hex2rgb;
193
169
 
194
170
  /**
195
171
  * Parses a URL string into its components.
@@ -211,7 +187,7 @@ const hextorgb = exports.hextorgb = hex2rgb;
211
187
  * @property {string} [query] - The query string (e.g., `key=value&key2=value2`).
212
188
  * @property {string} [fragment] - The fragment (hash) of the URL (e.g., `#section`).
213
189
  */
214
- const parse_url = function (str) {
190
+ export const parse_url = function (str) {
215
191
  const key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'fragment'],
216
192
  parser = /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/;
217
193
  const m = parser.exec(str);
@@ -242,10 +218,9 @@ const parse_url = function (str) {
242
218
  * addUrlParam('https://example.com', { key1: 'value1', key2: 'value2' });
243
219
  * // Returns: 'https://example.com?key1=value1&key2=value2'
244
220
  */
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) => {
221
+ export const addUrlParam = function (url, param, value = null) {
222
+ if (isPlainObject(param)) {
223
+ each(param, (key, val) => {
249
224
  url = addUrlParam(url, key, val);
250
225
  });
251
226
  } else {
@@ -282,95 +257,84 @@ const addUrlParam = function (url, param, value = null) {
282
257
  }
283
258
  return url;
284
259
  };
285
- exports.addUrlParam = addUrlParam;
286
- const decodeHtml = function (str) {
287
- if (!(0, _is.isString)(str)) return '';
260
+ export const decodeHtml = function (str) {
261
+ if (!isString(str)) return '';
288
262
  return str.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#039;/g, "'");
289
263
  };
290
- exports.decodeHtml = decodeHtml;
291
- const htmlquotes = function (str) {
292
- if (!(0, _is.isString)(str)) return '';
264
+ export const htmlquotes = function (str) {
265
+ if (!isString(str)) return '';
293
266
  return str.replace(/"/g, "&quot;").replace(/'/g, "&#039;");
294
267
  };
295
- exports.htmlquotes = htmlquotes;
296
- const htmlsimplequotes = function (str) {
297
- if (!(0, _is.isString)(str)) return '';
268
+ export const htmlsimplequotes = function (str) {
269
+ if (!isString(str)) return '';
298
270
  return str.replace(/'/g, "&#039;");
299
271
  };
300
- exports.htmlsimplequotes = htmlsimplequotes;
301
- const repeat = function (str, n) {
302
- if (!(0, _is.isString)(str) || !(0, _is.isFloat)(n)) return '';
272
+ export const repeat = function (str, n) {
273
+ if (!isString(str) || !isFloat(n)) return '';
303
274
  return new Array(Math.floor(n) + 1).join(str);
304
275
  };
305
- exports.repeat = repeat;
306
- const stripTags = function (str, tag) {
307
- if ((0, _is.isString)(tag)) {
276
+ export const stripTags = function (str, tag) {
277
+ if (isString(tag)) {
308
278
  const rStripTags = new RegExp(`<${tag}[^>]*>(.*?)</${tag}>|<${tag}[^>]*\/>`, 'ig');
309
279
  while (rStripTags.test(str)) str = str.replace(rStripTags, '$1');
310
280
  return str;
311
281
  }
312
282
  return str.replace(/(<([^>]+)>)/ig, "");
313
283
  };
314
- exports.stripTags = stripTags;
315
- const toUrl = function (str) {
284
+ export const toUrl = function (str) {
316
285
  return trim(noAccent(str).toLowerCase().replace(/[^a-z0-9]/g, '-').replace(/-{2,}/g, '-'), '-');
317
286
  };
318
287
 
319
288
  /**
320
289
  * @see http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
321
290
  */
322
- exports.toUrl = toUrl;
323
- const escapeRegex = function (str) {
291
+ export const escapeRegex = function (str) {
324
292
  return str.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&").replace(/[\n\t]/g, " ");
325
293
  };
326
- exports.escapeRegex = escapeRegex;
327
- const camelCase = function (str) {
328
- (0, _traversal.foreach)('-_', char => {
294
+ export const camelCase = function (str) {
295
+ foreach('-_', char => {
329
296
  str = trim(str, char).replace(new RegExp(`${char}+`, 'g'), char);
330
297
  });
331
298
  str = trim(str).replace(/\s+/g, '-');
332
299
  return str.toLowerCase().replace(/[_-](\w)/g, (match, p1) => p1.toUpperCase());
333
300
  };
334
- exports.camelCase = camelCase;
335
- const format = function (str, ...args) {
301
+ export const format = function (str, ...args) {
336
302
  if (args.length) {
337
- (0, _traversal.each)(args, (i, arg) => {
338
- if ((0, _is.isString)(arg)) {
303
+ each(args, (i, arg) => {
304
+ if (isString(arg)) {
339
305
  const o = {};
340
306
  o[i] = arg;
341
307
  arg = o;
342
308
  }
343
- (0, _traversal.each)(arg, (placeholder, replacement) => {
344
- str = str.replace(new RegExp('\\{' + placeholder + '\\}', 'gm'), match => (0, _is.isUndefined)(replacement) ? match : replacement);
309
+ each(arg, (placeholder, replacement) => {
310
+ str = str.replace(new RegExp('\\{' + placeholder + '\\}', 'gm'), match => isUndefined(replacement) ? match : replacement);
345
311
  });
346
312
  });
347
313
  }
348
314
  return str;
349
315
  };
350
- exports.format = format;
351
- const f = exports.f = format;
316
+ export const f = format;
352
317
 
353
318
  /**
354
319
  * @see https://stackoverflow.com/questions/7627000/javascript-convert-string-to-safe-class-name-for-css
355
320
  */
356
- const toCssClassName = function (str) {
321
+ export const toCssClassName = function (str) {
357
322
  return str.replace(/[^a-z0-9_-]/ig, s => {
358
323
  const c = s.charCodeAt(0);
359
324
  if (c === 32) return '-';
360
325
  return '__' + ('000' + c.toString(16)).slice(-4);
361
326
  });
362
327
  };
363
- exports.toCssClassName = toCssClassName;
364
- const hilite = function (str, req, tag = 'strong') {
328
+ export const hilite = function (str, req, tag = 'strong') {
365
329
  str = decodeHtml(str);
366
330
  let str_folded = noAccent(str).toLowerCase().replace(/[\[\]]+/g, '');
367
331
  let q_folded,
368
332
  re,
369
333
  hilite_hints = '';
370
- if (!(0, _is.isArray)(req)) {
334
+ if (!isArray(req)) {
371
335
  req = [req];
372
336
  }
373
- (0, _traversal.each)(req, (i, q) => {
337
+ each(req, (i, q) => {
374
338
  if (q.length) {
375
339
  q = decodeHtml(q);
376
340
  q_folded = noAccent(q).toLowerCase().replace(/[\[\]]+/g, '');
@@ -385,7 +349,7 @@ const hilite = function (str, req, tag = 'strong') {
385
349
  let spos = 0;
386
350
  let highlighted = '';
387
351
  let dirHook = 'end';
388
- (0, _traversal.each)(hilite_hints, (i, hint) => {
352
+ each(hilite_hints, (i, hint) => {
389
353
  const c = str.charAt(spos);
390
354
  if (hint === '[' && dirHook === 'end') {
391
355
  highlighted += `<${tag}>`;
@@ -400,31 +364,29 @@ const hilite = function (str, req, tag = 'strong') {
400
364
  });
401
365
  return highlighted.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(new RegExp(`&lt;${tag}&gt;`, 'g'), `<${tag}>`).replace(new RegExp(`&lt;/${tag}&gt;`, 'g'), `</${tag}>`).replace(new RegExp('&lt;br&gt;', 'g'), '<br>');
402
366
  };
403
- exports.hilite = hilite;
404
- const formatSize = function (bytes, decimalPoint = ',') {
367
+ export const formatSize = function (bytes, decimalPoint = ',') {
405
368
  let i = -1,
406
369
  decimals = 0;
407
370
  do {
408
371
  bytes /= 1024;
409
372
  i++;
410
373
  } while (bytes > 999);
411
- if (!(0, _is.isInteger)(bytes)) {
374
+ if (!isInteger(bytes)) {
412
375
  decimals = 1;
413
376
  }
414
- const units = (0, _traversal.map)(['k', 'M', 'G', 'T', 'P', 'E'], (i, prefix) => {
415
- return prefix + (0, _i18n.translate)('unitByte');
377
+ const units = map(['k', 'M', 'G', 'T', 'P', 'E'], (i, prefix) => {
378
+ return prefix + translate('unitByte');
416
379
  });
417
380
  return numberFormat(Math.max(bytes, 0), decimals, true, '', decimalPoint) + ' ' + units[i];
418
381
  };
419
- exports.formatSize = formatSize;
420
- const compareMixAlphaDigits = (a, b) => {
382
+ export const compareMixAlphaDigits = (a, b) => {
421
383
  if (a === b) return 0;
422
- if ((0, _is.isInteger)(a) && (0, _is.isInteger)(b)) {
384
+ if (isInteger(a) && isInteger(b)) {
423
385
  return Math.sign(a - b);
424
386
  }
425
387
  let startEq = '';
426
388
  for (let i = 0; i < Math.min(a.length, b.length); i++) {
427
- if (a.charAt(i) === b.charAt(i) && !(0, _is.isInteger)(a)) {
389
+ if (a.charAt(i) === b.charAt(i) && !isInteger(a)) {
428
390
  startEq += a.charAt(i);
429
391
  } else {
430
392
  break;
@@ -434,7 +396,7 @@ const compareMixAlphaDigits = (a, b) => {
434
396
  b = b.slice(startEq.length);
435
397
  let nbA = '';
436
398
  let idxDigitA = null;
437
- (0, _traversal.each)(a, (i, c) => {
399
+ each(a, (i, c) => {
438
400
  if (!nbA) {
439
401
  idxDigitA = i;
440
402
  if (c >= '0' && c <= '9') {
@@ -450,7 +412,7 @@ const compareMixAlphaDigits = (a, b) => {
450
412
  });
451
413
  let nbB = '';
452
414
  let idxDigitB = null;
453
- (0, _traversal.each)(b, (i, c) => {
415
+ each(b, (i, c) => {
454
416
  if (!nbB) {
455
417
  idxDigitB = i;
456
418
  if (c >= '0' && c <= '9') {
@@ -470,5 +432,4 @@ const compareMixAlphaDigits = (a, b) => {
470
432
  }
471
433
  }
472
434
  return a > b ? 1 : -1;
473
- };
474
- exports.compareMixAlphaDigits = compareMixAlphaDigits;
435
+ };
@@ -1,9 +1,6 @@
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) => {
1
+ import { each } from "./traversal.js";
2
+ import * as stringFunctions from "./string.js";
3
+ each(Object.keys(stringFunctions), (i, name) => {
7
4
  const f = stringFunctions[name],
8
5
  p = String.prototype;
9
6
  const origSF = p[name];
@@ -1,77 +1,142 @@
1
- "use strict";
1
+ import { isArray, isArrayLike, isBoolean, isObject, isPlainObject, isString, isUndefined } from "./is.js";
2
+ import { isWindow } from "./dom.js";
3
+ import { sizeOf } from "./utils.js";
2
4
 
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)) {
5
+ /**
6
+ * @template T
7
+ * @typedef {Array<T> | Set<T> | Map<any, T> | Object<string, T> | string} Collection
8
+ */
9
+
10
+ /**
11
+ * Iterates over Arrays, Strings, Maps, Sets and plain Objects.
12
+ *
13
+ * The callback receives:
14
+ * (keyOrIndex, value, o, index)
15
+ *
16
+ * If the callback returns `false`, the iteration stops.
17
+ *
18
+ * @template T
19
+ * @param {Collection<T>} o
20
+ * @param {(key: number|string, value: T, o: Collection<T>, index: number) => (void|boolean)} callback
21
+ * @param {any} [context] Optional "this" binding for the callback
22
+ * @returns {typeof o} Returns the original input
23
+ */
24
+ export const each = function (o, callback, context) {
25
+ if (isPlainObject(o)) {
20
26
  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)) {
27
+ for (let i in o) if (o.hasOwnProperty(i) && false === callback.call(context ?? o[i], i, o[i], o, ++index)) return;
28
+ } else if (isString(o)) {
23
29
  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;
30
+ for (let i = 0; i < arr.length; i++) if (false === callback.call(context ?? arr[i], i, arr[i], o, i)) return;
31
+ return o;
32
+ } else if (o instanceof Map) {
33
+ let index = 0;
34
+ for (const [key, value] of o.entries()) {
35
+ if (false === callback.call(context ?? value, key, value, o, index++)) return;
36
+ }
37
+ } else if (o instanceof Set) {
38
+ let index = 0;
39
+ for (const value of o.values()) {
40
+ if (false === callback.call(context ?? value, index, value, o, index)) return;
41
+ index++;
42
+ }
43
+ } else if (isArrayLike(o)) {
44
+ o = Array.from(o);
45
+ for (let i = 0; i < o.length; i++) if (false === callback.call(context || o[i], i, o[i], o, i)) return;
25
46
  }
26
47
  return o;
27
48
  };
28
- exports.each = each;
29
- const foreach = function (o, callback, context) {
49
+
50
+ /**
51
+ * Same as each except that key and value are reversed in callback
52
+ *
53
+ * The callback receives:
54
+ * (value, keyOrIndex, o, index)
55
+ *
56
+ * @template T
57
+ * @param {Collection<T>} o
58
+ * @param {(value: T, key: number|string, o: Collection<T>, index: number) => (void|boolean)} callback
59
+ * @param {any} [context] Optional "this" binding for the callback
60
+ * @returns {typeof o} Returns the original input
61
+ */
62
+ export const foreach = function (o, callback, context) {
30
63
  return each(o, (key, value, o, index) => callback.apply(context || value, [value, key, o, index]), context);
31
64
  };
32
- exports.foreach = foreach;
33
- const map = function (o, callback, context) {
65
+
66
+ /**
67
+ * Iterates over Arrays, Strings, Maps, Sets and plain Objects.
68
+ * Returns an array from the callback results.
69
+ * Values strictly equal to `null` are skipped.
70
+ * Values strictly equal to `false` stops the iteration.
71
+ *
72
+ * The callback receives:
73
+ * (keyOrIndex, value, o, index)
74
+ *
75
+ * @template T,R
76
+ * @param {Collection<T>} o
77
+ * @param {(key: number|string, value: T, o: Collection<T>, index: number) => (R|null|false)} callback
78
+ * @param {any} [context] Optional "this" binding for the callback
79
+ * @returns {Array} Returns the resulted array
80
+ */
81
+ export const map = function (o, callback, context) {
34
82
  let results = [];
35
83
  each(o, function (index, value, o, i) {
36
84
  const response = callback.call(context, index, value, o, i);
37
- if (response !== null) {
38
- results.push(response);
39
- }
85
+ if (false === response) return false;
86
+ if (null !== response) results.push(response);
40
87
  });
41
88
  return results;
42
89
  };
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) {
90
+
91
+ /**
92
+ * Reduces a collection to a single value
93
+ *
94
+ * The reducer receives:
95
+ * (accumulator, value, index, source)
96
+ *
97
+ * @template T,R
98
+ * @param {Collection<T>} o
99
+ * @param {(accumulator: R|T, value: T, key: any, index: number, o: Collection<T>) => R} callback
100
+ * @param {R} [initialValue] la valeur initiale
101
+ * @returns {R} Returns the accumulated value
102
+ */
103
+ export const reduce = function (o, callback, initialValue) {
104
+ const isInitialValueDefined = !isUndefined(initialValue);
105
+ if (!sizeOf(o) && !isInitialValueDefined) {
47
106
  throw new Error('Nothing to reduce and no initial value');
48
107
  }
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);
108
+ let accumulator = !isInitialValueDefined ? map(o, (key, v, o, i) => i === 0 ? v : null)[0] : initialValue;
109
+ each(o, (key, v, o, i) => {
110
+ if (i === 0 && !isInitialValueDefined) return;
111
+ accumulator = callback(accumulator, v, key, i, o);
55
112
  });
56
113
  return accumulator;
57
114
  };
58
- exports.reduce = reduce;
59
- const extend = function (...args) {
115
+
116
+ /**
117
+ * Creates a shallow or deep copy of one or more objects or arrays
118
+ * If the first argument is `true`, nested plain objects are merged recursively.
119
+ *
120
+ * @template T
121
+ * @param {...(boolean|T)} args
122
+ * @returns {T} A copy of the merged result
123
+ */
124
+ export const extend = function (...args) {
60
125
  let deep = false;
61
- if ((0, _is.isBoolean)(args[0])) {
126
+ if (isBoolean(args[0])) {
62
127
  deep = args.shift();
63
128
  }
64
- if (args.length < 2 || (0, _is.isUndefined)(args[0]) || null === args[0]) {
129
+ if (args.length < 2 || isUndefined(args[0]) || null === args[0]) {
65
130
  return args[0];
66
131
  }
67
132
  let dest = args[0];
68
- if (!(0, _is.isObject)(dest)) {
133
+ if (!isObject(dest)) {
69
134
  args[0] = dest = {};
70
135
  }
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])) {
136
+ foreach(args.slice(1), src => {
137
+ if (isObject(src)) {
138
+ for (const name in src) {
139
+ if (deep && isPlainObject(src[name])) {
75
140
  dest[name] = extend(true, {}, dest[name], src[name]);
76
141
  } else {
77
142
  dest[name] = src[name];
@@ -81,23 +146,39 @@ const extend = function (...args) {
81
146
  });
82
147
  return dest;
83
148
  };
84
- exports.extend = extend;
85
- const clone = function (o) {
86
- if (!(0, _is.isObject)(o) && !(0, _is.isArray)(o) || (0, _dom.isWindow)(o)) {
149
+
150
+ /**
151
+ * Creates a deep copy of an Object or Array
152
+ *
153
+ * @template T
154
+ * @param {T} o
155
+ * @returns {T} The copy of o
156
+ */
157
+ export const clone = function (o) {
158
+ if (!isObject(o) && !isArray(o) || isWindow(o)) {
87
159
  return o;
88
160
  }
89
- const c = (0, _is.isObject)(o) ? {} : [];
90
- each(o, (i, value) => {
91
- if ((0, _is.isObject)(value)) {
92
- c[i] = clone(value);
161
+ const c = isObject(o) ? {} : [];
162
+ each(o, (key, value) => {
163
+ if (isObject(value)) {
164
+ c[key] = clone(value);
93
165
  } else {
94
- c[i] = value;
166
+ c[key] = value;
95
167
  }
96
168
  });
97
169
  return c;
98
170
  };
99
- exports.clone = clone;
100
- const merge = function (first, second = [], ...args) {
171
+
172
+ /**
173
+ * Merge multiple collections into a single array
174
+ *
175
+ * @template T
176
+ * @param {Collection<T>} first
177
+ * @param {Collection<T>} [second]
178
+ * @param {...Collection<T>} args Remaining collections to merge
179
+ * @returns {Array<T>} the resulted merged array
180
+ */
181
+ export const merge = function (first, second = [], ...args) {
101
182
  const result = map(first, (i, elem) => elem);
102
183
  each(second, function (i, elem) {
103
184
  result.push(elem);
@@ -106,5 +187,4 @@ const merge = function (first, second = [], ...args) {
106
187
  return merge(result, ...args);
107
188
  }
108
189
  return result;
109
- };
110
- exports.merge = merge;
190
+ };