@zelgadis87/utils-core 5.3.4 → 5.3.5

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/.rollup/index.cjs CHANGED
@@ -447,9 +447,14 @@ function indexByWith(arr, keyGetter, valueMapper = v => v) {
447
447
  }
448
448
 
449
449
  function average(arr) {
450
+ if (arr.length === 0)
451
+ return null;
450
452
  const f = 1 / arr.length;
451
453
  return arr.reduce((tot, cur) => tot + (cur * f), 0);
452
454
  }
455
+ function averageBy(arr, getter) {
456
+ return average(arr.map(getter));
457
+ }
453
458
  function sum(arr) {
454
459
  return arr.reduce((tot, cur) => tot + cur, 0);
455
460
  }
@@ -458,7 +463,7 @@ function sumBy(arr, getter) {
458
463
  }
459
464
  function min(arr) {
460
465
  if (arr.length === 0)
461
- throw new Error('Cannot calculate value on empty array');
466
+ return null;
462
467
  return arr.reduce((min, cur) => cur < min ? cur : min);
463
468
  }
464
469
  function minBy(arr, getter) {
@@ -466,7 +471,7 @@ function minBy(arr, getter) {
466
471
  }
467
472
  function max(arr) {
468
473
  if (arr.length === 0)
469
- throw new Error('Cannot calculate value on empty array');
474
+ return null;
470
475
  return arr.reduce((max, cur) => cur > max ? cur : max);
471
476
  }
472
477
  function maxBy(arr, getter) {
@@ -621,9 +626,36 @@ function range(start, end) {
621
626
  let length = (end - start) + 1;
622
627
  return new Array(length).fill(1).map((_, i) => start + i);
623
628
  }
629
+ /**
630
+ * Creates an array of the specified length, where each element is filled with the given value.
631
+ * @param length - The length of the array to create. Must be a non-negative integer.
632
+ * @param value - The value to fill each element with.
633
+ * @returns A new array with all elements set to the given value.
634
+ * @throws {RangeError} If length is negative, not an integer, or NaN.
635
+ * @example
636
+ * ```ts
637
+ * fill(3, 'a'); // ['a', 'a', 'a']
638
+ * fill(0, 42); // []
639
+ * fill(5, null); // [null, null, null, null, null]
640
+ * ```
641
+ */
624
642
  function fill(length, value) {
643
+ if (!Number.isInteger(length) || length < 0)
644
+ throw new RangeError(`Length must be a non-negative integer. Got: ${length}`);
625
645
  return new Array(length).fill(value);
626
646
  }
647
+ /**
648
+ * Creates an array of the specified length, where each element is generated by the provided generator function.
649
+ * @param length - The length of the array to create. Must be a non-negative integer.
650
+ * @param generator - A function that takes an index and returns the value for that position.
651
+ * @returns A new array with elements generated by the generator function.
652
+ * @throws {RangeError} If length is negative or not an integer.
653
+ */
654
+ function fillWith(length, generator) {
655
+ if (!Number.isInteger(length) || length < 0)
656
+ throw new RangeError(`Length must be a non-negative integer. Got: ${length}`);
657
+ return Array.from({ length }, (_, i) => generator(i));
658
+ }
627
659
  function extendArray(arr, props) {
628
660
  return arr.map((t) => ({
629
661
  ...t,
@@ -2600,10 +2632,10 @@ function parseTimeInstantComponents(dateString, pattern, config = {}) {
2600
2632
  * @returns Partial time instant parameters that were parsed from the string
2601
2633
  * @throws Error if the string doesn't match the pattern or contains invalid values
2602
2634
  */
2603
- function parseTimeInstantBasicComponents(dateString, pattern) {
2635
+ function parseTimeInstantBasicComponents(dateString, pattern, ignoreIntlAvailability = false) {
2604
2636
  // Check if Intl is available, if so warn the user about the existing function
2605
2637
  const isIntlAvailable = typeof Intl !== 'undefined' && typeof Intl.DateTimeFormat !== 'undefined';
2606
- if (isIntlAvailable)
2638
+ if (isIntlAvailable && !ignoreIntlAvailability)
2607
2639
  console.warn('Intl is available, use parseTimeInstantComponents instead of parseTimeInstantBasicComponents.');
2608
2640
  const result = {};
2609
2641
  let patternIndex = 0;
@@ -3510,6 +3542,7 @@ exports.arrayIncludes = arrayIncludes;
3510
3542
  exports.asError = asError;
3511
3543
  exports.asPromise = asPromise;
3512
3544
  exports.average = average;
3545
+ exports.averageBy = averageBy;
3513
3546
  exports.awaitAtMost = awaitAtMost;
3514
3547
  exports.capitalizeWord = capitalizeWord;
3515
3548
  exports.clamp = clamp;
@@ -3542,6 +3575,7 @@ exports.entriesToList = entriesToList;
3542
3575
  exports.extendArray = extendArray;
3543
3576
  exports.extendArrayWith = extendArrayWith;
3544
3577
  exports.fill = fill;
3578
+ exports.fillWith = fillWith;
3545
3579
  exports.filterMap = filterMap;
3546
3580
  exports.filterMapReduce = filterMapReduce;
3547
3581
  exports.filterWithTypePredicate = filterWithTypePredicate;