@zelgadis87/utils-core 5.3.7 → 5.3.9

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
@@ -362,6 +362,13 @@ class Optional {
362
362
  static ofNullable(t) {
363
363
  return new Optional(t);
364
364
  }
365
+ static findInArray(arr, predicate) {
366
+ return Optional.ofNullable(arr.find(predicate));
367
+ }
368
+ static findIndexInArray(arr, predicate) {
369
+ const idx = arr.findIndex(predicate);
370
+ return idx === -1 ? Optional.empty() : Optional.of(idx);
371
+ }
365
372
  }
366
373
  class ErrorGetEmptyOptional extends Error {
367
374
  constructor() {
@@ -461,22 +468,82 @@ function sum(arr) {
461
468
  function sumBy(arr, getter) {
462
469
  return sum(arr.map(getter));
463
470
  }
471
+ /**
472
+ * Finds the minimum value in an array of numbers.
473
+ * @param arr - The array of numbers to search
474
+ * @returns The minimum value, or null if the array is empty
475
+ */
464
476
  function min(arr) {
465
477
  if (arr.length === 0)
466
478
  return null;
467
479
  return arr.reduce((min, cur) => cur < min ? cur : min);
468
480
  }
481
+ /**
482
+ * Finds the minimum numeric value extracted from array elements using a getter function.
483
+ * @param arr - The array of elements to search
484
+ * @param getter - Function to extract a numeric value from each element
485
+ * @returns The minimum extracted value, or null if the array is empty
486
+ */
469
487
  function minBy(arr, getter) {
470
488
  return min(arr.map(getter));
471
489
  }
490
+ /**
491
+ * Finds the maximum value in an array of numbers.
492
+ * @param arr - The array of numbers to search
493
+ * @returns The maximum value, or null if the array is empty
494
+ */
472
495
  function max(arr) {
473
496
  if (arr.length === 0)
474
497
  return null;
475
498
  return arr.reduce((max, cur) => cur > max ? cur : max);
476
499
  }
500
+ /**
501
+ * Finds the maximum numeric value extracted from array elements using a getter function.
502
+ * @param arr - The array of elements to search
503
+ * @param getter - Function to extract a numeric value from each element
504
+ * @returns The maximum extracted value, or null if the array is empty
505
+ */
477
506
  function maxBy(arr, getter) {
478
507
  return max(arr.map(getter));
479
508
  }
509
+ /**
510
+ * Finds the element with the maximum numeric value extracted using a getter function.
511
+ * @param arr - The array of elements to search
512
+ * @param getter - Function to extract a numeric value from each element
513
+ * @returns The element with the maximum value, or null if the array is empty
514
+ */
515
+ function havingMaxBy(arr, getter) {
516
+ if (arr.length === 0)
517
+ return null;
518
+ return arr.reduce((ret, cur) => {
519
+ const curValue = getter(cur);
520
+ if (curValue > ret.max) {
521
+ return { value: cur, max: curValue };
522
+ }
523
+ else {
524
+ return ret;
525
+ }
526
+ }, { value: null, max: -Infinity }).value;
527
+ }
528
+ /**
529
+ * Finds the element with the minimum numeric value extracted using a getter function.
530
+ * @param arr - The array of elements to search
531
+ * @param getter - Function to extract a numeric value from each element
532
+ * @returns The element with the minimum value, or null if the array is empty
533
+ */
534
+ function havingMinBy(arr, getter) {
535
+ if (arr.length === 0)
536
+ return null;
537
+ return arr.reduce((ret, cur) => {
538
+ const curValue = getter(cur);
539
+ if (curValue < ret.min) {
540
+ return { value: cur, min: curValue };
541
+ }
542
+ else {
543
+ return ret;
544
+ }
545
+ }, { value: null, min: +Infinity }).value;
546
+ }
480
547
 
481
548
  function constant(v) { return () => v; }
482
549
  function identity(t) { return t; }
@@ -747,12 +814,13 @@ function shallowArrayEquals(a, b) {
747
814
  }
748
815
  return true;
749
816
  }
817
+ /** @deprecated[2026.03.01]: Use {@link Optional.findInArray} instead. */
750
818
  function findInArray(arr, predicate) {
751
- return Optional.ofNullable(arr.find(predicate));
819
+ return Optional.findInArray(arr, predicate);
752
820
  }
821
+ /** @deprecated[2026.03.01]: Use {@link Optional.findIndexInArray} instead. */
753
822
  function findIndexInArray(arr, predicate) {
754
- const idx = arr.findIndex(predicate);
755
- return idx === -1 ? Optional.empty() : Optional.of(idx);
823
+ return Optional.findIndexInArray(arr, predicate);
756
824
  }
757
825
  function zip(ts, rs) {
758
826
  if (ts.length !== rs.length)
@@ -1074,11 +1142,38 @@ async function awaitAtMost(promise, duration) {
1074
1142
  }
1075
1143
  const NEVER = new Promise(_resolve => { });
1076
1144
 
1145
+ /**
1146
+ * Returns a random integer in the range [min, max].
1147
+ * @deprecated Use randomIntegerInInterval or randomDecimalInInterval instead
1148
+ */
1077
1149
  function randomNumberInInterval(min, max) {
1078
1150
  return Math.floor(Math.random() * (max - min + 1) + min);
1079
1151
  }
1152
+ /**
1153
+ * Returns a random integer in the range [min, max].
1154
+ * @param min - Minimum value (inclusive)
1155
+ * @param max - Maximum value (inclusive)
1156
+ * @returns A random integer between min and max
1157
+ */
1158
+ function randomIntegerInInterval(min, max) {
1159
+ return Math.floor(Math.random() * (max - min + 1) + min);
1160
+ }
1161
+ /**
1162
+ * Returns a random decimal in the range [min, max).
1163
+ * @param min - Minimum value (inclusive)
1164
+ * @param max - Maximum value (exclusive)
1165
+ * @returns A random decimal between min and max
1166
+ */
1167
+ function randomDecimalInInterval(min, max) {
1168
+ return Math.random() * (max - min) + min;
1169
+ }
1080
1170
  const randomId = (length) => {
1081
- return Math.random().toString(36).substring(2, length + 2);
1171
+ const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
1172
+ let result = '';
1173
+ for (let i = 0; i < length; i++) {
1174
+ result += characters.charAt(randomIntegerInInterval(0, characters.length - 1));
1175
+ }
1176
+ return result;
1082
1177
  };
1083
1178
  function randomPick(arr) {
1084
1179
  return first$1(randomPicks(arr, 1));
@@ -1087,7 +1182,7 @@ function randomPicks(arr, count) {
1087
1182
  const available = [...arr];
1088
1183
  const result = [];
1089
1184
  while (available.length > 0 && count > 0) {
1090
- const randomIndex = randomNumberInInterval(0, available.length - 1);
1185
+ const randomIndex = randomIntegerInInterval(0, available.length - 1);
1091
1186
  result.push(available[randomIndex]);
1092
1187
  available.splice(randomIndex, 1);
1093
1188
  count--;
@@ -3392,7 +3487,7 @@ const Sorter = {
3392
3487
 
3393
3488
  function randomize(unit) {
3394
3489
  return (a, b) => {
3395
- return TimeDuration.fromMs(randomNumberInInterval(unit.toMs(a), unit.toMs(b)));
3490
+ return TimeDuration.fromMs(randomIntegerInInterval(unit.toMs(a), unit.toMs(b)));
3396
3491
  };
3397
3492
  }
3398
3493
  class RandomTimeDuration {
@@ -3664,6 +3759,8 @@ exports.groupByStringWith = groupByStringWith;
3664
3759
  exports.groupBySymbol = groupBySymbol;
3665
3760
  exports.groupBySymbolWith = groupBySymbolWith;
3666
3761
  exports.hashCode = hashCode;
3762
+ exports.havingMaxBy = havingMaxBy;
3763
+ exports.havingMinBy = havingMinBy;
3667
3764
  exports.head = head;
3668
3765
  exports.identity = identity;
3669
3766
  exports.ifDefined = ifDefined;
@@ -3724,7 +3821,9 @@ exports.pipedInvoke = pipedInvoke;
3724
3821
  exports.pipedInvokeFromArray = pipedInvokeFromArray;
3725
3822
  exports.pluralize = pluralize;
3726
3823
  exports.promiseSequence = promiseSequence;
3824
+ exports.randomDecimalInInterval = randomDecimalInInterval;
3727
3825
  exports.randomId = randomId;
3826
+ exports.randomIntegerInInterval = randomIntegerInInterval;
3728
3827
  exports.randomNumberInInterval = randomNumberInInterval;
3729
3828
  exports.randomPick = randomPick;
3730
3829
  exports.randomPicks = randomPicks;