@zelgadis87/utils-core 5.0.2 → 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) {
@@ -511,17 +508,13 @@ function not(predicate) {
511
508
  return (t) => !predicate(t);
512
509
  }
513
510
  function and(...predicates) {
514
- if (predicates.length === 0)
515
- return constantTrue;
516
- else if (predicates.length === 1)
517
- return predicates[0];
511
+ if (predicates.length === 0) return constantTrue;
512
+ else if (predicates.length === 1) return predicates[0];
518
513
  return (t) => predicates.reduce((prev, cur) => prev ? cur(t) : false, true);
519
514
  }
520
515
  function or(...predicates) {
521
- if (predicates.length === 0)
522
- return constantTrue;
523
- else if (predicates.length === 1)
524
- return predicates[0];
516
+ if (predicates.length === 0) return constantTrue;
517
+ else if (predicates.length === 1) return predicates[0];
525
518
  return (t) => predicates.reduce((prev, cur) => prev ? true : cur(t), false);
526
519
  }
527
520
  function xor(a, b) {
@@ -565,13 +558,11 @@ function uniqByKey(arr, key) {
565
558
 
566
559
  // src/utils/arrays.ts
567
560
  function ensureArray(t) {
568
- if (isNullOrUndefined(t))
569
- return [];
561
+ if (isNullOrUndefined(t)) return [];
570
562
  return t instanceof Array ? t : [t];
571
563
  }
572
564
  function ensureReadableArray(t) {
573
- if (isNullOrUndefined(t))
574
- return [];
565
+ if (isNullOrUndefined(t)) return [];
575
566
  return t instanceof Array ? t : [t];
576
567
  }
577
568
  function isArray(t) {
@@ -590,8 +581,7 @@ function upsert(arr, item, isEqual) {
590
581
  }
591
582
  }
592
583
  function range(start, end) {
593
- if (end < start)
594
- throw new Error();
584
+ if (end < start) throw new Error();
595
585
  let length = end - start + 1;
596
586
  return new Array(length).fill(1).map((_, i) => start + i);
597
587
  }
@@ -650,8 +640,7 @@ function partition(arr, predicate) {
650
640
  function mapFirstTruthy(arr, mapFn) {
651
641
  for (let i = 0; i < arr.length; i++) {
652
642
  const result = mapFn(arr[i]);
653
- if (result)
654
- return result;
643
+ if (result) return result;
655
644
  }
656
645
  return null;
657
646
  }
@@ -679,8 +668,7 @@ function cssDeclarationRulesDictionaryToCss(syleDeclarationRulesForSelectorsProd
679
668
  const syleDeclarationRulesForSelectors = produceableToValue(syleDeclarationRulesForSelectorsProduceable);
680
669
  return Object.entries(syleDeclarationRulesForSelectors).map(([selector, styleDeclarationRules]) => {
681
670
  const cssRules = cssSelectorDeclarationRulesDictionaryToCss(styleDeclarationRules, indent + 1);
682
- if (!cssRules.length)
683
- return null;
671
+ if (!cssRules.length) return null;
684
672
  return repeat(tabulation, indent) + selector + space + openBracket + newLine + cssRules.join(newLine) + newLine + closeBracket;
685
673
  }).filter(Boolean).join(newLine + newLine);
686
674
  }
@@ -748,14 +736,14 @@ function asError(e) {
748
736
  function isError(e) {
749
737
  return e instanceof Error;
750
738
  }
751
- function printErrorMessage(error) {
752
- const maybeCause = error.cause ? printErrorMessage(asError(error.cause)) : null;
739
+ function getMessageFromError(error) {
740
+ const maybeCause = error.cause ? getMessageFromError(asError(error.cause)) : null;
753
741
  const cause = maybeCause ? `
754
742
  caused by: ${maybeCause}` : "";
755
743
  return `${error.name}: ${error.message}${cause}`;
756
744
  }
757
- function printErrorStack(error) {
758
- const maybeCause = error.cause ? printErrorStack(asError(error.cause)) : null;
745
+ function getStackFromError(error) {
746
+ const maybeCause = error.cause ? getStackFromError(asError(error.cause)) : null;
759
747
  const cause = maybeCause ? `
760
748
  caused by: ${maybeCause}` : "";
761
749
  const stack = error.stack && error.stack.includes(error.message) ? error.stack : error.message + " " + (error.stack ?? "");
@@ -767,8 +755,7 @@ function tryToParseJson(jsonContent) {
767
755
  return withTryCatch(() => JSON.parse(jsonContent));
768
756
  }
769
757
  function jsonCloneDeep(a) {
770
- if (null === a || "object" !== typeof a)
771
- return a;
758
+ if (null === a || "object" !== typeof a) return a;
772
759
  if (a instanceof Date) {
773
760
  return new Date(a.getTime());
774
761
  } else if (a instanceof Array) {
@@ -875,13 +862,10 @@ function clampInt0_100(n) {
875
862
  function tryToParseNumber(numberStr) {
876
863
  return withTryCatch(() => {
877
864
  const type = typeof ensureDefined(numberStr);
878
- if (type !== "string")
879
- throw new Error("Invalid number given: " + numberStr);
880
- if (numberStr.trim().length === 0)
881
- 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);
882
867
  const num = Number(numberStr);
883
- if (isNaN(num))
884
- throw new Error("Invalid number given: " + numberStr);
868
+ if (isNaN(num)) throw new Error("Invalid number given: " + numberStr);
885
869
  return num;
886
870
  });
887
871
  }
@@ -961,8 +945,7 @@ var StringParts = class _StringParts {
961
945
  return new _StringParts(...this.parts.map((part) => part.toLowerCase()));
962
946
  }
963
947
  capitalizeFirst() {
964
- if (!this.length)
965
- return this;
948
+ if (!this.length) return this;
966
949
  return new _StringParts(capitalizeWord(this.first), ...this.tail.map((part) => part.toLowerCase()));
967
950
  }
968
951
  capitalizeEach() {
@@ -991,8 +974,7 @@ var StringParts = class _StringParts {
991
974
  return this.toLowerCase().join("_");
992
975
  }
993
976
  toCamelCase() {
994
- if (!this.length)
995
- return "";
977
+ if (!this.length) return "";
996
978
  return [this.first.toLowerCase(), ...this.tail.map(capitalizeWord)].join("");
997
979
  }
998
980
  toKebabCase() {
@@ -1066,8 +1048,7 @@ function stringToNumber(s) {
1066
1048
  }
1067
1049
  function pad(str, n, char, where = "left") {
1068
1050
  const length = ensureDefined(str).length;
1069
- if (length >= ensureDefined(n))
1070
- return str;
1051
+ if (length >= ensureDefined(n)) return str;
1071
1052
  if (ensureDefined(char).length !== 1)
1072
1053
  throw new Error("Illegal pad character");
1073
1054
  const padding = repeat(char, n - length);
@@ -1297,15 +1278,11 @@ var TimeUnit = class _TimeUnit {
1297
1278
  toDays(value) {
1298
1279
  return this.toUnit(value, _TimeUnit.DAYS);
1299
1280
  }
1300
- toWeeks(value) {
1301
- return this.toUnit(value, _TimeUnit.WEEKS);
1302
- }
1303
1281
  static MILLISECONDS = new _TimeUnit(1);
1304
1282
  static SECONDS = new _TimeUnit(1e3 * _TimeUnit.MILLISECONDS.multiplier);
1305
1283
  static MINUTES = new _TimeUnit(60 * _TimeUnit.SECONDS.multiplier);
1306
1284
  static HOURS = new _TimeUnit(60 * _TimeUnit.MINUTES.multiplier);
1307
1285
  static DAYS = new _TimeUnit(24 * _TimeUnit.HOURS.multiplier);
1308
- static WEEKS = new _TimeUnit(7 * _TimeUnit.DAYS.multiplier);
1309
1286
  };
1310
1287
 
1311
1288
  // src/time/TimeBase.ts
@@ -1314,18 +1291,33 @@ var TimeBase = class {
1314
1291
  constructor(value, unit) {
1315
1292
  this._ms = unit.toMs(value);
1316
1293
  }
1294
+ /**
1295
+ * Total number of milliseconds, rounded down.
1296
+ */
1317
1297
  get ms() {
1318
1298
  return Math.floor(this._ms / TimeUnit.MILLISECONDS.multiplier);
1319
1299
  }
1300
+ /**
1301
+ * Total number of seconds, rounded down.
1302
+ */
1320
1303
  get seconds() {
1321
1304
  return Math.floor(this._ms / TimeUnit.SECONDS.multiplier);
1322
1305
  }
1306
+ /**
1307
+ * Total number of minutes, rounded down.
1308
+ */
1323
1309
  get minutes() {
1324
1310
  return Math.floor(this._ms / TimeUnit.MINUTES.multiplier);
1325
1311
  }
1312
+ /**
1313
+ * Total number of hours, rounded down.
1314
+ */
1326
1315
  get hours() {
1327
1316
  return Math.floor(this._ms / TimeUnit.HOURS.multiplier);
1328
1317
  }
1318
+ /**
1319
+ * Total number of days, rounded down.
1320
+ */
1329
1321
  get days() {
1330
1322
  return Math.floor(this._ms / TimeUnit.DAYS.multiplier);
1331
1323
  }
@@ -1370,17 +1362,18 @@ var TimeBase = class {
1370
1362
  }
1371
1363
  toUnits() {
1372
1364
  return {
1373
- days: Math.floor(this._ms / TimeUnit.DAYS.multiplier),
1374
- hours: Math.floor(this._ms / TimeUnit.HOURS.multiplier % 24),
1375
- minutes: Math.floor(this._ms / TimeUnit.MINUTES.multiplier % 60),
1376
- 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)
1377
1370
  };
1378
1371
  }
1379
- static toMs(units) {
1372
+ static unitsToMs(units) {
1380
1373
  if (!units)
1381
1374
  throw new Error("Invalid units given");
1382
1375
  let ms = 0;
1383
- ms += units.ms ?? 0 * TimeUnit.MILLISECONDS.multiplier;
1376
+ ms += (units.milliseconds ?? 0) * TimeUnit.MILLISECONDS.multiplier;
1384
1377
  ms += (units.seconds ?? 0) * TimeUnit.SECONDS.multiplier;
1385
1378
  ms += (units.minutes ?? 0) * TimeUnit.MINUTES.multiplier;
1386
1379
  ms += (units.hours ?? 0) * TimeUnit.HOURS.multiplier;
@@ -1430,8 +1423,7 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1430
1423
  get formatted() {
1431
1424
  const format2 = (x, u1, y, u2) => `${x}${u1} ${pad(y.toString(), 2, "0")}${u2}`;
1432
1425
  const units = this.toUnits();
1433
- if (units.days >= 1)
1434
- return format2(units.days, "d", units.hours, "h");
1426
+ if (units.days >= 1) return format2(units.days, "d", units.hours, "h");
1435
1427
  else if (units.hours >= 1)
1436
1428
  return format2(units.hours, "h", units.minutes, "m");
1437
1429
  else if (units.minutes >= 1)
@@ -1542,6 +1534,9 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1542
1534
  isNotEmpty() {
1543
1535
  return this.ms > 0;
1544
1536
  }
1537
+ toUnits() {
1538
+ return super.toUnits();
1539
+ }
1545
1540
  /**
1546
1541
  * This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
1547
1542
  */
@@ -1555,7 +1550,7 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1555
1550
  const match = humanTime.trim().match(/^(?:([0-9]+)d|)\s*(?:([0-9]+)h|)\s*(?:([0-9]+)m|)\s*(?:([0-9]+)s|)$/);
1556
1551
  if (match) {
1557
1552
  const [_, days, hours, minutes, seconds] = match;
1558
- 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);
1559
1554
  }
1560
1555
  throw new Error("Failed to parse time: " + humanTime);
1561
1556
  }
@@ -1607,6 +1602,9 @@ var TimeDuration = class _TimeDuration extends TimeBase {
1607
1602
  static fromJSON(ms) {
1608
1603
  return _TimeDuration.ms(ms);
1609
1604
  }
1605
+ static fromUnits(parameters) {
1606
+ return _TimeDuration.ms(TimeBase.unitsToMs(parameters));
1607
+ }
1610
1608
  };
1611
1609
  function isAllowedTimeDuration(t) {
1612
1610
  return typeof t === "number" && t > 0 || t instanceof TimeDuration;
@@ -1684,8 +1682,7 @@ var getFromDate = {
1684
1682
  };
1685
1683
  var toReferenceDate = (x) => {
1686
1684
  ensureDefined(x);
1687
- if (isTimeInstant(x))
1688
- return x.toDate();
1685
+ if (isTimeInstant(x)) return x.toDate();
1689
1686
  return x;
1690
1687
  };
1691
1688
  function createTimeInstantFromParameters(aParameters, aReferenceDate = TimeInstant.now()) {
@@ -2120,8 +2117,7 @@ var Optional = class _Optional {
2120
2117
  }
2121
2118
  }
2122
2119
  orElseNullable(newValue) {
2123
- if (this.isEmpty())
2124
- return _Optional.ofNullable(newValue);
2120
+ if (this.isEmpty()) return _Optional.ofNullable(newValue);
2125
2121
  return this;
2126
2122
  }
2127
2123
  orElseGetNullable(newValueProducer) {
@@ -2251,8 +2247,7 @@ var prioritizeSet = (fns, transform, set, reversed = false) => {
2251
2247
  var prioritizeArray = (fns, transform, arr, reversed = false) => {
2252
2248
  return compareNumbers(fns, (t) => {
2253
2249
  const r = transform(t);
2254
- if (!isDefined(r))
2255
- return Number.MAX_VALUE;
2250
+ if (!isDefined(r)) return Number.MAX_VALUE;
2256
2251
  const indexOf = arr.indexOf(r);
2257
2252
  return indexOf === -1 ? Number.MAX_VALUE : indexOf;
2258
2253
  }, { direction: reversed ? "DESC" : "ASC", nullsFirst: false });
@@ -2699,6 +2694,8 @@ export {
2699
2694
  filterWithTypePredicate,
2700
2695
  first,
2701
2696
  flatMapTruthys,
2697
+ getMessageFromError,
2698
+ getStackFromError,
2702
2699
  groupByBoolean,
2703
2700
  groupByBooleanWith,
2704
2701
  groupByNumber,
@@ -2760,8 +2757,6 @@ export {
2760
2757
  pipedInvoke,
2761
2758
  pipedInvokeFromArray,
2762
2759
  pluralize,
2763
- printErrorMessage,
2764
- printErrorStack,
2765
2760
  promiseSequence,
2766
2761
  randomId,
2767
2762
  randomNumberInInterval,