@zelgadis87/utils-core 5.3.6 → 5.3.8

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
@@ -461,22 +461,82 @@ function sum(arr) {
461
461
  function sumBy(arr, getter) {
462
462
  return sum(arr.map(getter));
463
463
  }
464
+ /**
465
+ * Finds the minimum value in an array of numbers.
466
+ * @param arr - The array of numbers to search
467
+ * @returns The minimum value, or null if the array is empty
468
+ */
464
469
  function min(arr) {
465
470
  if (arr.length === 0)
466
471
  return null;
467
472
  return arr.reduce((min, cur) => cur < min ? cur : min);
468
473
  }
474
+ /**
475
+ * Finds the minimum numeric value extracted from array elements using a getter function.
476
+ * @param arr - The array of elements to search
477
+ * @param getter - Function to extract a numeric value from each element
478
+ * @returns The minimum extracted value, or null if the array is empty
479
+ */
469
480
  function minBy(arr, getter) {
470
481
  return min(arr.map(getter));
471
482
  }
483
+ /**
484
+ * Finds the maximum value in an array of numbers.
485
+ * @param arr - The array of numbers to search
486
+ * @returns The maximum value, or null if the array is empty
487
+ */
472
488
  function max(arr) {
473
489
  if (arr.length === 0)
474
490
  return null;
475
491
  return arr.reduce((max, cur) => cur > max ? cur : max);
476
492
  }
493
+ /**
494
+ * Finds the maximum numeric value extracted from array elements using a getter function.
495
+ * @param arr - The array of elements to search
496
+ * @param getter - Function to extract a numeric value from each element
497
+ * @returns The maximum extracted value, or null if the array is empty
498
+ */
477
499
  function maxBy(arr, getter) {
478
500
  return max(arr.map(getter));
479
501
  }
502
+ /**
503
+ * Finds the element with the maximum numeric value extracted using a getter function.
504
+ * @param arr - The array of elements to search
505
+ * @param getter - Function to extract a numeric value from each element
506
+ * @returns The element with the maximum value, or null if the array is empty
507
+ */
508
+ function havingMaxBy(arr, getter) {
509
+ if (arr.length === 0)
510
+ return null;
511
+ return arr.reduce((ret, cur) => {
512
+ const curValue = getter(cur);
513
+ if (curValue > ret.max) {
514
+ return { value: cur, max: curValue };
515
+ }
516
+ else {
517
+ return ret;
518
+ }
519
+ }, { value: null, max: -Infinity }).value;
520
+ }
521
+ /**
522
+ * Finds the element with the minimum numeric value extracted using a getter function.
523
+ * @param arr - The array of elements to search
524
+ * @param getter - Function to extract a numeric value from each element
525
+ * @returns The element with the minimum value, or null if the array is empty
526
+ */
527
+ function havingMinBy(arr, getter) {
528
+ if (arr.length === 0)
529
+ return null;
530
+ return arr.reduce((ret, cur) => {
531
+ const curValue = getter(cur);
532
+ if (curValue < ret.min) {
533
+ return { value: cur, min: curValue };
534
+ }
535
+ else {
536
+ return ret;
537
+ }
538
+ }, { value: null, min: +Infinity }).value;
539
+ }
480
540
 
481
541
  function constant(v) { return () => v; }
482
542
  function identity(t) { return t; }
@@ -1080,6 +1140,20 @@ function randomNumberInInterval(min, max) {
1080
1140
  const randomId = (length) => {
1081
1141
  return Math.random().toString(36).substring(2, length + 2);
1082
1142
  };
1143
+ function randomPick(arr) {
1144
+ return first$1(randomPicks(arr, 1));
1145
+ }
1146
+ function randomPicks(arr, count) {
1147
+ const available = [...arr];
1148
+ const result = [];
1149
+ while (available.length > 0 && count > 0) {
1150
+ const randomIndex = randomNumberInInterval(0, available.length - 1);
1151
+ result.push(available[randomIndex]);
1152
+ available.splice(randomIndex, 1);
1153
+ count--;
1154
+ }
1155
+ return result;
1156
+ }
1083
1157
 
1084
1158
  function dictToEntries(obj) {
1085
1159
  return Object.entries(obj);
@@ -3650,6 +3724,8 @@ exports.groupByStringWith = groupByStringWith;
3650
3724
  exports.groupBySymbol = groupBySymbol;
3651
3725
  exports.groupBySymbolWith = groupBySymbolWith;
3652
3726
  exports.hashCode = hashCode;
3727
+ exports.havingMaxBy = havingMaxBy;
3728
+ exports.havingMinBy = havingMinBy;
3653
3729
  exports.head = head;
3654
3730
  exports.identity = identity;
3655
3731
  exports.ifDefined = ifDefined;
@@ -3712,6 +3788,8 @@ exports.pluralize = pluralize;
3712
3788
  exports.promiseSequence = promiseSequence;
3713
3789
  exports.randomId = randomId;
3714
3790
  exports.randomNumberInInterval = randomNumberInInterval;
3791
+ exports.randomPick = randomPick;
3792
+ exports.randomPicks = randomPicks;
3715
3793
  exports.range = range;
3716
3794
  exports.repeat = repeat;
3717
3795
  exports.reverse = reverse$1;