@zelgadis87/utils-core 5.2.1 → 5.2.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.cjs CHANGED
@@ -182,6 +182,7 @@ __export(index_exports, {
182
182
  LazyDictionary: () => LazyDictionary,
183
183
  Logger: () => Logger,
184
184
  NEVER: () => NEVER,
185
+ NonExhaustiveSwitchError: () => NonExhaustiveSwitchError,
185
186
  Optional: () => Optional,
186
187
  PredicateBuilder: () => PredicateBuilder,
187
188
  RandomTimeDuration: () => RandomTimeDuration,
@@ -237,6 +238,7 @@ __export(index_exports, {
237
238
  filterMap: () => filterMap,
238
239
  filterMapReduce: () => filterMapReduce,
239
240
  filterWithTypePredicate: () => filterWithTypePredicate,
241
+ findInArray: () => findInArray,
240
242
  first: () => first,
241
243
  flatMapTruthys: () => flatMapTruthys,
242
244
  getCauseMessageFromError: () => getCauseMessageFromError,
@@ -338,11 +340,13 @@ __export(index_exports, {
338
340
  uniq: () => uniq,
339
341
  uniqBy: () => uniqBy,
340
342
  uniqByKey: () => uniqByKey,
343
+ unzip: () => unzip,
341
344
  upsert: () => upsert,
342
345
  withTryCatch: () => withTryCatch,
343
346
  withTryCatchAsync: () => withTryCatchAsync,
344
347
  wrapWithString: () => wrapWithString,
345
- xor: () => xor
348
+ xor: () => xor,
349
+ zip: () => zip
346
350
  });
347
351
  module.exports = __toCommonJS(index_exports);
348
352
 
@@ -541,6 +545,179 @@ function throwIfNullOrUndefined(source, errorProducer = () => new Error(`Unexpec
541
545
  });
542
546
  }
543
547
 
548
+ // src/Optional.ts
549
+ var Optional = class _Optional {
550
+ _present;
551
+ _value;
552
+ constructor(t) {
553
+ const defined = isDefined(t);
554
+ this._value = defined ? t : void 0;
555
+ this._present = defined;
556
+ }
557
+ getRawValue() {
558
+ return this._value;
559
+ }
560
+ get() {
561
+ return this.getOrElseThrow(() => new ErrorGetEmptyOptional());
562
+ }
563
+ getOrElseThrow(errorProducer) {
564
+ if (this.isEmpty())
565
+ throw errorProducer();
566
+ return this._value;
567
+ }
568
+ set(t) {
569
+ if (isNullOrUndefined(t))
570
+ throw new ErrorSetEmptyOptional();
571
+ this._value = t;
572
+ this._present = true;
573
+ }
574
+ setNullable(t) {
575
+ if (isDefined(t)) {
576
+ return this.set(t);
577
+ }
578
+ return this;
579
+ }
580
+ clear() {
581
+ this._value = void 0;
582
+ this._present = false;
583
+ }
584
+ isEmpty() {
585
+ return !this._present;
586
+ }
587
+ isPresent() {
588
+ return this._present;
589
+ }
590
+ ifEmpty(callback) {
591
+ if (this.isEmpty())
592
+ callback();
593
+ }
594
+ ifPresent(callback) {
595
+ if (this.isPresent())
596
+ callback(this.get());
597
+ }
598
+ ifPresentThenClear(callback) {
599
+ if (this.isPresent()) {
600
+ callback(this.get());
601
+ this.clear();
602
+ }
603
+ }
604
+ apply(callbackIfPresent, callbackIfEmpty) {
605
+ if (this.isEmpty()) {
606
+ callbackIfEmpty();
607
+ } else {
608
+ callbackIfPresent(this.get());
609
+ }
610
+ }
611
+ orElseReturn(newValue) {
612
+ if (this.isPresent()) {
613
+ return this.get();
614
+ } else {
615
+ return newValue;
616
+ }
617
+ }
618
+ orElse = this.orElseReturn.bind(this);
619
+ orElseProduce(newValueProducer) {
620
+ if (this.isPresent()) {
621
+ return this.get();
622
+ } else {
623
+ return newValueProducer();
624
+ }
625
+ }
626
+ orElseGet = this.orElseProduce.bind(this);
627
+ orElseReturnAndApply(newValue) {
628
+ if (this.isPresent()) {
629
+ return this.get();
630
+ } else {
631
+ this.set(newValue);
632
+ return newValue;
633
+ }
634
+ }
635
+ orElseProduceAndApply(newValueProducer) {
636
+ if (this.isPresent()) {
637
+ return this.get();
638
+ } else {
639
+ const newValue = newValueProducer();
640
+ this.set(newValue);
641
+ return newValue;
642
+ }
643
+ }
644
+ orElseReturnNullableAndApply(newValue) {
645
+ if (this.isPresent()) {
646
+ return this;
647
+ } else {
648
+ this.setNullable(newValue);
649
+ return this;
650
+ }
651
+ }
652
+ orElseProduceNullableAndApply(newValueProducer) {
653
+ if (this.isPresent()) {
654
+ return this;
655
+ } else {
656
+ const newValue = newValueProducer();
657
+ this.setNullable(newValue);
658
+ return this;
659
+ }
660
+ }
661
+ orElseReturnNullable(newValue) {
662
+ if (this.isEmpty()) return _Optional.ofNullable(newValue);
663
+ return this;
664
+ }
665
+ orElseNullable = this.orElseReturnNullable.bind(this);
666
+ orElseProduceNullable(newValueProducer) {
667
+ if (this.isEmpty()) {
668
+ const newValue = newValueProducer();
669
+ return _Optional.ofNullable(newValue);
670
+ }
671
+ return this;
672
+ }
673
+ orElseGetNullable = this.orElseProduceNullable.bind(this);
674
+ orElseThrow(errorProducer) {
675
+ if (this.isEmpty())
676
+ throw errorProducer();
677
+ return this;
678
+ }
679
+ mapTo(mapper) {
680
+ return this.flatMapTo((t) => _Optional.ofNullable(mapper(t)));
681
+ }
682
+ flatMapTo(mapper) {
683
+ return this.isPresent() ? mapper(this.get()) : _Optional.empty();
684
+ }
685
+ filter(predicate) {
686
+ if (this.isEmpty())
687
+ return this;
688
+ if (predicate(this.get()))
689
+ return this;
690
+ return _Optional.empty();
691
+ }
692
+ static empty() {
693
+ return new _Optional(void 0);
694
+ }
695
+ static of(t) {
696
+ if (isNullOrUndefined(t))
697
+ throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
698
+ return new _Optional(t);
699
+ }
700
+ static ofNullable(t) {
701
+ return new _Optional(t);
702
+ }
703
+ };
704
+ var Optional_default = Optional;
705
+ var ErrorGetEmptyOptional = class extends Error {
706
+ constructor() {
707
+ super("Cannot retrieve a value from an empty Optional.");
708
+ }
709
+ };
710
+ var ErrorSetEmptyOptional = class extends Error {
711
+ constructor() {
712
+ super("Cannot set a null or undefined value.");
713
+ }
714
+ };
715
+ var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
716
+ constructor() {
717
+ super("Cannot initialize a PresentOptional with a null or undefined value.");
718
+ }
719
+ };
720
+
544
721
  // src/utils/arrays/groupBy.ts
545
722
  function groupByString(arr, field) {
546
723
  return groupByStringWith(arr, (t) => t[field]);
@@ -600,8 +777,12 @@ function indexBySymbolWith(arr, getter) {
600
777
  function doIndexByWith(arr, getter) {
601
778
  return arr.reduce((dict, cur) => {
602
779
  const key = getter(cur);
603
- if (key !== null && key !== void 0)
780
+ if (key !== null && key !== void 0) {
781
+ if (dict[key]) {
782
+ throw new Error(`Multiple values indexed by same key "${String(key)}".`);
783
+ }
604
784
  dict[key] = cur;
785
+ }
605
786
  return dict;
606
787
  }, {});
607
788
  }
@@ -860,6 +1041,19 @@ function shallowArrayEquals(a, b) {
860
1041
  }
861
1042
  return true;
862
1043
  }
1044
+ function findInArray(arr, predicate) {
1045
+ return Optional_default.ofNullable(arr.find(predicate));
1046
+ }
1047
+ function zip(ts, rs) {
1048
+ if (ts.length !== rs.length)
1049
+ throw new Error(`Arrays must have the same length. Got ${ts.length} and ${rs.length}`);
1050
+ return ts.map((t, i) => [t, rs[i]]);
1051
+ }
1052
+ function unzip(arr) {
1053
+ return arr.reduce(([ts, rs], [t, r]) => {
1054
+ return [[...ts, t], [...rs, r]];
1055
+ }, [[], []]);
1056
+ }
863
1057
 
864
1058
  // src/utils/booleans.ts
865
1059
  function isTrue(x) {
@@ -972,6 +1166,11 @@ function getCauseStackFromError(error) {
972
1166
  return `
973
1167
  caused by: ${getStackFromError(asError(error.cause))}`;
974
1168
  }
1169
+ var NonExhaustiveSwitchError = class extends Error {
1170
+ constructor(value) {
1171
+ super("Expected switch to be exhaustive, got: " + value);
1172
+ }
1173
+ };
975
1174
 
976
1175
  // src/utils/json.ts
977
1176
  function tryToParseJson(jsonContent) {
@@ -2311,178 +2510,6 @@ var Logger = class {
2311
2510
  }
2312
2511
  };
2313
2512
 
2314
- // src/Optional.ts
2315
- var Optional = class _Optional {
2316
- _present;
2317
- _value;
2318
- constructor(t) {
2319
- const defined = isDefined(t);
2320
- this._value = defined ? t : void 0;
2321
- this._present = defined;
2322
- }
2323
- getRawValue() {
2324
- return this._value;
2325
- }
2326
- get() {
2327
- return this.getOrElseThrow(() => new ErrorGetEmptyOptional());
2328
- }
2329
- getOrElseThrow(errorProducer) {
2330
- if (this.isEmpty())
2331
- throw errorProducer();
2332
- return this._value;
2333
- }
2334
- set(t) {
2335
- if (isNullOrUndefined(t))
2336
- throw new ErrorSetEmptyOptional();
2337
- this._value = t;
2338
- this._present = true;
2339
- }
2340
- setNullable(t) {
2341
- if (isDefined(t)) {
2342
- return this.set(t);
2343
- }
2344
- return this;
2345
- }
2346
- clear() {
2347
- this._value = void 0;
2348
- this._present = false;
2349
- }
2350
- isEmpty() {
2351
- return !this._present;
2352
- }
2353
- isPresent() {
2354
- return this._present;
2355
- }
2356
- ifEmpty(callback) {
2357
- if (this.isEmpty())
2358
- callback();
2359
- }
2360
- ifPresent(callback) {
2361
- if (this.isPresent())
2362
- callback(this.get());
2363
- }
2364
- ifPresentThenClear(callback) {
2365
- if (this.isPresent()) {
2366
- callback(this.get());
2367
- this.clear();
2368
- }
2369
- }
2370
- apply(callbackIfPresent, callbackIfEmpty) {
2371
- if (this.isEmpty()) {
2372
- callbackIfEmpty();
2373
- } else {
2374
- callbackIfPresent(this.get());
2375
- }
2376
- }
2377
- orElseReturn(newValue) {
2378
- if (this.isPresent()) {
2379
- return this.get();
2380
- } else {
2381
- return newValue;
2382
- }
2383
- }
2384
- orElse = this.orElseReturn.bind(this);
2385
- orElseProduce(newValueProducer) {
2386
- if (this.isPresent()) {
2387
- return this.get();
2388
- } else {
2389
- return newValueProducer();
2390
- }
2391
- }
2392
- orElseGet = this.orElseProduce.bind(this);
2393
- orElseReturnAndApply(newValue) {
2394
- if (this.isPresent()) {
2395
- return this.get();
2396
- } else {
2397
- this.set(newValue);
2398
- return newValue;
2399
- }
2400
- }
2401
- orElseProduceAndApply(newValueProducer) {
2402
- if (this.isPresent()) {
2403
- return this.get();
2404
- } else {
2405
- const newValue = newValueProducer();
2406
- this.set(newValue);
2407
- return newValue;
2408
- }
2409
- }
2410
- orElseReturnNullableAndApply(newValue) {
2411
- if (this.isPresent()) {
2412
- return this;
2413
- } else {
2414
- this.setNullable(newValue);
2415
- return this;
2416
- }
2417
- }
2418
- orElseProduceNullableAndApply(newValueProducer) {
2419
- if (this.isPresent()) {
2420
- return this;
2421
- } else {
2422
- const newValue = newValueProducer();
2423
- this.setNullable(newValue);
2424
- return this;
2425
- }
2426
- }
2427
- orElseReturnNullable(newValue) {
2428
- if (this.isEmpty()) return _Optional.ofNullable(newValue);
2429
- return this;
2430
- }
2431
- orElseNullable = this.orElseReturnNullable.bind(this);
2432
- orElseProduceNullable(newValueProducer) {
2433
- if (this.isEmpty()) {
2434
- const newValue = newValueProducer();
2435
- return _Optional.ofNullable(newValue);
2436
- }
2437
- return this;
2438
- }
2439
- orElseGetNullable = this.orElseProduceNullable.bind(this);
2440
- orElseThrow(errorProducer) {
2441
- if (this.isEmpty())
2442
- throw errorProducer();
2443
- return this;
2444
- }
2445
- mapTo(mapper) {
2446
- return this.flatMapTo((t) => _Optional.ofNullable(mapper(t)));
2447
- }
2448
- flatMapTo(mapper) {
2449
- return this.isPresent() ? mapper(this.get()) : _Optional.empty();
2450
- }
2451
- filter(predicate) {
2452
- if (this.isEmpty())
2453
- return this;
2454
- if (predicate(this.get()))
2455
- return this;
2456
- return _Optional.empty();
2457
- }
2458
- static empty() {
2459
- return new _Optional(void 0);
2460
- }
2461
- static of(t) {
2462
- if (isNullOrUndefined(t))
2463
- throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
2464
- return new _Optional(t);
2465
- }
2466
- static ofNullable(t) {
2467
- return new _Optional(t);
2468
- }
2469
- };
2470
- var ErrorGetEmptyOptional = class extends Error {
2471
- constructor() {
2472
- super("Cannot retrieve a value from an empty Optional.");
2473
- }
2474
- };
2475
- var ErrorSetEmptyOptional = class extends Error {
2476
- constructor() {
2477
- super("Cannot set a null or undefined value.");
2478
- }
2479
- };
2480
- var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
2481
- constructor() {
2482
- super("Cannot initialize a PresentOptional with a null or undefined value.");
2483
- }
2484
- };
2485
-
2486
2513
  // src/sorting/Sorter.ts
2487
2514
  var defaultCompareValuesOptions = {
2488
2515
  nullsFirst: false
@@ -2609,7 +2636,7 @@ var next = (fns, transform) => {
2609
2636
  return compareNumbers(fns, transform, { direction: "DESC", ...opts });
2610
2637
  }
2611
2638
  };
2612
- const retForStrings = {
2639
+ const retForStringsV1 = {
2613
2640
  ...retAsUsing,
2614
2641
  inLexographicalOrder(opts = {}) {
2615
2642
  return compareStrings(fns, transform, { direction: "ASC", ignoreCase: false, ...opts });
@@ -2624,6 +2651,22 @@ var next = (fns, transform) => {
2624
2651
  return compareStrings(fns, transform, { direction: "DESC", ignoreCase: true, ...opts });
2625
2652
  }
2626
2653
  };
2654
+ const retForStrings = {
2655
+ ...retAsUsing,
2656
+ ...retForStringsV1,
2657
+ inLexicographicalOrder(opts = {}) {
2658
+ return compareStrings(fns, transform, { direction: "ASC", ignoreCase: false, ...opts });
2659
+ },
2660
+ inLexicographicalOrderIgnoringCase(opts = {}) {
2661
+ return compareStrings(fns, transform, { direction: "ASC", ignoreCase: true, ...opts });
2662
+ },
2663
+ inReverseLexicographicalOrder(opts = {}) {
2664
+ return compareStrings(fns, transform, { direction: "DESC", ignoreCase: false, ...opts });
2665
+ },
2666
+ inReverseLexicographicalOrderIgnoringCase(opts = {}) {
2667
+ return compareStrings(fns, transform, { direction: "DESC", ignoreCase: true, ...opts });
2668
+ }
2669
+ };
2627
2670
  const retForBooleans = {
2628
2671
  ...retAsUsing,
2629
2672
  truesFirst(opts = {}) {
@@ -2951,6 +2994,7 @@ function isUpgradable(obj) {
2951
2994
  LazyDictionary,
2952
2995
  Logger,
2953
2996
  NEVER,
2997
+ NonExhaustiveSwitchError,
2954
2998
  Optional,
2955
2999
  PredicateBuilder,
2956
3000
  RandomTimeDuration,
@@ -3006,6 +3050,7 @@ function isUpgradable(obj) {
3006
3050
  filterMap,
3007
3051
  filterMapReduce,
3008
3052
  filterWithTypePredicate,
3053
+ findInArray,
3009
3054
  first,
3010
3055
  flatMapTruthys,
3011
3056
  getCauseMessageFromError,
@@ -3107,10 +3152,12 @@ function isUpgradable(obj) {
3107
3152
  uniq,
3108
3153
  uniqBy,
3109
3154
  uniqByKey,
3155
+ unzip,
3110
3156
  upsert,
3111
3157
  withTryCatch,
3112
3158
  withTryCatchAsync,
3113
3159
  wrapWithString,
3114
- xor
3160
+ xor,
3161
+ zip
3115
3162
  });
3116
3163
  //# sourceMappingURL=index.cjs.map