@zelgadis87/utils-core 4.3.5 → 4.4.0
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/dist/Optional.d.ts +1 -1
- package/dist/async/RateThrottler.d.ts +2 -1
- package/dist/index.d.ts +5 -8
- package/dist/lazy/index.d.ts +2 -0
- package/dist/sorting/ComparisonChain.d.ts +1 -1
- package/dist/sorting/Sorter.d.ts +1 -1
- package/dist/sorting/types.d.ts +1 -1
- package/dist/time/TimeDuration.d.ts +1 -1
- package/dist/time/TimeFrequency.d.ts +1 -1
- package/dist/time/TimeRange.d.ts +2 -2
- package/dist/time/types.d.ts +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/upgrade/DataUpgrader.d.ts +1 -1
- package/dist/upgrade/getTransitionsPath.d.ts +1 -1
- package/dist/upgrade/types.d.ts +1 -1
- package/dist/utils/{groupBy.d.ts → arrays/groupBy.d.ts} +1 -1
- package/dist/utils/{indexBy.d.ts → arrays/indexBy.d.ts} +1 -1
- package/dist/utils/{math.d.ts → arrays/statistics.d.ts} +1 -2
- package/dist/utils/arrays/uniqBy.d.ts +4 -0
- package/dist/{types → utils}/arrays.d.ts +10 -4
- package/dist/utils/errors/withTryCatch.d.ts +3 -0
- package/dist/{types → utils}/errors.d.ts +1 -0
- package/dist/utils/functions/bindThis.d.ts +1 -0
- package/dist/utils/{iff.d.ts → functions/iff.d.ts} +3 -3
- package/dist/{types → utils}/functions.d.ts +4 -0
- package/dist/utils/index.d.ts +13 -19
- package/dist/utils/json.d.ts +11 -0
- package/dist/{types → utils}/numbers.d.ts +3 -1
- package/dist/utils/primitives.d.ts +3 -0
- package/dist/utils/random.d.ts +2 -0
- package/dist/utils/{entries.d.ts → records/entries.d.ts} +1 -1
- package/dist/{types → utils}/records.d.ts +1 -0
- package/dist/{types/strings.d.ts → utils/strings/StringParts.d.ts} +2 -15
- package/dist/utils/strings.d.ts +17 -0
- package/esbuild/index.cjs +542 -701
- package/esbuild/index.mjs +536 -697
- package/package.json +1 -1
- package/src/Logger.ts +1 -1
- package/src/Optional.ts +2 -2
- package/src/async/Deferred.ts +1 -1
- package/src/async/RateThrottler.ts +8 -1
- package/src/async/index.ts +1 -0
- package/src/index.ts +5 -8
- package/src/{Lazy.ts → lazy/Lazy.ts} +1 -1
- package/src/{LazyAsync.ts → lazy/LazyAsync.ts} +1 -1
- package/src/lazy/index.ts +3 -0
- package/src/sorting/ComparisonChain.ts +2 -2
- package/src/sorting/Sorter.ts +2 -2
- package/src/sorting/types.ts +1 -1
- package/src/time/RandomTimeDuration.ts +1 -1
- package/src/time/TimeDuration.ts +1 -2
- package/src/time/TimeFrequency.ts +1 -1
- package/src/time/TimeInstantBuilder.ts +2 -2
- package/src/time/TimeRange.ts +2 -2
- package/src/time/types.ts +1 -1
- package/src/upgrade/DataUpgrader.ts +1 -1
- package/src/upgrade/getTransitionsPath.ts +1 -1
- package/src/upgrade/types.ts +1 -1
- package/src/utils/{groupBy.ts → arrays/groupBy.ts} +1 -1
- package/src/utils/{indexBy.ts → arrays/indexBy.ts} +1 -1
- package/src/utils/{math.ts → arrays/statistics.ts} +1 -13
- package/src/utils/arrays/uniqBy.ts +24 -0
- package/src/{types → utils}/arrays.ts +15 -6
- package/src/utils/errors/withTryCatch.ts +15 -0
- package/src/{types → utils}/errors.ts +2 -0
- package/src/utils/functions/bindThis.ts +4 -0
- package/src/utils/{iff.ts → functions/iff.ts} +4 -2
- package/src/{types → utils}/functions.ts +6 -0
- package/src/utils/index.ts +13 -19
- package/src/utils/json.ts +49 -0
- package/src/utils/{round.ts → numbers/round.ts} +1 -0
- package/src/{types → utils}/numbers.ts +15 -1
- package/src/utils/primitives.ts +5 -0
- package/src/utils/random.ts +9 -0
- package/src/utils/{entries.ts → records/entries.ts} +1 -1
- package/src/{types → utils}/records.ts +2 -0
- package/src/utils/strings/StringParts.ts +114 -0
- package/src/utils/strings.ts +91 -0
- package/dist/random/index.d.ts +0 -2
- package/dist/random/randomInterval.d.ts +0 -1
- package/dist/random/randomPercentage.d.ts +0 -1
- package/dist/types/index.d.ts +0 -14
- package/dist/types/json.d.ts +0 -7
- package/dist/utils/bindThis.d.ts +0 -1
- package/dist/utils/jsonCloneDeep.d.ts +0 -2
- package/dist/utils/noop.d.ts +0 -1
- package/dist/utils/omit.d.ts +0 -2
- package/dist/utils/sortBy.d.ts +0 -7
- package/dist/utils/throttle.d.ts +0 -3
- package/dist/utils/uniq.d.ts +0 -1
- package/dist/utils/uniqBy.d.ts +0 -2
- package/dist/utils/uniqByKey.d.ts +0 -1
- package/dist/utils/upsert.d.ts +0 -0
- package/dist/utils/withTryCatch.d.ts +0 -2
- package/dist/utils/withTryCatchAsync.d.ts +0 -2
- package/dist/utils/wrap.d.ts +0 -1
- package/src/random/index.ts +0 -3
- package/src/random/randomInterval.ts +0 -4
- package/src/random/randomPercentage.ts +0 -6
- package/src/types/index.ts +0 -20
- package/src/types/json.ts +0 -15
- package/src/types/strings.ts +0 -191
- package/src/utils/bindThis.ts +0 -4
- package/src/utils/jsonCloneDeep.ts +0 -31
- package/src/utils/noop.ts +0 -2
- package/src/utils/omit.ts +0 -8
- package/src/utils/sortBy.ts +0 -27
- package/src/utils/throttle.ts +0 -10
- package/src/utils/uniq.ts +0 -6
- package/src/utils/uniqBy.ts +0 -15
- package/src/utils/uniqByKey.ts +0 -5
- package/src/utils/upsert.ts +0 -2
- package/src/utils/withTryCatch.ts +0 -9
- package/src/utils/withTryCatchAsync.ts +0 -5
- package/src/utils/wrap.ts +0 -4
- /package/dist/{Lazy.d.ts → lazy/Lazy.d.ts} +0 -0
- /package/dist/{LazyAsync.d.ts → lazy/LazyAsync.d.ts} +0 -0
- /package/dist/{types → utils}/booleans.d.ts +0 -0
- /package/dist/{types → utils}/empties.d.ts +0 -0
- /package/dist/utils/{constant.d.ts → functions/constant.d.ts} +0 -0
- /package/dist/{types → utils}/nulls.d.ts +0 -0
- /package/dist/utils/{round.d.ts → numbers/round.d.ts} +0 -0
- /package/dist/{types → utils}/promises.d.ts +0 -0
- /package/src/{types → utils}/booleans.ts +0 -0
- /package/src/{types → utils}/empties.ts +0 -0
- /package/src/utils/{constant.ts → functions/constant.ts} +0 -0
- /package/src/{types → utils}/nulls.ts +0 -0
- /package/src/{types → utils}/promises.ts +0 -0
package/esbuild/index.cjs
CHANGED
|
@@ -178,8 +178,6 @@ __export(src_exports, {
|
|
|
178
178
|
ErrorCannotInstantiatePresentOptionalWithEmptyValue: () => ErrorCannotInstantiatePresentOptionalWithEmptyValue,
|
|
179
179
|
ErrorGetEmptyOptional: () => ErrorGetEmptyOptional,
|
|
180
180
|
ErrorSetEmptyOptional: () => ErrorSetEmptyOptional,
|
|
181
|
-
Lazy: () => Lazy,
|
|
182
|
-
LazyAsync: () => LazyAsync,
|
|
183
181
|
Logger: () => Logger,
|
|
184
182
|
Optional: () => Optional,
|
|
185
183
|
RandomTimeDuration: () => RandomTimeDuration,
|
|
@@ -224,6 +222,7 @@ __export(src_exports, {
|
|
|
224
222
|
extendArray: () => extendArray,
|
|
225
223
|
extendArrayWith: () => extendArrayWith,
|
|
226
224
|
fill: () => fill,
|
|
225
|
+
first: () => first,
|
|
227
226
|
flatMapTruthys: () => flatMapTruthys,
|
|
228
227
|
groupByBoolean: () => groupByBoolean,
|
|
229
228
|
groupByBooleanWith: () => groupByBooleanWith,
|
|
@@ -274,10 +273,12 @@ __export(src_exports, {
|
|
|
274
273
|
multiplyBy: () => multiplyBy,
|
|
275
274
|
noop: () => noop,
|
|
276
275
|
omit: () => omit,
|
|
276
|
+
omitFromJsonObject: () => omitFromJsonObject,
|
|
277
277
|
pad: () => pad,
|
|
278
278
|
padLeft: () => padLeft,
|
|
279
279
|
padRight: () => padRight,
|
|
280
280
|
pick: () => pick,
|
|
281
|
+
pluralize: () => pluralize,
|
|
281
282
|
promiseSequence: () => promiseSequence,
|
|
282
283
|
randomInterval: () => randomInterval,
|
|
283
284
|
randomPercentage: () => randomPercentage,
|
|
@@ -290,12 +291,12 @@ __export(src_exports, {
|
|
|
290
291
|
roundToNearest: () => roundToNearest,
|
|
291
292
|
roundToUpper: () => roundToUpper,
|
|
292
293
|
roundTowardsZero: () => roundTowardsZero,
|
|
293
|
-
sortBy: () => sortBy,
|
|
294
294
|
sortedArray: () => sortedArray,
|
|
295
295
|
splitWords: () => splitWords,
|
|
296
296
|
stringToNumber: () => stringToNumber,
|
|
297
297
|
sum: () => sum,
|
|
298
298
|
sumBy: () => sumBy,
|
|
299
|
+
tail: () => tail,
|
|
299
300
|
throttle: () => throttle,
|
|
300
301
|
tryToParseJson: () => tryToParseJson,
|
|
301
302
|
uniq: () => uniq,
|
|
@@ -304,11 +305,334 @@ __export(src_exports, {
|
|
|
304
305
|
upsert: () => upsert,
|
|
305
306
|
withTryCatch: () => withTryCatch,
|
|
306
307
|
withTryCatchAsync: () => withTryCatchAsync,
|
|
307
|
-
wrap: () => wrap
|
|
308
|
+
wrap: () => wrap,
|
|
309
|
+
wrapWithString: () => wrapWithString
|
|
308
310
|
});
|
|
309
311
|
module.exports = __toCommonJS(src_exports);
|
|
310
312
|
|
|
311
|
-
// src/
|
|
313
|
+
// src/async/Deferred.ts
|
|
314
|
+
var DeferredCanceledError = class extends Error {
|
|
315
|
+
constructor() {
|
|
316
|
+
super("Execution canceled");
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
var Deferred = class {
|
|
320
|
+
_pending;
|
|
321
|
+
_internals;
|
|
322
|
+
get pending() {
|
|
323
|
+
return this._pending;
|
|
324
|
+
}
|
|
325
|
+
/** @deprecated: Use resolve, then and catch directly; use asPromise to return a promise type. */
|
|
326
|
+
get promise() {
|
|
327
|
+
return this._internals.promise;
|
|
328
|
+
}
|
|
329
|
+
constructor() {
|
|
330
|
+
this._pending = true;
|
|
331
|
+
this._internals = this.createInternals();
|
|
332
|
+
}
|
|
333
|
+
resolve(val) {
|
|
334
|
+
if (!this._pending)
|
|
335
|
+
throw new Error("Illegal state exception");
|
|
336
|
+
this.resolveIfPending(val);
|
|
337
|
+
}
|
|
338
|
+
reject(reason) {
|
|
339
|
+
if (!this._pending)
|
|
340
|
+
throw new Error("Illegal state exception");
|
|
341
|
+
this.rejectIfPending(reason);
|
|
342
|
+
}
|
|
343
|
+
cancel() {
|
|
344
|
+
if (!this._pending)
|
|
345
|
+
throw new Error("Illegal state exception");
|
|
346
|
+
this.cancelIfPending();
|
|
347
|
+
}
|
|
348
|
+
resolveIfPending(val) {
|
|
349
|
+
if (!this._pending)
|
|
350
|
+
return;
|
|
351
|
+
this._pending = false;
|
|
352
|
+
this._internals.resolve(val);
|
|
353
|
+
}
|
|
354
|
+
rejectIfPending(reason) {
|
|
355
|
+
if (!this._pending)
|
|
356
|
+
return;
|
|
357
|
+
this._pending = false;
|
|
358
|
+
this._internals.reject(reason);
|
|
359
|
+
}
|
|
360
|
+
cancelIfPending() {
|
|
361
|
+
if (!this._pending)
|
|
362
|
+
return;
|
|
363
|
+
this.reject(new DeferredCanceledError());
|
|
364
|
+
}
|
|
365
|
+
then(onFulfilled, onRejected) {
|
|
366
|
+
return this._internals.promise.then(onFulfilled, onRejected);
|
|
367
|
+
}
|
|
368
|
+
catch(onRejected) {
|
|
369
|
+
return this._internals.promise.catch(onRejected);
|
|
370
|
+
}
|
|
371
|
+
finally(onfinally) {
|
|
372
|
+
return this._internals.promise.finally(onfinally);
|
|
373
|
+
}
|
|
374
|
+
asPromise() {
|
|
375
|
+
return this;
|
|
376
|
+
}
|
|
377
|
+
asCancelablePromise() {
|
|
378
|
+
return this;
|
|
379
|
+
}
|
|
380
|
+
createInternals() {
|
|
381
|
+
let resolveSelf, rejectSelf;
|
|
382
|
+
const promise = new Promise((resolve, reject) => {
|
|
383
|
+
resolveSelf = resolve;
|
|
384
|
+
rejectSelf = reject;
|
|
385
|
+
});
|
|
386
|
+
return {
|
|
387
|
+
promise,
|
|
388
|
+
resolve: resolveSelf,
|
|
389
|
+
reject: rejectSelf
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
get [Symbol.toStringTag]() {
|
|
393
|
+
return "[object Deferred]";
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
var Deferred_default = Deferred;
|
|
397
|
+
|
|
398
|
+
// src/async/RateThrottler.ts
|
|
399
|
+
var RateThrottler = class {
|
|
400
|
+
constructor(_frequency) {
|
|
401
|
+
this._frequency = _frequency;
|
|
402
|
+
const initialOffset = this.cooldown.divideBy(_frequency.times);
|
|
403
|
+
this._availableSlots = new Array(_frequency.times).fill(0).map((_, i) => TimeInstant.now().addMs(i * initialOffset.ms));
|
|
404
|
+
this._waitingRequests = [];
|
|
405
|
+
}
|
|
406
|
+
_availableSlots;
|
|
407
|
+
_waitingRequests;
|
|
408
|
+
get cooldown() {
|
|
409
|
+
return this._frequency.period;
|
|
410
|
+
}
|
|
411
|
+
async execute(fn) {
|
|
412
|
+
const wrappedFn = async () => {
|
|
413
|
+
const slot = this._availableSlots.shift();
|
|
414
|
+
try {
|
|
415
|
+
return slot.promise().then(() => fn());
|
|
416
|
+
} finally {
|
|
417
|
+
this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
|
|
418
|
+
if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
if (this._availableSlots.length > 0) {
|
|
422
|
+
return wrappedFn();
|
|
423
|
+
} else {
|
|
424
|
+
const waitingRequest = new Deferred_default();
|
|
425
|
+
this._waitingRequests.push(waitingRequest);
|
|
426
|
+
return waitingRequest.then(() => wrappedFn());
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
function throttle(fn, frequence) {
|
|
431
|
+
const semaphore = new RateThrottler(frequence);
|
|
432
|
+
return (...t) => {
|
|
433
|
+
return semaphore.execute(async () => fn(...t));
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// src/async/Semaphore.ts
|
|
438
|
+
var Semaphore = class {
|
|
439
|
+
constructor(_availableSlots = 1) {
|
|
440
|
+
this._availableSlots = _availableSlots;
|
|
441
|
+
this._queuedRequests = [];
|
|
442
|
+
}
|
|
443
|
+
_queuedRequests;
|
|
444
|
+
_inProgress = 0;
|
|
445
|
+
async _awaitSlot() {
|
|
446
|
+
if (this._availableSlots > 0) {
|
|
447
|
+
this._availableSlots -= 1;
|
|
448
|
+
this._inProgress += 1;
|
|
449
|
+
return void 0;
|
|
450
|
+
} else {
|
|
451
|
+
const deferred = new Deferred_default();
|
|
452
|
+
this._queuedRequests.push(deferred);
|
|
453
|
+
return deferred.asPromise();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
_releaseSlot() {
|
|
457
|
+
const waitingSlotToResolve = this._queuedRequests.shift();
|
|
458
|
+
if (waitingSlotToResolve) {
|
|
459
|
+
waitingSlotToResolve.resolve();
|
|
460
|
+
} else {
|
|
461
|
+
this._availableSlots += 1;
|
|
462
|
+
this._inProgress -= 1;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
async execute(fn, cooldown = TimeDuration.ZERO) {
|
|
466
|
+
return this._awaitSlot().then(() => fn()).finally(() => {
|
|
467
|
+
void cooldown.promise().then(() => this._releaseSlot());
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
get availableSlots() {
|
|
471
|
+
return this._availableSlots;
|
|
472
|
+
}
|
|
473
|
+
get queueSize() {
|
|
474
|
+
return this._queuedRequests.length;
|
|
475
|
+
}
|
|
476
|
+
get inProgressSize() {
|
|
477
|
+
return this._inProgress;
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
// src/utils/arrays/groupBy.ts
|
|
482
|
+
function groupByString(arr, field) {
|
|
483
|
+
return groupByStringWith(arr, (t) => t[field]);
|
|
484
|
+
}
|
|
485
|
+
function groupByNumber(arr, field) {
|
|
486
|
+
return groupByNumberWith(arr, (t) => t[field]);
|
|
487
|
+
}
|
|
488
|
+
function groupByBoolean(arr, field) {
|
|
489
|
+
return groupByBooleanWith(arr, (t) => t[field]);
|
|
490
|
+
}
|
|
491
|
+
function groupBySymbol(arr, field) {
|
|
492
|
+
return groupBySymbolWith(arr, (t) => t[field]);
|
|
493
|
+
}
|
|
494
|
+
function groupByStringWith(arr, getter) {
|
|
495
|
+
return doGroupByWith(arr, getter);
|
|
496
|
+
}
|
|
497
|
+
function groupByNumberWith(arr, getter) {
|
|
498
|
+
return doGroupByWith(arr, getter);
|
|
499
|
+
}
|
|
500
|
+
function groupByBooleanWith(arr, getter) {
|
|
501
|
+
return doGroupByWith(arr, (item) => getter(item) ? "true" : "false");
|
|
502
|
+
}
|
|
503
|
+
function groupBySymbolWith(arr, getter) {
|
|
504
|
+
return doGroupByWith(arr, getter);
|
|
505
|
+
}
|
|
506
|
+
function doGroupByWith(arr, getter) {
|
|
507
|
+
return arr.reduce((dict, cur) => {
|
|
508
|
+
const key = getter(cur);
|
|
509
|
+
if (key !== null && key !== void 0) {
|
|
510
|
+
const arr2 = dict[key] ?? [];
|
|
511
|
+
arr2.push(cur);
|
|
512
|
+
dict[key] = arr2;
|
|
513
|
+
}
|
|
514
|
+
return dict;
|
|
515
|
+
}, {});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// src/utils/arrays/indexBy.ts
|
|
519
|
+
function indexByString(arr, field) {
|
|
520
|
+
return indexByStringWith(arr, (t) => t[field]);
|
|
521
|
+
}
|
|
522
|
+
function indexByNumber(arr, field) {
|
|
523
|
+
return indexByNumberWith(arr, (t) => t[field]);
|
|
524
|
+
}
|
|
525
|
+
function indexBySymbol(arr, field) {
|
|
526
|
+
return indexBySymbolWith(arr, (t) => t[field]);
|
|
527
|
+
}
|
|
528
|
+
function indexByStringWith(arr, getter) {
|
|
529
|
+
return doIndexByWith(arr, getter);
|
|
530
|
+
}
|
|
531
|
+
function indexByNumberWith(arr, getter) {
|
|
532
|
+
return doIndexByWith(arr, getter);
|
|
533
|
+
}
|
|
534
|
+
function indexBySymbolWith(arr, getter) {
|
|
535
|
+
return doIndexByWith(arr, getter);
|
|
536
|
+
}
|
|
537
|
+
function doIndexByWith(arr, getter) {
|
|
538
|
+
return arr.reduce((dict, cur) => {
|
|
539
|
+
const key = getter(cur);
|
|
540
|
+
if (key !== null && key !== void 0)
|
|
541
|
+
dict[key] = cur;
|
|
542
|
+
return dict;
|
|
543
|
+
}, {});
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// src/utils/arrays/statistics.ts
|
|
547
|
+
function average(arr) {
|
|
548
|
+
const f = 1 / arr.length;
|
|
549
|
+
return arr.reduce((tot, cur) => tot + cur * f, 0);
|
|
550
|
+
}
|
|
551
|
+
function sum(arr) {
|
|
552
|
+
return arr.reduce((tot, cur) => tot + cur, 0);
|
|
553
|
+
}
|
|
554
|
+
function sumBy(arr, getter) {
|
|
555
|
+
return sum(arr.map(getter));
|
|
556
|
+
}
|
|
557
|
+
function min(arr) {
|
|
558
|
+
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
559
|
+
return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
|
|
560
|
+
}
|
|
561
|
+
function minBy(arr, getter) {
|
|
562
|
+
return min(arr.map(getter));
|
|
563
|
+
}
|
|
564
|
+
function max(arr) {
|
|
565
|
+
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
566
|
+
return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
|
|
567
|
+
}
|
|
568
|
+
function maxBy(arr, getter) {
|
|
569
|
+
return max(arr.map(getter));
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// src/utils/functions/bindThis.ts
|
|
573
|
+
function bindThis(fn, _this) {
|
|
574
|
+
return fn.bind(_this);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// src/utils/functions/constant.ts
|
|
578
|
+
function constant(v) {
|
|
579
|
+
return () => v;
|
|
580
|
+
}
|
|
581
|
+
function identity(t) {
|
|
582
|
+
return t;
|
|
583
|
+
}
|
|
584
|
+
var constantNull = constant(null);
|
|
585
|
+
var constantTrue = constant(true);
|
|
586
|
+
var constantFalse = constant(false);
|
|
587
|
+
var constantZero = constant(0);
|
|
588
|
+
var constantOne = constant(1);
|
|
589
|
+
|
|
590
|
+
// src/utils/functions/iff.ts
|
|
591
|
+
function iff(firstPredicate, valueIfTrue) {
|
|
592
|
+
if (firstPredicate === true || firstPredicate instanceof Function && firstPredicate() === true) {
|
|
593
|
+
const ret = {
|
|
594
|
+
elseIf: () => ret,
|
|
595
|
+
otherwise: () => valueIfTrue
|
|
596
|
+
};
|
|
597
|
+
return ret;
|
|
598
|
+
} else {
|
|
599
|
+
const ret = {
|
|
600
|
+
elseIf: (elseIf, valueIfElseIfTrue) => iff(elseIf, valueIfElseIfTrue),
|
|
601
|
+
otherwise: (valueIfElse) => valueIfElse
|
|
602
|
+
};
|
|
603
|
+
return ret;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// src/utils/functions.ts
|
|
608
|
+
function isFunction(t) {
|
|
609
|
+
return typeof t === "function";
|
|
610
|
+
}
|
|
611
|
+
function noop() {
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// src/utils/arrays/uniqBy.ts
|
|
615
|
+
function uniq(arr) {
|
|
616
|
+
return uniqBy(arr, identity);
|
|
617
|
+
}
|
|
618
|
+
function uniqBy(arr, getter) {
|
|
619
|
+
return arr.reduce((dict, cur) => {
|
|
620
|
+
const key = getter(cur);
|
|
621
|
+
if (dict.keys.includes(key)) {
|
|
622
|
+
return dict;
|
|
623
|
+
} else {
|
|
624
|
+
return {
|
|
625
|
+
keys: [...dict.keys, key],
|
|
626
|
+
values: [...dict.values, cur]
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
}, { keys: [], values: [] }).values;
|
|
630
|
+
}
|
|
631
|
+
function uniqByKey(arr, key) {
|
|
632
|
+
return uniqBy(arr, (item) => item[key]);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// src/utils/arrays.ts
|
|
312
636
|
function ensureArray(t) {
|
|
313
637
|
return t instanceof Array ? t : [t];
|
|
314
638
|
}
|
|
@@ -353,11 +677,15 @@ function extendArrayWith(arr, propsFn) {
|
|
|
353
677
|
function reverse(arr) {
|
|
354
678
|
return [...arr].reverse();
|
|
355
679
|
}
|
|
356
|
-
function
|
|
680
|
+
function first(arr, defaultValue = null) {
|
|
357
681
|
return arr.length ? arr[0] : defaultValue;
|
|
358
682
|
}
|
|
359
683
|
function last(arr, defaultValue = null) {
|
|
360
|
-
return
|
|
684
|
+
return arr.length ? arr[arr.length - 1] : defaultValue;
|
|
685
|
+
}
|
|
686
|
+
var head = first;
|
|
687
|
+
function tail(arr) {
|
|
688
|
+
return arr.length ? arr.slice(1) : [];
|
|
361
689
|
}
|
|
362
690
|
function sortedArray(arr, sortFn) {
|
|
363
691
|
return [...arr].sort(sortFn);
|
|
@@ -372,7 +700,7 @@ function flatMapTruthys(arr, mapper) {
|
|
|
372
700
|
return arr.flatMap(mapper).filter((value) => value !== void 0);
|
|
373
701
|
}
|
|
374
702
|
|
|
375
|
-
// src/
|
|
703
|
+
// src/utils/booleans.ts
|
|
376
704
|
function isTrue(x) {
|
|
377
705
|
return x === true;
|
|
378
706
|
}
|
|
@@ -380,7 +708,19 @@ function isFalse(x) {
|
|
|
380
708
|
return x === false;
|
|
381
709
|
}
|
|
382
710
|
|
|
383
|
-
// src/
|
|
711
|
+
// src/utils/errors/withTryCatch.ts
|
|
712
|
+
function withTryCatch(fn) {
|
|
713
|
+
try {
|
|
714
|
+
return [fn(), void 0];
|
|
715
|
+
} catch (e) {
|
|
716
|
+
return [void 0, asError(e)];
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
async function withTryCatchAsync(fn) {
|
|
720
|
+
return fn().then((value) => [value, void 0], (e) => [void 0, asError(e)]);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// src/utils/errors.ts
|
|
384
724
|
function asError(e) {
|
|
385
725
|
if (e === void 0 || e === null)
|
|
386
726
|
return new Error("Void message");
|
|
@@ -396,22 +736,39 @@ function isError(e) {
|
|
|
396
736
|
return e instanceof Error;
|
|
397
737
|
}
|
|
398
738
|
|
|
399
|
-
// src/
|
|
400
|
-
function isFunction(t) {
|
|
401
|
-
return typeof t === "function";
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// src/types/json.ts
|
|
739
|
+
// src/utils/json.ts
|
|
405
740
|
function tryToParseJson(jsonContent) {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
741
|
+
return withTryCatch(() => JSON.parse(jsonContent));
|
|
742
|
+
}
|
|
743
|
+
function jsonCloneDeep(a) {
|
|
744
|
+
if (null === a || "object" !== typeof a) return a;
|
|
745
|
+
if (a instanceof Date) {
|
|
746
|
+
return new Date(a.getTime());
|
|
747
|
+
} else if (a instanceof Array) {
|
|
748
|
+
const copy = [];
|
|
749
|
+
for (let i = 0, len = a.length; i < len; i++) {
|
|
750
|
+
copy[i] = jsonCloneDeep(a[i]);
|
|
751
|
+
}
|
|
752
|
+
return copy;
|
|
753
|
+
} else if (a instanceof Object) {
|
|
754
|
+
const copy = {};
|
|
755
|
+
for (let attr in a) {
|
|
756
|
+
if (a.hasOwnProperty(attr))
|
|
757
|
+
copy[attr] = jsonCloneDeep(a[attr]);
|
|
758
|
+
}
|
|
759
|
+
return copy;
|
|
411
760
|
}
|
|
761
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
762
|
+
}
|
|
763
|
+
function omitFromJsonObject(o, ...keys) {
|
|
764
|
+
return keys.reduce((obj, key) => {
|
|
765
|
+
delete obj[key];
|
|
766
|
+
return obj;
|
|
767
|
+
}, { ...o });
|
|
412
768
|
}
|
|
769
|
+
var omit = omitFromJsonObject;
|
|
413
770
|
|
|
414
|
-
// src/
|
|
771
|
+
// src/utils/nulls.ts
|
|
415
772
|
function ensureDefined(v, name = "value") {
|
|
416
773
|
if (isDefined(v))
|
|
417
774
|
return v;
|
|
@@ -437,7 +794,26 @@ function ifNullOrUndefined(source, callback) {
|
|
|
437
794
|
callback();
|
|
438
795
|
}
|
|
439
796
|
|
|
440
|
-
// src/
|
|
797
|
+
// src/utils/numbers/round.ts
|
|
798
|
+
var roundModes = {
|
|
799
|
+
"toNearest": Math.round,
|
|
800
|
+
"toLower": Math.floor,
|
|
801
|
+
"toUpper": Math.ceil,
|
|
802
|
+
"towardsZero": (n) => n > 0 ? Math.floor(n) : Math.ceil(n),
|
|
803
|
+
"awayFromZero": (n) => n > 0 ? Math.ceil(n) : Math.floor(n)
|
|
804
|
+
};
|
|
805
|
+
function round(n, precision = 0, mode = "toNearest") {
|
|
806
|
+
const base = 10;
|
|
807
|
+
const power = Math.pow(base, precision);
|
|
808
|
+
return roundModes[mode](n * power) / power;
|
|
809
|
+
}
|
|
810
|
+
var roundToNearest = (n, precision = 0) => round(n, precision, "toNearest");
|
|
811
|
+
var roundToLower = (n, precision = 0) => round(n, precision, "toLower");
|
|
812
|
+
var roundToUpper = (n, precision = 0) => round(n, precision, "toUpper");
|
|
813
|
+
var roundTowardsZero = (n, precision = 0) => round(n, precision, "towardsZero");
|
|
814
|
+
var roundAwayFromZero = (n, precision = 0) => round(n, precision, "awayFromZero");
|
|
815
|
+
|
|
816
|
+
// src/utils/numbers.ts
|
|
441
817
|
function ensurePositiveNumber(v, name = "value") {
|
|
442
818
|
if (v !== void 0 && v !== null && v > 0)
|
|
443
819
|
return v;
|
|
@@ -482,8 +858,19 @@ function isNegativeNumber(v) {
|
|
|
482
858
|
function isZero(v) {
|
|
483
859
|
return v === 0;
|
|
484
860
|
}
|
|
861
|
+
function clamp(n, min2, max2) {
|
|
862
|
+
if (min2 === null || max2 === null || n === null || isNaN(min2) || isNaN(max2) || isNaN(n) || min2 > max2)
|
|
863
|
+
throw new Error();
|
|
864
|
+
if (n > max2) {
|
|
865
|
+
return max2;
|
|
866
|
+
} else if (n < min2) {
|
|
867
|
+
return min2;
|
|
868
|
+
} else {
|
|
869
|
+
return n;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
485
872
|
|
|
486
|
-
// src/
|
|
873
|
+
// src/utils/promises.ts
|
|
487
874
|
function asPromise(promisable) {
|
|
488
875
|
return Promise.resolve(promisable);
|
|
489
876
|
}
|
|
@@ -501,7 +888,26 @@ function delayPromise(duration) {
|
|
|
501
888
|
return (result) => duration.promise().then(() => result);
|
|
502
889
|
}
|
|
503
890
|
|
|
504
|
-
// src/
|
|
891
|
+
// src/utils/random.ts
|
|
892
|
+
function randomInterval(min2, max2) {
|
|
893
|
+
return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
|
|
894
|
+
}
|
|
895
|
+
function randomPercentage(min2, max2) {
|
|
896
|
+
return randomInterval(min2, max2) / 100;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// src/utils/records/entries.ts
|
|
900
|
+
function dictToEntries(obj) {
|
|
901
|
+
return Object.entries(obj);
|
|
902
|
+
}
|
|
903
|
+
function entriesToDict(entries) {
|
|
904
|
+
return Object.fromEntries(entries);
|
|
905
|
+
}
|
|
906
|
+
function mapEntries(dict, mapper) {
|
|
907
|
+
return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// src/utils/records.ts
|
|
505
911
|
function dictToList(obj) {
|
|
506
912
|
return Object.keys(obj).map((key) => obj[key]);
|
|
507
913
|
}
|
|
@@ -512,50 +918,7 @@ function pick(o, keys) {
|
|
|
512
918
|
}, {});
|
|
513
919
|
}
|
|
514
920
|
|
|
515
|
-
// src/
|
|
516
|
-
function hashCode(str) {
|
|
517
|
-
let hash = 0;
|
|
518
|
-
for (let i = 0; i < str.length; ++i)
|
|
519
|
-
hash = Math.imul(31, hash) + str.charCodeAt(i);
|
|
520
|
-
return hash | 0;
|
|
521
|
-
}
|
|
522
|
-
function repeat(char, times) {
|
|
523
|
-
return times > 0 ? range(0, times - 1).map((_) => char).join("") : "";
|
|
524
|
-
}
|
|
525
|
-
function capitalizeWord(word) {
|
|
526
|
-
return word[0].toUpperCase() + word.substring(1).toLowerCase();
|
|
527
|
-
}
|
|
528
|
-
function splitWords(text, regEx = /\s+/) {
|
|
529
|
-
return text.split(regEx).filter((t) => t.length > 0);
|
|
530
|
-
}
|
|
531
|
-
function stringToNumber(s) {
|
|
532
|
-
if (isNullOrUndefined(s))
|
|
533
|
-
return null;
|
|
534
|
-
return Number(s);
|
|
535
|
-
}
|
|
536
|
-
function pad(str, n, char, where = "left") {
|
|
537
|
-
const length = ensureDefined(str).length;
|
|
538
|
-
if (length >= ensureDefined(n)) return str;
|
|
539
|
-
if (ensureDefined(char).length !== 1)
|
|
540
|
-
throw new Error("Illegal pad character");
|
|
541
|
-
const padding = repeat(char, n - length);
|
|
542
|
-
return (where === "left" ? padding : "") + str + (where === "right" ? padding : "");
|
|
543
|
-
}
|
|
544
|
-
function padLeft(str, n, char) {
|
|
545
|
-
return pad(str, n, char, "left");
|
|
546
|
-
}
|
|
547
|
-
function padRight(str, n, char) {
|
|
548
|
-
return pad(str, n, char, "right");
|
|
549
|
-
}
|
|
550
|
-
function ellipsis(str, maxLength, padChar, padWhere = "left") {
|
|
551
|
-
if (maxLength < 4)
|
|
552
|
-
throw new Error("Invalid argument maxLength");
|
|
553
|
-
if (str.length <= maxLength) {
|
|
554
|
-
return pad(str, maxLength, padChar, padWhere);
|
|
555
|
-
} else {
|
|
556
|
-
return str.substring(0, maxLength - 3) + "...";
|
|
557
|
-
}
|
|
558
|
-
}
|
|
921
|
+
// src/utils/strings/StringParts.ts
|
|
559
922
|
var StringParts = class _StringParts {
|
|
560
923
|
_parts;
|
|
561
924
|
constructor(...parts) {
|
|
@@ -578,8 +941,8 @@ var StringParts = class _StringParts {
|
|
|
578
941
|
return this._parts;
|
|
579
942
|
}
|
|
580
943
|
get tail() {
|
|
581
|
-
const [_first, ...
|
|
582
|
-
return
|
|
944
|
+
const [_first, ...tail2] = this.parts;
|
|
945
|
+
return tail2;
|
|
583
946
|
}
|
|
584
947
|
get first() {
|
|
585
948
|
return this._parts[0] ?? null;
|
|
@@ -609,177 +972,100 @@ var StringParts = class _StringParts {
|
|
|
609
972
|
toHumanCase() {
|
|
610
973
|
return this.capitalizeFirst().join(" ");
|
|
611
974
|
}
|
|
612
|
-
join(separator) {
|
|
613
|
-
return this.parts.join(separator);
|
|
614
|
-
}
|
|
615
|
-
mergeWith({ parts: otherParts }) {
|
|
616
|
-
return new _StringParts(...this.parts, ...otherParts);
|
|
617
|
-
}
|
|
618
|
-
slice(start, end) {
|
|
619
|
-
return new _StringParts(...this.parts.slice(start, end));
|
|
620
|
-
}
|
|
621
|
-
splice(start, deleteCount, ...items) {
|
|
622
|
-
return new _StringParts(...this.parts.splice(start, deleteCount, ...items));
|
|
623
|
-
}
|
|
624
|
-
push(part) {
|
|
625
|
-
return new _StringParts(...this.parts, part);
|
|
626
|
-
}
|
|
627
|
-
shift(part) {
|
|
628
|
-
return new _StringParts(part, ...this.parts);
|
|
629
|
-
}
|
|
630
|
-
reverse() {
|
|
631
|
-
return new _StringParts(...this.parts.reverse());
|
|
632
|
-
}
|
|
633
|
-
static fromString = (s, separator = /\s+/g) => {
|
|
634
|
-
if (s === null || separator === null)
|
|
635
|
-
throw new Error("Invalid arguments");
|
|
636
|
-
return new _StringParts(...s.split(separator));
|
|
637
|
-
};
|
|
638
|
-
static fromParts = (...parts) => {
|
|
639
|
-
return new _StringParts(...parts);
|
|
640
|
-
};
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
// src/Lazy.ts
|
|
644
|
-
var Lazy = class _Lazy {
|
|
645
|
-
_value = void 0;
|
|
646
|
-
_initialized = false;
|
|
647
|
-
_generator;
|
|
648
|
-
constructor(generator) {
|
|
649
|
-
this._generator = ensureDefined(generator);
|
|
650
|
-
}
|
|
651
|
-
get value() {
|
|
652
|
-
return this._value;
|
|
653
|
-
}
|
|
654
|
-
getOrCreate() {
|
|
655
|
-
if (!this._initialized) {
|
|
656
|
-
this._value = this._generator();
|
|
657
|
-
this._initialized = true;
|
|
658
|
-
}
|
|
659
|
-
return this._value;
|
|
660
|
-
}
|
|
661
|
-
getOrThrow(errorMessage) {
|
|
662
|
-
if (!this._initialized)
|
|
663
|
-
throw new Error(errorMessage);
|
|
664
|
-
return this._value;
|
|
665
|
-
}
|
|
666
|
-
or(t) {
|
|
667
|
-
if (!this._initialized) {
|
|
668
|
-
return t;
|
|
669
|
-
} else {
|
|
670
|
-
return this._value;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
isPresent() {
|
|
674
|
-
return this._initialized;
|
|
675
|
-
}
|
|
676
|
-
isEmpty() {
|
|
677
|
-
return !this._initialized;
|
|
678
|
-
}
|
|
679
|
-
ifPresent(fn) {
|
|
680
|
-
if (this.isPresent()) {
|
|
681
|
-
fn(this._value);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
ifEmpty(fn) {
|
|
685
|
-
if (this.isEmpty()) {
|
|
686
|
-
fn();
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
apply(fnPresent, fnEmpty) {
|
|
690
|
-
if (this.isPresent()) {
|
|
691
|
-
fnPresent(this._value);
|
|
692
|
-
} else {
|
|
693
|
-
fnEmpty();
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
static of(generator) {
|
|
697
|
-
return new _Lazy(generator);
|
|
698
|
-
}
|
|
699
|
-
};
|
|
700
|
-
|
|
701
|
-
// src/LazyAsync.ts
|
|
702
|
-
var LazyAsync = class _LazyAsync {
|
|
703
|
-
_promise = void 0;
|
|
704
|
-
_pending = false;
|
|
705
|
-
_resolvedValue = void 0;
|
|
706
|
-
_error = void 0;
|
|
707
|
-
_generator;
|
|
708
|
-
constructor(generator) {
|
|
709
|
-
this._generator = ensureDefined(generator);
|
|
710
|
-
}
|
|
711
|
-
getOrCreate() {
|
|
712
|
-
if (this.isEmpty()) {
|
|
713
|
-
this._pending = true;
|
|
714
|
-
this._promise = this._generator();
|
|
715
|
-
this._promise.then((value) => {
|
|
716
|
-
this._pending = false;
|
|
717
|
-
this._resolvedValue = value;
|
|
718
|
-
}, (err) => {
|
|
719
|
-
this._pending = false;
|
|
720
|
-
this._error = err;
|
|
721
|
-
});
|
|
722
|
-
}
|
|
723
|
-
return this._promise;
|
|
724
|
-
}
|
|
725
|
-
isPresent() {
|
|
726
|
-
return this._promise !== void 0;
|
|
727
|
-
}
|
|
728
|
-
isEmpty() {
|
|
729
|
-
return this._promise === void 0;
|
|
730
|
-
}
|
|
731
|
-
isPending() {
|
|
732
|
-
return this.isPresent() && this._pending === true;
|
|
733
|
-
}
|
|
734
|
-
isReady() {
|
|
735
|
-
return this.isPresent() && this._pending === false;
|
|
736
|
-
}
|
|
737
|
-
isResolved() {
|
|
738
|
-
return this.isReady() && this._error === void 0;
|
|
739
|
-
}
|
|
740
|
-
isError() {
|
|
741
|
-
return this.isReady() && this._error !== void 0;
|
|
742
|
-
}
|
|
743
|
-
ifPresent(fn) {
|
|
744
|
-
if (this.isPresent()) {
|
|
745
|
-
fn(this._promise);
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
ifEmpty(fn) {
|
|
749
|
-
if (this.isEmpty()) {
|
|
750
|
-
fn();
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
ifPending(fn) {
|
|
754
|
-
if (this.isPending()) {
|
|
755
|
-
fn(this._promise);
|
|
756
|
-
}
|
|
975
|
+
join(separator) {
|
|
976
|
+
return this.parts.join(separator);
|
|
757
977
|
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
fn(this._promise);
|
|
761
|
-
}
|
|
978
|
+
mergeWith({ parts: otherParts }) {
|
|
979
|
+
return new _StringParts(...this.parts, ...otherParts);
|
|
762
980
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
fn(this._resolvedValue);
|
|
766
|
-
}
|
|
981
|
+
slice(start, end) {
|
|
982
|
+
return new _StringParts(...this.parts.slice(start, end));
|
|
767
983
|
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
fn(this._error);
|
|
771
|
-
}
|
|
984
|
+
splice(start, deleteCount, ...items) {
|
|
985
|
+
return new _StringParts(...this.parts.splice(start, deleteCount, ...items));
|
|
772
986
|
}
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
987
|
+
push(part) {
|
|
988
|
+
return new _StringParts(...this.parts, part);
|
|
989
|
+
}
|
|
990
|
+
shift(part) {
|
|
991
|
+
return new _StringParts(part, ...this.parts);
|
|
777
992
|
}
|
|
778
|
-
|
|
779
|
-
return new
|
|
993
|
+
reverse() {
|
|
994
|
+
return new _StringParts(...this.parts.reverse());
|
|
780
995
|
}
|
|
996
|
+
static fromString = (s, separator = /\s+/g) => {
|
|
997
|
+
if (s === null || separator === null)
|
|
998
|
+
throw new Error("Invalid arguments");
|
|
999
|
+
return new _StringParts(...s.split(separator));
|
|
1000
|
+
};
|
|
1001
|
+
static fromParts = (...parts) => {
|
|
1002
|
+
return new _StringParts(...parts);
|
|
1003
|
+
};
|
|
781
1004
|
};
|
|
782
1005
|
|
|
1006
|
+
// src/utils/strings.ts
|
|
1007
|
+
function hashCode(str) {
|
|
1008
|
+
let hash = 0;
|
|
1009
|
+
for (let i = 0; i < str.length; ++i)
|
|
1010
|
+
hash = Math.imul(31, hash) + str.charCodeAt(i);
|
|
1011
|
+
return hash | 0;
|
|
1012
|
+
}
|
|
1013
|
+
function repeat(char, times) {
|
|
1014
|
+
return times > 0 ? range(0, times - 1).map((_) => char).join("") : "";
|
|
1015
|
+
}
|
|
1016
|
+
function capitalizeWord(word) {
|
|
1017
|
+
return word[0].toUpperCase() + word.substring(1).toLowerCase();
|
|
1018
|
+
}
|
|
1019
|
+
function splitWords(text, regEx = /\s+/) {
|
|
1020
|
+
return text.split(regEx).filter((t) => t.length > 0);
|
|
1021
|
+
}
|
|
1022
|
+
function stringToNumber(s) {
|
|
1023
|
+
if (isNullOrUndefined(s))
|
|
1024
|
+
return null;
|
|
1025
|
+
return Number(s);
|
|
1026
|
+
}
|
|
1027
|
+
function pad(str, n, char, where = "left") {
|
|
1028
|
+
const length = ensureDefined(str).length;
|
|
1029
|
+
if (length >= ensureDefined(n)) return str;
|
|
1030
|
+
if (ensureDefined(char).length !== 1)
|
|
1031
|
+
throw new Error("Illegal pad character");
|
|
1032
|
+
const padding = repeat(char, n - length);
|
|
1033
|
+
return (where === "left" ? padding : "") + str + (where === "right" ? padding : "");
|
|
1034
|
+
}
|
|
1035
|
+
function padLeft(str, n, char) {
|
|
1036
|
+
return pad(str, n, char, "left");
|
|
1037
|
+
}
|
|
1038
|
+
function padRight(str, n, char) {
|
|
1039
|
+
return pad(str, n, char, "right");
|
|
1040
|
+
}
|
|
1041
|
+
function ellipsis(str, maxLength, padChar, padWhere = "left") {
|
|
1042
|
+
if (maxLength < 4)
|
|
1043
|
+
throw new Error("Invalid argument maxLength");
|
|
1044
|
+
if (str.length <= maxLength) {
|
|
1045
|
+
return pad(str, maxLength, padChar, padWhere);
|
|
1046
|
+
} else {
|
|
1047
|
+
return str.substring(0, maxLength - 3) + "...";
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
function pluralize(n, singular, plural) {
|
|
1051
|
+
if (!singular || !singular.length)
|
|
1052
|
+
throw new Error();
|
|
1053
|
+
if (n === 1)
|
|
1054
|
+
return singular;
|
|
1055
|
+
plural = plural ?? singular + "s";
|
|
1056
|
+
const firstUppercase = singular.charAt(0) === singular.charAt(0).toUpperCase();
|
|
1057
|
+
if (firstUppercase) {
|
|
1058
|
+
const PLURAL = plural.toUpperCase();
|
|
1059
|
+
const isAllUppercase = plural === PLURAL;
|
|
1060
|
+
plural = isAllUppercase ? PLURAL : plural.charAt(0).toUpperCase() + plural.slice(1).toLowerCase();
|
|
1061
|
+
}
|
|
1062
|
+
return plural;
|
|
1063
|
+
}
|
|
1064
|
+
function wrapWithString(str, delimiter) {
|
|
1065
|
+
return delimiter + str + delimiter;
|
|
1066
|
+
}
|
|
1067
|
+
var wrap = wrapWithString;
|
|
1068
|
+
|
|
783
1069
|
// src/time/TimeInstant.ts
|
|
784
1070
|
var import_small_date = __toESM(require_lib());
|
|
785
1071
|
|
|
@@ -901,101 +1187,6 @@ var TimeBase = class {
|
|
|
901
1187
|
}
|
|
902
1188
|
};
|
|
903
1189
|
|
|
904
|
-
// src/async/Deferred.ts
|
|
905
|
-
var DeferredCanceledError = class extends Error {
|
|
906
|
-
constructor() {
|
|
907
|
-
super("Execution canceled");
|
|
908
|
-
}
|
|
909
|
-
};
|
|
910
|
-
var Deferred = class {
|
|
911
|
-
_pending;
|
|
912
|
-
_internals;
|
|
913
|
-
get pending() {
|
|
914
|
-
return this._pending;
|
|
915
|
-
}
|
|
916
|
-
/** @deprecated: Use resolve, then and catch directly; use asPromise to return a promise type. */
|
|
917
|
-
get promise() {
|
|
918
|
-
return this._internals.promise;
|
|
919
|
-
}
|
|
920
|
-
constructor() {
|
|
921
|
-
this._pending = true;
|
|
922
|
-
this._internals = this.createInternals();
|
|
923
|
-
}
|
|
924
|
-
resolve(val) {
|
|
925
|
-
if (!this._pending)
|
|
926
|
-
throw new Error("Illegal state exception");
|
|
927
|
-
this.resolveIfPending(val);
|
|
928
|
-
}
|
|
929
|
-
reject(reason) {
|
|
930
|
-
if (!this._pending)
|
|
931
|
-
throw new Error("Illegal state exception");
|
|
932
|
-
this.rejectIfPending(reason);
|
|
933
|
-
}
|
|
934
|
-
cancel() {
|
|
935
|
-
if (!this._pending)
|
|
936
|
-
throw new Error("Illegal state exception");
|
|
937
|
-
this.cancelIfPending();
|
|
938
|
-
}
|
|
939
|
-
resolveIfPending(val) {
|
|
940
|
-
if (!this._pending)
|
|
941
|
-
return;
|
|
942
|
-
this._pending = false;
|
|
943
|
-
this._internals.resolve(val);
|
|
944
|
-
}
|
|
945
|
-
rejectIfPending(reason) {
|
|
946
|
-
if (!this._pending)
|
|
947
|
-
return;
|
|
948
|
-
this._pending = false;
|
|
949
|
-
this._internals.reject(reason);
|
|
950
|
-
}
|
|
951
|
-
cancelIfPending() {
|
|
952
|
-
if (!this._pending)
|
|
953
|
-
return;
|
|
954
|
-
this.reject(new DeferredCanceledError());
|
|
955
|
-
}
|
|
956
|
-
then(onFulfilled, onRejected) {
|
|
957
|
-
return this._internals.promise.then(onFulfilled, onRejected);
|
|
958
|
-
}
|
|
959
|
-
catch(onRejected) {
|
|
960
|
-
return this._internals.promise.catch(onRejected);
|
|
961
|
-
}
|
|
962
|
-
finally(onfinally) {
|
|
963
|
-
return this._internals.promise.finally(onfinally);
|
|
964
|
-
}
|
|
965
|
-
asPromise() {
|
|
966
|
-
return this;
|
|
967
|
-
}
|
|
968
|
-
asCancelablePromise() {
|
|
969
|
-
return this;
|
|
970
|
-
}
|
|
971
|
-
createInternals() {
|
|
972
|
-
let resolveSelf, rejectSelf;
|
|
973
|
-
const promise = new Promise((resolve, reject) => {
|
|
974
|
-
resolveSelf = resolve;
|
|
975
|
-
rejectSelf = reject;
|
|
976
|
-
});
|
|
977
|
-
return {
|
|
978
|
-
promise,
|
|
979
|
-
resolve: resolveSelf,
|
|
980
|
-
reject: rejectSelf
|
|
981
|
-
};
|
|
982
|
-
}
|
|
983
|
-
get [Symbol.toStringTag]() {
|
|
984
|
-
return "[object Deferred]";
|
|
985
|
-
}
|
|
986
|
-
};
|
|
987
|
-
var Deferred_default = Deferred;
|
|
988
|
-
|
|
989
|
-
// src/random/randomInterval.ts
|
|
990
|
-
function randomInterval(min2, max2) {
|
|
991
|
-
return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
// src/random/randomPercentage.ts
|
|
995
|
-
function randomPercentage(min2, max2) {
|
|
996
|
-
return randomInterval(min2, max2) / 100;
|
|
997
|
-
}
|
|
998
|
-
|
|
999
1190
|
// src/time/TimeDuration.ts
|
|
1000
1191
|
var TimeDuration = class _TimeDuration extends TimeBase {
|
|
1001
1192
|
constructor(value, unit) {
|
|
@@ -1660,10 +1851,6 @@ function isTimeInstant(x) {
|
|
|
1660
1851
|
}
|
|
1661
1852
|
var TimeInstant_default = TimeInstant;
|
|
1662
1853
|
|
|
1663
|
-
// src/utils/noop.ts
|
|
1664
|
-
function noop() {
|
|
1665
|
-
}
|
|
1666
|
-
|
|
1667
1854
|
// src/Logger.ts
|
|
1668
1855
|
var LEVELS = ["log", "debug", "info", "warn", "error"];
|
|
1669
1856
|
var timestamp = () => TimeInstant_default.now().asTimeString();
|
|
@@ -1795,115 +1982,25 @@ var Optional = class _Optional {
|
|
|
1795
1982
|
throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
|
|
1796
1983
|
return new _Optional(t);
|
|
1797
1984
|
}
|
|
1798
|
-
static ofNullable(t) {
|
|
1799
|
-
return new _Optional(t);
|
|
1800
|
-
}
|
|
1801
|
-
};
|
|
1802
|
-
var ErrorGetEmptyOptional = class extends Error {
|
|
1803
|
-
constructor() {
|
|
1804
|
-
super("Cannot retrieve a value from an EmptyOptional.");
|
|
1805
|
-
}
|
|
1806
|
-
};
|
|
1807
|
-
var ErrorSetEmptyOptional = class extends Error {
|
|
1808
|
-
constructor() {
|
|
1809
|
-
super("Cannot set a null or undefined value.");
|
|
1810
|
-
}
|
|
1811
|
-
};
|
|
1812
|
-
var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
|
|
1813
|
-
constructor() {
|
|
1814
|
-
super("Cannot initialize a PresentOptional with a null or undefined value.");
|
|
1815
|
-
}
|
|
1816
|
-
};
|
|
1817
|
-
|
|
1818
|
-
// src/async/RateThrottler.ts
|
|
1819
|
-
var RateThrottler = class {
|
|
1820
|
-
constructor(_frequency) {
|
|
1821
|
-
this._frequency = _frequency;
|
|
1822
|
-
const initialOffset = this.cooldown.divideBy(_frequency.times);
|
|
1823
|
-
this._availableSlots = new Array(_frequency.times).fill(0).map((_, i) => TimeInstant.now().addMs(i * initialOffset.ms));
|
|
1824
|
-
this._waitingRequests = [];
|
|
1825
|
-
}
|
|
1826
|
-
_availableSlots;
|
|
1827
|
-
_waitingRequests;
|
|
1828
|
-
get cooldown() {
|
|
1829
|
-
return this._frequency.period;
|
|
1830
|
-
}
|
|
1831
|
-
async execute(fn) {
|
|
1832
|
-
const wrappedFn = async () => {
|
|
1833
|
-
const slot = this._availableSlots.shift();
|
|
1834
|
-
try {
|
|
1835
|
-
return slot.promise().then(() => fn());
|
|
1836
|
-
} finally {
|
|
1837
|
-
this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
|
|
1838
|
-
if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
|
|
1839
|
-
}
|
|
1840
|
-
};
|
|
1841
|
-
if (this._availableSlots.length > 0) {
|
|
1842
|
-
return wrappedFn();
|
|
1843
|
-
} else {
|
|
1844
|
-
const waitingRequest = new Deferred_default();
|
|
1845
|
-
this._waitingRequests.push(waitingRequest);
|
|
1846
|
-
return waitingRequest.then(() => wrappedFn());
|
|
1847
|
-
}
|
|
1848
|
-
}
|
|
1849
|
-
};
|
|
1850
|
-
|
|
1851
|
-
// src/async/Semaphore.ts
|
|
1852
|
-
var Semaphore = class {
|
|
1853
|
-
constructor(_availableSlots = 1) {
|
|
1854
|
-
this._availableSlots = _availableSlots;
|
|
1855
|
-
this._queuedRequests = [];
|
|
1856
|
-
}
|
|
1857
|
-
_queuedRequests;
|
|
1858
|
-
_inProgress = 0;
|
|
1859
|
-
async _awaitSlot() {
|
|
1860
|
-
if (this._availableSlots > 0) {
|
|
1861
|
-
this._availableSlots -= 1;
|
|
1862
|
-
this._inProgress += 1;
|
|
1863
|
-
return void 0;
|
|
1864
|
-
} else {
|
|
1865
|
-
const deferred = new Deferred_default();
|
|
1866
|
-
this._queuedRequests.push(deferred);
|
|
1867
|
-
return deferred.asPromise();
|
|
1868
|
-
}
|
|
1869
|
-
}
|
|
1870
|
-
_releaseSlot() {
|
|
1871
|
-
const waitingSlotToResolve = this._queuedRequests.shift();
|
|
1872
|
-
if (waitingSlotToResolve) {
|
|
1873
|
-
waitingSlotToResolve.resolve();
|
|
1874
|
-
} else {
|
|
1875
|
-
this._availableSlots += 1;
|
|
1876
|
-
this._inProgress -= 1;
|
|
1877
|
-
}
|
|
1878
|
-
}
|
|
1879
|
-
async execute(fn, cooldown = TimeDuration.ZERO) {
|
|
1880
|
-
return this._awaitSlot().then(() => fn()).finally(() => {
|
|
1881
|
-
void cooldown.promise().then(() => this._releaseSlot());
|
|
1882
|
-
});
|
|
1883
|
-
}
|
|
1884
|
-
get availableSlots() {
|
|
1885
|
-
return this._availableSlots;
|
|
1985
|
+
static ofNullable(t) {
|
|
1986
|
+
return new _Optional(t);
|
|
1886
1987
|
}
|
|
1887
|
-
|
|
1888
|
-
|
|
1988
|
+
};
|
|
1989
|
+
var ErrorGetEmptyOptional = class extends Error {
|
|
1990
|
+
constructor() {
|
|
1991
|
+
super("Cannot retrieve a value from an EmptyOptional.");
|
|
1889
1992
|
}
|
|
1890
|
-
|
|
1891
|
-
|
|
1993
|
+
};
|
|
1994
|
+
var ErrorSetEmptyOptional = class extends Error {
|
|
1995
|
+
constructor() {
|
|
1996
|
+
super("Cannot set a null or undefined value.");
|
|
1997
|
+
}
|
|
1998
|
+
};
|
|
1999
|
+
var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
|
|
2000
|
+
constructor() {
|
|
2001
|
+
super("Cannot initialize a PresentOptional with a null or undefined value.");
|
|
1892
2002
|
}
|
|
1893
2003
|
};
|
|
1894
|
-
|
|
1895
|
-
// src/utils/constant.ts
|
|
1896
|
-
function constant(v) {
|
|
1897
|
-
return () => v;
|
|
1898
|
-
}
|
|
1899
|
-
function identity(t) {
|
|
1900
|
-
return t;
|
|
1901
|
-
}
|
|
1902
|
-
var constantNull = constant(null);
|
|
1903
|
-
var constantTrue = constant(true);
|
|
1904
|
-
var constantFalse = constant(false);
|
|
1905
|
-
var constantZero = constant(0);
|
|
1906
|
-
var constantOne = constant(1);
|
|
1907
2004
|
|
|
1908
2005
|
// src/sorting/ComparisonChain.ts
|
|
1909
2006
|
var defaultCompareValueOptions = {
|
|
@@ -2241,7 +2338,7 @@ var top = (fns, arr, n) => {
|
|
|
2241
2338
|
var bottom = (fns, arr, n) => {
|
|
2242
2339
|
return sort(fns, arr).slice(-Math.max(0, n)).reverse();
|
|
2243
2340
|
};
|
|
2244
|
-
var
|
|
2341
|
+
var first2 = (fns, arr) => {
|
|
2245
2342
|
return arr.length ? top(fns, arr, 1)[0] : null;
|
|
2246
2343
|
};
|
|
2247
2344
|
var last2 = (fns, arr) => {
|
|
@@ -2271,7 +2368,7 @@ function doCreateWithFunctions(fns) {
|
|
|
2271
2368
|
return bottom(fns, arr, n);
|
|
2272
2369
|
},
|
|
2273
2370
|
first: (arr) => {
|
|
2274
|
-
return
|
|
2371
|
+
return first2(fns, arr);
|
|
2275
2372
|
},
|
|
2276
2373
|
last: (arr) => {
|
|
2277
2374
|
return last2(fns, arr);
|
|
@@ -2353,264 +2450,6 @@ var TimeRange = class _TimeRange {
|
|
|
2353
2450
|
}
|
|
2354
2451
|
};
|
|
2355
2452
|
|
|
2356
|
-
// src/utils/bindThis.ts
|
|
2357
|
-
function bindThis(fn, _this) {
|
|
2358
|
-
return fn.bind(_this);
|
|
2359
|
-
}
|
|
2360
|
-
|
|
2361
|
-
// src/utils/entries.ts
|
|
2362
|
-
function dictToEntries(obj) {
|
|
2363
|
-
return Object.entries(obj);
|
|
2364
|
-
}
|
|
2365
|
-
function entriesToDict(entries) {
|
|
2366
|
-
return Object.fromEntries(entries);
|
|
2367
|
-
}
|
|
2368
|
-
function mapEntries(dict, mapper) {
|
|
2369
|
-
return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
|
|
2370
|
-
}
|
|
2371
|
-
|
|
2372
|
-
// src/utils/groupBy.ts
|
|
2373
|
-
function groupByString(arr, field) {
|
|
2374
|
-
return groupByStringWith(arr, (t) => t[field]);
|
|
2375
|
-
}
|
|
2376
|
-
function groupByNumber(arr, field) {
|
|
2377
|
-
return groupByNumberWith(arr, (t) => t[field]);
|
|
2378
|
-
}
|
|
2379
|
-
function groupByBoolean(arr, field) {
|
|
2380
|
-
return groupByBooleanWith(arr, (t) => t[field]);
|
|
2381
|
-
}
|
|
2382
|
-
function groupBySymbol(arr, field) {
|
|
2383
|
-
return groupBySymbolWith(arr, (t) => t[field]);
|
|
2384
|
-
}
|
|
2385
|
-
function groupByStringWith(arr, getter) {
|
|
2386
|
-
return doGroupByWith(arr, getter);
|
|
2387
|
-
}
|
|
2388
|
-
function groupByNumberWith(arr, getter) {
|
|
2389
|
-
return doGroupByWith(arr, getter);
|
|
2390
|
-
}
|
|
2391
|
-
function groupByBooleanWith(arr, getter) {
|
|
2392
|
-
return doGroupByWith(arr, (item) => getter(item) ? "true" : "false");
|
|
2393
|
-
}
|
|
2394
|
-
function groupBySymbolWith(arr, getter) {
|
|
2395
|
-
return doGroupByWith(arr, getter);
|
|
2396
|
-
}
|
|
2397
|
-
function doGroupByWith(arr, getter) {
|
|
2398
|
-
return arr.reduce((dict, cur) => {
|
|
2399
|
-
const key = getter(cur);
|
|
2400
|
-
if (key !== null && key !== void 0) {
|
|
2401
|
-
const arr2 = dict[key] ?? [];
|
|
2402
|
-
arr2.push(cur);
|
|
2403
|
-
dict[key] = arr2;
|
|
2404
|
-
}
|
|
2405
|
-
return dict;
|
|
2406
|
-
}, {});
|
|
2407
|
-
}
|
|
2408
|
-
|
|
2409
|
-
// src/utils/iff.ts
|
|
2410
|
-
function iff(firstPredicate, valueIfTrue) {
|
|
2411
|
-
if (firstPredicate === true || firstPredicate instanceof Function && firstPredicate() === true) {
|
|
2412
|
-
const ret = {
|
|
2413
|
-
elseIf: () => ret,
|
|
2414
|
-
otherwise: () => valueIfTrue
|
|
2415
|
-
};
|
|
2416
|
-
return ret;
|
|
2417
|
-
} else {
|
|
2418
|
-
const ret = {
|
|
2419
|
-
elseIf: (elseIf, valueIfElseIfTrue) => iff(elseIf, valueIfElseIfTrue),
|
|
2420
|
-
otherwise: (valueIfElse) => valueIfElse
|
|
2421
|
-
};
|
|
2422
|
-
return ret;
|
|
2423
|
-
}
|
|
2424
|
-
}
|
|
2425
|
-
|
|
2426
|
-
// src/utils/indexBy.ts
|
|
2427
|
-
function indexByString(arr, field) {
|
|
2428
|
-
return indexByStringWith(arr, (t) => t[field]);
|
|
2429
|
-
}
|
|
2430
|
-
function indexByNumber(arr, field) {
|
|
2431
|
-
return indexByNumberWith(arr, (t) => t[field]);
|
|
2432
|
-
}
|
|
2433
|
-
function indexBySymbol(arr, field) {
|
|
2434
|
-
return indexBySymbolWith(arr, (t) => t[field]);
|
|
2435
|
-
}
|
|
2436
|
-
function indexByStringWith(arr, getter) {
|
|
2437
|
-
return doIndexByWith(arr, getter);
|
|
2438
|
-
}
|
|
2439
|
-
function indexByNumberWith(arr, getter) {
|
|
2440
|
-
return doIndexByWith(arr, getter);
|
|
2441
|
-
}
|
|
2442
|
-
function indexBySymbolWith(arr, getter) {
|
|
2443
|
-
return doIndexByWith(arr, getter);
|
|
2444
|
-
}
|
|
2445
|
-
function doIndexByWith(arr, getter) {
|
|
2446
|
-
return arr.reduce((dict, cur) => {
|
|
2447
|
-
const key = getter(cur);
|
|
2448
|
-
if (key !== null && key !== void 0)
|
|
2449
|
-
dict[key] = cur;
|
|
2450
|
-
return dict;
|
|
2451
|
-
}, {});
|
|
2452
|
-
}
|
|
2453
|
-
|
|
2454
|
-
// src/utils/jsonCloneDeep.ts
|
|
2455
|
-
function jsonCloneDeep(a) {
|
|
2456
|
-
if (null === a || "object" !== typeof a) return a;
|
|
2457
|
-
if (a instanceof Date) {
|
|
2458
|
-
return new Date(a.getTime());
|
|
2459
|
-
} else if (a instanceof Array) {
|
|
2460
|
-
const copy = [];
|
|
2461
|
-
for (let i = 0, len = a.length; i < len; i++) {
|
|
2462
|
-
copy[i] = jsonCloneDeep(a[i]);
|
|
2463
|
-
}
|
|
2464
|
-
return copy;
|
|
2465
|
-
} else if (a instanceof Object) {
|
|
2466
|
-
const copy = {};
|
|
2467
|
-
for (let attr in a) {
|
|
2468
|
-
if (a.hasOwnProperty(attr))
|
|
2469
|
-
copy[attr] = jsonCloneDeep(a[attr]);
|
|
2470
|
-
}
|
|
2471
|
-
return copy;
|
|
2472
|
-
}
|
|
2473
|
-
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
2474
|
-
}
|
|
2475
|
-
|
|
2476
|
-
// src/utils/math.ts
|
|
2477
|
-
function clamp(n, min2, max2) {
|
|
2478
|
-
if (min2 === null || max2 === null || n === null || isNaN(min2) || isNaN(max2) || isNaN(n) || min2 > max2)
|
|
2479
|
-
throw new Error();
|
|
2480
|
-
if (n > max2) {
|
|
2481
|
-
return max2;
|
|
2482
|
-
} else if (n < min2) {
|
|
2483
|
-
return min2;
|
|
2484
|
-
} else {
|
|
2485
|
-
return n;
|
|
2486
|
-
}
|
|
2487
|
-
}
|
|
2488
|
-
function average(arr) {
|
|
2489
|
-
const f = 1 / arr.length;
|
|
2490
|
-
return arr.reduce((tot, cur) => tot + cur * f, 0);
|
|
2491
|
-
}
|
|
2492
|
-
function sum(arr) {
|
|
2493
|
-
return arr.reduce((tot, cur) => tot + cur, 0);
|
|
2494
|
-
}
|
|
2495
|
-
function sumBy(arr, getter) {
|
|
2496
|
-
return sum(arr.map(getter));
|
|
2497
|
-
}
|
|
2498
|
-
function min(arr) {
|
|
2499
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
2500
|
-
return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
|
|
2501
|
-
}
|
|
2502
|
-
function minBy(arr, getter) {
|
|
2503
|
-
return min(arr.map(getter));
|
|
2504
|
-
}
|
|
2505
|
-
function max(arr) {
|
|
2506
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
2507
|
-
return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
|
|
2508
|
-
}
|
|
2509
|
-
function maxBy(arr, getter) {
|
|
2510
|
-
return max(arr.map(getter));
|
|
2511
|
-
}
|
|
2512
|
-
|
|
2513
|
-
// src/utils/omit.ts
|
|
2514
|
-
function omit(o, ...keys) {
|
|
2515
|
-
return keys.reduce((obj, key) => {
|
|
2516
|
-
delete obj[key];
|
|
2517
|
-
return obj;
|
|
2518
|
-
}, { ...o });
|
|
2519
|
-
}
|
|
2520
|
-
|
|
2521
|
-
// src/utils/round.ts
|
|
2522
|
-
var roundModes = {
|
|
2523
|
-
"toNearest": Math.round,
|
|
2524
|
-
"toLower": Math.floor,
|
|
2525
|
-
"toUpper": Math.ceil,
|
|
2526
|
-
"towardsZero": (n) => n > 0 ? Math.floor(n) : Math.ceil(n),
|
|
2527
|
-
"awayFromZero": (n) => n > 0 ? Math.ceil(n) : Math.floor(n)
|
|
2528
|
-
};
|
|
2529
|
-
function round(n, precision = 0, mode = "toNearest") {
|
|
2530
|
-
const base = 10;
|
|
2531
|
-
const power = Math.pow(base, precision);
|
|
2532
|
-
return roundModes[mode](n * power) / power;
|
|
2533
|
-
}
|
|
2534
|
-
var roundToNearest = (n, precision = 0) => round(n, precision, "toNearest");
|
|
2535
|
-
var roundToLower = (n, precision = 0) => round(n, precision, "toLower");
|
|
2536
|
-
var roundToUpper = (n, precision = 0) => round(n, precision, "toUpper");
|
|
2537
|
-
var roundTowardsZero = (n, precision = 0) => round(n, precision, "towardsZero");
|
|
2538
|
-
var roundAwayFromZero = (n, precision = 0) => round(n, precision, "awayFromZero");
|
|
2539
|
-
|
|
2540
|
-
// src/utils/sortBy.ts
|
|
2541
|
-
function sortBy(keyOrGetter, direction = "ASC", nulls = "LAST") {
|
|
2542
|
-
const directionNum = direction === "ASC" ? -1 : 1;
|
|
2543
|
-
const nullsNum = nulls === "LAST" ? -1 : 1;
|
|
2544
|
-
const sortByNum = (a, b) => {
|
|
2545
|
-
if (a === b)
|
|
2546
|
-
return -1;
|
|
2547
|
-
const aDefined = !(a === null || a === void 0 || isNaN(a));
|
|
2548
|
-
const bDefined = !(b === null || b === void 0 || isNaN(b));
|
|
2549
|
-
if (!aDefined && !bDefined)
|
|
2550
|
-
return -1;
|
|
2551
|
-
if (aDefined !== bDefined)
|
|
2552
|
-
return nullsNum * (aDefined ? 1 : -1);
|
|
2553
|
-
return (a > b ? -1 : 1) * directionNum;
|
|
2554
|
-
};
|
|
2555
|
-
if (typeof keyOrGetter === "function") {
|
|
2556
|
-
return (a, b) => sortByNum(keyOrGetter(a), keyOrGetter(b));
|
|
2557
|
-
} else {
|
|
2558
|
-
return (a, b) => sortByNum(a[keyOrGetter], b[keyOrGetter]);
|
|
2559
|
-
}
|
|
2560
|
-
}
|
|
2561
|
-
|
|
2562
|
-
// src/utils/throttle.ts
|
|
2563
|
-
function throttle(fn, frequence) {
|
|
2564
|
-
const semaphore = new RateThrottler(frequence);
|
|
2565
|
-
return (...t) => {
|
|
2566
|
-
return semaphore.execute(async () => fn(...t));
|
|
2567
|
-
};
|
|
2568
|
-
}
|
|
2569
|
-
|
|
2570
|
-
// src/utils/uniqBy.ts
|
|
2571
|
-
function uniqBy(arr, getter) {
|
|
2572
|
-
return arr.reduce((dict, cur) => {
|
|
2573
|
-
const key = getter(cur);
|
|
2574
|
-
if (dict.keys.includes(key)) {
|
|
2575
|
-
return dict;
|
|
2576
|
-
} else {
|
|
2577
|
-
return {
|
|
2578
|
-
keys: [...dict.keys, key],
|
|
2579
|
-
values: [...dict.values, cur]
|
|
2580
|
-
};
|
|
2581
|
-
}
|
|
2582
|
-
}, { keys: [], values: [] }).values;
|
|
2583
|
-
}
|
|
2584
|
-
|
|
2585
|
-
// src/utils/uniq.ts
|
|
2586
|
-
function uniq(arr) {
|
|
2587
|
-
return uniqBy(arr, identity);
|
|
2588
|
-
}
|
|
2589
|
-
|
|
2590
|
-
// src/utils/uniqByKey.ts
|
|
2591
|
-
function uniqByKey(arr, key) {
|
|
2592
|
-
return uniqBy(arr, (item) => item[key]);
|
|
2593
|
-
}
|
|
2594
|
-
|
|
2595
|
-
// src/utils/withTryCatch.ts
|
|
2596
|
-
function withTryCatch(fn) {
|
|
2597
|
-
try {
|
|
2598
|
-
return fn();
|
|
2599
|
-
} catch (e) {
|
|
2600
|
-
return asError(e);
|
|
2601
|
-
}
|
|
2602
|
-
}
|
|
2603
|
-
|
|
2604
|
-
// src/utils/withTryCatchAsync.ts
|
|
2605
|
-
async function withTryCatchAsync(fn) {
|
|
2606
|
-
return fn().catch((e) => asError(e));
|
|
2607
|
-
}
|
|
2608
|
-
|
|
2609
|
-
// src/utils/wrap.ts
|
|
2610
|
-
function wrap(str, delimiter) {
|
|
2611
|
-
return delimiter + str + delimiter;
|
|
2612
|
-
}
|
|
2613
|
-
|
|
2614
2453
|
// src/upgrade/errors.ts
|
|
2615
2454
|
var UnavailableUpgradeError = class extends Error {
|
|
2616
2455
|
constructor(data, from, to) {
|
|
@@ -2747,8 +2586,6 @@ function isUpgradable(obj) {
|
|
|
2747
2586
|
ErrorCannotInstantiatePresentOptionalWithEmptyValue,
|
|
2748
2587
|
ErrorGetEmptyOptional,
|
|
2749
2588
|
ErrorSetEmptyOptional,
|
|
2750
|
-
Lazy,
|
|
2751
|
-
LazyAsync,
|
|
2752
2589
|
Logger,
|
|
2753
2590
|
Optional,
|
|
2754
2591
|
RandomTimeDuration,
|
|
@@ -2793,6 +2630,7 @@ function isUpgradable(obj) {
|
|
|
2793
2630
|
extendArray,
|
|
2794
2631
|
extendArrayWith,
|
|
2795
2632
|
fill,
|
|
2633
|
+
first,
|
|
2796
2634
|
flatMapTruthys,
|
|
2797
2635
|
groupByBoolean,
|
|
2798
2636
|
groupByBooleanWith,
|
|
@@ -2843,10 +2681,12 @@ function isUpgradable(obj) {
|
|
|
2843
2681
|
multiplyBy,
|
|
2844
2682
|
noop,
|
|
2845
2683
|
omit,
|
|
2684
|
+
omitFromJsonObject,
|
|
2846
2685
|
pad,
|
|
2847
2686
|
padLeft,
|
|
2848
2687
|
padRight,
|
|
2849
2688
|
pick,
|
|
2689
|
+
pluralize,
|
|
2850
2690
|
promiseSequence,
|
|
2851
2691
|
randomInterval,
|
|
2852
2692
|
randomPercentage,
|
|
@@ -2859,12 +2699,12 @@ function isUpgradable(obj) {
|
|
|
2859
2699
|
roundToNearest,
|
|
2860
2700
|
roundToUpper,
|
|
2861
2701
|
roundTowardsZero,
|
|
2862
|
-
sortBy,
|
|
2863
2702
|
sortedArray,
|
|
2864
2703
|
splitWords,
|
|
2865
2704
|
stringToNumber,
|
|
2866
2705
|
sum,
|
|
2867
2706
|
sumBy,
|
|
2707
|
+
tail,
|
|
2868
2708
|
throttle,
|
|
2869
2709
|
tryToParseJson,
|
|
2870
2710
|
uniq,
|
|
@@ -2873,5 +2713,6 @@ function isUpgradable(obj) {
|
|
|
2873
2713
|
upsert,
|
|
2874
2714
|
withTryCatch,
|
|
2875
2715
|
withTryCatchAsync,
|
|
2876
|
-
wrap
|
|
2716
|
+
wrap,
|
|
2717
|
+
wrapWithString
|
|
2877
2718
|
});
|