@zelgadis87/utils-core 5.0.1 → 5.0.3

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/esbuild/index.mjs CHANGED
@@ -263,8 +263,7 @@ var RateThrottler = class {
263
263
  return slot.promise().then(() => fn());
264
264
  } finally {
265
265
  this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
266
- if (this._waitingRequests.length > 0)
267
- this._waitingRequests.shift().resolve();
266
+ if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
268
267
  }
269
268
  };
270
269
  if (this._availableSlots.length > 0) {
@@ -430,16 +429,14 @@ function sumBy(arr, getter) {
430
429
  return sum(arr.map(getter));
431
430
  }
432
431
  function min(arr) {
433
- if (arr.length === 0)
434
- throw new Error("Cannot calculate value on empty array");
432
+ if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
435
433
  return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
436
434
  }
437
435
  function minBy(arr, getter) {
438
436
  return min(arr.map(getter));
439
437
  }
440
438
  function max(arr) {
441
- if (arr.length === 0)
442
- throw new Error("Cannot calculate value on empty array");
439
+ if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
443
440
  return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
444
441
  }
445
442
  function maxBy(arr, getter) {
@@ -479,6 +476,25 @@ function iff(firstPredicate, valueIfTrue) {
479
476
  }
480
477
  }
481
478
 
479
+ // src/utils/functions/predicateBuilder.ts
480
+ var PredicateBuilder = class {
481
+ _currentPredicate = constantTrue;
482
+ and(predicate) {
483
+ const curPredicate = this._currentPredicate.bind(void 0);
484
+ const newPredicate = (t) => curPredicate(t) && predicate(t);
485
+ this._currentPredicate = newPredicate;
486
+ return this;
487
+ }
488
+ or(predicate) {
489
+ const newPredicate = (t) => this._currentPredicate(t) || predicate(t);
490
+ this._currentPredicate = newPredicate;
491
+ return this;
492
+ }
493
+ toPredicate() {
494
+ return this._currentPredicate;
495
+ }
496
+ };
497
+
482
498
  // src/utils/functions.ts
483
499
  function isFunction(t) {
484
500
  return typeof t === "function";
@@ -492,17 +508,13 @@ function not(predicate) {
492
508
  return (t) => !predicate(t);
493
509
  }
494
510
  function and(...predicates) {
495
- if (predicates.length === 0)
496
- return constantTrue;
497
- else if (predicates.length === 1)
498
- return predicates[0];
511
+ if (predicates.length === 0) return constantTrue;
512
+ else if (predicates.length === 1) return predicates[0];
499
513
  return (t) => predicates.reduce((prev, cur) => prev ? cur(t) : false, true);
500
514
  }
501
515
  function or(...predicates) {
502
- if (predicates.length === 0)
503
- return constantTrue;
504
- else if (predicates.length === 1)
505
- return predicates[0];
516
+ if (predicates.length === 0) return constantTrue;
517
+ else if (predicates.length === 1) return predicates[0];
506
518
  return (t) => predicates.reduce((prev, cur) => prev ? true : cur(t), false);
507
519
  }
508
520
  function xor(a, b) {
@@ -546,13 +558,11 @@ function uniqByKey(arr, key) {
546
558
 
547
559
  // src/utils/arrays.ts
548
560
  function ensureArray(t) {
549
- if (isNullOrUndefined(t))
550
- return [];
561
+ if (isNullOrUndefined(t)) return [];
551
562
  return t instanceof Array ? t : [t];
552
563
  }
553
564
  function ensureReadableArray(t) {
554
- if (isNullOrUndefined(t))
555
- return [];
565
+ if (isNullOrUndefined(t)) return [];
556
566
  return t instanceof Array ? t : [t];
557
567
  }
558
568
  function isArray(t) {
@@ -571,8 +581,7 @@ function upsert(arr, item, isEqual) {
571
581
  }
572
582
  }
573
583
  function range(start, end) {
574
- if (end < start)
575
- throw new Error();
584
+ if (end < start) throw new Error();
576
585
  let length = end - start + 1;
577
586
  return new Array(length).fill(1).map((_, i) => start + i);
578
587
  }
@@ -631,8 +640,7 @@ function partition(arr, predicate) {
631
640
  function mapFirstTruthy(arr, mapFn) {
632
641
  for (let i = 0; i < arr.length; i++) {
633
642
  const result = mapFn(arr[i]);
634
- if (result)
635
- return result;
643
+ if (result) return result;
636
644
  }
637
645
  return null;
638
646
  }
@@ -660,8 +668,7 @@ function cssDeclarationRulesDictionaryToCss(syleDeclarationRulesForSelectorsProd
660
668
  const syleDeclarationRulesForSelectors = produceableToValue(syleDeclarationRulesForSelectorsProduceable);
661
669
  return Object.entries(syleDeclarationRulesForSelectors).map(([selector, styleDeclarationRules]) => {
662
670
  const cssRules = cssSelectorDeclarationRulesDictionaryToCss(styleDeclarationRules, indent + 1);
663
- if (!cssRules.length)
664
- return null;
671
+ if (!cssRules.length) return null;
665
672
  return repeat(tabulation, indent) + selector + space + openBracket + newLine + cssRules.join(newLine) + newLine + closeBracket;
666
673
  }).filter(Boolean).join(newLine + newLine);
667
674
  }
@@ -729,14 +736,26 @@ function asError(e) {
729
736
  function isError(e) {
730
737
  return e instanceof Error;
731
738
  }
739
+ function getMessageFromError(error) {
740
+ const maybeCause = error.cause ? getMessageFromError(asError(error.cause)) : null;
741
+ const cause = maybeCause ? `
742
+ caused by: ${maybeCause}` : "";
743
+ return `${error.name}: ${error.message}${cause}`;
744
+ }
745
+ function getStackFromError(error) {
746
+ const maybeCause = error.cause ? getStackFromError(asError(error.cause)) : null;
747
+ const cause = maybeCause ? `
748
+ caused by: ${maybeCause}` : "";
749
+ const stack = error.stack && error.stack.includes(error.message) ? error.stack : error.message + " " + (error.stack ?? "");
750
+ return `${error.name}: ${stack}${cause}`;
751
+ }
732
752
 
733
753
  // src/utils/json.ts
734
754
  function tryToParseJson(jsonContent) {
735
755
  return withTryCatch(() => JSON.parse(jsonContent));
736
756
  }
737
757
  function jsonCloneDeep(a) {
738
- if (null === a || "object" !== typeof a)
739
- return a;
758
+ if (null === a || "object" !== typeof a) return a;
740
759
  if (a instanceof Date) {
741
760
  return new Date(a.getTime());
742
761
  } else if (a instanceof Array) {
@@ -843,13 +862,10 @@ function clampInt0_100(n) {
843
862
  function tryToParseNumber(numberStr) {
844
863
  return withTryCatch(() => {
845
864
  const type = typeof ensureDefined(numberStr);
846
- if (type !== "string")
847
- throw new Error("Invalid number given: " + numberStr);
848
- if (numberStr.trim().length === 0)
849
- throw new Error("Invalid number given: " + numberStr);
865
+ if (type !== "string") throw new Error("Invalid number given: " + numberStr);
866
+ if (numberStr.trim().length === 0) throw new Error("Invalid number given: " + numberStr);
850
867
  const num = Number(numberStr);
851
- if (isNaN(num))
852
- throw new Error("Invalid number given: " + numberStr);
868
+ if (isNaN(num)) throw new Error("Invalid number given: " + numberStr);
853
869
  return num;
854
870
  });
855
871
  }
@@ -929,8 +945,7 @@ var StringParts = class _StringParts {
929
945
  return new _StringParts(...this.parts.map((part) => part.toLowerCase()));
930
946
  }
931
947
  capitalizeFirst() {
932
- if (!this.length)
933
- return this;
948
+ if (!this.length) return this;
934
949
  return new _StringParts(capitalizeWord(this.first), ...this.tail.map((part) => part.toLowerCase()));
935
950
  }
936
951
  capitalizeEach() {
@@ -959,8 +974,7 @@ var StringParts = class _StringParts {
959
974
  return this.toLowerCase().join("_");
960
975
  }
961
976
  toCamelCase() {
962
- if (!this.length)
963
- return "";
977
+ if (!this.length) return "";
964
978
  return [this.first.toLowerCase(), ...this.tail.map(capitalizeWord)].join("");
965
979
  }
966
980
  toKebabCase() {
@@ -1034,8 +1048,7 @@ function stringToNumber(s) {
1034
1048
  }
1035
1049
  function pad(str, n, char, where = "left") {
1036
1050
  const length = ensureDefined(str).length;
1037
- if (length >= ensureDefined(n))
1038
- return str;
1051
+ if (length >= ensureDefined(n)) return str;
1039
1052
  if (ensureDefined(char).length !== 1)
1040
1053
  throw new Error("Illegal pad character");
1041
1054
  const padding = repeat(char, n - length);
@@ -1265,15 +1278,11 @@ var TimeUnit = class _TimeUnit {
1265
1278
  toDays(value) {
1266
1279
  return this.toUnit(value, _TimeUnit.DAYS);
1267
1280
  }
1268
- toWeeks(value) {
1269
- return this.toUnit(value, _TimeUnit.WEEKS);
1270
- }
1271
1281
  static MILLISECONDS = new _TimeUnit(1);
1272
1282
  static SECONDS = new _TimeUnit(1e3 * _TimeUnit.MILLISECONDS.multiplier);
1273
1283
  static MINUTES = new _TimeUnit(60 * _TimeUnit.SECONDS.multiplier);
1274
1284
  static HOURS = new _TimeUnit(60 * _TimeUnit.MINUTES.multiplier);
1275
1285
  static DAYS = new _TimeUnit(24 * _TimeUnit.HOURS.multiplier);
1276
- static WEEKS = new _TimeUnit(7 * _TimeUnit.DAYS.multiplier);
1277
1286
  };
1278
1287
 
1279
1288
  // src/time/TimeBase.ts
@@ -1282,18 +1291,33 @@ var TimeBase = class {
1282
1291
  constructor(value, unit) {
1283
1292
  this._ms = unit.toMs(value);
1284
1293
  }
1294
+ /**
1295
+ * Total number of milliseconds, rounded down.
1296
+ */
1285
1297
  get ms() {
1286
1298
  return Math.floor(this._ms / TimeUnit.MILLISECONDS.multiplier);
1287
1299
  }
1300
+ /**
1301
+ * Total number of seconds, rounded down.
1302
+ */
1288
1303
  get seconds() {
1289
1304
  return Math.floor(this._ms / TimeUnit.SECONDS.multiplier);
1290
1305
  }
1306
+ /**
1307
+ * Total number of minutes, rounded down.
1308
+ */
1291
1309
  get minutes() {
1292
1310
  return Math.floor(this._ms / TimeUnit.MINUTES.multiplier);
1293
1311
  }
1312
+ /**
1313
+ * Total number of hours, rounded down.
1314
+ */
1294
1315
  get hours() {
1295
1316
  return Math.floor(this._ms / TimeUnit.HOURS.multiplier);
1296
1317
  }
1318
+ /**
1319
+ * Total number of days, rounded down.
1320
+ */
1297
1321
  get days() {
1298
1322
  return Math.floor(this._ms / TimeUnit.DAYS.multiplier);
1299
1323
  }
@@ -1338,17 +1362,18 @@ var TimeBase = class {
1338
1362
  }
1339
1363
  toUnits() {
1340
1364
  return {
1341
- days: Math.floor(this._ms / TimeUnit.DAYS.multiplier),
1342
- hours: Math.floor(this._ms / TimeUnit.HOURS.multiplier % 24),
1343
- minutes: Math.floor(this._ms / TimeUnit.MINUTES.multiplier % 60),
1344
- seconds: Math.floor(this._ms / TimeUnit.SECONDS.multiplier % 60)
1365
+ milliseconds: this.ms % TimeUnit.SECONDS.multiplier,
1366
+ seconds: Math.floor(this.seconds % 60),
1367
+ minutes: Math.floor(this.minutes % 60),
1368
+ hours: Math.floor(this.hours % 24),
1369
+ days: Math.floor(this.days)
1345
1370
  };
1346
1371
  }
1347
- static toMs(units) {
1372
+ static unitsToMs(units) {
1348
1373
  if (!units)
1349
1374
  throw new Error("Invalid units given");
1350
1375
  let ms = 0;
1351
- ms += units.ms ?? 0 * TimeUnit.MILLISECONDS.multiplier;
1376
+ ms += (units.milliseconds ?? 0) * TimeUnit.MILLISECONDS.multiplier;
1352
1377
  ms += (units.seconds ?? 0) * TimeUnit.SECONDS.multiplier;
1353
1378
  ms += (units.minutes ?? 0) * TimeUnit.MINUTES.multiplier;
1354
1379
  ms += (units.hours ?? 0) * TimeUnit.HOURS.multiplier;
@@ -1398,8 +1423,7 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1398
1423
  get formatted() {
1399
1424
  const format2 = (x, u1, y, u2) => `${x}${u1} ${pad(y.toString(), 2, "0")}${u2}`;
1400
1425
  const units = this.toUnits();
1401
- if (units.days >= 1)
1402
- return format2(units.days, "d", units.hours, "h");
1426
+ if (units.days >= 1) return format2(units.days, "d", units.hours, "h");
1403
1427
  else if (units.hours >= 1)
1404
1428
  return format2(units.hours, "h", units.minutes, "m");
1405
1429
  else if (units.minutes >= 1)
@@ -1510,6 +1534,9 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1510
1534
  isNotEmpty() {
1511
1535
  return this.ms > 0;
1512
1536
  }
1537
+ toUnits() {
1538
+ return super.toUnits();
1539
+ }
1513
1540
  /**
1514
1541
  * This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
1515
1542
  */
@@ -1523,7 +1550,7 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1523
1550
  const match = humanTime.trim().match(/^(?:([0-9]+)d|)\s*(?:([0-9]+)h|)\s*(?:([0-9]+)m|)\s*(?:([0-9]+)s|)$/);
1524
1551
  if (match) {
1525
1552
  const [_, days, hours, minutes, seconds] = match;
1526
- return new _TimeDuration(TimeBase.toMs({ days: Number(days ?? 0), hours: Number(hours ?? 0), minutes: Number(minutes ?? 0), seconds: Number(seconds ?? 0) }), TimeUnit.MILLISECONDS);
1553
+ return new _TimeDuration(TimeBase.unitsToMs({ days: Number(days ?? 0), hours: Number(hours ?? 0), minutes: Number(minutes ?? 0), seconds: Number(seconds ?? 0) }), TimeUnit.MILLISECONDS);
1527
1554
  }
1528
1555
  throw new Error("Failed to parse time: " + humanTime);
1529
1556
  }
@@ -1575,6 +1602,9 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1575
1602
  static fromJSON(ms) {
1576
1603
  return _TimeDuration.ms(ms);
1577
1604
  }
1605
+ static fromUnits(parameters) {
1606
+ return _TimeDuration.ms(TimeBase.unitsToMs(parameters));
1607
+ }
1578
1608
  };
1579
1609
  function isAllowedTimeDuration(t) {
1580
1610
  return typeof t === "number" && t > 0 || t instanceof TimeDuration;
@@ -1652,8 +1682,7 @@ var getFromDate = {
1652
1682
  };
1653
1683
  var toReferenceDate = (x) => {
1654
1684
  ensureDefined(x);
1655
- if (isTimeInstant(x))
1656
- return x.toDate();
1685
+ if (isTimeInstant(x)) return x.toDate();
1657
1686
  return x;
1658
1687
  };
1659
1688
  function createTimeInstantFromParameters(aParameters, aReferenceDate = TimeInstant.now()) {
@@ -2088,8 +2117,7 @@ var Optional = class _Optional {
2088
2117
  }
2089
2118
  }
2090
2119
  orElseNullable(newValue) {
2091
- if (this.isEmpty())
2092
- return _Optional.ofNullable(newValue);
2120
+ if (this.isEmpty()) return _Optional.ofNullable(newValue);
2093
2121
  return this;
2094
2122
  }
2095
2123
  orElseGetNullable(newValueProducer) {
@@ -2219,8 +2247,7 @@ var prioritizeSet = (fns, transform, set, reversed = false) => {
2219
2247
  var prioritizeArray = (fns, transform, arr, reversed = false) => {
2220
2248
  return compareNumbers(fns, (t) => {
2221
2249
  const r = transform(t);
2222
- if (!isDefined(r))
2223
- return Number.MAX_VALUE;
2250
+ if (!isDefined(r)) return Number.MAX_VALUE;
2224
2251
  const indexOf = arr.indexOf(r);
2225
2252
  return indexOf === -1 ? Number.MAX_VALUE : indexOf;
2226
2253
  }, { direction: reversed ? "DESC" : "ASC", nullsFirst: false });
@@ -2614,6 +2641,7 @@ export {
2614
2641
  Logger,
2615
2642
  NEVER,
2616
2643
  Optional,
2644
+ PredicateBuilder,
2617
2645
  RandomTimeDuration,
2618
2646
  RateThrottler,
2619
2647
  Semaphore,
@@ -2666,6 +2694,8 @@ export {
2666
2694
  filterWithTypePredicate,
2667
2695
  first,
2668
2696
  flatMapTruthys,
2697
+ getMessageFromError,
2698
+ getStackFromError,
2669
2699
  groupByBoolean,
2670
2700
  groupByBooleanWith,
2671
2701
  groupByNumber,