@zelgadis87/utils-core 5.2.2 → 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/CHANGELOG.md +8 -0
- package/dist/sorting/Sorter.d.ts +16 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/arrays.d.ts +4 -0
- package/esbuild/index.cjs +211 -175
- package/esbuild/index.cjs.map +3 -3
- package/esbuild/index.mjs +207 -174
- package/esbuild/index.mjs.map +3 -3
- package/package.json +1 -1
- package/src/sorting/Sorter.ts +25 -5
- package/src/utils/arrays.ts +17 -0
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]);
|
|
@@ -680,6 +853,19 @@ function shallowArrayEquals(a, b) {
|
|
|
680
853
|
}
|
|
681
854
|
return true;
|
|
682
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
|
+
}
|
|
683
869
|
|
|
684
870
|
// src/utils/booleans.ts
|
|
685
871
|
function isTrue(x) {
|
|
@@ -2136,178 +2322,6 @@ var Logger = class {
|
|
|
2136
2322
|
}
|
|
2137
2323
|
};
|
|
2138
2324
|
|
|
2139
|
-
// src/Optional.ts
|
|
2140
|
-
var Optional = class _Optional {
|
|
2141
|
-
_present;
|
|
2142
|
-
_value;
|
|
2143
|
-
constructor(t) {
|
|
2144
|
-
const defined = isDefined(t);
|
|
2145
|
-
this._value = defined ? t : void 0;
|
|
2146
|
-
this._present = defined;
|
|
2147
|
-
}
|
|
2148
|
-
getRawValue() {
|
|
2149
|
-
return this._value;
|
|
2150
|
-
}
|
|
2151
|
-
get() {
|
|
2152
|
-
return this.getOrElseThrow(() => new ErrorGetEmptyOptional());
|
|
2153
|
-
}
|
|
2154
|
-
getOrElseThrow(errorProducer) {
|
|
2155
|
-
if (this.isEmpty())
|
|
2156
|
-
throw errorProducer();
|
|
2157
|
-
return this._value;
|
|
2158
|
-
}
|
|
2159
|
-
set(t) {
|
|
2160
|
-
if (isNullOrUndefined(t))
|
|
2161
|
-
throw new ErrorSetEmptyOptional();
|
|
2162
|
-
this._value = t;
|
|
2163
|
-
this._present = true;
|
|
2164
|
-
}
|
|
2165
|
-
setNullable(t) {
|
|
2166
|
-
if (isDefined(t)) {
|
|
2167
|
-
return this.set(t);
|
|
2168
|
-
}
|
|
2169
|
-
return this;
|
|
2170
|
-
}
|
|
2171
|
-
clear() {
|
|
2172
|
-
this._value = void 0;
|
|
2173
|
-
this._present = false;
|
|
2174
|
-
}
|
|
2175
|
-
isEmpty() {
|
|
2176
|
-
return !this._present;
|
|
2177
|
-
}
|
|
2178
|
-
isPresent() {
|
|
2179
|
-
return this._present;
|
|
2180
|
-
}
|
|
2181
|
-
ifEmpty(callback) {
|
|
2182
|
-
if (this.isEmpty())
|
|
2183
|
-
callback();
|
|
2184
|
-
}
|
|
2185
|
-
ifPresent(callback) {
|
|
2186
|
-
if (this.isPresent())
|
|
2187
|
-
callback(this.get());
|
|
2188
|
-
}
|
|
2189
|
-
ifPresentThenClear(callback) {
|
|
2190
|
-
if (this.isPresent()) {
|
|
2191
|
-
callback(this.get());
|
|
2192
|
-
this.clear();
|
|
2193
|
-
}
|
|
2194
|
-
}
|
|
2195
|
-
apply(callbackIfPresent, callbackIfEmpty) {
|
|
2196
|
-
if (this.isEmpty()) {
|
|
2197
|
-
callbackIfEmpty();
|
|
2198
|
-
} else {
|
|
2199
|
-
callbackIfPresent(this.get());
|
|
2200
|
-
}
|
|
2201
|
-
}
|
|
2202
|
-
orElseReturn(newValue) {
|
|
2203
|
-
if (this.isPresent()) {
|
|
2204
|
-
return this.get();
|
|
2205
|
-
} else {
|
|
2206
|
-
return newValue;
|
|
2207
|
-
}
|
|
2208
|
-
}
|
|
2209
|
-
orElse = this.orElseReturn.bind(this);
|
|
2210
|
-
orElseProduce(newValueProducer) {
|
|
2211
|
-
if (this.isPresent()) {
|
|
2212
|
-
return this.get();
|
|
2213
|
-
} else {
|
|
2214
|
-
return newValueProducer();
|
|
2215
|
-
}
|
|
2216
|
-
}
|
|
2217
|
-
orElseGet = this.orElseProduce.bind(this);
|
|
2218
|
-
orElseReturnAndApply(newValue) {
|
|
2219
|
-
if (this.isPresent()) {
|
|
2220
|
-
return this.get();
|
|
2221
|
-
} else {
|
|
2222
|
-
this.set(newValue);
|
|
2223
|
-
return newValue;
|
|
2224
|
-
}
|
|
2225
|
-
}
|
|
2226
|
-
orElseProduceAndApply(newValueProducer) {
|
|
2227
|
-
if (this.isPresent()) {
|
|
2228
|
-
return this.get();
|
|
2229
|
-
} else {
|
|
2230
|
-
const newValue = newValueProducer();
|
|
2231
|
-
this.set(newValue);
|
|
2232
|
-
return newValue;
|
|
2233
|
-
}
|
|
2234
|
-
}
|
|
2235
|
-
orElseReturnNullableAndApply(newValue) {
|
|
2236
|
-
if (this.isPresent()) {
|
|
2237
|
-
return this;
|
|
2238
|
-
} else {
|
|
2239
|
-
this.setNullable(newValue);
|
|
2240
|
-
return this;
|
|
2241
|
-
}
|
|
2242
|
-
}
|
|
2243
|
-
orElseProduceNullableAndApply(newValueProducer) {
|
|
2244
|
-
if (this.isPresent()) {
|
|
2245
|
-
return this;
|
|
2246
|
-
} else {
|
|
2247
|
-
const newValue = newValueProducer();
|
|
2248
|
-
this.setNullable(newValue);
|
|
2249
|
-
return this;
|
|
2250
|
-
}
|
|
2251
|
-
}
|
|
2252
|
-
orElseReturnNullable(newValue) {
|
|
2253
|
-
if (this.isEmpty()) return _Optional.ofNullable(newValue);
|
|
2254
|
-
return this;
|
|
2255
|
-
}
|
|
2256
|
-
orElseNullable = this.orElseReturnNullable.bind(this);
|
|
2257
|
-
orElseProduceNullable(newValueProducer) {
|
|
2258
|
-
if (this.isEmpty()) {
|
|
2259
|
-
const newValue = newValueProducer();
|
|
2260
|
-
return _Optional.ofNullable(newValue);
|
|
2261
|
-
}
|
|
2262
|
-
return this;
|
|
2263
|
-
}
|
|
2264
|
-
orElseGetNullable = this.orElseProduceNullable.bind(this);
|
|
2265
|
-
orElseThrow(errorProducer) {
|
|
2266
|
-
if (this.isEmpty())
|
|
2267
|
-
throw errorProducer();
|
|
2268
|
-
return this;
|
|
2269
|
-
}
|
|
2270
|
-
mapTo(mapper) {
|
|
2271
|
-
return this.flatMapTo((t) => _Optional.ofNullable(mapper(t)));
|
|
2272
|
-
}
|
|
2273
|
-
flatMapTo(mapper) {
|
|
2274
|
-
return this.isPresent() ? mapper(this.get()) : _Optional.empty();
|
|
2275
|
-
}
|
|
2276
|
-
filter(predicate) {
|
|
2277
|
-
if (this.isEmpty())
|
|
2278
|
-
return this;
|
|
2279
|
-
if (predicate(this.get()))
|
|
2280
|
-
return this;
|
|
2281
|
-
return _Optional.empty();
|
|
2282
|
-
}
|
|
2283
|
-
static empty() {
|
|
2284
|
-
return new _Optional(void 0);
|
|
2285
|
-
}
|
|
2286
|
-
static of(t) {
|
|
2287
|
-
if (isNullOrUndefined(t))
|
|
2288
|
-
throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
|
|
2289
|
-
return new _Optional(t);
|
|
2290
|
-
}
|
|
2291
|
-
static ofNullable(t) {
|
|
2292
|
-
return new _Optional(t);
|
|
2293
|
-
}
|
|
2294
|
-
};
|
|
2295
|
-
var ErrorGetEmptyOptional = class extends Error {
|
|
2296
|
-
constructor() {
|
|
2297
|
-
super("Cannot retrieve a value from an empty Optional.");
|
|
2298
|
-
}
|
|
2299
|
-
};
|
|
2300
|
-
var ErrorSetEmptyOptional = class extends Error {
|
|
2301
|
-
constructor() {
|
|
2302
|
-
super("Cannot set a null or undefined value.");
|
|
2303
|
-
}
|
|
2304
|
-
};
|
|
2305
|
-
var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
|
|
2306
|
-
constructor() {
|
|
2307
|
-
super("Cannot initialize a PresentOptional with a null or undefined value.");
|
|
2308
|
-
}
|
|
2309
|
-
};
|
|
2310
|
-
|
|
2311
2325
|
// src/sorting/Sorter.ts
|
|
2312
2326
|
var defaultCompareValuesOptions = {
|
|
2313
2327
|
nullsFirst: false
|
|
@@ -2434,7 +2448,7 @@ var next = (fns, transform) => {
|
|
|
2434
2448
|
return compareNumbers(fns, transform, { direction: "DESC", ...opts });
|
|
2435
2449
|
}
|
|
2436
2450
|
};
|
|
2437
|
-
const
|
|
2451
|
+
const retForStringsV1 = {
|
|
2438
2452
|
...retAsUsing,
|
|
2439
2453
|
inLexographicalOrder(opts = {}) {
|
|
2440
2454
|
return compareStrings(fns, transform, { direction: "ASC", ignoreCase: false, ...opts });
|
|
@@ -2449,6 +2463,22 @@ var next = (fns, transform) => {
|
|
|
2449
2463
|
return compareStrings(fns, transform, { direction: "DESC", ignoreCase: true, ...opts });
|
|
2450
2464
|
}
|
|
2451
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
|
+
};
|
|
2452
2482
|
const retForBooleans = {
|
|
2453
2483
|
...retAsUsing,
|
|
2454
2484
|
truesFirst(opts = {}) {
|
|
@@ -2831,6 +2861,7 @@ export {
|
|
|
2831
2861
|
filterMap,
|
|
2832
2862
|
filterMapReduce,
|
|
2833
2863
|
filterWithTypePredicate,
|
|
2864
|
+
findInArray,
|
|
2834
2865
|
first,
|
|
2835
2866
|
flatMapTruthys,
|
|
2836
2867
|
getCauseMessageFromError,
|
|
@@ -2932,10 +2963,12 @@ export {
|
|
|
2932
2963
|
uniq,
|
|
2933
2964
|
uniqBy,
|
|
2934
2965
|
uniqByKey,
|
|
2966
|
+
unzip,
|
|
2935
2967
|
upsert,
|
|
2936
2968
|
withTryCatch,
|
|
2937
2969
|
withTryCatchAsync,
|
|
2938
2970
|
wrapWithString,
|
|
2939
|
-
xor
|
|
2971
|
+
xor,
|
|
2972
|
+
zip
|
|
2940
2973
|
};
|
|
2941
2974
|
//# sourceMappingURL=index.mjs.map
|