@zelgadis87/utils-core 4.3.5 → 4.4.1
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.d.ts → lazy/Lazy.d.ts} +2 -1
- package/dist/{LazyAsync.d.ts → lazy/LazyAsync.d.ts} +2 -1
- 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 +524 -539
- package/esbuild/index.mjs +518 -537
- 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} +4 -2
- package/src/{LazyAsync.ts → lazy/LazyAsync.ts} +4 -2
- 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/{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.mjs
CHANGED
|
@@ -162,7 +162,329 @@ var require_lib = __commonJS({
|
|
|
162
162
|
}
|
|
163
163
|
});
|
|
164
164
|
|
|
165
|
-
// src/
|
|
165
|
+
// src/async/Deferred.ts
|
|
166
|
+
var DeferredCanceledError = class extends Error {
|
|
167
|
+
constructor() {
|
|
168
|
+
super("Execution canceled");
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
var Deferred = class {
|
|
172
|
+
_pending;
|
|
173
|
+
_internals;
|
|
174
|
+
get pending() {
|
|
175
|
+
return this._pending;
|
|
176
|
+
}
|
|
177
|
+
/** @deprecated: Use resolve, then and catch directly; use asPromise to return a promise type. */
|
|
178
|
+
get promise() {
|
|
179
|
+
return this._internals.promise;
|
|
180
|
+
}
|
|
181
|
+
constructor() {
|
|
182
|
+
this._pending = true;
|
|
183
|
+
this._internals = this.createInternals();
|
|
184
|
+
}
|
|
185
|
+
resolve(val) {
|
|
186
|
+
if (!this._pending)
|
|
187
|
+
throw new Error("Illegal state exception");
|
|
188
|
+
this.resolveIfPending(val);
|
|
189
|
+
}
|
|
190
|
+
reject(reason) {
|
|
191
|
+
if (!this._pending)
|
|
192
|
+
throw new Error("Illegal state exception");
|
|
193
|
+
this.rejectIfPending(reason);
|
|
194
|
+
}
|
|
195
|
+
cancel() {
|
|
196
|
+
if (!this._pending)
|
|
197
|
+
throw new Error("Illegal state exception");
|
|
198
|
+
this.cancelIfPending();
|
|
199
|
+
}
|
|
200
|
+
resolveIfPending(val) {
|
|
201
|
+
if (!this._pending)
|
|
202
|
+
return;
|
|
203
|
+
this._pending = false;
|
|
204
|
+
this._internals.resolve(val);
|
|
205
|
+
}
|
|
206
|
+
rejectIfPending(reason) {
|
|
207
|
+
if (!this._pending)
|
|
208
|
+
return;
|
|
209
|
+
this._pending = false;
|
|
210
|
+
this._internals.reject(reason);
|
|
211
|
+
}
|
|
212
|
+
cancelIfPending() {
|
|
213
|
+
if (!this._pending)
|
|
214
|
+
return;
|
|
215
|
+
this.reject(new DeferredCanceledError());
|
|
216
|
+
}
|
|
217
|
+
then(onFulfilled, onRejected) {
|
|
218
|
+
return this._internals.promise.then(onFulfilled, onRejected);
|
|
219
|
+
}
|
|
220
|
+
catch(onRejected) {
|
|
221
|
+
return this._internals.promise.catch(onRejected);
|
|
222
|
+
}
|
|
223
|
+
finally(onfinally) {
|
|
224
|
+
return this._internals.promise.finally(onfinally);
|
|
225
|
+
}
|
|
226
|
+
asPromise() {
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
asCancelablePromise() {
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
createInternals() {
|
|
233
|
+
let resolveSelf, rejectSelf;
|
|
234
|
+
const promise = new Promise((resolve, reject) => {
|
|
235
|
+
resolveSelf = resolve;
|
|
236
|
+
rejectSelf = reject;
|
|
237
|
+
});
|
|
238
|
+
return {
|
|
239
|
+
promise,
|
|
240
|
+
resolve: resolveSelf,
|
|
241
|
+
reject: rejectSelf
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
get [Symbol.toStringTag]() {
|
|
245
|
+
return "[object Deferred]";
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
var Deferred_default = Deferred;
|
|
249
|
+
|
|
250
|
+
// src/async/RateThrottler.ts
|
|
251
|
+
var RateThrottler = class {
|
|
252
|
+
constructor(_frequency) {
|
|
253
|
+
this._frequency = _frequency;
|
|
254
|
+
const initialOffset = this.cooldown.divideBy(_frequency.times);
|
|
255
|
+
this._availableSlots = new Array(_frequency.times).fill(0).map((_, i) => TimeInstant.now().addMs(i * initialOffset.ms));
|
|
256
|
+
this._waitingRequests = [];
|
|
257
|
+
}
|
|
258
|
+
_availableSlots;
|
|
259
|
+
_waitingRequests;
|
|
260
|
+
get cooldown() {
|
|
261
|
+
return this._frequency.period;
|
|
262
|
+
}
|
|
263
|
+
async execute(fn) {
|
|
264
|
+
const wrappedFn = async () => {
|
|
265
|
+
const slot = this._availableSlots.shift();
|
|
266
|
+
try {
|
|
267
|
+
return slot.promise().then(() => fn());
|
|
268
|
+
} finally {
|
|
269
|
+
this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
|
|
270
|
+
if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
if (this._availableSlots.length > 0) {
|
|
274
|
+
return wrappedFn();
|
|
275
|
+
} else {
|
|
276
|
+
const waitingRequest = new Deferred_default();
|
|
277
|
+
this._waitingRequests.push(waitingRequest);
|
|
278
|
+
return waitingRequest.then(() => wrappedFn());
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
function throttle(fn, frequence) {
|
|
283
|
+
const semaphore = new RateThrottler(frequence);
|
|
284
|
+
return (...t) => {
|
|
285
|
+
return semaphore.execute(async () => fn(...t));
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/async/Semaphore.ts
|
|
290
|
+
var Semaphore = class {
|
|
291
|
+
constructor(_availableSlots = 1) {
|
|
292
|
+
this._availableSlots = _availableSlots;
|
|
293
|
+
this._queuedRequests = [];
|
|
294
|
+
}
|
|
295
|
+
_queuedRequests;
|
|
296
|
+
_inProgress = 0;
|
|
297
|
+
async _awaitSlot() {
|
|
298
|
+
if (this._availableSlots > 0) {
|
|
299
|
+
this._availableSlots -= 1;
|
|
300
|
+
this._inProgress += 1;
|
|
301
|
+
return void 0;
|
|
302
|
+
} else {
|
|
303
|
+
const deferred = new Deferred_default();
|
|
304
|
+
this._queuedRequests.push(deferred);
|
|
305
|
+
return deferred.asPromise();
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
_releaseSlot() {
|
|
309
|
+
const waitingSlotToResolve = this._queuedRequests.shift();
|
|
310
|
+
if (waitingSlotToResolve) {
|
|
311
|
+
waitingSlotToResolve.resolve();
|
|
312
|
+
} else {
|
|
313
|
+
this._availableSlots += 1;
|
|
314
|
+
this._inProgress -= 1;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async execute(fn, cooldown = TimeDuration.ZERO) {
|
|
318
|
+
return this._awaitSlot().then(() => fn()).finally(() => {
|
|
319
|
+
void cooldown.promise().then(() => this._releaseSlot());
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
get availableSlots() {
|
|
323
|
+
return this._availableSlots;
|
|
324
|
+
}
|
|
325
|
+
get queueSize() {
|
|
326
|
+
return this._queuedRequests.length;
|
|
327
|
+
}
|
|
328
|
+
get inProgressSize() {
|
|
329
|
+
return this._inProgress;
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
// src/utils/arrays/groupBy.ts
|
|
334
|
+
function groupByString(arr, field) {
|
|
335
|
+
return groupByStringWith(arr, (t) => t[field]);
|
|
336
|
+
}
|
|
337
|
+
function groupByNumber(arr, field) {
|
|
338
|
+
return groupByNumberWith(arr, (t) => t[field]);
|
|
339
|
+
}
|
|
340
|
+
function groupByBoolean(arr, field) {
|
|
341
|
+
return groupByBooleanWith(arr, (t) => t[field]);
|
|
342
|
+
}
|
|
343
|
+
function groupBySymbol(arr, field) {
|
|
344
|
+
return groupBySymbolWith(arr, (t) => t[field]);
|
|
345
|
+
}
|
|
346
|
+
function groupByStringWith(arr, getter) {
|
|
347
|
+
return doGroupByWith(arr, getter);
|
|
348
|
+
}
|
|
349
|
+
function groupByNumberWith(arr, getter) {
|
|
350
|
+
return doGroupByWith(arr, getter);
|
|
351
|
+
}
|
|
352
|
+
function groupByBooleanWith(arr, getter) {
|
|
353
|
+
return doGroupByWith(arr, (item) => getter(item) ? "true" : "false");
|
|
354
|
+
}
|
|
355
|
+
function groupBySymbolWith(arr, getter) {
|
|
356
|
+
return doGroupByWith(arr, getter);
|
|
357
|
+
}
|
|
358
|
+
function doGroupByWith(arr, getter) {
|
|
359
|
+
return arr.reduce((dict, cur) => {
|
|
360
|
+
const key = getter(cur);
|
|
361
|
+
if (key !== null && key !== void 0) {
|
|
362
|
+
const arr2 = dict[key] ?? [];
|
|
363
|
+
arr2.push(cur);
|
|
364
|
+
dict[key] = arr2;
|
|
365
|
+
}
|
|
366
|
+
return dict;
|
|
367
|
+
}, {});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// src/utils/arrays/indexBy.ts
|
|
371
|
+
function indexByString(arr, field) {
|
|
372
|
+
return indexByStringWith(arr, (t) => t[field]);
|
|
373
|
+
}
|
|
374
|
+
function indexByNumber(arr, field) {
|
|
375
|
+
return indexByNumberWith(arr, (t) => t[field]);
|
|
376
|
+
}
|
|
377
|
+
function indexBySymbol(arr, field) {
|
|
378
|
+
return indexBySymbolWith(arr, (t) => t[field]);
|
|
379
|
+
}
|
|
380
|
+
function indexByStringWith(arr, getter) {
|
|
381
|
+
return doIndexByWith(arr, getter);
|
|
382
|
+
}
|
|
383
|
+
function indexByNumberWith(arr, getter) {
|
|
384
|
+
return doIndexByWith(arr, getter);
|
|
385
|
+
}
|
|
386
|
+
function indexBySymbolWith(arr, getter) {
|
|
387
|
+
return doIndexByWith(arr, getter);
|
|
388
|
+
}
|
|
389
|
+
function doIndexByWith(arr, getter) {
|
|
390
|
+
return arr.reduce((dict, cur) => {
|
|
391
|
+
const key = getter(cur);
|
|
392
|
+
if (key !== null && key !== void 0)
|
|
393
|
+
dict[key] = cur;
|
|
394
|
+
return dict;
|
|
395
|
+
}, {});
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// src/utils/arrays/statistics.ts
|
|
399
|
+
function average(arr) {
|
|
400
|
+
const f = 1 / arr.length;
|
|
401
|
+
return arr.reduce((tot, cur) => tot + cur * f, 0);
|
|
402
|
+
}
|
|
403
|
+
function sum(arr) {
|
|
404
|
+
return arr.reduce((tot, cur) => tot + cur, 0);
|
|
405
|
+
}
|
|
406
|
+
function sumBy(arr, getter) {
|
|
407
|
+
return sum(arr.map(getter));
|
|
408
|
+
}
|
|
409
|
+
function min(arr) {
|
|
410
|
+
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
411
|
+
return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
|
|
412
|
+
}
|
|
413
|
+
function minBy(arr, getter) {
|
|
414
|
+
return min(arr.map(getter));
|
|
415
|
+
}
|
|
416
|
+
function max(arr) {
|
|
417
|
+
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
418
|
+
return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
|
|
419
|
+
}
|
|
420
|
+
function maxBy(arr, getter) {
|
|
421
|
+
return max(arr.map(getter));
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// src/utils/functions/bindThis.ts
|
|
425
|
+
function bindThis(fn, _this) {
|
|
426
|
+
return fn.bind(_this);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/utils/functions/constant.ts
|
|
430
|
+
function constant(v) {
|
|
431
|
+
return () => v;
|
|
432
|
+
}
|
|
433
|
+
function identity(t) {
|
|
434
|
+
return t;
|
|
435
|
+
}
|
|
436
|
+
var constantNull = constant(null);
|
|
437
|
+
var constantTrue = constant(true);
|
|
438
|
+
var constantFalse = constant(false);
|
|
439
|
+
var constantZero = constant(0);
|
|
440
|
+
var constantOne = constant(1);
|
|
441
|
+
|
|
442
|
+
// src/utils/functions/iff.ts
|
|
443
|
+
function iff(firstPredicate, valueIfTrue) {
|
|
444
|
+
if (firstPredicate === true || firstPredicate instanceof Function && firstPredicate() === true) {
|
|
445
|
+
const ret = {
|
|
446
|
+
elseIf: () => ret,
|
|
447
|
+
otherwise: () => valueIfTrue
|
|
448
|
+
};
|
|
449
|
+
return ret;
|
|
450
|
+
} else {
|
|
451
|
+
const ret = {
|
|
452
|
+
elseIf: (elseIf, valueIfElseIfTrue) => iff(elseIf, valueIfElseIfTrue),
|
|
453
|
+
otherwise: (valueIfElse) => valueIfElse
|
|
454
|
+
};
|
|
455
|
+
return ret;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/utils/functions.ts
|
|
460
|
+
function isFunction(t) {
|
|
461
|
+
return typeof t === "function";
|
|
462
|
+
}
|
|
463
|
+
function noop() {
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// src/utils/arrays/uniqBy.ts
|
|
467
|
+
function uniq(arr) {
|
|
468
|
+
return uniqBy(arr, identity);
|
|
469
|
+
}
|
|
470
|
+
function uniqBy(arr, getter) {
|
|
471
|
+
return arr.reduce((dict, cur) => {
|
|
472
|
+
const key = getter(cur);
|
|
473
|
+
if (dict.keys.includes(key)) {
|
|
474
|
+
return dict;
|
|
475
|
+
} else {
|
|
476
|
+
return {
|
|
477
|
+
keys: [...dict.keys, key],
|
|
478
|
+
values: [...dict.values, cur]
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
}, { keys: [], values: [] }).values;
|
|
482
|
+
}
|
|
483
|
+
function uniqByKey(arr, key) {
|
|
484
|
+
return uniqBy(arr, (item) => item[key]);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// src/utils/arrays.ts
|
|
166
488
|
function ensureArray(t) {
|
|
167
489
|
return t instanceof Array ? t : [t];
|
|
168
490
|
}
|
|
@@ -207,11 +529,15 @@ function extendArrayWith(arr, propsFn) {
|
|
|
207
529
|
function reverse(arr) {
|
|
208
530
|
return [...arr].reverse();
|
|
209
531
|
}
|
|
210
|
-
function
|
|
532
|
+
function first(arr, defaultValue = null) {
|
|
211
533
|
return arr.length ? arr[0] : defaultValue;
|
|
212
534
|
}
|
|
213
535
|
function last(arr, defaultValue = null) {
|
|
214
|
-
return
|
|
536
|
+
return arr.length ? arr[arr.length - 1] : defaultValue;
|
|
537
|
+
}
|
|
538
|
+
var head = first;
|
|
539
|
+
function tail(arr) {
|
|
540
|
+
return arr.length ? arr.slice(1) : [];
|
|
215
541
|
}
|
|
216
542
|
function sortedArray(arr, sortFn) {
|
|
217
543
|
return [...arr].sort(sortFn);
|
|
@@ -226,7 +552,7 @@ function flatMapTruthys(arr, mapper) {
|
|
|
226
552
|
return arr.flatMap(mapper).filter((value) => value !== void 0);
|
|
227
553
|
}
|
|
228
554
|
|
|
229
|
-
// src/
|
|
555
|
+
// src/utils/booleans.ts
|
|
230
556
|
function isTrue(x) {
|
|
231
557
|
return x === true;
|
|
232
558
|
}
|
|
@@ -234,7 +560,19 @@ function isFalse(x) {
|
|
|
234
560
|
return x === false;
|
|
235
561
|
}
|
|
236
562
|
|
|
237
|
-
// src/
|
|
563
|
+
// src/utils/errors/withTryCatch.ts
|
|
564
|
+
function withTryCatch(fn) {
|
|
565
|
+
try {
|
|
566
|
+
return [fn(), void 0];
|
|
567
|
+
} catch (e) {
|
|
568
|
+
return [void 0, asError(e)];
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
async function withTryCatchAsync(fn) {
|
|
572
|
+
return fn().then((value) => [value, void 0], (e) => [void 0, asError(e)]);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// src/utils/errors.ts
|
|
238
576
|
function asError(e) {
|
|
239
577
|
if (e === void 0 || e === null)
|
|
240
578
|
return new Error("Void message");
|
|
@@ -250,22 +588,39 @@ function isError(e) {
|
|
|
250
588
|
return e instanceof Error;
|
|
251
589
|
}
|
|
252
590
|
|
|
253
|
-
// src/
|
|
254
|
-
function isFunction(t) {
|
|
255
|
-
return typeof t === "function";
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// src/types/json.ts
|
|
591
|
+
// src/utils/json.ts
|
|
259
592
|
function tryToParseJson(jsonContent) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
593
|
+
return withTryCatch(() => JSON.parse(jsonContent));
|
|
594
|
+
}
|
|
595
|
+
function jsonCloneDeep(a) {
|
|
596
|
+
if (null === a || "object" !== typeof a) return a;
|
|
597
|
+
if (a instanceof Date) {
|
|
598
|
+
return new Date(a.getTime());
|
|
599
|
+
} else if (a instanceof Array) {
|
|
600
|
+
const copy = [];
|
|
601
|
+
for (let i = 0, len = a.length; i < len; i++) {
|
|
602
|
+
copy[i] = jsonCloneDeep(a[i]);
|
|
603
|
+
}
|
|
604
|
+
return copy;
|
|
605
|
+
} else if (a instanceof Object) {
|
|
606
|
+
const copy = {};
|
|
607
|
+
for (let attr in a) {
|
|
608
|
+
if (a.hasOwnProperty(attr))
|
|
609
|
+
copy[attr] = jsonCloneDeep(a[attr]);
|
|
610
|
+
}
|
|
611
|
+
return copy;
|
|
265
612
|
}
|
|
613
|
+
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
266
614
|
}
|
|
615
|
+
function omitFromJsonObject(o, ...keys) {
|
|
616
|
+
return keys.reduce((obj, key) => {
|
|
617
|
+
delete obj[key];
|
|
618
|
+
return obj;
|
|
619
|
+
}, { ...o });
|
|
620
|
+
}
|
|
621
|
+
var omit = omitFromJsonObject;
|
|
267
622
|
|
|
268
|
-
// src/
|
|
623
|
+
// src/utils/nulls.ts
|
|
269
624
|
function ensureDefined(v, name = "value") {
|
|
270
625
|
if (isDefined(v))
|
|
271
626
|
return v;
|
|
@@ -291,7 +646,26 @@ function ifNullOrUndefined(source, callback) {
|
|
|
291
646
|
callback();
|
|
292
647
|
}
|
|
293
648
|
|
|
294
|
-
// src/
|
|
649
|
+
// src/utils/numbers/round.ts
|
|
650
|
+
var roundModes = {
|
|
651
|
+
"toNearest": Math.round,
|
|
652
|
+
"toLower": Math.floor,
|
|
653
|
+
"toUpper": Math.ceil,
|
|
654
|
+
"towardsZero": (n) => n > 0 ? Math.floor(n) : Math.ceil(n),
|
|
655
|
+
"awayFromZero": (n) => n > 0 ? Math.ceil(n) : Math.floor(n)
|
|
656
|
+
};
|
|
657
|
+
function round(n, precision = 0, mode = "toNearest") {
|
|
658
|
+
const base = 10;
|
|
659
|
+
const power = Math.pow(base, precision);
|
|
660
|
+
return roundModes[mode](n * power) / power;
|
|
661
|
+
}
|
|
662
|
+
var roundToNearest = (n, precision = 0) => round(n, precision, "toNearest");
|
|
663
|
+
var roundToLower = (n, precision = 0) => round(n, precision, "toLower");
|
|
664
|
+
var roundToUpper = (n, precision = 0) => round(n, precision, "toUpper");
|
|
665
|
+
var roundTowardsZero = (n, precision = 0) => round(n, precision, "towardsZero");
|
|
666
|
+
var roundAwayFromZero = (n, precision = 0) => round(n, precision, "awayFromZero");
|
|
667
|
+
|
|
668
|
+
// src/utils/numbers.ts
|
|
295
669
|
function ensurePositiveNumber(v, name = "value") {
|
|
296
670
|
if (v !== void 0 && v !== null && v > 0)
|
|
297
671
|
return v;
|
|
@@ -336,8 +710,19 @@ function isNegativeNumber(v) {
|
|
|
336
710
|
function isZero(v) {
|
|
337
711
|
return v === 0;
|
|
338
712
|
}
|
|
713
|
+
function clamp(n, min2, max2) {
|
|
714
|
+
if (min2 === null || max2 === null || n === null || isNaN(min2) || isNaN(max2) || isNaN(n) || min2 > max2)
|
|
715
|
+
throw new Error();
|
|
716
|
+
if (n > max2) {
|
|
717
|
+
return max2;
|
|
718
|
+
} else if (n < min2) {
|
|
719
|
+
return min2;
|
|
720
|
+
} else {
|
|
721
|
+
return n;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
339
724
|
|
|
340
|
-
// src/
|
|
725
|
+
// src/utils/promises.ts
|
|
341
726
|
function asPromise(promisable) {
|
|
342
727
|
return Promise.resolve(promisable);
|
|
343
728
|
}
|
|
@@ -355,61 +740,37 @@ function delayPromise(duration) {
|
|
|
355
740
|
return (result) => duration.promise().then(() => result);
|
|
356
741
|
}
|
|
357
742
|
|
|
358
|
-
// src/
|
|
359
|
-
function
|
|
360
|
-
return
|
|
743
|
+
// src/utils/random.ts
|
|
744
|
+
function randomInterval(min2, max2) {
|
|
745
|
+
return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
|
|
361
746
|
}
|
|
362
|
-
function
|
|
363
|
-
return
|
|
364
|
-
obj[key] = o[key];
|
|
365
|
-
return obj;
|
|
366
|
-
}, {});
|
|
747
|
+
function randomPercentage(min2, max2) {
|
|
748
|
+
return randomInterval(min2, max2) / 100;
|
|
367
749
|
}
|
|
368
750
|
|
|
369
|
-
// src/
|
|
370
|
-
function
|
|
371
|
-
|
|
372
|
-
for (let i = 0; i < str.length; ++i)
|
|
373
|
-
hash = Math.imul(31, hash) + str.charCodeAt(i);
|
|
374
|
-
return hash | 0;
|
|
375
|
-
}
|
|
376
|
-
function repeat(char, times) {
|
|
377
|
-
return times > 0 ? range(0, times - 1).map((_) => char).join("") : "";
|
|
378
|
-
}
|
|
379
|
-
function capitalizeWord(word) {
|
|
380
|
-
return word[0].toUpperCase() + word.substring(1).toLowerCase();
|
|
381
|
-
}
|
|
382
|
-
function splitWords(text, regEx = /\s+/) {
|
|
383
|
-
return text.split(regEx).filter((t) => t.length > 0);
|
|
384
|
-
}
|
|
385
|
-
function stringToNumber(s) {
|
|
386
|
-
if (isNullOrUndefined(s))
|
|
387
|
-
return null;
|
|
388
|
-
return Number(s);
|
|
389
|
-
}
|
|
390
|
-
function pad(str, n, char, where = "left") {
|
|
391
|
-
const length = ensureDefined(str).length;
|
|
392
|
-
if (length >= ensureDefined(n)) return str;
|
|
393
|
-
if (ensureDefined(char).length !== 1)
|
|
394
|
-
throw new Error("Illegal pad character");
|
|
395
|
-
const padding = repeat(char, n - length);
|
|
396
|
-
return (where === "left" ? padding : "") + str + (where === "right" ? padding : "");
|
|
751
|
+
// src/utils/records/entries.ts
|
|
752
|
+
function dictToEntries(obj) {
|
|
753
|
+
return Object.entries(obj);
|
|
397
754
|
}
|
|
398
|
-
function
|
|
399
|
-
return
|
|
755
|
+
function entriesToDict(entries) {
|
|
756
|
+
return Object.fromEntries(entries);
|
|
400
757
|
}
|
|
401
|
-
function
|
|
402
|
-
return
|
|
758
|
+
function mapEntries(dict, mapper) {
|
|
759
|
+
return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
|
|
403
760
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
761
|
+
|
|
762
|
+
// src/utils/records.ts
|
|
763
|
+
function dictToList(obj) {
|
|
764
|
+
return Object.keys(obj).map((key) => obj[key]);
|
|
765
|
+
}
|
|
766
|
+
function pick(o, keys) {
|
|
767
|
+
return keys.reduce((obj, key) => {
|
|
768
|
+
obj[key] = o[key];
|
|
769
|
+
return obj;
|
|
770
|
+
}, {});
|
|
412
771
|
}
|
|
772
|
+
|
|
773
|
+
// src/utils/strings/StringParts.ts
|
|
413
774
|
var StringParts = class _StringParts {
|
|
414
775
|
_parts;
|
|
415
776
|
constructor(...parts) {
|
|
@@ -432,8 +793,8 @@ var StringParts = class _StringParts {
|
|
|
432
793
|
return this._parts;
|
|
433
794
|
}
|
|
434
795
|
get tail() {
|
|
435
|
-
const [_first, ...
|
|
436
|
-
return
|
|
796
|
+
const [_first, ...tail2] = this.parts;
|
|
797
|
+
return tail2;
|
|
437
798
|
}
|
|
438
799
|
get first() {
|
|
439
800
|
return this._parts[0] ?? null;
|
|
@@ -494,7 +855,70 @@ var StringParts = class _StringParts {
|
|
|
494
855
|
};
|
|
495
856
|
};
|
|
496
857
|
|
|
497
|
-
// src/
|
|
858
|
+
// src/utils/strings.ts
|
|
859
|
+
function hashCode(str) {
|
|
860
|
+
let hash = 0;
|
|
861
|
+
for (let i = 0; i < str.length; ++i)
|
|
862
|
+
hash = Math.imul(31, hash) + str.charCodeAt(i);
|
|
863
|
+
return hash | 0;
|
|
864
|
+
}
|
|
865
|
+
function repeat(char, times) {
|
|
866
|
+
return times > 0 ? range(0, times - 1).map((_) => char).join("") : "";
|
|
867
|
+
}
|
|
868
|
+
function capitalizeWord(word) {
|
|
869
|
+
return word[0].toUpperCase() + word.substring(1).toLowerCase();
|
|
870
|
+
}
|
|
871
|
+
function splitWords(text, regEx = /\s+/) {
|
|
872
|
+
return text.split(regEx).filter((t) => t.length > 0);
|
|
873
|
+
}
|
|
874
|
+
function stringToNumber(s) {
|
|
875
|
+
if (isNullOrUndefined(s))
|
|
876
|
+
return null;
|
|
877
|
+
return Number(s);
|
|
878
|
+
}
|
|
879
|
+
function pad(str, n, char, where = "left") {
|
|
880
|
+
const length = ensureDefined(str).length;
|
|
881
|
+
if (length >= ensureDefined(n)) return str;
|
|
882
|
+
if (ensureDefined(char).length !== 1)
|
|
883
|
+
throw new Error("Illegal pad character");
|
|
884
|
+
const padding = repeat(char, n - length);
|
|
885
|
+
return (where === "left" ? padding : "") + str + (where === "right" ? padding : "");
|
|
886
|
+
}
|
|
887
|
+
function padLeft(str, n, char) {
|
|
888
|
+
return pad(str, n, char, "left");
|
|
889
|
+
}
|
|
890
|
+
function padRight(str, n, char) {
|
|
891
|
+
return pad(str, n, char, "right");
|
|
892
|
+
}
|
|
893
|
+
function ellipsis(str, maxLength, padChar, padWhere = "left") {
|
|
894
|
+
if (maxLength < 4)
|
|
895
|
+
throw new Error("Invalid argument maxLength");
|
|
896
|
+
if (str.length <= maxLength) {
|
|
897
|
+
return pad(str, maxLength, padChar, padWhere);
|
|
898
|
+
} else {
|
|
899
|
+
return str.substring(0, maxLength - 3) + "...";
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
function pluralize(n, singular, plural) {
|
|
903
|
+
if (!singular || !singular.length)
|
|
904
|
+
throw new Error();
|
|
905
|
+
if (n === 1)
|
|
906
|
+
return singular;
|
|
907
|
+
plural = plural ?? singular + "s";
|
|
908
|
+
const firstUppercase = singular.charAt(0) === singular.charAt(0).toUpperCase();
|
|
909
|
+
if (firstUppercase) {
|
|
910
|
+
const PLURAL = plural.toUpperCase();
|
|
911
|
+
const isAllUppercase = plural === PLURAL;
|
|
912
|
+
plural = isAllUppercase ? PLURAL : plural.charAt(0).toUpperCase() + plural.slice(1).toLowerCase();
|
|
913
|
+
}
|
|
914
|
+
return plural;
|
|
915
|
+
}
|
|
916
|
+
function wrapWithString(str, delimiter) {
|
|
917
|
+
return delimiter + str + delimiter;
|
|
918
|
+
}
|
|
919
|
+
var wrap = wrapWithString;
|
|
920
|
+
|
|
921
|
+
// src/lazy/Lazy.ts
|
|
498
922
|
var Lazy = class _Lazy {
|
|
499
923
|
_value = void 0;
|
|
500
924
|
_initialized = false;
|
|
@@ -552,7 +976,7 @@ var Lazy = class _Lazy {
|
|
|
552
976
|
}
|
|
553
977
|
};
|
|
554
978
|
|
|
555
|
-
// src/LazyAsync.ts
|
|
979
|
+
// src/lazy/LazyAsync.ts
|
|
556
980
|
var LazyAsync = class _LazyAsync {
|
|
557
981
|
_promise = void 0;
|
|
558
982
|
_pending = false;
|
|
@@ -755,101 +1179,6 @@ var TimeBase = class {
|
|
|
755
1179
|
}
|
|
756
1180
|
};
|
|
757
1181
|
|
|
758
|
-
// src/async/Deferred.ts
|
|
759
|
-
var DeferredCanceledError = class extends Error {
|
|
760
|
-
constructor() {
|
|
761
|
-
super("Execution canceled");
|
|
762
|
-
}
|
|
763
|
-
};
|
|
764
|
-
var Deferred = class {
|
|
765
|
-
_pending;
|
|
766
|
-
_internals;
|
|
767
|
-
get pending() {
|
|
768
|
-
return this._pending;
|
|
769
|
-
}
|
|
770
|
-
/** @deprecated: Use resolve, then and catch directly; use asPromise to return a promise type. */
|
|
771
|
-
get promise() {
|
|
772
|
-
return this._internals.promise;
|
|
773
|
-
}
|
|
774
|
-
constructor() {
|
|
775
|
-
this._pending = true;
|
|
776
|
-
this._internals = this.createInternals();
|
|
777
|
-
}
|
|
778
|
-
resolve(val) {
|
|
779
|
-
if (!this._pending)
|
|
780
|
-
throw new Error("Illegal state exception");
|
|
781
|
-
this.resolveIfPending(val);
|
|
782
|
-
}
|
|
783
|
-
reject(reason) {
|
|
784
|
-
if (!this._pending)
|
|
785
|
-
throw new Error("Illegal state exception");
|
|
786
|
-
this.rejectIfPending(reason);
|
|
787
|
-
}
|
|
788
|
-
cancel() {
|
|
789
|
-
if (!this._pending)
|
|
790
|
-
throw new Error("Illegal state exception");
|
|
791
|
-
this.cancelIfPending();
|
|
792
|
-
}
|
|
793
|
-
resolveIfPending(val) {
|
|
794
|
-
if (!this._pending)
|
|
795
|
-
return;
|
|
796
|
-
this._pending = false;
|
|
797
|
-
this._internals.resolve(val);
|
|
798
|
-
}
|
|
799
|
-
rejectIfPending(reason) {
|
|
800
|
-
if (!this._pending)
|
|
801
|
-
return;
|
|
802
|
-
this._pending = false;
|
|
803
|
-
this._internals.reject(reason);
|
|
804
|
-
}
|
|
805
|
-
cancelIfPending() {
|
|
806
|
-
if (!this._pending)
|
|
807
|
-
return;
|
|
808
|
-
this.reject(new DeferredCanceledError());
|
|
809
|
-
}
|
|
810
|
-
then(onFulfilled, onRejected) {
|
|
811
|
-
return this._internals.promise.then(onFulfilled, onRejected);
|
|
812
|
-
}
|
|
813
|
-
catch(onRejected) {
|
|
814
|
-
return this._internals.promise.catch(onRejected);
|
|
815
|
-
}
|
|
816
|
-
finally(onfinally) {
|
|
817
|
-
return this._internals.promise.finally(onfinally);
|
|
818
|
-
}
|
|
819
|
-
asPromise() {
|
|
820
|
-
return this;
|
|
821
|
-
}
|
|
822
|
-
asCancelablePromise() {
|
|
823
|
-
return this;
|
|
824
|
-
}
|
|
825
|
-
createInternals() {
|
|
826
|
-
let resolveSelf, rejectSelf;
|
|
827
|
-
const promise = new Promise((resolve, reject) => {
|
|
828
|
-
resolveSelf = resolve;
|
|
829
|
-
rejectSelf = reject;
|
|
830
|
-
});
|
|
831
|
-
return {
|
|
832
|
-
promise,
|
|
833
|
-
resolve: resolveSelf,
|
|
834
|
-
reject: rejectSelf
|
|
835
|
-
};
|
|
836
|
-
}
|
|
837
|
-
get [Symbol.toStringTag]() {
|
|
838
|
-
return "[object Deferred]";
|
|
839
|
-
}
|
|
840
|
-
};
|
|
841
|
-
var Deferred_default = Deferred;
|
|
842
|
-
|
|
843
|
-
// src/random/randomInterval.ts
|
|
844
|
-
function randomInterval(min2, max2) {
|
|
845
|
-
return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
// src/random/randomPercentage.ts
|
|
849
|
-
function randomPercentage(min2, max2) {
|
|
850
|
-
return randomInterval(min2, max2) / 100;
|
|
851
|
-
}
|
|
852
|
-
|
|
853
1182
|
// src/time/TimeDuration.ts
|
|
854
1183
|
var TimeDuration = class _TimeDuration extends TimeBase {
|
|
855
1184
|
constructor(value, unit) {
|
|
@@ -1514,10 +1843,6 @@ function isTimeInstant(x) {
|
|
|
1514
1843
|
}
|
|
1515
1844
|
var TimeInstant_default = TimeInstant;
|
|
1516
1845
|
|
|
1517
|
-
// src/utils/noop.ts
|
|
1518
|
-
function noop() {
|
|
1519
|
-
}
|
|
1520
|
-
|
|
1521
1846
|
// src/Logger.ts
|
|
1522
1847
|
var LEVELS = ["log", "debug", "info", "warn", "error"];
|
|
1523
1848
|
var timestamp = () => TimeInstant_default.now().asTimeString();
|
|
@@ -1649,115 +1974,25 @@ var Optional = class _Optional {
|
|
|
1649
1974
|
throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
|
|
1650
1975
|
return new _Optional(t);
|
|
1651
1976
|
}
|
|
1652
|
-
static ofNullable(t) {
|
|
1653
|
-
return new _Optional(t);
|
|
1654
|
-
}
|
|
1655
|
-
};
|
|
1656
|
-
var ErrorGetEmptyOptional = class extends Error {
|
|
1657
|
-
constructor() {
|
|
1658
|
-
super("Cannot retrieve a value from an EmptyOptional.");
|
|
1659
|
-
}
|
|
1660
|
-
};
|
|
1661
|
-
var ErrorSetEmptyOptional = class extends Error {
|
|
1662
|
-
constructor() {
|
|
1663
|
-
super("Cannot set a null or undefined value.");
|
|
1664
|
-
}
|
|
1665
|
-
};
|
|
1666
|
-
var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
|
|
1667
|
-
constructor() {
|
|
1668
|
-
super("Cannot initialize a PresentOptional with a null or undefined value.");
|
|
1669
|
-
}
|
|
1670
|
-
};
|
|
1671
|
-
|
|
1672
|
-
// src/async/RateThrottler.ts
|
|
1673
|
-
var RateThrottler = class {
|
|
1674
|
-
constructor(_frequency) {
|
|
1675
|
-
this._frequency = _frequency;
|
|
1676
|
-
const initialOffset = this.cooldown.divideBy(_frequency.times);
|
|
1677
|
-
this._availableSlots = new Array(_frequency.times).fill(0).map((_, i) => TimeInstant.now().addMs(i * initialOffset.ms));
|
|
1678
|
-
this._waitingRequests = [];
|
|
1679
|
-
}
|
|
1680
|
-
_availableSlots;
|
|
1681
|
-
_waitingRequests;
|
|
1682
|
-
get cooldown() {
|
|
1683
|
-
return this._frequency.period;
|
|
1684
|
-
}
|
|
1685
|
-
async execute(fn) {
|
|
1686
|
-
const wrappedFn = async () => {
|
|
1687
|
-
const slot = this._availableSlots.shift();
|
|
1688
|
-
try {
|
|
1689
|
-
return slot.promise().then(() => fn());
|
|
1690
|
-
} finally {
|
|
1691
|
-
this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
|
|
1692
|
-
if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
|
|
1693
|
-
}
|
|
1694
|
-
};
|
|
1695
|
-
if (this._availableSlots.length > 0) {
|
|
1696
|
-
return wrappedFn();
|
|
1697
|
-
} else {
|
|
1698
|
-
const waitingRequest = new Deferred_default();
|
|
1699
|
-
this._waitingRequests.push(waitingRequest);
|
|
1700
|
-
return waitingRequest.then(() => wrappedFn());
|
|
1701
|
-
}
|
|
1702
|
-
}
|
|
1703
|
-
};
|
|
1704
|
-
|
|
1705
|
-
// src/async/Semaphore.ts
|
|
1706
|
-
var Semaphore = class {
|
|
1707
|
-
constructor(_availableSlots = 1) {
|
|
1708
|
-
this._availableSlots = _availableSlots;
|
|
1709
|
-
this._queuedRequests = [];
|
|
1710
|
-
}
|
|
1711
|
-
_queuedRequests;
|
|
1712
|
-
_inProgress = 0;
|
|
1713
|
-
async _awaitSlot() {
|
|
1714
|
-
if (this._availableSlots > 0) {
|
|
1715
|
-
this._availableSlots -= 1;
|
|
1716
|
-
this._inProgress += 1;
|
|
1717
|
-
return void 0;
|
|
1718
|
-
} else {
|
|
1719
|
-
const deferred = new Deferred_default();
|
|
1720
|
-
this._queuedRequests.push(deferred);
|
|
1721
|
-
return deferred.asPromise();
|
|
1722
|
-
}
|
|
1723
|
-
}
|
|
1724
|
-
_releaseSlot() {
|
|
1725
|
-
const waitingSlotToResolve = this._queuedRequests.shift();
|
|
1726
|
-
if (waitingSlotToResolve) {
|
|
1727
|
-
waitingSlotToResolve.resolve();
|
|
1728
|
-
} else {
|
|
1729
|
-
this._availableSlots += 1;
|
|
1730
|
-
this._inProgress -= 1;
|
|
1731
|
-
}
|
|
1732
|
-
}
|
|
1733
|
-
async execute(fn, cooldown = TimeDuration.ZERO) {
|
|
1734
|
-
return this._awaitSlot().then(() => fn()).finally(() => {
|
|
1735
|
-
void cooldown.promise().then(() => this._releaseSlot());
|
|
1736
|
-
});
|
|
1737
|
-
}
|
|
1738
|
-
get availableSlots() {
|
|
1739
|
-
return this._availableSlots;
|
|
1977
|
+
static ofNullable(t) {
|
|
1978
|
+
return new _Optional(t);
|
|
1740
1979
|
}
|
|
1741
|
-
|
|
1742
|
-
|
|
1980
|
+
};
|
|
1981
|
+
var ErrorGetEmptyOptional = class extends Error {
|
|
1982
|
+
constructor() {
|
|
1983
|
+
super("Cannot retrieve a value from an EmptyOptional.");
|
|
1743
1984
|
}
|
|
1744
|
-
|
|
1745
|
-
|
|
1985
|
+
};
|
|
1986
|
+
var ErrorSetEmptyOptional = class extends Error {
|
|
1987
|
+
constructor() {
|
|
1988
|
+
super("Cannot set a null or undefined value.");
|
|
1989
|
+
}
|
|
1990
|
+
};
|
|
1991
|
+
var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
|
|
1992
|
+
constructor() {
|
|
1993
|
+
super("Cannot initialize a PresentOptional with a null or undefined value.");
|
|
1746
1994
|
}
|
|
1747
1995
|
};
|
|
1748
|
-
|
|
1749
|
-
// src/utils/constant.ts
|
|
1750
|
-
function constant(v) {
|
|
1751
|
-
return () => v;
|
|
1752
|
-
}
|
|
1753
|
-
function identity(t) {
|
|
1754
|
-
return t;
|
|
1755
|
-
}
|
|
1756
|
-
var constantNull = constant(null);
|
|
1757
|
-
var constantTrue = constant(true);
|
|
1758
|
-
var constantFalse = constant(false);
|
|
1759
|
-
var constantZero = constant(0);
|
|
1760
|
-
var constantOne = constant(1);
|
|
1761
1996
|
|
|
1762
1997
|
// src/sorting/ComparisonChain.ts
|
|
1763
1998
|
var defaultCompareValueOptions = {
|
|
@@ -2095,7 +2330,7 @@ var top = (fns, arr, n) => {
|
|
|
2095
2330
|
var bottom = (fns, arr, n) => {
|
|
2096
2331
|
return sort(fns, arr).slice(-Math.max(0, n)).reverse();
|
|
2097
2332
|
};
|
|
2098
|
-
var
|
|
2333
|
+
var first2 = (fns, arr) => {
|
|
2099
2334
|
return arr.length ? top(fns, arr, 1)[0] : null;
|
|
2100
2335
|
};
|
|
2101
2336
|
var last2 = (fns, arr) => {
|
|
@@ -2125,7 +2360,7 @@ function doCreateWithFunctions(fns) {
|
|
|
2125
2360
|
return bottom(fns, arr, n);
|
|
2126
2361
|
},
|
|
2127
2362
|
first: (arr) => {
|
|
2128
|
-
return
|
|
2363
|
+
return first2(fns, arr);
|
|
2129
2364
|
},
|
|
2130
2365
|
last: (arr) => {
|
|
2131
2366
|
return last2(fns, arr);
|
|
@@ -2207,264 +2442,6 @@ var TimeRange = class _TimeRange {
|
|
|
2207
2442
|
}
|
|
2208
2443
|
};
|
|
2209
2444
|
|
|
2210
|
-
// src/utils/bindThis.ts
|
|
2211
|
-
function bindThis(fn, _this) {
|
|
2212
|
-
return fn.bind(_this);
|
|
2213
|
-
}
|
|
2214
|
-
|
|
2215
|
-
// src/utils/entries.ts
|
|
2216
|
-
function dictToEntries(obj) {
|
|
2217
|
-
return Object.entries(obj);
|
|
2218
|
-
}
|
|
2219
|
-
function entriesToDict(entries) {
|
|
2220
|
-
return Object.fromEntries(entries);
|
|
2221
|
-
}
|
|
2222
|
-
function mapEntries(dict, mapper) {
|
|
2223
|
-
return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
|
|
2224
|
-
}
|
|
2225
|
-
|
|
2226
|
-
// src/utils/groupBy.ts
|
|
2227
|
-
function groupByString(arr, field) {
|
|
2228
|
-
return groupByStringWith(arr, (t) => t[field]);
|
|
2229
|
-
}
|
|
2230
|
-
function groupByNumber(arr, field) {
|
|
2231
|
-
return groupByNumberWith(arr, (t) => t[field]);
|
|
2232
|
-
}
|
|
2233
|
-
function groupByBoolean(arr, field) {
|
|
2234
|
-
return groupByBooleanWith(arr, (t) => t[field]);
|
|
2235
|
-
}
|
|
2236
|
-
function groupBySymbol(arr, field) {
|
|
2237
|
-
return groupBySymbolWith(arr, (t) => t[field]);
|
|
2238
|
-
}
|
|
2239
|
-
function groupByStringWith(arr, getter) {
|
|
2240
|
-
return doGroupByWith(arr, getter);
|
|
2241
|
-
}
|
|
2242
|
-
function groupByNumberWith(arr, getter) {
|
|
2243
|
-
return doGroupByWith(arr, getter);
|
|
2244
|
-
}
|
|
2245
|
-
function groupByBooleanWith(arr, getter) {
|
|
2246
|
-
return doGroupByWith(arr, (item) => getter(item) ? "true" : "false");
|
|
2247
|
-
}
|
|
2248
|
-
function groupBySymbolWith(arr, getter) {
|
|
2249
|
-
return doGroupByWith(arr, getter);
|
|
2250
|
-
}
|
|
2251
|
-
function doGroupByWith(arr, getter) {
|
|
2252
|
-
return arr.reduce((dict, cur) => {
|
|
2253
|
-
const key = getter(cur);
|
|
2254
|
-
if (key !== null && key !== void 0) {
|
|
2255
|
-
const arr2 = dict[key] ?? [];
|
|
2256
|
-
arr2.push(cur);
|
|
2257
|
-
dict[key] = arr2;
|
|
2258
|
-
}
|
|
2259
|
-
return dict;
|
|
2260
|
-
}, {});
|
|
2261
|
-
}
|
|
2262
|
-
|
|
2263
|
-
// src/utils/iff.ts
|
|
2264
|
-
function iff(firstPredicate, valueIfTrue) {
|
|
2265
|
-
if (firstPredicate === true || firstPredicate instanceof Function && firstPredicate() === true) {
|
|
2266
|
-
const ret = {
|
|
2267
|
-
elseIf: () => ret,
|
|
2268
|
-
otherwise: () => valueIfTrue
|
|
2269
|
-
};
|
|
2270
|
-
return ret;
|
|
2271
|
-
} else {
|
|
2272
|
-
const ret = {
|
|
2273
|
-
elseIf: (elseIf, valueIfElseIfTrue) => iff(elseIf, valueIfElseIfTrue),
|
|
2274
|
-
otherwise: (valueIfElse) => valueIfElse
|
|
2275
|
-
};
|
|
2276
|
-
return ret;
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
|
-
|
|
2280
|
-
// src/utils/indexBy.ts
|
|
2281
|
-
function indexByString(arr, field) {
|
|
2282
|
-
return indexByStringWith(arr, (t) => t[field]);
|
|
2283
|
-
}
|
|
2284
|
-
function indexByNumber(arr, field) {
|
|
2285
|
-
return indexByNumberWith(arr, (t) => t[field]);
|
|
2286
|
-
}
|
|
2287
|
-
function indexBySymbol(arr, field) {
|
|
2288
|
-
return indexBySymbolWith(arr, (t) => t[field]);
|
|
2289
|
-
}
|
|
2290
|
-
function indexByStringWith(arr, getter) {
|
|
2291
|
-
return doIndexByWith(arr, getter);
|
|
2292
|
-
}
|
|
2293
|
-
function indexByNumberWith(arr, getter) {
|
|
2294
|
-
return doIndexByWith(arr, getter);
|
|
2295
|
-
}
|
|
2296
|
-
function indexBySymbolWith(arr, getter) {
|
|
2297
|
-
return doIndexByWith(arr, getter);
|
|
2298
|
-
}
|
|
2299
|
-
function doIndexByWith(arr, getter) {
|
|
2300
|
-
return arr.reduce((dict, cur) => {
|
|
2301
|
-
const key = getter(cur);
|
|
2302
|
-
if (key !== null && key !== void 0)
|
|
2303
|
-
dict[key] = cur;
|
|
2304
|
-
return dict;
|
|
2305
|
-
}, {});
|
|
2306
|
-
}
|
|
2307
|
-
|
|
2308
|
-
// src/utils/jsonCloneDeep.ts
|
|
2309
|
-
function jsonCloneDeep(a) {
|
|
2310
|
-
if (null === a || "object" !== typeof a) return a;
|
|
2311
|
-
if (a instanceof Date) {
|
|
2312
|
-
return new Date(a.getTime());
|
|
2313
|
-
} else if (a instanceof Array) {
|
|
2314
|
-
const copy = [];
|
|
2315
|
-
for (let i = 0, len = a.length; i < len; i++) {
|
|
2316
|
-
copy[i] = jsonCloneDeep(a[i]);
|
|
2317
|
-
}
|
|
2318
|
-
return copy;
|
|
2319
|
-
} else if (a instanceof Object) {
|
|
2320
|
-
const copy = {};
|
|
2321
|
-
for (let attr in a) {
|
|
2322
|
-
if (a.hasOwnProperty(attr))
|
|
2323
|
-
copy[attr] = jsonCloneDeep(a[attr]);
|
|
2324
|
-
}
|
|
2325
|
-
return copy;
|
|
2326
|
-
}
|
|
2327
|
-
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
2328
|
-
}
|
|
2329
|
-
|
|
2330
|
-
// src/utils/math.ts
|
|
2331
|
-
function clamp(n, min2, max2) {
|
|
2332
|
-
if (min2 === null || max2 === null || n === null || isNaN(min2) || isNaN(max2) || isNaN(n) || min2 > max2)
|
|
2333
|
-
throw new Error();
|
|
2334
|
-
if (n > max2) {
|
|
2335
|
-
return max2;
|
|
2336
|
-
} else if (n < min2) {
|
|
2337
|
-
return min2;
|
|
2338
|
-
} else {
|
|
2339
|
-
return n;
|
|
2340
|
-
}
|
|
2341
|
-
}
|
|
2342
|
-
function average(arr) {
|
|
2343
|
-
const f = 1 / arr.length;
|
|
2344
|
-
return arr.reduce((tot, cur) => tot + cur * f, 0);
|
|
2345
|
-
}
|
|
2346
|
-
function sum(arr) {
|
|
2347
|
-
return arr.reduce((tot, cur) => tot + cur, 0);
|
|
2348
|
-
}
|
|
2349
|
-
function sumBy(arr, getter) {
|
|
2350
|
-
return sum(arr.map(getter));
|
|
2351
|
-
}
|
|
2352
|
-
function min(arr) {
|
|
2353
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
2354
|
-
return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
|
|
2355
|
-
}
|
|
2356
|
-
function minBy(arr, getter) {
|
|
2357
|
-
return min(arr.map(getter));
|
|
2358
|
-
}
|
|
2359
|
-
function max(arr) {
|
|
2360
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
2361
|
-
return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
|
|
2362
|
-
}
|
|
2363
|
-
function maxBy(arr, getter) {
|
|
2364
|
-
return max(arr.map(getter));
|
|
2365
|
-
}
|
|
2366
|
-
|
|
2367
|
-
// src/utils/omit.ts
|
|
2368
|
-
function omit(o, ...keys) {
|
|
2369
|
-
return keys.reduce((obj, key) => {
|
|
2370
|
-
delete obj[key];
|
|
2371
|
-
return obj;
|
|
2372
|
-
}, { ...o });
|
|
2373
|
-
}
|
|
2374
|
-
|
|
2375
|
-
// src/utils/round.ts
|
|
2376
|
-
var roundModes = {
|
|
2377
|
-
"toNearest": Math.round,
|
|
2378
|
-
"toLower": Math.floor,
|
|
2379
|
-
"toUpper": Math.ceil,
|
|
2380
|
-
"towardsZero": (n) => n > 0 ? Math.floor(n) : Math.ceil(n),
|
|
2381
|
-
"awayFromZero": (n) => n > 0 ? Math.ceil(n) : Math.floor(n)
|
|
2382
|
-
};
|
|
2383
|
-
function round(n, precision = 0, mode = "toNearest") {
|
|
2384
|
-
const base = 10;
|
|
2385
|
-
const power = Math.pow(base, precision);
|
|
2386
|
-
return roundModes[mode](n * power) / power;
|
|
2387
|
-
}
|
|
2388
|
-
var roundToNearest = (n, precision = 0) => round(n, precision, "toNearest");
|
|
2389
|
-
var roundToLower = (n, precision = 0) => round(n, precision, "toLower");
|
|
2390
|
-
var roundToUpper = (n, precision = 0) => round(n, precision, "toUpper");
|
|
2391
|
-
var roundTowardsZero = (n, precision = 0) => round(n, precision, "towardsZero");
|
|
2392
|
-
var roundAwayFromZero = (n, precision = 0) => round(n, precision, "awayFromZero");
|
|
2393
|
-
|
|
2394
|
-
// src/utils/sortBy.ts
|
|
2395
|
-
function sortBy(keyOrGetter, direction = "ASC", nulls = "LAST") {
|
|
2396
|
-
const directionNum = direction === "ASC" ? -1 : 1;
|
|
2397
|
-
const nullsNum = nulls === "LAST" ? -1 : 1;
|
|
2398
|
-
const sortByNum = (a, b) => {
|
|
2399
|
-
if (a === b)
|
|
2400
|
-
return -1;
|
|
2401
|
-
const aDefined = !(a === null || a === void 0 || isNaN(a));
|
|
2402
|
-
const bDefined = !(b === null || b === void 0 || isNaN(b));
|
|
2403
|
-
if (!aDefined && !bDefined)
|
|
2404
|
-
return -1;
|
|
2405
|
-
if (aDefined !== bDefined)
|
|
2406
|
-
return nullsNum * (aDefined ? 1 : -1);
|
|
2407
|
-
return (a > b ? -1 : 1) * directionNum;
|
|
2408
|
-
};
|
|
2409
|
-
if (typeof keyOrGetter === "function") {
|
|
2410
|
-
return (a, b) => sortByNum(keyOrGetter(a), keyOrGetter(b));
|
|
2411
|
-
} else {
|
|
2412
|
-
return (a, b) => sortByNum(a[keyOrGetter], b[keyOrGetter]);
|
|
2413
|
-
}
|
|
2414
|
-
}
|
|
2415
|
-
|
|
2416
|
-
// src/utils/throttle.ts
|
|
2417
|
-
function throttle(fn, frequence) {
|
|
2418
|
-
const semaphore = new RateThrottler(frequence);
|
|
2419
|
-
return (...t) => {
|
|
2420
|
-
return semaphore.execute(async () => fn(...t));
|
|
2421
|
-
};
|
|
2422
|
-
}
|
|
2423
|
-
|
|
2424
|
-
// src/utils/uniqBy.ts
|
|
2425
|
-
function uniqBy(arr, getter) {
|
|
2426
|
-
return arr.reduce((dict, cur) => {
|
|
2427
|
-
const key = getter(cur);
|
|
2428
|
-
if (dict.keys.includes(key)) {
|
|
2429
|
-
return dict;
|
|
2430
|
-
} else {
|
|
2431
|
-
return {
|
|
2432
|
-
keys: [...dict.keys, key],
|
|
2433
|
-
values: [...dict.values, cur]
|
|
2434
|
-
};
|
|
2435
|
-
}
|
|
2436
|
-
}, { keys: [], values: [] }).values;
|
|
2437
|
-
}
|
|
2438
|
-
|
|
2439
|
-
// src/utils/uniq.ts
|
|
2440
|
-
function uniq(arr) {
|
|
2441
|
-
return uniqBy(arr, identity);
|
|
2442
|
-
}
|
|
2443
|
-
|
|
2444
|
-
// src/utils/uniqByKey.ts
|
|
2445
|
-
function uniqByKey(arr, key) {
|
|
2446
|
-
return uniqBy(arr, (item) => item[key]);
|
|
2447
|
-
}
|
|
2448
|
-
|
|
2449
|
-
// src/utils/withTryCatch.ts
|
|
2450
|
-
function withTryCatch(fn) {
|
|
2451
|
-
try {
|
|
2452
|
-
return fn();
|
|
2453
|
-
} catch (e) {
|
|
2454
|
-
return asError(e);
|
|
2455
|
-
}
|
|
2456
|
-
}
|
|
2457
|
-
|
|
2458
|
-
// src/utils/withTryCatchAsync.ts
|
|
2459
|
-
async function withTryCatchAsync(fn) {
|
|
2460
|
-
return fn().catch((e) => asError(e));
|
|
2461
|
-
}
|
|
2462
|
-
|
|
2463
|
-
// src/utils/wrap.ts
|
|
2464
|
-
function wrap(str, delimiter) {
|
|
2465
|
-
return delimiter + str + delimiter;
|
|
2466
|
-
}
|
|
2467
|
-
|
|
2468
2445
|
// src/upgrade/errors.ts
|
|
2469
2446
|
var UnavailableUpgradeError = class extends Error {
|
|
2470
2447
|
constructor(data, from, to) {
|
|
@@ -2646,6 +2623,7 @@ export {
|
|
|
2646
2623
|
extendArray,
|
|
2647
2624
|
extendArrayWith,
|
|
2648
2625
|
fill,
|
|
2626
|
+
first,
|
|
2649
2627
|
flatMapTruthys,
|
|
2650
2628
|
groupByBoolean,
|
|
2651
2629
|
groupByBooleanWith,
|
|
@@ -2696,10 +2674,12 @@ export {
|
|
|
2696
2674
|
multiplyBy,
|
|
2697
2675
|
noop,
|
|
2698
2676
|
omit,
|
|
2677
|
+
omitFromJsonObject,
|
|
2699
2678
|
pad,
|
|
2700
2679
|
padLeft,
|
|
2701
2680
|
padRight,
|
|
2702
2681
|
pick,
|
|
2682
|
+
pluralize,
|
|
2703
2683
|
promiseSequence,
|
|
2704
2684
|
randomInterval,
|
|
2705
2685
|
randomPercentage,
|
|
@@ -2712,12 +2692,12 @@ export {
|
|
|
2712
2692
|
roundToNearest,
|
|
2713
2693
|
roundToUpper,
|
|
2714
2694
|
roundTowardsZero,
|
|
2715
|
-
sortBy,
|
|
2716
2695
|
sortedArray,
|
|
2717
2696
|
splitWords,
|
|
2718
2697
|
stringToNumber,
|
|
2719
2698
|
sum,
|
|
2720
2699
|
sumBy,
|
|
2700
|
+
tail,
|
|
2721
2701
|
throttle,
|
|
2722
2702
|
tryToParseJson,
|
|
2723
2703
|
uniq,
|
|
@@ -2726,5 +2706,6 @@ export {
|
|
|
2726
2706
|
upsert,
|
|
2727
2707
|
withTryCatch,
|
|
2728
2708
|
withTryCatchAsync,
|
|
2729
|
-
wrap
|
|
2709
|
+
wrap,
|
|
2710
|
+
wrapWithString
|
|
2730
2711
|
};
|