@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.mjs CHANGED
@@ -357,6 +357,179 @@ function throwIfNullOrUndefined(source, errorProducer = () => new Error(`Unexpec
357
357
  });
358
358
  }
359
359
 
360
+ // src/Optional.ts
361
+ var Optional = class _Optional {
362
+ _present;
363
+ _value;
364
+ constructor(t) {
365
+ const defined = isDefined(t);
366
+ this._value = defined ? t : void 0;
367
+ this._present = defined;
368
+ }
369
+ getRawValue() {
370
+ return this._value;
371
+ }
372
+ get() {
373
+ return this.getOrElseThrow(() => new ErrorGetEmptyOptional());
374
+ }
375
+ getOrElseThrow(errorProducer) {
376
+ if (this.isEmpty())
377
+ throw errorProducer();
378
+ return this._value;
379
+ }
380
+ set(t) {
381
+ if (isNullOrUndefined(t))
382
+ throw new ErrorSetEmptyOptional();
383
+ this._value = t;
384
+ this._present = true;
385
+ }
386
+ setNullable(t) {
387
+ if (isDefined(t)) {
388
+ return this.set(t);
389
+ }
390
+ return this;
391
+ }
392
+ clear() {
393
+ this._value = void 0;
394
+ this._present = false;
395
+ }
396
+ isEmpty() {
397
+ return !this._present;
398
+ }
399
+ isPresent() {
400
+ return this._present;
401
+ }
402
+ ifEmpty(callback) {
403
+ if (this.isEmpty())
404
+ callback();
405
+ }
406
+ ifPresent(callback) {
407
+ if (this.isPresent())
408
+ callback(this.get());
409
+ }
410
+ ifPresentThenClear(callback) {
411
+ if (this.isPresent()) {
412
+ callback(this.get());
413
+ this.clear();
414
+ }
415
+ }
416
+ apply(callbackIfPresent, callbackIfEmpty) {
417
+ if (this.isEmpty()) {
418
+ callbackIfEmpty();
419
+ } else {
420
+ callbackIfPresent(this.get());
421
+ }
422
+ }
423
+ orElseReturn(newValue) {
424
+ if (this.isPresent()) {
425
+ return this.get();
426
+ } else {
427
+ return newValue;
428
+ }
429
+ }
430
+ orElse = this.orElseReturn.bind(this);
431
+ orElseProduce(newValueProducer) {
432
+ if (this.isPresent()) {
433
+ return this.get();
434
+ } else {
435
+ return newValueProducer();
436
+ }
437
+ }
438
+ orElseGet = this.orElseProduce.bind(this);
439
+ orElseReturnAndApply(newValue) {
440
+ if (this.isPresent()) {
441
+ return this.get();
442
+ } else {
443
+ this.set(newValue);
444
+ return newValue;
445
+ }
446
+ }
447
+ orElseProduceAndApply(newValueProducer) {
448
+ if (this.isPresent()) {
449
+ return this.get();
450
+ } else {
451
+ const newValue = newValueProducer();
452
+ this.set(newValue);
453
+ return newValue;
454
+ }
455
+ }
456
+ orElseReturnNullableAndApply(newValue) {
457
+ if (this.isPresent()) {
458
+ return this;
459
+ } else {
460
+ this.setNullable(newValue);
461
+ return this;
462
+ }
463
+ }
464
+ orElseProduceNullableAndApply(newValueProducer) {
465
+ if (this.isPresent()) {
466
+ return this;
467
+ } else {
468
+ const newValue = newValueProducer();
469
+ this.setNullable(newValue);
470
+ return this;
471
+ }
472
+ }
473
+ orElseReturnNullable(newValue) {
474
+ if (this.isEmpty()) return _Optional.ofNullable(newValue);
475
+ return this;
476
+ }
477
+ orElseNullable = this.orElseReturnNullable.bind(this);
478
+ orElseProduceNullable(newValueProducer) {
479
+ if (this.isEmpty()) {
480
+ const newValue = newValueProducer();
481
+ return _Optional.ofNullable(newValue);
482
+ }
483
+ return this;
484
+ }
485
+ orElseGetNullable = this.orElseProduceNullable.bind(this);
486
+ orElseThrow(errorProducer) {
487
+ if (this.isEmpty())
488
+ throw errorProducer();
489
+ return this;
490
+ }
491
+ mapTo(mapper) {
492
+ return this.flatMapTo((t) => _Optional.ofNullable(mapper(t)));
493
+ }
494
+ flatMapTo(mapper) {
495
+ return this.isPresent() ? mapper(this.get()) : _Optional.empty();
496
+ }
497
+ filter(predicate) {
498
+ if (this.isEmpty())
499
+ return this;
500
+ if (predicate(this.get()))
501
+ return this;
502
+ return _Optional.empty();
503
+ }
504
+ static empty() {
505
+ return new _Optional(void 0);
506
+ }
507
+ static of(t) {
508
+ if (isNullOrUndefined(t))
509
+ throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
510
+ return new _Optional(t);
511
+ }
512
+ static ofNullable(t) {
513
+ return new _Optional(t);
514
+ }
515
+ };
516
+ var Optional_default = Optional;
517
+ var ErrorGetEmptyOptional = class extends Error {
518
+ constructor() {
519
+ super("Cannot retrieve a value from an empty Optional.");
520
+ }
521
+ };
522
+ var ErrorSetEmptyOptional = class extends Error {
523
+ constructor() {
524
+ super("Cannot set a null or undefined value.");
525
+ }
526
+ };
527
+ var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
528
+ constructor() {
529
+ super("Cannot initialize a PresentOptional with a null or undefined value.");
530
+ }
531
+ };
532
+
360
533
  // src/utils/arrays/groupBy.ts
361
534
  function groupByString(arr, field) {
362
535
  return groupByStringWith(arr, (t) => t[field]);
@@ -416,8 +589,12 @@ function indexBySymbolWith(arr, getter) {
416
589
  function doIndexByWith(arr, getter) {
417
590
  return arr.reduce((dict, cur) => {
418
591
  const key = getter(cur);
419
- if (key !== null && key !== void 0)
592
+ if (key !== null && key !== void 0) {
593
+ if (dict[key]) {
594
+ throw new Error(`Multiple values indexed by same key "${String(key)}".`);
595
+ }
420
596
  dict[key] = cur;
597
+ }
421
598
  return dict;
422
599
  }, {});
423
600
  }
@@ -676,6 +853,19 @@ function shallowArrayEquals(a, b) {
676
853
  }
677
854
  return true;
678
855
  }
856
+ function findInArray(arr, predicate) {
857
+ return Optional_default.ofNullable(arr.find(predicate));
858
+ }
859
+ function zip(ts, rs) {
860
+ if (ts.length !== rs.length)
861
+ throw new Error(`Arrays must have the same length. Got ${ts.length} and ${rs.length}`);
862
+ return ts.map((t, i) => [t, rs[i]]);
863
+ }
864
+ function unzip(arr) {
865
+ return arr.reduce(([ts, rs], [t, r]) => {
866
+ return [[...ts, t], [...rs, r]];
867
+ }, [[], []]);
868
+ }
679
869
 
680
870
  // src/utils/booleans.ts
681
871
  function isTrue(x) {
@@ -788,6 +978,11 @@ function getCauseStackFromError(error) {
788
978
  return `
789
979
  caused by: ${getStackFromError(asError(error.cause))}`;
790
980
  }
981
+ var NonExhaustiveSwitchError = class extends Error {
982
+ constructor(value) {
983
+ super("Expected switch to be exhaustive, got: " + value);
984
+ }
985
+ };
791
986
 
792
987
  // src/utils/json.ts
793
988
  function tryToParseJson(jsonContent) {
@@ -2127,178 +2322,6 @@ var Logger = class {
2127
2322
  }
2128
2323
  };
2129
2324
 
2130
- // src/Optional.ts
2131
- var Optional = class _Optional {
2132
- _present;
2133
- _value;
2134
- constructor(t) {
2135
- const defined = isDefined(t);
2136
- this._value = defined ? t : void 0;
2137
- this._present = defined;
2138
- }
2139
- getRawValue() {
2140
- return this._value;
2141
- }
2142
- get() {
2143
- return this.getOrElseThrow(() => new ErrorGetEmptyOptional());
2144
- }
2145
- getOrElseThrow(errorProducer) {
2146
- if (this.isEmpty())
2147
- throw errorProducer();
2148
- return this._value;
2149
- }
2150
- set(t) {
2151
- if (isNullOrUndefined(t))
2152
- throw new ErrorSetEmptyOptional();
2153
- this._value = t;
2154
- this._present = true;
2155
- }
2156
- setNullable(t) {
2157
- if (isDefined(t)) {
2158
- return this.set(t);
2159
- }
2160
- return this;
2161
- }
2162
- clear() {
2163
- this._value = void 0;
2164
- this._present = false;
2165
- }
2166
- isEmpty() {
2167
- return !this._present;
2168
- }
2169
- isPresent() {
2170
- return this._present;
2171
- }
2172
- ifEmpty(callback) {
2173
- if (this.isEmpty())
2174
- callback();
2175
- }
2176
- ifPresent(callback) {
2177
- if (this.isPresent())
2178
- callback(this.get());
2179
- }
2180
- ifPresentThenClear(callback) {
2181
- if (this.isPresent()) {
2182
- callback(this.get());
2183
- this.clear();
2184
- }
2185
- }
2186
- apply(callbackIfPresent, callbackIfEmpty) {
2187
- if (this.isEmpty()) {
2188
- callbackIfEmpty();
2189
- } else {
2190
- callbackIfPresent(this.get());
2191
- }
2192
- }
2193
- orElseReturn(newValue) {
2194
- if (this.isPresent()) {
2195
- return this.get();
2196
- } else {
2197
- return newValue;
2198
- }
2199
- }
2200
- orElse = this.orElseReturn.bind(this);
2201
- orElseProduce(newValueProducer) {
2202
- if (this.isPresent()) {
2203
- return this.get();
2204
- } else {
2205
- return newValueProducer();
2206
- }
2207
- }
2208
- orElseGet = this.orElseProduce.bind(this);
2209
- orElseReturnAndApply(newValue) {
2210
- if (this.isPresent()) {
2211
- return this.get();
2212
- } else {
2213
- this.set(newValue);
2214
- return newValue;
2215
- }
2216
- }
2217
- orElseProduceAndApply(newValueProducer) {
2218
- if (this.isPresent()) {
2219
- return this.get();
2220
- } else {
2221
- const newValue = newValueProducer();
2222
- this.set(newValue);
2223
- return newValue;
2224
- }
2225
- }
2226
- orElseReturnNullableAndApply(newValue) {
2227
- if (this.isPresent()) {
2228
- return this;
2229
- } else {
2230
- this.setNullable(newValue);
2231
- return this;
2232
- }
2233
- }
2234
- orElseProduceNullableAndApply(newValueProducer) {
2235
- if (this.isPresent()) {
2236
- return this;
2237
- } else {
2238
- const newValue = newValueProducer();
2239
- this.setNullable(newValue);
2240
- return this;
2241
- }
2242
- }
2243
- orElseReturnNullable(newValue) {
2244
- if (this.isEmpty()) return _Optional.ofNullable(newValue);
2245
- return this;
2246
- }
2247
- orElseNullable = this.orElseReturnNullable.bind(this);
2248
- orElseProduceNullable(newValueProducer) {
2249
- if (this.isEmpty()) {
2250
- const newValue = newValueProducer();
2251
- return _Optional.ofNullable(newValue);
2252
- }
2253
- return this;
2254
- }
2255
- orElseGetNullable = this.orElseProduceNullable.bind(this);
2256
- orElseThrow(errorProducer) {
2257
- if (this.isEmpty())
2258
- throw errorProducer();
2259
- return this;
2260
- }
2261
- mapTo(mapper) {
2262
- return this.flatMapTo((t) => _Optional.ofNullable(mapper(t)));
2263
- }
2264
- flatMapTo(mapper) {
2265
- return this.isPresent() ? mapper(this.get()) : _Optional.empty();
2266
- }
2267
- filter(predicate) {
2268
- if (this.isEmpty())
2269
- return this;
2270
- if (predicate(this.get()))
2271
- return this;
2272
- return _Optional.empty();
2273
- }
2274
- static empty() {
2275
- return new _Optional(void 0);
2276
- }
2277
- static of(t) {
2278
- if (isNullOrUndefined(t))
2279
- throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
2280
- return new _Optional(t);
2281
- }
2282
- static ofNullable(t) {
2283
- return new _Optional(t);
2284
- }
2285
- };
2286
- var ErrorGetEmptyOptional = class extends Error {
2287
- constructor() {
2288
- super("Cannot retrieve a value from an empty Optional.");
2289
- }
2290
- };
2291
- var ErrorSetEmptyOptional = class extends Error {
2292
- constructor() {
2293
- super("Cannot set a null or undefined value.");
2294
- }
2295
- };
2296
- var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
2297
- constructor() {
2298
- super("Cannot initialize a PresentOptional with a null or undefined value.");
2299
- }
2300
- };
2301
-
2302
2325
  // src/sorting/Sorter.ts
2303
2326
  var defaultCompareValuesOptions = {
2304
2327
  nullsFirst: false
@@ -2425,7 +2448,7 @@ var next = (fns, transform) => {
2425
2448
  return compareNumbers(fns, transform, { direction: "DESC", ...opts });
2426
2449
  }
2427
2450
  };
2428
- const retForStrings = {
2451
+ const retForStringsV1 = {
2429
2452
  ...retAsUsing,
2430
2453
  inLexographicalOrder(opts = {}) {
2431
2454
  return compareStrings(fns, transform, { direction: "ASC", ignoreCase: false, ...opts });
@@ -2440,6 +2463,22 @@ var next = (fns, transform) => {
2440
2463
  return compareStrings(fns, transform, { direction: "DESC", ignoreCase: true, ...opts });
2441
2464
  }
2442
2465
  };
2466
+ const retForStrings = {
2467
+ ...retAsUsing,
2468
+ ...retForStringsV1,
2469
+ inLexicographicalOrder(opts = {}) {
2470
+ return compareStrings(fns, transform, { direction: "ASC", ignoreCase: false, ...opts });
2471
+ },
2472
+ inLexicographicalOrderIgnoringCase(opts = {}) {
2473
+ return compareStrings(fns, transform, { direction: "ASC", ignoreCase: true, ...opts });
2474
+ },
2475
+ inReverseLexicographicalOrder(opts = {}) {
2476
+ return compareStrings(fns, transform, { direction: "DESC", ignoreCase: false, ...opts });
2477
+ },
2478
+ inReverseLexicographicalOrderIgnoringCase(opts = {}) {
2479
+ return compareStrings(fns, transform, { direction: "DESC", ignoreCase: true, ...opts });
2480
+ }
2481
+ };
2443
2482
  const retForBooleans = {
2444
2483
  ...retAsUsing,
2445
2484
  truesFirst(opts = {}) {
@@ -2766,6 +2805,7 @@ export {
2766
2805
  LazyDictionary,
2767
2806
  Logger,
2768
2807
  NEVER,
2808
+ NonExhaustiveSwitchError,
2769
2809
  Optional,
2770
2810
  PredicateBuilder,
2771
2811
  RandomTimeDuration,
@@ -2821,6 +2861,7 @@ export {
2821
2861
  filterMap,
2822
2862
  filterMapReduce,
2823
2863
  filterWithTypePredicate,
2864
+ findInArray,
2824
2865
  first,
2825
2866
  flatMapTruthys,
2826
2867
  getCauseMessageFromError,
@@ -2922,10 +2963,12 @@ export {
2922
2963
  uniq,
2923
2964
  uniqBy,
2924
2965
  uniqByKey,
2966
+ unzip,
2925
2967
  upsert,
2926
2968
  withTryCatch,
2927
2969
  withTryCatchAsync,
2928
2970
  wrapWithString,
2929
- xor
2971
+ xor,
2972
+ zip
2930
2973
  };
2931
2974
  //# sourceMappingURL=index.mjs.map