@zelgadis87/utils-core 5.2.11 → 5.3.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/.rollup/index.cjs +3267 -0
- package/.rollup/index.cjs.map +1 -0
- package/.rollup/index.d.ts +1303 -0
- package/.rollup/index.mjs +3088 -0
- package/.rollup/index.mjs.map +1 -0
- package/.rollup/tsconfig.tsbuildinfo +1 -0
- package/CHANGELOG.md +11 -1
- package/package.json +6 -8
- package/src/time/TimeInstant.ts +5 -5
- package/src/time/TimeInstantBuilder.ts +2 -2
- package/src/time/types.ts +2 -1
- package/src/utils/arrays.ts +3 -1
- package/dist/Logger.d.ts +0 -22
- package/dist/Optional.d.ts +0 -108
- package/dist/async/CancelableDeferred.d.ts +0 -17
- package/dist/async/Deferred.d.ts +0 -32
- package/dist/async/RateThrottler.d.ts +0 -13
- package/dist/async/Semaphore.d.ts +0 -17
- package/dist/async/_index.d.ts +0 -3
- package/dist/index.d.ts +0 -8
- package/dist/lazy/Lazy.d.ts +0 -22
- package/dist/lazy/LazyAsync.d.ts +0 -24
- package/dist/lazy/LazyDictionary.d.ts +0 -9
- package/dist/lazy/_index.d.ts +0 -3
- package/dist/sorting/ComparisonChain.d.ts +0 -57
- package/dist/sorting/Sorter.d.ts +0 -121
- package/dist/sorting/_index.d.ts +0 -2
- package/dist/sorting/types.d.ts +0 -5
- package/dist/time/RandomTimeDuration.d.ts +0 -9
- package/dist/time/TimeBase.d.ts +0 -49
- package/dist/time/TimeDuration.d.ts +0 -71
- package/dist/time/TimeFrequency.d.ts +0 -10
- package/dist/time/TimeInstant.d.ts +0 -174
- package/dist/time/TimeInstantBuilder.d.ts +0 -72
- package/dist/time/TimeRange.d.ts +0 -11
- package/dist/time/TimeUnit.d.ts +0 -15
- package/dist/time/_index.d.ts +0 -8
- package/dist/time/constants.d.ts +0 -14
- package/dist/time/types.d.ts +0 -28
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/upgrade/DataUpgrader.d.ts +0 -22
- package/dist/upgrade/_index.d.ts +0 -2
- package/dist/upgrade/errors.d.ts +0 -12
- package/dist/upgrade/getTransitionsPath.d.ts +0 -3
- package/dist/upgrade/types.d.ts +0 -31
- package/dist/utils/_index.d.ts +0 -15
- package/dist/utils/arrays/groupBy.d.ts +0 -9
- package/dist/utils/arrays/indexBy.d.ts +0 -7
- package/dist/utils/arrays/statistics.d.ts +0 -8
- package/dist/utils/arrays/uniqBy.d.ts +0 -4
- package/dist/utils/arrays.d.ts +0 -71
- package/dist/utils/booleans.d.ts +0 -2
- package/dist/utils/css.d.ts +0 -14
- package/dist/utils/empties.d.ts +0 -18
- package/dist/utils/errors/withTryCatch.d.ts +0 -4
- package/dist/utils/errors.d.ts +0 -20
- package/dist/utils/functions/_index.d.ts +0 -3
- package/dist/utils/functions/constant.d.ts +0 -11
- package/dist/utils/functions/iff.d.ts +0 -8
- package/dist/utils/functions/predicateBuilder.d.ts +0 -7
- package/dist/utils/functions.d.ts +0 -41
- package/dist/utils/json.d.ts +0 -11
- package/dist/utils/nulls.d.ts +0 -9
- package/dist/utils/numbers/round.d.ts +0 -8
- package/dist/utils/numbers.d.ts +0 -35
- package/dist/utils/operations.d.ts +0 -28
- package/dist/utils/primitives.d.ts +0 -3
- package/dist/utils/promises.d.ts +0 -10
- package/dist/utils/random.d.ts +0 -3
- package/dist/utils/records/entries.d.ts +0 -7
- package/dist/utils/records.d.ts +0 -63
- package/dist/utils/strings/StringParts.d.ts +0 -38
- package/dist/utils/strings.d.ts +0 -20
- package/esbuild/index.cjs +0 -3380
- package/esbuild/index.cjs.map +0 -7
- package/esbuild/index.mjs +0 -3177
- package/esbuild/index.mjs.map +0 -7
package/esbuild/index.cjs
DELETED
|
@@ -1,3380 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
DataUpgrader: () => DataUpgrader,
|
|
24
|
-
Deferred: () => Deferred,
|
|
25
|
-
DeferredCanceledError: () => DeferredCanceledError,
|
|
26
|
-
ErrorCannotInstantiatePresentOptionalWithEmptyValue: () => ErrorCannotInstantiatePresentOptionalWithEmptyValue,
|
|
27
|
-
ErrorGetEmptyOptional: () => ErrorGetEmptyOptional,
|
|
28
|
-
ErrorSetEmptyOptional: () => ErrorSetEmptyOptional,
|
|
29
|
-
Lazy: () => Lazy,
|
|
30
|
-
LazyAsync: () => LazyAsync,
|
|
31
|
-
LazyDictionary: () => LazyDictionary,
|
|
32
|
-
Logger: () => Logger,
|
|
33
|
-
NEVER: () => NEVER,
|
|
34
|
-
NonExhaustiveSwitchError: () => NonExhaustiveSwitchError,
|
|
35
|
-
Operation: () => Operation,
|
|
36
|
-
Optional: () => Optional,
|
|
37
|
-
PredicateBuilder: () => PredicateBuilder,
|
|
38
|
-
RandomTimeDuration: () => RandomTimeDuration,
|
|
39
|
-
RateThrottler: () => RateThrottler,
|
|
40
|
-
Semaphore: () => Semaphore,
|
|
41
|
-
Sorter: () => Sorter,
|
|
42
|
-
StringParts: () => StringParts,
|
|
43
|
-
TimeDuration: () => TimeDuration,
|
|
44
|
-
TimeFrequency: () => TimeFrequency,
|
|
45
|
-
TimeInstant: () => TimeInstant,
|
|
46
|
-
TimeRange: () => TimeRange,
|
|
47
|
-
TimeUnit: () => TimeUnit,
|
|
48
|
-
TimeoutError: () => TimeoutError,
|
|
49
|
-
alwaysFalse: () => alwaysFalse,
|
|
50
|
-
alwaysTrue: () => alwaysTrue,
|
|
51
|
-
and: () => and,
|
|
52
|
-
arrayGet: () => arrayGet,
|
|
53
|
-
asError: () => asError,
|
|
54
|
-
asPromise: () => asPromise,
|
|
55
|
-
average: () => average,
|
|
56
|
-
awaitAtMost: () => awaitAtMost,
|
|
57
|
-
capitalizeWord: () => capitalizeWord,
|
|
58
|
-
clamp: () => clamp,
|
|
59
|
-
clampInt0_100: () => clampInt0_100,
|
|
60
|
-
constant: () => constant,
|
|
61
|
-
constantFalse: () => constantFalse,
|
|
62
|
-
constantNull: () => constantNull,
|
|
63
|
-
constantOne: () => constantOne,
|
|
64
|
-
constantTrue: () => constantTrue,
|
|
65
|
-
constantUndefined: () => constantUndefined,
|
|
66
|
-
constantZero: () => constantZero,
|
|
67
|
-
cssDeclarationRulesDictionaryToCss: () => cssDeclarationRulesDictionaryToCss,
|
|
68
|
-
decrement: () => decrement,
|
|
69
|
-
decrementBy: () => decrementBy,
|
|
70
|
-
delayPromise: () => delayPromise,
|
|
71
|
-
dictToEntries: () => dictToEntries,
|
|
72
|
-
dictToList: () => dictToList,
|
|
73
|
-
divideBy: () => divideBy,
|
|
74
|
-
ellipsis: () => ellipsis,
|
|
75
|
-
ensureArray: () => ensureArray,
|
|
76
|
-
ensureDefined: () => ensureDefined,
|
|
77
|
-
ensureNegativeNumber: () => ensureNegativeNumber,
|
|
78
|
-
ensureNonNegativeNumber: () => ensureNonNegativeNumber,
|
|
79
|
-
ensureNonPositiveNumber: () => ensureNonPositiveNumber,
|
|
80
|
-
ensurePositiveNumber: () => ensurePositiveNumber,
|
|
81
|
-
ensureReadableArray: () => ensureReadableArray,
|
|
82
|
-
entriesToDict: () => entriesToDict,
|
|
83
|
-
entriesToEntries: () => entriesToEntries,
|
|
84
|
-
entriesToList: () => entriesToList,
|
|
85
|
-
extendArray: () => extendArray,
|
|
86
|
-
extendArrayWith: () => extendArrayWith,
|
|
87
|
-
fill: () => fill,
|
|
88
|
-
filterMap: () => filterMap,
|
|
89
|
-
filterMapReduce: () => filterMapReduce,
|
|
90
|
-
filterWithTypePredicate: () => filterWithTypePredicate,
|
|
91
|
-
findInArray: () => findInArray,
|
|
92
|
-
findIndexInArray: () => findIndexInArray,
|
|
93
|
-
first: () => first,
|
|
94
|
-
flatMapTruthys: () => flatMapTruthys,
|
|
95
|
-
getCauseMessageFromError: () => getCauseMessageFromError,
|
|
96
|
-
getCauseStackFromError: () => getCauseStackFromError,
|
|
97
|
-
getMessageFromError: () => getMessageFromError,
|
|
98
|
-
getStackFromError: () => getStackFromError,
|
|
99
|
-
groupByBoolean: () => groupByBoolean,
|
|
100
|
-
groupByBooleanWith: () => groupByBooleanWith,
|
|
101
|
-
groupByNumber: () => groupByNumber,
|
|
102
|
-
groupByNumberWith: () => groupByNumberWith,
|
|
103
|
-
groupByString: () => groupByString,
|
|
104
|
-
groupByStringWith: () => groupByStringWith,
|
|
105
|
-
groupBySymbol: () => groupBySymbol,
|
|
106
|
-
groupBySymbolWith: () => groupBySymbolWith,
|
|
107
|
-
hashCode: () => hashCode,
|
|
108
|
-
head: () => head,
|
|
109
|
-
identity: () => identity,
|
|
110
|
-
ifDefined: () => ifDefined,
|
|
111
|
-
ifNullOrUndefined: () => ifNullOrUndefined,
|
|
112
|
-
iff: () => iff,
|
|
113
|
-
includes: () => includes,
|
|
114
|
-
increment: () => increment,
|
|
115
|
-
incrementBy: () => incrementBy,
|
|
116
|
-
indexByNumber: () => indexByNumber,
|
|
117
|
-
indexByNumberWith: () => indexByNumberWith,
|
|
118
|
-
indexByString: () => indexByString,
|
|
119
|
-
indexByStringWith: () => indexByStringWith,
|
|
120
|
-
indexBySymbol: () => indexBySymbol,
|
|
121
|
-
indexBySymbolWith: () => indexBySymbolWith,
|
|
122
|
-
isAllowedTimeDuration: () => isAllowedTimeDuration,
|
|
123
|
-
isArray: () => isArray,
|
|
124
|
-
isDefined: () => isDefined,
|
|
125
|
-
isEmpty: () => isEmpty,
|
|
126
|
-
isError: () => isError,
|
|
127
|
-
isFalse: () => isFalse,
|
|
128
|
-
isFunction: () => isFunction,
|
|
129
|
-
isNegativeNumber: () => isNegativeNumber,
|
|
130
|
-
isNullOrUndefined: () => isNullOrUndefined,
|
|
131
|
-
isNullOrUndefinedOrEmpty: () => isNullOrUndefinedOrEmpty,
|
|
132
|
-
isNumber: () => isNumber,
|
|
133
|
-
isPositiveNumber: () => isPositiveNumber,
|
|
134
|
-
isString: () => isString,
|
|
135
|
-
isTimeInstant: () => isTimeInstant,
|
|
136
|
-
isTrue: () => isTrue,
|
|
137
|
-
isUpgradable: () => isUpgradable,
|
|
138
|
-
isZero: () => isZero,
|
|
139
|
-
jsonCloneDeep: () => jsonCloneDeep,
|
|
140
|
-
last: () => last,
|
|
141
|
-
listToDict: () => listToDict,
|
|
142
|
-
mapDefined: () => mapDefined,
|
|
143
|
-
mapEntries: () => mapEntries,
|
|
144
|
-
mapFirstTruthy: () => mapFirstTruthy,
|
|
145
|
-
mapTruthys: () => mapTruthys,
|
|
146
|
-
max: () => max,
|
|
147
|
-
maxBy: () => maxBy,
|
|
148
|
-
min: () => min,
|
|
149
|
-
minBy: () => minBy,
|
|
150
|
-
multiplyBy: () => multiplyBy,
|
|
151
|
-
noop: () => noop,
|
|
152
|
-
not: () => not,
|
|
153
|
-
omitFromJsonObject: () => omitFromJsonObject,
|
|
154
|
-
or: () => or,
|
|
155
|
-
pad: () => pad,
|
|
156
|
-
padLeft: () => padLeft,
|
|
157
|
-
padRight: () => padRight,
|
|
158
|
-
parseJson: () => parseJson,
|
|
159
|
-
partition: () => partition,
|
|
160
|
-
pick: () => pick,
|
|
161
|
-
pipedInvoke: () => pipedInvoke,
|
|
162
|
-
pipedInvokeFromArray: () => pipedInvokeFromArray,
|
|
163
|
-
pluralize: () => pluralize,
|
|
164
|
-
promiseSequence: () => promiseSequence,
|
|
165
|
-
randomId: () => randomId,
|
|
166
|
-
randomNumberInInterval: () => randomNumberInInterval,
|
|
167
|
-
range: () => range,
|
|
168
|
-
repeat: () => repeat,
|
|
169
|
-
reverse: () => reverse,
|
|
170
|
-
round: () => round,
|
|
171
|
-
roundAwayFromZero: () => roundAwayFromZero,
|
|
172
|
-
roundToLower: () => roundToLower,
|
|
173
|
-
roundToNearest: () => roundToNearest,
|
|
174
|
-
roundToUpper: () => roundToUpper,
|
|
175
|
-
roundTowardsZero: () => roundTowardsZero,
|
|
176
|
-
shallowArrayEquals: () => shallowArrayEquals,
|
|
177
|
-
shallowRecordEquals: () => shallowRecordEquals,
|
|
178
|
-
sortedArray: () => sortedArray,
|
|
179
|
-
splitWords: () => splitWords,
|
|
180
|
-
stringToNumber: () => stringToNumber,
|
|
181
|
-
stringifyJson: () => stringifyJson,
|
|
182
|
-
sum: () => sum,
|
|
183
|
-
sumBy: () => sumBy,
|
|
184
|
-
tail: () => tail,
|
|
185
|
-
throttle: () => throttle,
|
|
186
|
-
throwIfNullOrUndefined: () => throwIfNullOrUndefined,
|
|
187
|
-
transformCssDictionary: () => transformCssDictionary,
|
|
188
|
-
tryToParseJson: () => tryToParseJson,
|
|
189
|
-
tryToParseNumber: () => tryToParseNumber,
|
|
190
|
-
uniq: () => uniq,
|
|
191
|
-
uniqBy: () => uniqBy,
|
|
192
|
-
uniqByKey: () => uniqByKey,
|
|
193
|
-
unzip: () => unzip,
|
|
194
|
-
upsert: () => upsert,
|
|
195
|
-
withTryCatch: () => withTryCatch,
|
|
196
|
-
withTryCatchAsync: () => withTryCatchAsync,
|
|
197
|
-
wrapWithString: () => wrapWithString,
|
|
198
|
-
xor: () => xor,
|
|
199
|
-
zip: () => zip
|
|
200
|
-
});
|
|
201
|
-
module.exports = __toCommonJS(index_exports);
|
|
202
|
-
|
|
203
|
-
// src/async/Deferred.ts
|
|
204
|
-
var DeferredCanceledError = class extends Error {
|
|
205
|
-
constructor() {
|
|
206
|
-
super("Execution canceled");
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
var Deferred = class {
|
|
210
|
-
_pending;
|
|
211
|
-
_internals;
|
|
212
|
-
get pending() {
|
|
213
|
-
return this._pending;
|
|
214
|
-
}
|
|
215
|
-
constructor() {
|
|
216
|
-
this._pending = true;
|
|
217
|
-
this._internals = this.createInternals();
|
|
218
|
-
}
|
|
219
|
-
resolve(val) {
|
|
220
|
-
if (!this._pending)
|
|
221
|
-
throw new Error("Illegal state exception");
|
|
222
|
-
this.resolveIfPending(val);
|
|
223
|
-
}
|
|
224
|
-
reject(reason) {
|
|
225
|
-
if (!this._pending)
|
|
226
|
-
throw new Error("Illegal state exception");
|
|
227
|
-
this.rejectIfPending(reason);
|
|
228
|
-
}
|
|
229
|
-
cancel() {
|
|
230
|
-
if (!this._pending)
|
|
231
|
-
throw new Error("Illegal state exception");
|
|
232
|
-
this.cancelIfPending();
|
|
233
|
-
}
|
|
234
|
-
resolveIfPending(val) {
|
|
235
|
-
if (!this._pending)
|
|
236
|
-
return;
|
|
237
|
-
this._pending = false;
|
|
238
|
-
this._internals.resolve(val);
|
|
239
|
-
}
|
|
240
|
-
rejectIfPending(reason) {
|
|
241
|
-
if (!this._pending)
|
|
242
|
-
return;
|
|
243
|
-
this._pending = false;
|
|
244
|
-
this._internals.reject(reason);
|
|
245
|
-
}
|
|
246
|
-
cancelIfPending() {
|
|
247
|
-
if (!this._pending)
|
|
248
|
-
return;
|
|
249
|
-
this.reject(new DeferredCanceledError());
|
|
250
|
-
}
|
|
251
|
-
then(onFulfilled, onRejected) {
|
|
252
|
-
return this._internals.promise.then(onFulfilled, onRejected);
|
|
253
|
-
}
|
|
254
|
-
catch(onRejected) {
|
|
255
|
-
return this._internals.promise.catch(onRejected);
|
|
256
|
-
}
|
|
257
|
-
finally(onfinally) {
|
|
258
|
-
return this._internals.promise.finally(onfinally);
|
|
259
|
-
}
|
|
260
|
-
asPromise() {
|
|
261
|
-
return this;
|
|
262
|
-
}
|
|
263
|
-
asCancelablePromise() {
|
|
264
|
-
return this;
|
|
265
|
-
}
|
|
266
|
-
createInternals() {
|
|
267
|
-
let resolveSelf, rejectSelf;
|
|
268
|
-
const promise = new Promise((resolve, reject) => {
|
|
269
|
-
resolveSelf = resolve;
|
|
270
|
-
rejectSelf = reject;
|
|
271
|
-
});
|
|
272
|
-
return {
|
|
273
|
-
promise,
|
|
274
|
-
resolve: resolveSelf,
|
|
275
|
-
reject: rejectSelf
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
get [Symbol.toStringTag]() {
|
|
279
|
-
return "[object Deferred]";
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
var Deferred_default = Deferred;
|
|
283
|
-
|
|
284
|
-
// src/async/RateThrottler.ts
|
|
285
|
-
var RateThrottler = class {
|
|
286
|
-
constructor(_frequency) {
|
|
287
|
-
this._frequency = _frequency;
|
|
288
|
-
const initialOffset = this.cooldown.divideBy(_frequency.times);
|
|
289
|
-
this._availableSlots = new Array(_frequency.times).fill(0).map((_, i) => TimeInstant.now().addMs(i * initialOffset.ms));
|
|
290
|
-
this._waitingRequests = [];
|
|
291
|
-
}
|
|
292
|
-
_availableSlots;
|
|
293
|
-
_waitingRequests;
|
|
294
|
-
get cooldown() {
|
|
295
|
-
return this._frequency.period;
|
|
296
|
-
}
|
|
297
|
-
async execute(fn) {
|
|
298
|
-
const wrappedFn = async () => {
|
|
299
|
-
const slot = this._availableSlots.shift();
|
|
300
|
-
try {
|
|
301
|
-
return slot.promise().then(() => fn());
|
|
302
|
-
} finally {
|
|
303
|
-
this._availableSlots.push(TimeInstant.now().addDuration(this.cooldown));
|
|
304
|
-
if (this._waitingRequests.length > 0) this._waitingRequests.shift().resolve();
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
if (this._availableSlots.length > 0) {
|
|
308
|
-
return wrappedFn();
|
|
309
|
-
} else {
|
|
310
|
-
const waitingRequest = new Deferred_default();
|
|
311
|
-
this._waitingRequests.push(waitingRequest);
|
|
312
|
-
return waitingRequest.then(() => wrappedFn());
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
function throttle(fn, frequence) {
|
|
317
|
-
const semaphore = new RateThrottler(frequence);
|
|
318
|
-
return (...t) => {
|
|
319
|
-
return semaphore.execute(async () => fn(...t));
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// src/async/Semaphore.ts
|
|
324
|
-
var Semaphore = class {
|
|
325
|
-
constructor(_availableSlots = 1) {
|
|
326
|
-
this._availableSlots = _availableSlots;
|
|
327
|
-
this._queuedRequests = [];
|
|
328
|
-
}
|
|
329
|
-
_queuedRequests;
|
|
330
|
-
_inProgress = 0;
|
|
331
|
-
async _awaitSlot() {
|
|
332
|
-
if (this._availableSlots > 0) {
|
|
333
|
-
this._availableSlots -= 1;
|
|
334
|
-
this._inProgress += 1;
|
|
335
|
-
return void 0;
|
|
336
|
-
} else {
|
|
337
|
-
const deferred = new Deferred_default();
|
|
338
|
-
this._queuedRequests.push(deferred);
|
|
339
|
-
return deferred.asPromise();
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
_releaseSlot() {
|
|
343
|
-
const waitingSlotToResolve = this._queuedRequests.shift();
|
|
344
|
-
if (waitingSlotToResolve) {
|
|
345
|
-
waitingSlotToResolve.resolve();
|
|
346
|
-
} else {
|
|
347
|
-
this._availableSlots += 1;
|
|
348
|
-
this._inProgress -= 1;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
async execute(fn, cooldown = TimeDuration.ZERO) {
|
|
352
|
-
return this._awaitSlot().then(fn).finally(() => {
|
|
353
|
-
void cooldown.delay(() => this._releaseSlot());
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
get availableSlots() {
|
|
357
|
-
return this._availableSlots;
|
|
358
|
-
}
|
|
359
|
-
get queueSize() {
|
|
360
|
-
return this._queuedRequests.length;
|
|
361
|
-
}
|
|
362
|
-
get inProgressSize() {
|
|
363
|
-
return this._inProgress;
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
// src/utils/nulls.ts
|
|
368
|
-
function ensureDefined(v, name = "value") {
|
|
369
|
-
if (isDefined(v))
|
|
370
|
-
return v;
|
|
371
|
-
throw new Error("Expected " + name + " to be defined, got: " + v);
|
|
372
|
-
}
|
|
373
|
-
function isDefined(x) {
|
|
374
|
-
return x !== null && x !== void 0;
|
|
375
|
-
}
|
|
376
|
-
function isNullOrUndefined(x) {
|
|
377
|
-
return x === null || x === void 0;
|
|
378
|
-
}
|
|
379
|
-
function ifDefined(source, callback) {
|
|
380
|
-
if (isDefined(source))
|
|
381
|
-
callback(source);
|
|
382
|
-
}
|
|
383
|
-
function mapDefined(source, mapper) {
|
|
384
|
-
if (isDefined(source))
|
|
385
|
-
return mapper(source);
|
|
386
|
-
return null;
|
|
387
|
-
}
|
|
388
|
-
function ifNullOrUndefined(source, callback) {
|
|
389
|
-
if (isNullOrUndefined(source))
|
|
390
|
-
callback();
|
|
391
|
-
}
|
|
392
|
-
function throwIfNullOrUndefined(source, errorProducer = () => new Error(`Unexpected ${source} value`)) {
|
|
393
|
-
return ifNullOrUndefined(source, () => {
|
|
394
|
-
throw errorProducer();
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// src/Optional.ts
|
|
399
|
-
var Optional = class _Optional {
|
|
400
|
-
_present;
|
|
401
|
-
_value;
|
|
402
|
-
constructor(t) {
|
|
403
|
-
const defined = isDefined(t);
|
|
404
|
-
this._value = defined ? t : void 0;
|
|
405
|
-
this._present = defined;
|
|
406
|
-
}
|
|
407
|
-
getRawValue() {
|
|
408
|
-
return this._value;
|
|
409
|
-
}
|
|
410
|
-
get() {
|
|
411
|
-
return this.getOrElseThrow(() => new ErrorGetEmptyOptional());
|
|
412
|
-
}
|
|
413
|
-
getOrElseThrow(errorProducer) {
|
|
414
|
-
if (this.isEmpty())
|
|
415
|
-
throw errorProducer();
|
|
416
|
-
return this._value;
|
|
417
|
-
}
|
|
418
|
-
set(t) {
|
|
419
|
-
if (isNullOrUndefined(t))
|
|
420
|
-
throw new ErrorSetEmptyOptional();
|
|
421
|
-
this._value = t;
|
|
422
|
-
this._present = true;
|
|
423
|
-
}
|
|
424
|
-
setNullable(t) {
|
|
425
|
-
if (isDefined(t)) {
|
|
426
|
-
return this.set(t);
|
|
427
|
-
}
|
|
428
|
-
return this;
|
|
429
|
-
}
|
|
430
|
-
clear() {
|
|
431
|
-
this._value = void 0;
|
|
432
|
-
this._present = false;
|
|
433
|
-
}
|
|
434
|
-
isEmpty() {
|
|
435
|
-
return !this._present;
|
|
436
|
-
}
|
|
437
|
-
isPresent() {
|
|
438
|
-
return this._present;
|
|
439
|
-
}
|
|
440
|
-
ifEmpty(callback) {
|
|
441
|
-
if (this.isEmpty())
|
|
442
|
-
return callback();
|
|
443
|
-
}
|
|
444
|
-
ifPresent(callback) {
|
|
445
|
-
if (this.isPresent())
|
|
446
|
-
return callback(this.get());
|
|
447
|
-
}
|
|
448
|
-
ifPresentThenClear(callback) {
|
|
449
|
-
if (this.isPresent()) {
|
|
450
|
-
callback(this.get());
|
|
451
|
-
this.clear();
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
apply(callbackIfPresent, callbackIfEmpty) {
|
|
455
|
-
if (this.isEmpty()) {
|
|
456
|
-
return callbackIfEmpty();
|
|
457
|
-
} else {
|
|
458
|
-
return callbackIfPresent(this.get());
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
orElseReturn(newValue) {
|
|
462
|
-
if (this.isPresent()) {
|
|
463
|
-
return this.get();
|
|
464
|
-
} else {
|
|
465
|
-
return newValue;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
orElse = this.orElseReturn.bind(this);
|
|
469
|
-
orElseProduce(newValueProducer) {
|
|
470
|
-
if (this.isPresent()) {
|
|
471
|
-
return this.get();
|
|
472
|
-
} else {
|
|
473
|
-
return newValueProducer();
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
orElseGet = this.orElseProduce.bind(this);
|
|
477
|
-
orElseReturnAndApply(newValue) {
|
|
478
|
-
if (this.isPresent()) {
|
|
479
|
-
return this.get();
|
|
480
|
-
} else {
|
|
481
|
-
this.set(newValue);
|
|
482
|
-
return newValue;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
orElseProduceAndApply(newValueProducer) {
|
|
486
|
-
if (this.isPresent()) {
|
|
487
|
-
return this.get();
|
|
488
|
-
} else {
|
|
489
|
-
const newValue = newValueProducer();
|
|
490
|
-
this.set(newValue);
|
|
491
|
-
return newValue;
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
orElseReturnNullableAndApply(newValue) {
|
|
495
|
-
if (this.isPresent()) {
|
|
496
|
-
return this;
|
|
497
|
-
} else {
|
|
498
|
-
this.setNullable(newValue);
|
|
499
|
-
return this;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
orElseProduceNullableAndApply(newValueProducer) {
|
|
503
|
-
if (this.isPresent()) {
|
|
504
|
-
return this;
|
|
505
|
-
} else {
|
|
506
|
-
const newValue = newValueProducer();
|
|
507
|
-
this.setNullable(newValue);
|
|
508
|
-
return this;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
orElseReturnNullable(newValue) {
|
|
512
|
-
if (this.isEmpty()) return _Optional.ofNullable(newValue);
|
|
513
|
-
return this;
|
|
514
|
-
}
|
|
515
|
-
orElseNullable = this.orElseReturnNullable.bind(this);
|
|
516
|
-
orElseProduceNullable(newValueProducer) {
|
|
517
|
-
if (this.isEmpty()) {
|
|
518
|
-
const newValue = newValueProducer();
|
|
519
|
-
return _Optional.ofNullable(newValue);
|
|
520
|
-
}
|
|
521
|
-
return this;
|
|
522
|
-
}
|
|
523
|
-
orElseGetNullable = this.orElseProduceNullable.bind(this);
|
|
524
|
-
orElseThrow(errorProducer) {
|
|
525
|
-
if (this.isEmpty())
|
|
526
|
-
throw errorProducer();
|
|
527
|
-
return this;
|
|
528
|
-
}
|
|
529
|
-
throwIfPresent(errorProducer) {
|
|
530
|
-
if (this.isEmpty())
|
|
531
|
-
return this;
|
|
532
|
-
throw errorProducer(this.get());
|
|
533
|
-
}
|
|
534
|
-
mapTo(mapper) {
|
|
535
|
-
return this.flatMapTo((t) => _Optional.ofNullable(mapper(t)));
|
|
536
|
-
}
|
|
537
|
-
flatMapTo(mapper) {
|
|
538
|
-
return this.isPresent() ? mapper(this.get()) : _Optional.empty();
|
|
539
|
-
}
|
|
540
|
-
filter(predicate) {
|
|
541
|
-
if (this.isEmpty())
|
|
542
|
-
return this;
|
|
543
|
-
if (predicate(this.get()))
|
|
544
|
-
return this;
|
|
545
|
-
return _Optional.empty();
|
|
546
|
-
}
|
|
547
|
-
static empty() {
|
|
548
|
-
return new _Optional(void 0);
|
|
549
|
-
}
|
|
550
|
-
static of(t) {
|
|
551
|
-
if (isNullOrUndefined(t))
|
|
552
|
-
throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
|
|
553
|
-
return new _Optional(t);
|
|
554
|
-
}
|
|
555
|
-
static ofNullable(t) {
|
|
556
|
-
return new _Optional(t);
|
|
557
|
-
}
|
|
558
|
-
};
|
|
559
|
-
var Optional_default = Optional;
|
|
560
|
-
var ErrorGetEmptyOptional = class extends Error {
|
|
561
|
-
constructor() {
|
|
562
|
-
super("Cannot retrieve a value from an empty Optional.");
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
|
-
var ErrorSetEmptyOptional = class extends Error {
|
|
566
|
-
constructor() {
|
|
567
|
-
super("Cannot set a null or undefined value.");
|
|
568
|
-
}
|
|
569
|
-
};
|
|
570
|
-
var ErrorCannotInstantiatePresentOptionalWithEmptyValue = class extends Error {
|
|
571
|
-
constructor() {
|
|
572
|
-
super("Cannot initialize a PresentOptional with a null or undefined value.");
|
|
573
|
-
}
|
|
574
|
-
};
|
|
575
|
-
|
|
576
|
-
// src/utils/arrays/groupBy.ts
|
|
577
|
-
function groupByString(arr, field) {
|
|
578
|
-
return groupByStringWith(arr, (t) => t[field]);
|
|
579
|
-
}
|
|
580
|
-
function groupByNumber(arr, field) {
|
|
581
|
-
return groupByNumberWith(arr, (t) => t[field]);
|
|
582
|
-
}
|
|
583
|
-
function groupByBoolean(arr, field) {
|
|
584
|
-
return groupByBooleanWith(arr, (t) => t[field]);
|
|
585
|
-
}
|
|
586
|
-
function groupBySymbol(arr, field) {
|
|
587
|
-
return groupBySymbolWith(arr, (t) => t[field]);
|
|
588
|
-
}
|
|
589
|
-
function groupByStringWith(arr, getter) {
|
|
590
|
-
return doGroupByWith(arr, getter);
|
|
591
|
-
}
|
|
592
|
-
function groupByNumberWith(arr, getter) {
|
|
593
|
-
return doGroupByWith(arr, getter);
|
|
594
|
-
}
|
|
595
|
-
function groupByBooleanWith(arr, getter) {
|
|
596
|
-
return doGroupByWith(arr, (item) => getter(item) ? "true" : "false");
|
|
597
|
-
}
|
|
598
|
-
function groupBySymbolWith(arr, getter) {
|
|
599
|
-
return doGroupByWith(arr, getter);
|
|
600
|
-
}
|
|
601
|
-
function doGroupByWith(arr, getter) {
|
|
602
|
-
return [...arr].reduce((dict, cur) => {
|
|
603
|
-
const key = getter(cur);
|
|
604
|
-
if (key !== null && key !== void 0) {
|
|
605
|
-
const arr2 = dict[key] ?? [];
|
|
606
|
-
arr2.push(cur);
|
|
607
|
-
dict[key] = arr2;
|
|
608
|
-
}
|
|
609
|
-
return dict;
|
|
610
|
-
}, {});
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
// src/utils/arrays/indexBy.ts
|
|
614
|
-
function indexByString(arr, field) {
|
|
615
|
-
return indexByStringWith(arr, (t) => t[field]);
|
|
616
|
-
}
|
|
617
|
-
function indexByNumber(arr, field) {
|
|
618
|
-
return indexByNumberWith(arr, (t) => t[field]);
|
|
619
|
-
}
|
|
620
|
-
function indexBySymbol(arr, field) {
|
|
621
|
-
return indexBySymbolWith(arr, (t) => t[field]);
|
|
622
|
-
}
|
|
623
|
-
function indexByStringWith(arr, getter) {
|
|
624
|
-
return doIndexByWith(arr, getter);
|
|
625
|
-
}
|
|
626
|
-
function indexByNumberWith(arr, getter) {
|
|
627
|
-
return doIndexByWith(arr, getter);
|
|
628
|
-
}
|
|
629
|
-
function indexBySymbolWith(arr, getter) {
|
|
630
|
-
return doIndexByWith(arr, getter);
|
|
631
|
-
}
|
|
632
|
-
function doIndexByWith(arr, getter) {
|
|
633
|
-
return arr.reduce((dict, cur) => {
|
|
634
|
-
const key = getter(cur);
|
|
635
|
-
if (key !== null && key !== void 0) {
|
|
636
|
-
if (dict[key]) {
|
|
637
|
-
throw new Error(`Multiple values indexed by same key "${String(key)}".`);
|
|
638
|
-
}
|
|
639
|
-
dict[key] = cur;
|
|
640
|
-
}
|
|
641
|
-
return dict;
|
|
642
|
-
}, {});
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
// src/utils/arrays/statistics.ts
|
|
646
|
-
function average(arr) {
|
|
647
|
-
const f = 1 / arr.length;
|
|
648
|
-
return arr.reduce((tot, cur) => tot + cur * f, 0);
|
|
649
|
-
}
|
|
650
|
-
function sum(arr) {
|
|
651
|
-
return arr.reduce((tot, cur) => tot + cur, 0);
|
|
652
|
-
}
|
|
653
|
-
function sumBy(arr, getter) {
|
|
654
|
-
return sum(arr.map(getter));
|
|
655
|
-
}
|
|
656
|
-
function min(arr) {
|
|
657
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
658
|
-
return arr.reduce((min2, cur) => cur < min2 ? cur : min2);
|
|
659
|
-
}
|
|
660
|
-
function minBy(arr, getter) {
|
|
661
|
-
return min(arr.map(getter));
|
|
662
|
-
}
|
|
663
|
-
function max(arr) {
|
|
664
|
-
if (arr.length === 0) throw new Error("Cannot calculate value on empty array");
|
|
665
|
-
return arr.reduce((max2, cur) => cur > max2 ? cur : max2);
|
|
666
|
-
}
|
|
667
|
-
function maxBy(arr, getter) {
|
|
668
|
-
return max(arr.map(getter));
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
// src/utils/functions/constant.ts
|
|
672
|
-
function constant(v) {
|
|
673
|
-
return () => v;
|
|
674
|
-
}
|
|
675
|
-
function identity(t) {
|
|
676
|
-
return t;
|
|
677
|
-
}
|
|
678
|
-
var constantNull = constant(null);
|
|
679
|
-
var constantTrue = constant(true);
|
|
680
|
-
var constantFalse = constant(false);
|
|
681
|
-
var constantUndefined = constant(void 0);
|
|
682
|
-
var alwaysTrue = constantTrue;
|
|
683
|
-
var alwaysFalse = constantFalse;
|
|
684
|
-
var constantZero = constant(0);
|
|
685
|
-
var constantOne = constant(1);
|
|
686
|
-
|
|
687
|
-
// src/utils/functions/iff.ts
|
|
688
|
-
function iff(firstPredicate, valueIfTrue) {
|
|
689
|
-
if (firstPredicate === true || firstPredicate instanceof Function && firstPredicate() === true) {
|
|
690
|
-
const ret = {
|
|
691
|
-
elseIf: () => ret,
|
|
692
|
-
otherwise: () => valueIfTrue
|
|
693
|
-
};
|
|
694
|
-
return ret;
|
|
695
|
-
} else {
|
|
696
|
-
const ret = {
|
|
697
|
-
elseIf: (elseIf, valueIfElseIfTrue) => iff(elseIf, valueIfElseIfTrue),
|
|
698
|
-
otherwise: (valueIfElse) => valueIfElse
|
|
699
|
-
};
|
|
700
|
-
return ret;
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
// src/utils/functions/predicateBuilder.ts
|
|
705
|
-
var PredicateBuilder = class {
|
|
706
|
-
_currentPredicate = constantTrue;
|
|
707
|
-
and(predicate) {
|
|
708
|
-
const curPredicate = this._currentPredicate.bind(void 0);
|
|
709
|
-
const newPredicate = (t) => curPredicate(t) && predicate(t);
|
|
710
|
-
this._currentPredicate = newPredicate;
|
|
711
|
-
return this;
|
|
712
|
-
}
|
|
713
|
-
or(predicate) {
|
|
714
|
-
const newPredicate = (t) => this._currentPredicate(t) || predicate(t);
|
|
715
|
-
this._currentPredicate = newPredicate;
|
|
716
|
-
return this;
|
|
717
|
-
}
|
|
718
|
-
toPredicate() {
|
|
719
|
-
return this._currentPredicate;
|
|
720
|
-
}
|
|
721
|
-
};
|
|
722
|
-
|
|
723
|
-
// src/utils/functions.ts
|
|
724
|
-
function isFunction(t) {
|
|
725
|
-
return typeof t === "function";
|
|
726
|
-
}
|
|
727
|
-
function noop() {
|
|
728
|
-
}
|
|
729
|
-
function filterWithTypePredicate(filter) {
|
|
730
|
-
return (t) => filter(t);
|
|
731
|
-
}
|
|
732
|
-
function not(predicate) {
|
|
733
|
-
return (t) => !predicate(t);
|
|
734
|
-
}
|
|
735
|
-
function and(...predicates) {
|
|
736
|
-
if (predicates.length === 0) return constantTrue;
|
|
737
|
-
else if (predicates.length === 1) return predicates[0];
|
|
738
|
-
return (t) => predicates.reduce((prev, cur) => prev ? cur(t) : false, true);
|
|
739
|
-
}
|
|
740
|
-
function or(...predicates) {
|
|
741
|
-
if (predicates.length === 0) return constantTrue;
|
|
742
|
-
else if (predicates.length === 1) return predicates[0];
|
|
743
|
-
return (t) => predicates.reduce((prev, cur) => prev ? true : cur(t), false);
|
|
744
|
-
}
|
|
745
|
-
function xor(a, b) {
|
|
746
|
-
return (t) => a(t) !== b(t);
|
|
747
|
-
}
|
|
748
|
-
function pipedInvoke(...fns) {
|
|
749
|
-
return pipedInvokeFromArray(fns);
|
|
750
|
-
}
|
|
751
|
-
function pipedInvokeFromArray(fns) {
|
|
752
|
-
if (fns.length === 0) {
|
|
753
|
-
return identity;
|
|
754
|
-
}
|
|
755
|
-
if (fns.length === 1) {
|
|
756
|
-
return fns[0];
|
|
757
|
-
}
|
|
758
|
-
return function piped(input) {
|
|
759
|
-
return fns.reduce((prev, fn) => fn(prev), input);
|
|
760
|
-
};
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
// src/utils/arrays/uniqBy.ts
|
|
764
|
-
function uniq(arr) {
|
|
765
|
-
return uniqBy(arr, identity);
|
|
766
|
-
}
|
|
767
|
-
function uniqBy(arr, getter) {
|
|
768
|
-
return arr.reduce((dict, cur) => {
|
|
769
|
-
const key = getter(cur);
|
|
770
|
-
if (dict.keys.includes(key)) {
|
|
771
|
-
return dict;
|
|
772
|
-
} else {
|
|
773
|
-
return {
|
|
774
|
-
keys: [...dict.keys, key],
|
|
775
|
-
values: [...dict.values, cur]
|
|
776
|
-
};
|
|
777
|
-
}
|
|
778
|
-
}, { keys: [], values: [] }).values;
|
|
779
|
-
}
|
|
780
|
-
function uniqByKey(arr, key) {
|
|
781
|
-
return uniqBy(arr, (item) => item[key]);
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
// src/utils/arrays.ts
|
|
785
|
-
function ensureArray(t) {
|
|
786
|
-
if (isNullOrUndefined(t)) return [];
|
|
787
|
-
return t instanceof Array ? t : [t];
|
|
788
|
-
}
|
|
789
|
-
function ensureReadableArray(t) {
|
|
790
|
-
if (isNullOrUndefined(t)) return [];
|
|
791
|
-
return t instanceof Array ? t : [t];
|
|
792
|
-
}
|
|
793
|
-
function isArray(t) {
|
|
794
|
-
return t instanceof Array;
|
|
795
|
-
}
|
|
796
|
-
function upsert(arr, item, isEqual) {
|
|
797
|
-
const index = arr.findIndex((a) => isEqual(a, item));
|
|
798
|
-
if (index === -1) {
|
|
799
|
-
return [...arr, item];
|
|
800
|
-
} else {
|
|
801
|
-
return [
|
|
802
|
-
...arr.slice(0, index),
|
|
803
|
-
item,
|
|
804
|
-
...arr.slice(index + 1)
|
|
805
|
-
];
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
function range(start, end) {
|
|
809
|
-
if (end < start) throw new Error();
|
|
810
|
-
let length = end - start + 1;
|
|
811
|
-
return new Array(length).fill(1).map((_, i) => start + i);
|
|
812
|
-
}
|
|
813
|
-
function fill(length, value) {
|
|
814
|
-
return new Array(length).fill(value);
|
|
815
|
-
}
|
|
816
|
-
function extendArray(arr, props) {
|
|
817
|
-
return arr.map((t) => ({
|
|
818
|
-
...t,
|
|
819
|
-
...props
|
|
820
|
-
}));
|
|
821
|
-
}
|
|
822
|
-
function extendArrayWith(arr, propsFn) {
|
|
823
|
-
return arr.map((t) => ({
|
|
824
|
-
...t,
|
|
825
|
-
...propsFn(t)
|
|
826
|
-
}));
|
|
827
|
-
}
|
|
828
|
-
function reverse(arr) {
|
|
829
|
-
return [...arr].reverse();
|
|
830
|
-
}
|
|
831
|
-
function first(arr, defaultValue = null) {
|
|
832
|
-
return arr.length ? arr[0] : defaultValue;
|
|
833
|
-
}
|
|
834
|
-
function last(arr, defaultValue = null) {
|
|
835
|
-
return arr.length ? arr[arr.length - 1] : defaultValue;
|
|
836
|
-
}
|
|
837
|
-
var head = first;
|
|
838
|
-
function tail(arr) {
|
|
839
|
-
return arr.length ? arr.slice(1) : [];
|
|
840
|
-
}
|
|
841
|
-
function sortedArray(arr, sortFn) {
|
|
842
|
-
return [...arr].sort(sortFn);
|
|
843
|
-
}
|
|
844
|
-
function includes(arr, item, fromIndex) {
|
|
845
|
-
return arr.includes(item, fromIndex);
|
|
846
|
-
}
|
|
847
|
-
function mapTruthys(arr, mapper) {
|
|
848
|
-
return arr.map(mapper).filter((value) => value !== void 0);
|
|
849
|
-
}
|
|
850
|
-
function flatMapTruthys(arr, mapper) {
|
|
851
|
-
return arr.flatMap(mapper).filter((value) => value !== void 0);
|
|
852
|
-
}
|
|
853
|
-
function filterMap(array, filterFn, mapFn) {
|
|
854
|
-
return array.filter(filterFn).map(mapFn);
|
|
855
|
-
}
|
|
856
|
-
function filterMapReduce(array, filterFn, mapFn, reduceFn, initialValue) {
|
|
857
|
-
return array.filter(filterFn).map(mapFn).reduce(reduceFn, initialValue);
|
|
858
|
-
}
|
|
859
|
-
function partition(arr, predicate) {
|
|
860
|
-
return arr.reduce((partition2, item) => {
|
|
861
|
-
partition2[predicate(item) ? 0 : 1].push(item);
|
|
862
|
-
return partition2;
|
|
863
|
-
}, [[], []]);
|
|
864
|
-
}
|
|
865
|
-
function mapFirstTruthy(arr, mapFn) {
|
|
866
|
-
for (let i = 0; i < arr.length; i++) {
|
|
867
|
-
const result = mapFn(arr[i]);
|
|
868
|
-
if (result) return result;
|
|
869
|
-
}
|
|
870
|
-
return null;
|
|
871
|
-
}
|
|
872
|
-
function listToDict(arr, mapFn) {
|
|
873
|
-
return arr.reduce((dict, cur) => {
|
|
874
|
-
const [key, value] = mapFn(cur);
|
|
875
|
-
return { ...dict, [key]: value };
|
|
876
|
-
}, {});
|
|
877
|
-
}
|
|
878
|
-
function shallowArrayEquals(a, b) {
|
|
879
|
-
if (a === b) return true;
|
|
880
|
-
if (a.length !== b.length) return false;
|
|
881
|
-
for (let i = 0; i < a.length; i++) {
|
|
882
|
-
if (a[i] !== b[i]) return false;
|
|
883
|
-
}
|
|
884
|
-
return true;
|
|
885
|
-
}
|
|
886
|
-
function findInArray(arr, predicate) {
|
|
887
|
-
return Optional_default.ofNullable(arr.find(predicate));
|
|
888
|
-
}
|
|
889
|
-
function findIndexInArray(arr, predicate) {
|
|
890
|
-
const idx = arr.findIndex(predicate);
|
|
891
|
-
return idx === -1 ? Optional_default.empty() : Optional_default.of(idx);
|
|
892
|
-
}
|
|
893
|
-
function zip(ts, rs) {
|
|
894
|
-
if (ts.length !== rs.length)
|
|
895
|
-
throw new Error(`Arrays must have the same length. Got ${ts.length} and ${rs.length}`);
|
|
896
|
-
return ts.map((t, i) => [t, rs[i]]);
|
|
897
|
-
}
|
|
898
|
-
function unzip(arr) {
|
|
899
|
-
return arr.reduce(([ts, rs], [t, r]) => {
|
|
900
|
-
return [[...ts, t], [...rs, r]];
|
|
901
|
-
}, [[], []]);
|
|
902
|
-
}
|
|
903
|
-
function arrayGet(arr, index) {
|
|
904
|
-
if (index < 0 || index >= arr.length)
|
|
905
|
-
return Optional_default.empty();
|
|
906
|
-
return Optional_default.of(arr[index]);
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
// src/utils/booleans.ts
|
|
910
|
-
function isTrue(x) {
|
|
911
|
-
return x === true;
|
|
912
|
-
}
|
|
913
|
-
function isFalse(x) {
|
|
914
|
-
return x === false;
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
// src/utils/css.ts
|
|
918
|
-
var newLine = "\n";
|
|
919
|
-
var tabulation = " ";
|
|
920
|
-
var colon = ":";
|
|
921
|
-
var semiColon = ";";
|
|
922
|
-
var space = " ";
|
|
923
|
-
var openBracket = "{";
|
|
924
|
-
var closeBracket = "}";
|
|
925
|
-
function cssDeclarationRulesDictionaryToCss(syleDeclarationRulesForSelectorsProduceable, indent = 0) {
|
|
926
|
-
const syleDeclarationRulesForSelectors = produceableToValue(syleDeclarationRulesForSelectorsProduceable);
|
|
927
|
-
return Object.entries(syleDeclarationRulesForSelectors).map(([selector, styleDeclarationRules]) => {
|
|
928
|
-
const cssRules = cssSelectorDeclarationRulesDictionaryToCss(styleDeclarationRules, indent + 1);
|
|
929
|
-
if (!cssRules.length) return null;
|
|
930
|
-
return repeat(tabulation, indent) + selector + space + openBracket + newLine + cssRules.join(newLine) + newLine + closeBracket;
|
|
931
|
-
}).filter(Boolean).join(newLine + newLine);
|
|
932
|
-
}
|
|
933
|
-
function cssSelectorDeclarationRulesDictionaryToCss(styleDeclarationRules, indent = 0) {
|
|
934
|
-
return Object.entries(styleDeclarationRules).map(([key, value]) => {
|
|
935
|
-
if (typeof value === "string") {
|
|
936
|
-
if (key.startsWith("--")) {
|
|
937
|
-
return repeat(tabulation, indent) + key + colon + space + value + semiColon;
|
|
938
|
-
} else {
|
|
939
|
-
return repeat(tabulation, indent) + pascalCaseToKebabCase(key) + colon + space + value + semiColon;
|
|
940
|
-
}
|
|
941
|
-
} else {
|
|
942
|
-
return repeat(tabulation, indent) + key + space + openBracket + newLine + cssSelectorDeclarationRulesDictionaryToCss(value, indent + 1).join(newLine) + newLine + repeat(tabulation, indent) + closeBracket;
|
|
943
|
-
}
|
|
944
|
-
});
|
|
945
|
-
}
|
|
946
|
-
function transformCssDictionary(dict, transformer) {
|
|
947
|
-
const recurse = (dict2) => {
|
|
948
|
-
const newDict = {};
|
|
949
|
-
for (const [key, value] of Object.entries(dict2)) {
|
|
950
|
-
if (typeof value === "string") {
|
|
951
|
-
const newValue = transformer(key, value);
|
|
952
|
-
if (isDefined(newValue)) {
|
|
953
|
-
newDict[key] = newValue;
|
|
954
|
-
}
|
|
955
|
-
} else {
|
|
956
|
-
newDict[key] = transformCssDictionary(value, transformer);
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
return newDict;
|
|
960
|
-
};
|
|
961
|
-
return recurse(dict);
|
|
962
|
-
}
|
|
963
|
-
function pascalCaseToKebabCase(s) {
|
|
964
|
-
return s.split(/([A-Z][a-z]*)/).filter(Boolean).map((n) => n.toLowerCase()).join("-");
|
|
965
|
-
}
|
|
966
|
-
function produceableToValue(t) {
|
|
967
|
-
return isFunction(t) ? t() : t;
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
// src/utils/errors/withTryCatch.ts
|
|
971
|
-
function withTryCatch(fn, errMapFn = identity) {
|
|
972
|
-
try {
|
|
973
|
-
return [fn(), void 0];
|
|
974
|
-
} catch (e) {
|
|
975
|
-
return [void 0, errMapFn(asError(e))];
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
async function withTryCatchAsync(fn, errMapFn = identity) {
|
|
979
|
-
try {
|
|
980
|
-
return [await fn(), void 0];
|
|
981
|
-
} catch (e) {
|
|
982
|
-
return [void 0, errMapFn(asError(e))];
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
// src/utils/errors.ts
|
|
987
|
-
function asError(e) {
|
|
988
|
-
if (e === void 0 || e === null)
|
|
989
|
-
return new Error("Void message");
|
|
990
|
-
if (isError(e))
|
|
991
|
-
return e;
|
|
992
|
-
if (typeof e === "string")
|
|
993
|
-
return new Error(e);
|
|
994
|
-
if (e instanceof String)
|
|
995
|
-
return new Error(e.toString());
|
|
996
|
-
return new Error(JSON.stringify(e));
|
|
997
|
-
}
|
|
998
|
-
function isError(e) {
|
|
999
|
-
return e instanceof Error;
|
|
1000
|
-
}
|
|
1001
|
-
function getMessageFromError(error) {
|
|
1002
|
-
return `${error.name}: ${error.message}${getCauseMessageFromError(error)}`;
|
|
1003
|
-
}
|
|
1004
|
-
function getStackFromError(error) {
|
|
1005
|
-
const stack = error.stack && error.stack.includes(error.message) ? error.stack : error.message + " " + (error.stack ?? "");
|
|
1006
|
-
return `${error.name}: ${stack}${getCauseStackFromError(error)}`;
|
|
1007
|
-
}
|
|
1008
|
-
function getCauseMessageFromError(error) {
|
|
1009
|
-
if (isNullOrUndefined(error.cause))
|
|
1010
|
-
return "";
|
|
1011
|
-
return `
|
|
1012
|
-
caused by: ${getMessageFromError(asError(error.cause))}`;
|
|
1013
|
-
}
|
|
1014
|
-
function getCauseStackFromError(error) {
|
|
1015
|
-
if (isNullOrUndefined(error.cause))
|
|
1016
|
-
return "";
|
|
1017
|
-
return `
|
|
1018
|
-
caused by: ${getStackFromError(asError(error.cause))}`;
|
|
1019
|
-
}
|
|
1020
|
-
var NonExhaustiveSwitchError = class extends Error {
|
|
1021
|
-
constructor(value) {
|
|
1022
|
-
super("Expected switch to be exhaustive, got: " + value);
|
|
1023
|
-
}
|
|
1024
|
-
};
|
|
1025
|
-
|
|
1026
|
-
// src/utils/json.ts
|
|
1027
|
-
function tryToParseJson(jsonContent) {
|
|
1028
|
-
return withTryCatch(() => parseJson(jsonContent));
|
|
1029
|
-
}
|
|
1030
|
-
function parseJson(jsonContent) {
|
|
1031
|
-
return JSON.parse(jsonContent);
|
|
1032
|
-
}
|
|
1033
|
-
function stringifyJson(jsonSerializable, minify = false) {
|
|
1034
|
-
return JSON.stringify(jsonSerializable, void 0, minify ? void 0 : 2);
|
|
1035
|
-
}
|
|
1036
|
-
function jsonCloneDeep(a) {
|
|
1037
|
-
if (null === a || "object" !== typeof a) return a;
|
|
1038
|
-
if (a instanceof Date) {
|
|
1039
|
-
return new Date(a.getTime());
|
|
1040
|
-
} else if (a instanceof Array) {
|
|
1041
|
-
const copy = [];
|
|
1042
|
-
for (let i = 0, len = a.length; i < len; i++) {
|
|
1043
|
-
copy[i] = jsonCloneDeep(a[i]);
|
|
1044
|
-
}
|
|
1045
|
-
return copy;
|
|
1046
|
-
} else if (a instanceof Object) {
|
|
1047
|
-
const copy = {};
|
|
1048
|
-
for (let attr in a) {
|
|
1049
|
-
if (a.hasOwnProperty(attr))
|
|
1050
|
-
copy[attr] = jsonCloneDeep(a[attr]);
|
|
1051
|
-
}
|
|
1052
|
-
return copy;
|
|
1053
|
-
}
|
|
1054
|
-
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
1055
|
-
}
|
|
1056
|
-
function omitFromJsonObject(o, ...keys) {
|
|
1057
|
-
return keys.reduce((obj, key) => {
|
|
1058
|
-
delete obj[key];
|
|
1059
|
-
return obj;
|
|
1060
|
-
}, { ...o });
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
// src/utils/numbers/round.ts
|
|
1064
|
-
var roundModes = {
|
|
1065
|
-
"toNearest": Math.round,
|
|
1066
|
-
"toLower": Math.floor,
|
|
1067
|
-
"toUpper": Math.ceil,
|
|
1068
|
-
"towardsZero": (n) => n > 0 ? Math.floor(n) : Math.ceil(n),
|
|
1069
|
-
"awayFromZero": (n) => n > 0 ? Math.ceil(n) : Math.floor(n)
|
|
1070
|
-
};
|
|
1071
|
-
function round(n, precision = 0, mode = "toNearest") {
|
|
1072
|
-
const base = 10;
|
|
1073
|
-
const power = Math.pow(base, precision);
|
|
1074
|
-
return roundModes[mode](n * power) / power;
|
|
1075
|
-
}
|
|
1076
|
-
var roundToNearest = (n, precision = 0) => round(n, precision, "toNearest");
|
|
1077
|
-
var roundToLower = (n, precision = 0) => round(n, precision, "toLower");
|
|
1078
|
-
var roundToUpper = (n, precision = 0) => round(n, precision, "toUpper");
|
|
1079
|
-
var roundTowardsZero = (n, precision = 0) => round(n, precision, "towardsZero");
|
|
1080
|
-
var roundAwayFromZero = (n, precision = 0) => round(n, precision, "awayFromZero");
|
|
1081
|
-
|
|
1082
|
-
// src/utils/numbers.ts
|
|
1083
|
-
function ensurePositiveNumber(v, name = "value") {
|
|
1084
|
-
if (v !== void 0 && v !== null && v > 0)
|
|
1085
|
-
return v;
|
|
1086
|
-
throw new Error(`Expected ${name} to be positive, got: ${v}`);
|
|
1087
|
-
}
|
|
1088
|
-
function ensureNonNegativeNumber(v, name = "value") {
|
|
1089
|
-
if (v !== void 0 && v !== null && v >= 0)
|
|
1090
|
-
return v;
|
|
1091
|
-
throw new Error(`Expected ${name} to be non-negative, got: ${v}`);
|
|
1092
|
-
}
|
|
1093
|
-
function ensureNonPositiveNumber(v, name = "value") {
|
|
1094
|
-
if (v !== void 0 && v !== null && v <= 0)
|
|
1095
|
-
return v;
|
|
1096
|
-
throw new Error(`Expected ${name} to be non-positive, got: ${v}`);
|
|
1097
|
-
}
|
|
1098
|
-
function ensureNegativeNumber(v, name = "value") {
|
|
1099
|
-
if (v !== void 0 && v !== null && v < 0)
|
|
1100
|
-
return v;
|
|
1101
|
-
throw new Error(`Expected ${name} to be negative, got: ${v}`);
|
|
1102
|
-
}
|
|
1103
|
-
function incrementBy(n) {
|
|
1104
|
-
return (v) => v + n;
|
|
1105
|
-
}
|
|
1106
|
-
function multiplyBy(n) {
|
|
1107
|
-
return (v) => v * n;
|
|
1108
|
-
}
|
|
1109
|
-
function divideBy(n) {
|
|
1110
|
-
return (v) => v / n;
|
|
1111
|
-
}
|
|
1112
|
-
var increment = incrementBy(1);
|
|
1113
|
-
var decrement = incrementBy(-1);
|
|
1114
|
-
var decrementBy = (n) => incrementBy(-n);
|
|
1115
|
-
function isNumber(v) {
|
|
1116
|
-
return typeof v === "number";
|
|
1117
|
-
}
|
|
1118
|
-
function isPositiveNumber(v) {
|
|
1119
|
-
return v > 0;
|
|
1120
|
-
}
|
|
1121
|
-
function isNegativeNumber(v) {
|
|
1122
|
-
return v < 0;
|
|
1123
|
-
}
|
|
1124
|
-
function isZero(v) {
|
|
1125
|
-
return v === 0;
|
|
1126
|
-
}
|
|
1127
|
-
function clamp(n, min2, max2) {
|
|
1128
|
-
if (min2 === null || max2 === null || n === null || isNaN(min2) || isNaN(max2) || isNaN(n) || min2 > max2)
|
|
1129
|
-
throw new Error();
|
|
1130
|
-
if (n > max2) {
|
|
1131
|
-
return max2;
|
|
1132
|
-
} else if (n < min2) {
|
|
1133
|
-
return min2;
|
|
1134
|
-
} else {
|
|
1135
|
-
return n;
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
function clampInt0_100(n) {
|
|
1139
|
-
return clamp(Math.round(n), 0, 100);
|
|
1140
|
-
}
|
|
1141
|
-
function tryToParseNumber(numberStr) {
|
|
1142
|
-
return withTryCatch(() => {
|
|
1143
|
-
const type = typeof ensureDefined(numberStr);
|
|
1144
|
-
if (type !== "string") throw new Error("Invalid number given: " + numberStr);
|
|
1145
|
-
if (numberStr.trim().length === 0) throw new Error("Invalid number given: " + numberStr);
|
|
1146
|
-
const num = Number(numberStr);
|
|
1147
|
-
if (isNaN(num)) throw new Error("Invalid number given: " + numberStr);
|
|
1148
|
-
return num;
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
|
-
// src/utils/operations.ts
|
|
1153
|
-
var Operation = {
|
|
1154
|
-
ok: (data) => {
|
|
1155
|
-
return { success: true, data };
|
|
1156
|
-
},
|
|
1157
|
-
ko: (error) => {
|
|
1158
|
-
return { success: false, error };
|
|
1159
|
-
}
|
|
1160
|
-
};
|
|
1161
|
-
|
|
1162
|
-
// src/utils/promises.ts
|
|
1163
|
-
function asPromise(promisable) {
|
|
1164
|
-
return Promise.resolve(promisable);
|
|
1165
|
-
}
|
|
1166
|
-
function promiseSequence(...fns) {
|
|
1167
|
-
const fn = async function() {
|
|
1168
|
-
const results = [];
|
|
1169
|
-
for (let i = 0; i < fns.length; i++) {
|
|
1170
|
-
results[i] = await fns[i]();
|
|
1171
|
-
}
|
|
1172
|
-
return results;
|
|
1173
|
-
};
|
|
1174
|
-
return fn;
|
|
1175
|
-
}
|
|
1176
|
-
function delayPromise(duration) {
|
|
1177
|
-
return (result) => duration.promise().then(() => result);
|
|
1178
|
-
}
|
|
1179
|
-
var TimeoutError = class extends Error {
|
|
1180
|
-
};
|
|
1181
|
-
async function awaitAtMost(promise, duration) {
|
|
1182
|
-
return Promise.race([
|
|
1183
|
-
promise.then((value) => ({ status: "ok", value })),
|
|
1184
|
-
duration.promise().then(() => ({ status: "timeout" }))
|
|
1185
|
-
]).then(({ status, value }) => {
|
|
1186
|
-
if (status === "ok")
|
|
1187
|
-
return value;
|
|
1188
|
-
throw new TimeoutError("Time out reached while waiting for a promise to resolve.");
|
|
1189
|
-
});
|
|
1190
|
-
}
|
|
1191
|
-
var NEVER = new Promise((_resolve) => {
|
|
1192
|
-
});
|
|
1193
|
-
|
|
1194
|
-
// src/utils/random.ts
|
|
1195
|
-
function randomNumberInInterval(min2, max2) {
|
|
1196
|
-
return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
|
|
1197
|
-
}
|
|
1198
|
-
var randomId = (length) => {
|
|
1199
|
-
return Math.random().toString(36).substring(2, length + 2);
|
|
1200
|
-
};
|
|
1201
|
-
|
|
1202
|
-
// src/utils/records/entries.ts
|
|
1203
|
-
function dictToEntries(obj) {
|
|
1204
|
-
return Object.entries(obj);
|
|
1205
|
-
}
|
|
1206
|
-
function entriesToDict(entries) {
|
|
1207
|
-
return Object.fromEntries(entries);
|
|
1208
|
-
}
|
|
1209
|
-
function entriesToEntries(dict, mapper) {
|
|
1210
|
-
return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
|
|
1211
|
-
}
|
|
1212
|
-
var mapEntries = entriesToEntries;
|
|
1213
|
-
function entriesToList(dict, mapper) {
|
|
1214
|
-
return dictToEntries(dict).map((entry) => mapper(entry));
|
|
1215
|
-
}
|
|
1216
|
-
|
|
1217
|
-
// src/utils/records.ts
|
|
1218
|
-
function dictToList(obj) {
|
|
1219
|
-
return Object.keys(obj).map((key) => obj[key]);
|
|
1220
|
-
}
|
|
1221
|
-
function pick(o, keys) {
|
|
1222
|
-
return keys.reduce((obj, key) => {
|
|
1223
|
-
obj[key] = o[key];
|
|
1224
|
-
return obj;
|
|
1225
|
-
}, {});
|
|
1226
|
-
}
|
|
1227
|
-
function shallowRecordEquals(a, b) {
|
|
1228
|
-
if (a === b) return true;
|
|
1229
|
-
const aKeys = Object.keys(a), bKeys = Object.keys(b);
|
|
1230
|
-
if (aKeys.length !== bKeys.length) return false;
|
|
1231
|
-
for (const key of aKeys) {
|
|
1232
|
-
if (a[key] !== b[key]) return false;
|
|
1233
|
-
}
|
|
1234
|
-
return true;
|
|
1235
|
-
}
|
|
1236
|
-
|
|
1237
|
-
// src/utils/strings/StringParts.ts
|
|
1238
|
-
var StringParts = class _StringParts {
|
|
1239
|
-
_parts;
|
|
1240
|
-
constructor(...parts) {
|
|
1241
|
-
this._parts = parts;
|
|
1242
|
-
}
|
|
1243
|
-
toUpperCase() {
|
|
1244
|
-
return new _StringParts(...this.parts.map((part) => part.toUpperCase()));
|
|
1245
|
-
}
|
|
1246
|
-
toLowerCase() {
|
|
1247
|
-
return new _StringParts(...this.parts.map((part) => part.toLowerCase()));
|
|
1248
|
-
}
|
|
1249
|
-
capitalizeFirst() {
|
|
1250
|
-
if (!this.length) return this;
|
|
1251
|
-
return new _StringParts(capitalizeWord(this.first), ...this.tail.map((part) => part.toLowerCase()));
|
|
1252
|
-
}
|
|
1253
|
-
capitalizeEach() {
|
|
1254
|
-
return new _StringParts(...this.parts.map((part) => capitalizeWord(part)));
|
|
1255
|
-
}
|
|
1256
|
-
get parts() {
|
|
1257
|
-
return this._parts;
|
|
1258
|
-
}
|
|
1259
|
-
get tail() {
|
|
1260
|
-
const [_first, ...tail2] = this.parts;
|
|
1261
|
-
return tail2;
|
|
1262
|
-
}
|
|
1263
|
-
get first() {
|
|
1264
|
-
return this._parts[0] ?? null;
|
|
1265
|
-
}
|
|
1266
|
-
get last() {
|
|
1267
|
-
return this._parts[this.length - 1] ?? null;
|
|
1268
|
-
}
|
|
1269
|
-
get length() {
|
|
1270
|
-
return this._parts.length;
|
|
1271
|
-
}
|
|
1272
|
-
trim() {
|
|
1273
|
-
return new _StringParts(...this.parts.map((part) => part.trim()));
|
|
1274
|
-
}
|
|
1275
|
-
toSnakeCase() {
|
|
1276
|
-
return this.toLowerCase().join("_");
|
|
1277
|
-
}
|
|
1278
|
-
toCamelCase() {
|
|
1279
|
-
if (!this.length) return "";
|
|
1280
|
-
return [this.first.toLowerCase(), ...this.tail.map(capitalizeWord)].join("");
|
|
1281
|
-
}
|
|
1282
|
-
toKebabCase() {
|
|
1283
|
-
return this.toLowerCase().join("-");
|
|
1284
|
-
}
|
|
1285
|
-
toPascalCase() {
|
|
1286
|
-
return this.capitalizeEach().join("");
|
|
1287
|
-
}
|
|
1288
|
-
toHumanCase() {
|
|
1289
|
-
return this.capitalizeFirst().join(" ");
|
|
1290
|
-
}
|
|
1291
|
-
join(separator) {
|
|
1292
|
-
return this.parts.join(separator);
|
|
1293
|
-
}
|
|
1294
|
-
mergeWith({ parts: otherParts }) {
|
|
1295
|
-
return new _StringParts(...this.parts, ...otherParts);
|
|
1296
|
-
}
|
|
1297
|
-
slice(start, end) {
|
|
1298
|
-
return new _StringParts(...this.parts.slice(start, end));
|
|
1299
|
-
}
|
|
1300
|
-
splice(start, deleteCount, ...items) {
|
|
1301
|
-
return new _StringParts(...this.parts.splice(start, deleteCount, ...items));
|
|
1302
|
-
}
|
|
1303
|
-
push(part) {
|
|
1304
|
-
return new _StringParts(...this.parts, part);
|
|
1305
|
-
}
|
|
1306
|
-
shift(part) {
|
|
1307
|
-
return new _StringParts(part, ...this.parts);
|
|
1308
|
-
}
|
|
1309
|
-
reverse() {
|
|
1310
|
-
return new _StringParts(...this.parts.reverse());
|
|
1311
|
-
}
|
|
1312
|
-
static fromString = (s, separator = /\s+/g) => {
|
|
1313
|
-
if (s === null || separator === null)
|
|
1314
|
-
throw new Error("Invalid arguments");
|
|
1315
|
-
return new _StringParts(...s.split(separator));
|
|
1316
|
-
};
|
|
1317
|
-
static fromParts = (...parts) => {
|
|
1318
|
-
return new _StringParts(...parts);
|
|
1319
|
-
};
|
|
1320
|
-
/**
|
|
1321
|
-
* Tries to convert a word in PascalCase to an array of words. Will not work if there are spaces or special characters. Does not cope well on uppercase abbreviations (eg, "CSS").
|
|
1322
|
-
* @param s a string in PascalCase.
|
|
1323
|
-
* @returns a StringParts where each sub-word in PascalCase is now a part.
|
|
1324
|
-
*/
|
|
1325
|
-
static tryToParsePascalCaseWord(s) {
|
|
1326
|
-
return new _StringParts(...s.split(/([A-Z][a-z]*)/).filter(Boolean).map((n) => n.toLowerCase()));
|
|
1327
|
-
}
|
|
1328
|
-
};
|
|
1329
|
-
|
|
1330
|
-
// src/utils/strings.ts
|
|
1331
|
-
function hashCode(str) {
|
|
1332
|
-
let hash = 0;
|
|
1333
|
-
for (let i = 0; i < str.length; ++i)
|
|
1334
|
-
hash = Math.imul(31, hash) + str.charCodeAt(i);
|
|
1335
|
-
return hash | 0;
|
|
1336
|
-
}
|
|
1337
|
-
function repeat(char, times) {
|
|
1338
|
-
return times > 0 ? range(0, times - 1).map((_) => char).join("") : "";
|
|
1339
|
-
}
|
|
1340
|
-
function capitalizeWord(word) {
|
|
1341
|
-
return word[0].toUpperCase() + word.substring(1).toLowerCase();
|
|
1342
|
-
}
|
|
1343
|
-
function splitWords(text, regEx = /\s+/) {
|
|
1344
|
-
return text.split(regEx).filter((t) => t.length > 0);
|
|
1345
|
-
}
|
|
1346
|
-
function stringToNumber(s) {
|
|
1347
|
-
if (isNullOrUndefined(s))
|
|
1348
|
-
return null;
|
|
1349
|
-
return Number(s);
|
|
1350
|
-
}
|
|
1351
|
-
function pad(str, n, char, where = "left") {
|
|
1352
|
-
const length = ensureDefined(str).length;
|
|
1353
|
-
if (length >= ensureDefined(n)) return str;
|
|
1354
|
-
if (ensureDefined(char).length !== 1)
|
|
1355
|
-
throw new Error("Illegal pad character");
|
|
1356
|
-
const padding = repeat(char, n - length);
|
|
1357
|
-
return (where === "left" ? padding : "") + str + (where === "right" ? padding : "");
|
|
1358
|
-
}
|
|
1359
|
-
function padLeft(str, n, char) {
|
|
1360
|
-
return pad(str, n, char, "left");
|
|
1361
|
-
}
|
|
1362
|
-
function padRight(str, n, char) {
|
|
1363
|
-
return pad(str, n, char, "right");
|
|
1364
|
-
}
|
|
1365
|
-
function ellipsis(str, maxLength) {
|
|
1366
|
-
if (maxLength < 4)
|
|
1367
|
-
throw new Error("Invalid argument maxLength");
|
|
1368
|
-
if (str.length <= maxLength) {
|
|
1369
|
-
return str;
|
|
1370
|
-
} else {
|
|
1371
|
-
return str.substring(0, maxLength - 3) + "...";
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
function pluralize(n, singular, plural) {
|
|
1375
|
-
if (!singular || !singular.length)
|
|
1376
|
-
throw new Error();
|
|
1377
|
-
if (n === 1)
|
|
1378
|
-
return singular;
|
|
1379
|
-
plural = plural ?? singular + "s";
|
|
1380
|
-
const firstUppercase = singular.charAt(0) === singular.charAt(0).toUpperCase();
|
|
1381
|
-
if (firstUppercase) {
|
|
1382
|
-
const PLURAL = plural.toUpperCase();
|
|
1383
|
-
const isAllUppercase = plural === PLURAL;
|
|
1384
|
-
plural = isAllUppercase ? PLURAL : plural.charAt(0).toUpperCase() + plural.slice(1).toLowerCase();
|
|
1385
|
-
}
|
|
1386
|
-
return plural;
|
|
1387
|
-
}
|
|
1388
|
-
function isString(source) {
|
|
1389
|
-
return isDefined(source) && typeof source === "string";
|
|
1390
|
-
}
|
|
1391
|
-
function isEmpty(source) {
|
|
1392
|
-
return source.trim().length === 0;
|
|
1393
|
-
}
|
|
1394
|
-
function isNullOrUndefinedOrEmpty(source) {
|
|
1395
|
-
return isNullOrUndefined(source) || isEmpty(source);
|
|
1396
|
-
}
|
|
1397
|
-
function wrapWithString(str, start, end = start) {
|
|
1398
|
-
if (isNullOrUndefinedOrEmpty(str))
|
|
1399
|
-
throw new Error(`Expected string to be non-empty, got: "${str}"`);
|
|
1400
|
-
if (isNullOrUndefinedOrEmpty(start))
|
|
1401
|
-
throw new Error(`Expected start delimiter to be non-empty, got: "${start}"`);
|
|
1402
|
-
if (isNullOrUndefinedOrEmpty(end))
|
|
1403
|
-
throw new Error(`Expected end delimiter to be non-empty, got: "${end}"`);
|
|
1404
|
-
return start + str + end;
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
// src/lazy/Lazy.ts
|
|
1408
|
-
var Lazy = class _Lazy {
|
|
1409
|
-
_value = void 0;
|
|
1410
|
-
_initialized = false;
|
|
1411
|
-
_generator;
|
|
1412
|
-
constructor(generator) {
|
|
1413
|
-
this._generator = ensureDefined(generator);
|
|
1414
|
-
}
|
|
1415
|
-
get value() {
|
|
1416
|
-
return this._value;
|
|
1417
|
-
}
|
|
1418
|
-
getOrCreate() {
|
|
1419
|
-
if (!this._initialized) {
|
|
1420
|
-
this._value = this._generator();
|
|
1421
|
-
this._initialized = true;
|
|
1422
|
-
}
|
|
1423
|
-
return this._value;
|
|
1424
|
-
}
|
|
1425
|
-
getOrThrow(errorMessage) {
|
|
1426
|
-
if (!this._initialized)
|
|
1427
|
-
throw new Error(errorMessage);
|
|
1428
|
-
return this._value;
|
|
1429
|
-
}
|
|
1430
|
-
or(t) {
|
|
1431
|
-
if (!this._initialized) {
|
|
1432
|
-
return t;
|
|
1433
|
-
} else {
|
|
1434
|
-
return this._value;
|
|
1435
|
-
}
|
|
1436
|
-
}
|
|
1437
|
-
isPresent() {
|
|
1438
|
-
return this._initialized;
|
|
1439
|
-
}
|
|
1440
|
-
isEmpty() {
|
|
1441
|
-
return !this._initialized;
|
|
1442
|
-
}
|
|
1443
|
-
ifPresent(fn) {
|
|
1444
|
-
if (this.isPresent()) {
|
|
1445
|
-
fn(this._value);
|
|
1446
|
-
}
|
|
1447
|
-
}
|
|
1448
|
-
ifEmpty(fn) {
|
|
1449
|
-
if (this.isEmpty()) {
|
|
1450
|
-
fn();
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
|
-
apply(fnPresent, fnEmpty) {
|
|
1454
|
-
if (this.isPresent()) {
|
|
1455
|
-
fnPresent(this._value);
|
|
1456
|
-
} else {
|
|
1457
|
-
fnEmpty();
|
|
1458
|
-
}
|
|
1459
|
-
}
|
|
1460
|
-
static of(generator) {
|
|
1461
|
-
return new _Lazy(generator);
|
|
1462
|
-
}
|
|
1463
|
-
};
|
|
1464
|
-
|
|
1465
|
-
// src/lazy/LazyAsync.ts
|
|
1466
|
-
var LazyAsync = class _LazyAsync {
|
|
1467
|
-
_promise = void 0;
|
|
1468
|
-
_pending = false;
|
|
1469
|
-
_resolvedValue = void 0;
|
|
1470
|
-
_error = void 0;
|
|
1471
|
-
_generator;
|
|
1472
|
-
constructor(generator) {
|
|
1473
|
-
this._generator = ensureDefined(generator);
|
|
1474
|
-
}
|
|
1475
|
-
getOrCreate() {
|
|
1476
|
-
if (this.isEmpty()) {
|
|
1477
|
-
this._pending = true;
|
|
1478
|
-
this._promise = this._generator();
|
|
1479
|
-
this._promise.then((value) => {
|
|
1480
|
-
this._pending = false;
|
|
1481
|
-
this._resolvedValue = value;
|
|
1482
|
-
}, (err) => {
|
|
1483
|
-
this._pending = false;
|
|
1484
|
-
this._error = err;
|
|
1485
|
-
});
|
|
1486
|
-
}
|
|
1487
|
-
return this._promise;
|
|
1488
|
-
}
|
|
1489
|
-
isPresent() {
|
|
1490
|
-
return this._promise !== void 0;
|
|
1491
|
-
}
|
|
1492
|
-
isEmpty() {
|
|
1493
|
-
return this._promise === void 0;
|
|
1494
|
-
}
|
|
1495
|
-
isPending() {
|
|
1496
|
-
return this.isPresent() && this._pending === true;
|
|
1497
|
-
}
|
|
1498
|
-
isReady() {
|
|
1499
|
-
return this.isPresent() && this._pending === false;
|
|
1500
|
-
}
|
|
1501
|
-
isResolved() {
|
|
1502
|
-
return this.isReady() && this._error === void 0;
|
|
1503
|
-
}
|
|
1504
|
-
isError() {
|
|
1505
|
-
return this.isReady() && this._error !== void 0;
|
|
1506
|
-
}
|
|
1507
|
-
ifPresent(fn) {
|
|
1508
|
-
if (this.isPresent()) {
|
|
1509
|
-
fn(this._promise);
|
|
1510
|
-
}
|
|
1511
|
-
}
|
|
1512
|
-
ifEmpty(fn) {
|
|
1513
|
-
if (this.isEmpty()) {
|
|
1514
|
-
fn();
|
|
1515
|
-
}
|
|
1516
|
-
}
|
|
1517
|
-
ifPending(fn) {
|
|
1518
|
-
if (this.isPending()) {
|
|
1519
|
-
fn(this._promise);
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
|
-
ifReady(fn) {
|
|
1523
|
-
if (this.isReady()) {
|
|
1524
|
-
fn(this._promise);
|
|
1525
|
-
}
|
|
1526
|
-
}
|
|
1527
|
-
ifResolved(fn) {
|
|
1528
|
-
if (this.isResolved()) {
|
|
1529
|
-
fn(this._resolvedValue);
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
|
-
ifError(fn) {
|
|
1533
|
-
if (this.isError()) {
|
|
1534
|
-
fn(this._error);
|
|
1535
|
-
}
|
|
1536
|
-
}
|
|
1537
|
-
getOrThrow(errorMessage) {
|
|
1538
|
-
if (!this._promise)
|
|
1539
|
-
throw new Error(errorMessage);
|
|
1540
|
-
return this._promise;
|
|
1541
|
-
}
|
|
1542
|
-
static of(generator) {
|
|
1543
|
-
return new _LazyAsync(generator);
|
|
1544
|
-
}
|
|
1545
|
-
};
|
|
1546
|
-
|
|
1547
|
-
// src/lazy/LazyDictionary.ts
|
|
1548
|
-
var LazyDictionary = class _LazyDictionary {
|
|
1549
|
-
constructor(_generator) {
|
|
1550
|
-
this._generator = _generator;
|
|
1551
|
-
}
|
|
1552
|
-
_dictionary = {};
|
|
1553
|
-
static of(generator) {
|
|
1554
|
-
return new _LazyDictionary(generator);
|
|
1555
|
-
}
|
|
1556
|
-
getOrCreate(key) {
|
|
1557
|
-
if (key in this._dictionary) {
|
|
1558
|
-
return this._dictionary[key];
|
|
1559
|
-
}
|
|
1560
|
-
const value = this._generator(key);
|
|
1561
|
-
this._dictionary[key] = value;
|
|
1562
|
-
return value;
|
|
1563
|
-
}
|
|
1564
|
-
getOrThrow(key, errorMessage) {
|
|
1565
|
-
if (key in this._dictionary) {
|
|
1566
|
-
return this._dictionary[key];
|
|
1567
|
-
}
|
|
1568
|
-
throw new Error(errorMessage);
|
|
1569
|
-
}
|
|
1570
|
-
};
|
|
1571
|
-
|
|
1572
|
-
// src/time/TimeUnit.ts
|
|
1573
|
-
var TimeUnit = class _TimeUnit {
|
|
1574
|
-
constructor(multiplier) {
|
|
1575
|
-
this.multiplier = multiplier;
|
|
1576
|
-
}
|
|
1577
|
-
toUnit(value, unit) {
|
|
1578
|
-
return value * (this.multiplier / unit.multiplier);
|
|
1579
|
-
}
|
|
1580
|
-
toMs(value) {
|
|
1581
|
-
return this.toUnit(value, _TimeUnit.MILLISECONDS);
|
|
1582
|
-
}
|
|
1583
|
-
toSeconds(value) {
|
|
1584
|
-
return this.toUnit(value, _TimeUnit.SECONDS);
|
|
1585
|
-
}
|
|
1586
|
-
toMinutes(value) {
|
|
1587
|
-
return this.toUnit(value, _TimeUnit.MINUTES);
|
|
1588
|
-
}
|
|
1589
|
-
toHours(value) {
|
|
1590
|
-
return this.toUnit(value, _TimeUnit.HOURS);
|
|
1591
|
-
}
|
|
1592
|
-
toDays(value) {
|
|
1593
|
-
return this.toUnit(value, _TimeUnit.DAYS);
|
|
1594
|
-
}
|
|
1595
|
-
static MILLISECONDS = new _TimeUnit(1);
|
|
1596
|
-
static SECONDS = new _TimeUnit(1e3 * _TimeUnit.MILLISECONDS.multiplier);
|
|
1597
|
-
static MINUTES = new _TimeUnit(60 * _TimeUnit.SECONDS.multiplier);
|
|
1598
|
-
static HOURS = new _TimeUnit(60 * _TimeUnit.MINUTES.multiplier);
|
|
1599
|
-
static DAYS = new _TimeUnit(24 * _TimeUnit.HOURS.multiplier);
|
|
1600
|
-
};
|
|
1601
|
-
|
|
1602
|
-
// src/time/TimeBase.ts
|
|
1603
|
-
var TimeBase = class {
|
|
1604
|
-
_ms;
|
|
1605
|
-
constructor(value, unit) {
|
|
1606
|
-
this._ms = unit.toMs(value);
|
|
1607
|
-
}
|
|
1608
|
-
/**
|
|
1609
|
-
* Total number of milliseconds, rounded down.
|
|
1610
|
-
*/
|
|
1611
|
-
get ms() {
|
|
1612
|
-
return Math.floor(this._ms / TimeUnit.MILLISECONDS.multiplier);
|
|
1613
|
-
}
|
|
1614
|
-
/**
|
|
1615
|
-
* Total number of seconds, rounded down.
|
|
1616
|
-
*/
|
|
1617
|
-
get seconds() {
|
|
1618
|
-
return Math.floor(this._ms / TimeUnit.SECONDS.multiplier);
|
|
1619
|
-
}
|
|
1620
|
-
/**
|
|
1621
|
-
* Total number of minutes, rounded down.
|
|
1622
|
-
*/
|
|
1623
|
-
get minutes() {
|
|
1624
|
-
return Math.floor(this._ms / TimeUnit.MINUTES.multiplier);
|
|
1625
|
-
}
|
|
1626
|
-
/**
|
|
1627
|
-
* Total number of hours, rounded down.
|
|
1628
|
-
*/
|
|
1629
|
-
get hours() {
|
|
1630
|
-
return Math.floor(this._ms / TimeUnit.HOURS.multiplier);
|
|
1631
|
-
}
|
|
1632
|
-
/**
|
|
1633
|
-
* Total number of days, rounded down.
|
|
1634
|
-
*/
|
|
1635
|
-
get days() {
|
|
1636
|
-
return Math.floor(this._ms / TimeUnit.DAYS.multiplier);
|
|
1637
|
-
}
|
|
1638
|
-
addMs(milliseconds) {
|
|
1639
|
-
return this.addUnits(milliseconds, TimeUnit.MILLISECONDS);
|
|
1640
|
-
}
|
|
1641
|
-
addSeconds(seconds) {
|
|
1642
|
-
return this.addUnits(seconds, TimeUnit.SECONDS);
|
|
1643
|
-
}
|
|
1644
|
-
addMinutes(minutes) {
|
|
1645
|
-
return this.addUnits(minutes, TimeUnit.MINUTES);
|
|
1646
|
-
}
|
|
1647
|
-
addHours(hours) {
|
|
1648
|
-
return this.addUnits(hours, TimeUnit.HOURS);
|
|
1649
|
-
}
|
|
1650
|
-
addDays(days) {
|
|
1651
|
-
return this.addUnits(days, TimeUnit.DAYS);
|
|
1652
|
-
}
|
|
1653
|
-
removeDays(days) {
|
|
1654
|
-
return this.removeUnits(days, TimeUnit.DAYS);
|
|
1655
|
-
}
|
|
1656
|
-
addUnits(n, unit) {
|
|
1657
|
-
return this.create(this._ms + unit.toMs(n), TimeUnit.MILLISECONDS);
|
|
1658
|
-
}
|
|
1659
|
-
removeMs(milliseconds) {
|
|
1660
|
-
return this.addUnits(-milliseconds, TimeUnit.MILLISECONDS);
|
|
1661
|
-
}
|
|
1662
|
-
removeSeconds(seconds) {
|
|
1663
|
-
return this.addUnits(-seconds, TimeUnit.SECONDS);
|
|
1664
|
-
}
|
|
1665
|
-
removeMinutes(minutes) {
|
|
1666
|
-
return this.addUnits(-minutes, TimeUnit.MINUTES);
|
|
1667
|
-
}
|
|
1668
|
-
removeHours(hours) {
|
|
1669
|
-
return this.addUnits(-hours, TimeUnit.HOURS);
|
|
1670
|
-
}
|
|
1671
|
-
removeUnits(n, unit) {
|
|
1672
|
-
return this.addUnits(-n, unit);
|
|
1673
|
-
}
|
|
1674
|
-
getUnit(unit) {
|
|
1675
|
-
return this._ms / unit.multiplier;
|
|
1676
|
-
}
|
|
1677
|
-
toUnits() {
|
|
1678
|
-
return {
|
|
1679
|
-
milliseconds: this.ms % TimeUnit.SECONDS.multiplier,
|
|
1680
|
-
seconds: Math.floor(this.seconds % 60),
|
|
1681
|
-
minutes: Math.floor(this.minutes % 60),
|
|
1682
|
-
hours: Math.floor(this.hours % 24),
|
|
1683
|
-
days: Math.floor(this.days)
|
|
1684
|
-
};
|
|
1685
|
-
}
|
|
1686
|
-
static unitsToMs(units) {
|
|
1687
|
-
if (!units)
|
|
1688
|
-
throw new Error("Invalid units given");
|
|
1689
|
-
let ms = 0;
|
|
1690
|
-
ms += (units.milliseconds ?? 0) * TimeUnit.MILLISECONDS.multiplier;
|
|
1691
|
-
ms += (units.seconds ?? 0) * TimeUnit.SECONDS.multiplier;
|
|
1692
|
-
ms += (units.minutes ?? 0) * TimeUnit.MINUTES.multiplier;
|
|
1693
|
-
ms += (units.hours ?? 0) * TimeUnit.HOURS.multiplier;
|
|
1694
|
-
ms += (units.days ?? 0) * TimeUnit.DAYS.multiplier;
|
|
1695
|
-
return ms;
|
|
1696
|
-
}
|
|
1697
|
-
toJSON() {
|
|
1698
|
-
return this._ms;
|
|
1699
|
-
}
|
|
1700
|
-
};
|
|
1701
|
-
|
|
1702
|
-
// src/time/TimeDuration.ts
|
|
1703
|
-
var TimeDuration = class _TimeDuration extends TimeBase {
|
|
1704
|
-
constructor(value, unit) {
|
|
1705
|
-
super(value, unit);
|
|
1706
|
-
if (value < 0)
|
|
1707
|
-
throw new Error("Duration cannot be less than 0");
|
|
1708
|
-
}
|
|
1709
|
-
create(value, unit) {
|
|
1710
|
-
return new _TimeDuration(value, unit);
|
|
1711
|
-
}
|
|
1712
|
-
addDuration(duration) {
|
|
1713
|
-
return this.addUnits(duration.ms, TimeUnit.MILLISECONDS);
|
|
1714
|
-
}
|
|
1715
|
-
removeDuration(duration) {
|
|
1716
|
-
return this.removeUnits(duration.ms, TimeUnit.MILLISECONDS);
|
|
1717
|
-
}
|
|
1718
|
-
removeUnits(n, unit) {
|
|
1719
|
-
const nn = Math.min(n, this.getUnit(unit));
|
|
1720
|
-
return super.removeUnits(nn, unit);
|
|
1721
|
-
}
|
|
1722
|
-
multiplyBy(times) {
|
|
1723
|
-
ensureNonNegativeNumber(times, "times");
|
|
1724
|
-
return _TimeDuration.ms(this.ms * times);
|
|
1725
|
-
}
|
|
1726
|
-
divideBy(times) {
|
|
1727
|
-
ensurePositiveNumber(times, "times");
|
|
1728
|
-
return _TimeDuration.ms(this.ms / times);
|
|
1729
|
-
}
|
|
1730
|
-
/**
|
|
1731
|
-
* Returns the current duration in a human readable format, prioritizing the most significant unit of time and excluding the rest.
|
|
1732
|
-
* @todo[2023-06] Should allow more customization options
|
|
1733
|
-
* @todo[2023-06] By default should show the secondary unit only when actually needed (eg, 1h 20m & 3h, instead of 1h 20m & 3h 28m)
|
|
1734
|
-
* @todo[2023-06] Should not allow negative durations, as this is a duration and not an instant.
|
|
1735
|
-
* @returns the duration, with only the most significant units displayed as a human readable string, eg: 1d 20h, 10m 30s.
|
|
1736
|
-
*/
|
|
1737
|
-
get formatted() {
|
|
1738
|
-
const format = (x, u1, y, u2) => `${x}${u1} ${pad(y.toString(), 2, "0")}${u2}`;
|
|
1739
|
-
const units = this.toUnits();
|
|
1740
|
-
if (units.days >= 1) return format(units.days, "d", units.hours, "h");
|
|
1741
|
-
else if (units.hours >= 1)
|
|
1742
|
-
return format(units.hours, "h", units.minutes, "m");
|
|
1743
|
-
else if (units.minutes >= 1)
|
|
1744
|
-
return format(units.minutes, "m", units.seconds, "s");
|
|
1745
|
-
else if (units.seconds >= 0)
|
|
1746
|
-
return units.seconds.toString() + "s";
|
|
1747
|
-
else if (units.seconds > -60)
|
|
1748
|
-
return "A few moments ago";
|
|
1749
|
-
else
|
|
1750
|
-
return "Some time ago";
|
|
1751
|
-
}
|
|
1752
|
-
interval(cb) {
|
|
1753
|
-
return setInterval(cb, this.ms);
|
|
1754
|
-
}
|
|
1755
|
-
timeout(cb) {
|
|
1756
|
-
return setTimeout(cb, this.ms);
|
|
1757
|
-
}
|
|
1758
|
-
cancelablePromise() {
|
|
1759
|
-
const deferred = new Deferred_default();
|
|
1760
|
-
if (this.ms > 0) {
|
|
1761
|
-
this.timeout(() => deferred.resolve());
|
|
1762
|
-
} else {
|
|
1763
|
-
deferred.resolve();
|
|
1764
|
-
}
|
|
1765
|
-
return deferred.asCancelablePromise();
|
|
1766
|
-
}
|
|
1767
|
-
promise() {
|
|
1768
|
-
if (this.isEmpty())
|
|
1769
|
-
return Promise.resolve();
|
|
1770
|
-
const deferred = new Deferred_default();
|
|
1771
|
-
this.timeout(() => deferred.resolve());
|
|
1772
|
-
return deferred.asPromise();
|
|
1773
|
-
}
|
|
1774
|
-
delay(cb) {
|
|
1775
|
-
void this.promise().then(() => cb());
|
|
1776
|
-
}
|
|
1777
|
-
cancelableDelay(cb) {
|
|
1778
|
-
const deferred = this.cancelablePromise();
|
|
1779
|
-
void deferred.then(() => {
|
|
1780
|
-
cb();
|
|
1781
|
-
}, (err) => {
|
|
1782
|
-
if (err instanceof DeferredCanceledError)
|
|
1783
|
-
return;
|
|
1784
|
-
throw err;
|
|
1785
|
-
});
|
|
1786
|
-
return { cancel: () => deferred.cancel() };
|
|
1787
|
-
}
|
|
1788
|
-
debounce(fn) {
|
|
1789
|
-
let handle = null;
|
|
1790
|
-
return (t) => {
|
|
1791
|
-
if (handle)
|
|
1792
|
-
clearTimeout(handle);
|
|
1793
|
-
handle = this.timeout(() => {
|
|
1794
|
-
handle = null;
|
|
1795
|
-
fn(t);
|
|
1796
|
-
});
|
|
1797
|
-
};
|
|
1798
|
-
}
|
|
1799
|
-
toDigitalClock() {
|
|
1800
|
-
const units = this.toUnits();
|
|
1801
|
-
return pad(units.hours.toString(), 2, "0") + ":" + pad(units.minutes.toString(), 2, "0") + ":" + pad(units.seconds.toString(), 2, "0");
|
|
1802
|
-
}
|
|
1803
|
-
get asSeconds() {
|
|
1804
|
-
if (this.ms < 0)
|
|
1805
|
-
return "0";
|
|
1806
|
-
if (this.ms < 1e3)
|
|
1807
|
-
return (this.ms / 1e3).toFixed(1);
|
|
1808
|
-
return Math.floor(this.seconds).toString();
|
|
1809
|
-
}
|
|
1810
|
-
fromNow() {
|
|
1811
|
-
return TimeInstant_default.now().addDuration(this);
|
|
1812
|
-
}
|
|
1813
|
-
differenceFrom(other) {
|
|
1814
|
-
return new _TimeDuration(Math.abs(this.ms - other.ms), TimeUnit.MILLISECONDS);
|
|
1815
|
-
}
|
|
1816
|
-
isGreaterThan(other) {
|
|
1817
|
-
return this.ms > other.ms;
|
|
1818
|
-
}
|
|
1819
|
-
isLessThan(other) {
|
|
1820
|
-
return this.ms < other.ms;
|
|
1821
|
-
}
|
|
1822
|
-
compareTo(other) {
|
|
1823
|
-
if (this.ms === other.ms) {
|
|
1824
|
-
return 0;
|
|
1825
|
-
} else if (this.isLessThan(other)) {
|
|
1826
|
-
return -1;
|
|
1827
|
-
} else {
|
|
1828
|
-
return 1;
|
|
1829
|
-
}
|
|
1830
|
-
}
|
|
1831
|
-
atLeast(other) {
|
|
1832
|
-
if (!this.isLessThan(other)) {
|
|
1833
|
-
return this;
|
|
1834
|
-
} else {
|
|
1835
|
-
return other;
|
|
1836
|
-
}
|
|
1837
|
-
}
|
|
1838
|
-
atMost(other) {
|
|
1839
|
-
if (!this.isGreaterThan(other)) {
|
|
1840
|
-
return this;
|
|
1841
|
-
} else {
|
|
1842
|
-
return other;
|
|
1843
|
-
}
|
|
1844
|
-
}
|
|
1845
|
-
isEmpty() {
|
|
1846
|
-
return this.ms <= 0;
|
|
1847
|
-
}
|
|
1848
|
-
isNotEmpty() {
|
|
1849
|
-
return this.ms > 0;
|
|
1850
|
-
}
|
|
1851
|
-
toUnits() {
|
|
1852
|
-
return super.toUnits();
|
|
1853
|
-
}
|
|
1854
|
-
/**
|
|
1855
|
-
* This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
|
|
1856
|
-
*/
|
|
1857
|
-
get _duration() {
|
|
1858
|
-
return this.formatted;
|
|
1859
|
-
}
|
|
1860
|
-
toString() {
|
|
1861
|
-
return `${this.constructor.name}[${this.formatted}]`;
|
|
1862
|
-
}
|
|
1863
|
-
static parseHumanTime(humanTime) {
|
|
1864
|
-
const match = humanTime.trim().match(/^(?:([0-9]+)d|)\s*(?:([0-9]+)h|)\s*(?:([0-9]+)m|)\s*(?:([0-9]+)s|)$/);
|
|
1865
|
-
if (match) {
|
|
1866
|
-
const [_, days, hours, minutes, seconds] = match;
|
|
1867
|
-
return new _TimeDuration(TimeBase.unitsToMs({ days: Number(days ?? 0), hours: Number(hours ?? 0), minutes: Number(minutes ?? 0), seconds: Number(seconds ?? 0) }), TimeUnit.MILLISECONDS);
|
|
1868
|
-
}
|
|
1869
|
-
throw new Error("Failed to parse time: " + humanTime);
|
|
1870
|
-
}
|
|
1871
|
-
static compare(a, b) {
|
|
1872
|
-
return a.compareTo(b);
|
|
1873
|
-
}
|
|
1874
|
-
static ms = (value) => new _TimeDuration(value, TimeUnit.MILLISECONDS);
|
|
1875
|
-
static seconds = (value) => new _TimeDuration(value, TimeUnit.SECONDS);
|
|
1876
|
-
static minutes = (value) => new _TimeDuration(value, TimeUnit.MINUTES);
|
|
1877
|
-
static hours = (value) => new _TimeDuration(value, TimeUnit.HOURS);
|
|
1878
|
-
static days = (value) => new _TimeDuration(value, TimeUnit.DAYS);
|
|
1879
|
-
static shamefulUnref = (ms) => {
|
|
1880
|
-
if (ms instanceof _TimeDuration) {
|
|
1881
|
-
return ms.ms;
|
|
1882
|
-
}
|
|
1883
|
-
return ms;
|
|
1884
|
-
};
|
|
1885
|
-
static shamefulRef = (ms) => {
|
|
1886
|
-
if (ms instanceof _TimeDuration) {
|
|
1887
|
-
return ms;
|
|
1888
|
-
}
|
|
1889
|
-
if (ms < 0) {
|
|
1890
|
-
ms = 0;
|
|
1891
|
-
}
|
|
1892
|
-
return _TimeDuration.ms(ms);
|
|
1893
|
-
};
|
|
1894
|
-
static unref = (ms) => {
|
|
1895
|
-
return _TimeDuration.shamefulUnref(ms);
|
|
1896
|
-
};
|
|
1897
|
-
static ref = (ms) => {
|
|
1898
|
-
return _TimeDuration.shamefulRef(ms);
|
|
1899
|
-
};
|
|
1900
|
-
static fromMs(ms) {
|
|
1901
|
-
return new _TimeDuration(ms, TimeUnit.MILLISECONDS);
|
|
1902
|
-
}
|
|
1903
|
-
static distanceFromNow(instant) {
|
|
1904
|
-
return _TimeDuration.distance(TimeInstant_default.now(), instant);
|
|
1905
|
-
}
|
|
1906
|
-
static distance(instant1, instant2) {
|
|
1907
|
-
return instant1.distanceFrom(instant2);
|
|
1908
|
-
}
|
|
1909
|
-
static greatest(duration1, duration2) {
|
|
1910
|
-
return duration1.isGreaterThan(duration2) ? duration1 : duration2;
|
|
1911
|
-
}
|
|
1912
|
-
static lowest(duration1, duration2) {
|
|
1913
|
-
return duration1.isLessThan(duration2) ? duration1 : duration2;
|
|
1914
|
-
}
|
|
1915
|
-
static ZERO = new _TimeDuration(0, TimeUnit.MILLISECONDS);
|
|
1916
|
-
static fromJSON(ms) {
|
|
1917
|
-
return _TimeDuration.ms(ms);
|
|
1918
|
-
}
|
|
1919
|
-
static fromUnits(parameters) {
|
|
1920
|
-
return _TimeDuration.ms(TimeBase.unitsToMs(parameters));
|
|
1921
|
-
}
|
|
1922
|
-
};
|
|
1923
|
-
function isAllowedTimeDuration(t) {
|
|
1924
|
-
return typeof t === "number" && t > 0 || t instanceof TimeDuration;
|
|
1925
|
-
}
|
|
1926
|
-
var TimeDuration_default = TimeDuration;
|
|
1927
|
-
|
|
1928
|
-
// src/time/constants.ts
|
|
1929
|
-
var monthNames = {
|
|
1930
|
-
january: 1,
|
|
1931
|
-
february: 2,
|
|
1932
|
-
march: 3,
|
|
1933
|
-
april: 4,
|
|
1934
|
-
may: 5,
|
|
1935
|
-
june: 6,
|
|
1936
|
-
july: 7,
|
|
1937
|
-
august: 8,
|
|
1938
|
-
september: 9,
|
|
1939
|
-
october: 10,
|
|
1940
|
-
november: 11,
|
|
1941
|
-
december: 12
|
|
1942
|
-
};
|
|
1943
|
-
|
|
1944
|
-
// src/time/TimeInstantBuilder.ts
|
|
1945
|
-
var isRelativeNumber = (x) => x !== void 0 && x.length > 0 && (x[0] === "+" || x[0] === "-");
|
|
1946
|
-
var defaultTimeInstantCreationParameters = {
|
|
1947
|
-
year: { relative: 0, relativeTo: "now" },
|
|
1948
|
-
month: { relative: 0, relativeTo: "now" },
|
|
1949
|
-
date: { relative: 0, relativeTo: "now" },
|
|
1950
|
-
hours: { relative: 0, relativeTo: "now" },
|
|
1951
|
-
minutes: { relative: 0, relativeTo: "now" },
|
|
1952
|
-
seconds: { relative: 0, relativeTo: "now" },
|
|
1953
|
-
milliseconds: { relative: 0, relativeTo: "now" }
|
|
1954
|
-
};
|
|
1955
|
-
var timeInstantCreationRelativeAliases = {
|
|
1956
|
-
current: 0,
|
|
1957
|
-
last: -1,
|
|
1958
|
-
next: 1
|
|
1959
|
-
};
|
|
1960
|
-
var timeInstantResolveValue = (getFromDate2, referenceDate, x) => {
|
|
1961
|
-
if (x === void 0) {
|
|
1962
|
-
return getFromDate2(referenceDate);
|
|
1963
|
-
} else if (typeof x === "number") {
|
|
1964
|
-
return x;
|
|
1965
|
-
} else if (typeof x === "string") {
|
|
1966
|
-
if (isRelativeNumber(x)) {
|
|
1967
|
-
return getFromDate2(referenceDate) + parseInt(x);
|
|
1968
|
-
} else if (x in timeInstantCreationRelativeAliases) {
|
|
1969
|
-
return getFromDate2(referenceDate) + timeInstantCreationRelativeAliases[x];
|
|
1970
|
-
} else if (x in monthNames) {
|
|
1971
|
-
return monthNames[x];
|
|
1972
|
-
} else {
|
|
1973
|
-
throw new Error("Uparseable string detected: " + x);
|
|
1974
|
-
}
|
|
1975
|
-
} else if ("relative" in x) {
|
|
1976
|
-
const { relative, relativeTo } = x;
|
|
1977
|
-
if (relativeTo === void 0 || relativeTo === "now") {
|
|
1978
|
-
return getFromDate2(referenceDate) + relative;
|
|
1979
|
-
} else {
|
|
1980
|
-
return getFromDate2(relativeTo.toDate()) + relative;
|
|
1981
|
-
}
|
|
1982
|
-
} else if ("absolute" in x) {
|
|
1983
|
-
return x.absolute;
|
|
1984
|
-
} else {
|
|
1985
|
-
throw new Error("Uparseable value detected: " + x);
|
|
1986
|
-
}
|
|
1987
|
-
};
|
|
1988
|
-
var getFromDate = {
|
|
1989
|
-
year: (d) => d.getFullYear(),
|
|
1990
|
-
month: (d) => d.getMonth() + 1,
|
|
1991
|
-
date: (d) => d.getDate(),
|
|
1992
|
-
hours: (d) => d.getHours(),
|
|
1993
|
-
minutes: (d) => d.getMinutes(),
|
|
1994
|
-
seconds: (d) => d.getSeconds(),
|
|
1995
|
-
milliseconds: (d) => d.getMilliseconds()
|
|
1996
|
-
};
|
|
1997
|
-
var toReferenceDate = (x) => {
|
|
1998
|
-
ensureDefined(x);
|
|
1999
|
-
if (isTimeInstant(x)) return x.toDate();
|
|
2000
|
-
return x;
|
|
2001
|
-
};
|
|
2002
|
-
function createTimeInstantFromParameters(aParameters, aReferenceDate = TimeInstant.now()) {
|
|
2003
|
-
const { year, month, date, hours, minutes, seconds, milliseconds } = { ...defaultTimeInstantCreationParameters, ...aParameters };
|
|
2004
|
-
const referenceDate = toReferenceDate(aReferenceDate);
|
|
2005
|
-
const timestamp2 = Date.UTC(
|
|
2006
|
-
timeInstantResolveValue(getFromDate.year, referenceDate, year),
|
|
2007
|
-
timeInstantResolveValue(getFromDate.month, referenceDate, month) - 1,
|
|
2008
|
-
// Convert from 1-based to 0-based for Date.UTC()
|
|
2009
|
-
timeInstantResolveValue(getFromDate.date, referenceDate, date),
|
|
2010
|
-
timeInstantResolveValue(getFromDate.hours, referenceDate, hours),
|
|
2011
|
-
timeInstantResolveValue(getFromDate.minutes, referenceDate, minutes),
|
|
2012
|
-
timeInstantResolveValue(getFromDate.seconds, referenceDate, seconds),
|
|
2013
|
-
timeInstantResolveValue(getFromDate.milliseconds, referenceDate, milliseconds)
|
|
2014
|
-
);
|
|
2015
|
-
if (isNaN(timestamp2))
|
|
2016
|
-
throw new Error(`Unparseable date: ${JSON.stringify(aParameters)} }`);
|
|
2017
|
-
return TimeInstant.fromUnixTimestamp(timestamp2);
|
|
2018
|
-
}
|
|
2019
|
-
function timeInstantBuilder() {
|
|
2020
|
-
let referenceDate = TimeInstant.now().toDate();
|
|
2021
|
-
const value = { ...defaultTimeInstantCreationParameters };
|
|
2022
|
-
const ret = {
|
|
2023
|
-
year: (x) => {
|
|
2024
|
-
value.year = x;
|
|
2025
|
-
return ret;
|
|
2026
|
-
},
|
|
2027
|
-
month: (x) => {
|
|
2028
|
-
value.month = x;
|
|
2029
|
-
return ret;
|
|
2030
|
-
},
|
|
2031
|
-
date: (x) => {
|
|
2032
|
-
value.date = x;
|
|
2033
|
-
return ret;
|
|
2034
|
-
},
|
|
2035
|
-
hours: (x) => {
|
|
2036
|
-
value.hours = x;
|
|
2037
|
-
return ret;
|
|
2038
|
-
},
|
|
2039
|
-
minutes: (x) => {
|
|
2040
|
-
value.minutes = x;
|
|
2041
|
-
return ret;
|
|
2042
|
-
},
|
|
2043
|
-
seconds: (x) => {
|
|
2044
|
-
value.seconds = x;
|
|
2045
|
-
return ret;
|
|
2046
|
-
},
|
|
2047
|
-
milliseconds: (x) => {
|
|
2048
|
-
value.milliseconds = x;
|
|
2049
|
-
return ret;
|
|
2050
|
-
},
|
|
2051
|
-
values: (x) => {
|
|
2052
|
-
Object.keys(value).forEach((key) => value[key] = x[key] ?? value[key]);
|
|
2053
|
-
return ret;
|
|
2054
|
-
},
|
|
2055
|
-
relativeTo: (x) => {
|
|
2056
|
-
referenceDate = toReferenceDate(x);
|
|
2057
|
-
return ret;
|
|
2058
|
-
},
|
|
2059
|
-
build: () => createTimeInstantFromParameters(value, referenceDate)
|
|
2060
|
-
};
|
|
2061
|
-
return ret;
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
// src/time/TimeInstant.ts
|
|
2065
|
-
var TimeInstant = class _TimeInstant extends TimeBase {
|
|
2066
|
-
constructor(number, unit) {
|
|
2067
|
-
super(number, unit);
|
|
2068
|
-
}
|
|
2069
|
-
create(value, unit) {
|
|
2070
|
-
return new _TimeInstant(value, unit);
|
|
2071
|
-
}
|
|
2072
|
-
addDuration(duration) {
|
|
2073
|
-
return _TimeInstant.fromUnixTimestamp(this.ms + duration.ms);
|
|
2074
|
-
}
|
|
2075
|
-
removeDuration(duration) {
|
|
2076
|
-
return _TimeInstant.fromUnixTimestamp(this.ms - duration.ms);
|
|
2077
|
-
}
|
|
2078
|
-
distanceFrom(instant) {
|
|
2079
|
-
return TimeDuration_default.fromMs(Math.abs(this.ms - instant.ms));
|
|
2080
|
-
}
|
|
2081
|
-
distanceFromNow() {
|
|
2082
|
-
return TimeDuration_default.fromMs(Math.abs(this.ms - Date.now()));
|
|
2083
|
-
}
|
|
2084
|
-
distanceFromUnixTimestamp(timestamp2) {
|
|
2085
|
-
return TimeDuration_default.ms(this.ms - timestamp2);
|
|
2086
|
-
}
|
|
2087
|
-
timeLeftFrom(instant) {
|
|
2088
|
-
const distance = this.ms - instant.ms;
|
|
2089
|
-
return TimeDuration_default.fromMs(Math.max(distance, 0));
|
|
2090
|
-
}
|
|
2091
|
-
timeLeftFromNow() {
|
|
2092
|
-
const distance = this.ms - Date.now();
|
|
2093
|
-
return TimeDuration_default.fromMs(Math.max(distance, 0));
|
|
2094
|
-
}
|
|
2095
|
-
distanceFromStartOfDay() {
|
|
2096
|
-
const params = this.toParameters();
|
|
2097
|
-
const msInDay = params.hours * 36e5 + params.minutes * 6e4 + params.seconds * 1e3 + params.milliseconds;
|
|
2098
|
-
return TimeDuration_default.fromMs(msInDay);
|
|
2099
|
-
}
|
|
2100
|
-
atStartOfDay() {
|
|
2101
|
-
return this.atTime({ hours: 0, minutes: 0, seconds: 0, milliseconds: 0 });
|
|
2102
|
-
}
|
|
2103
|
-
distanceFromEndOfDay() {
|
|
2104
|
-
const params = this.toParameters();
|
|
2105
|
-
const msInDay = params.hours * 36e5 + params.minutes * 6e4 + params.seconds * 1e3 + params.milliseconds;
|
|
2106
|
-
const msUntilEndOfDay = 86399999 - msInDay;
|
|
2107
|
-
return TimeDuration_default.fromMs(msUntilEndOfDay);
|
|
2108
|
-
}
|
|
2109
|
-
atEndOfDay() {
|
|
2110
|
-
return this.atTime({ hours: 23, minutes: 59, seconds: 59, milliseconds: 999 });
|
|
2111
|
-
}
|
|
2112
|
-
promise() {
|
|
2113
|
-
return this.timeLeftFromNow().promise();
|
|
2114
|
-
}
|
|
2115
|
-
cancelablePromise() {
|
|
2116
|
-
return this.timeLeftFromNow().cancelablePromise();
|
|
2117
|
-
}
|
|
2118
|
-
delay(cb) {
|
|
2119
|
-
return this.timeLeftFromNow().delay(cb);
|
|
2120
|
-
}
|
|
2121
|
-
cancelableDelay(cb) {
|
|
2122
|
-
return this.timeLeftFromNow().cancelableDelay(cb);
|
|
2123
|
-
}
|
|
2124
|
-
ensureInTheFuture() {
|
|
2125
|
-
if (!this.isInTheFuture())
|
|
2126
|
-
throw new Error("Instant is not in the future");
|
|
2127
|
-
}
|
|
2128
|
-
ensureInThePast() {
|
|
2129
|
-
if (!this.isInThePast())
|
|
2130
|
-
throw new Error("Instant is not in the past");
|
|
2131
|
-
}
|
|
2132
|
-
isToday() {
|
|
2133
|
-
return Math.floor(this.ms / 864e5) === Math.floor(Date.now() / 864e5);
|
|
2134
|
-
}
|
|
2135
|
-
/**
|
|
2136
|
-
* Formats this instant using the given pattern. The pattern can contain the following tokens:
|
|
2137
|
-
*
|
|
2138
|
-
* Note: Implementation inspired by the small-date library (https://github.com/robinweser/small-date).
|
|
2139
|
-
*
|
|
2140
|
-
* | Token | Description | Example |
|
|
2141
|
-
* |:------|:--------------------------------|:------------------------------|
|
|
2142
|
-
* | D | Weekday, 1 letter | W |
|
|
2143
|
-
* | DD | Weekday, 3 letters | Wed |
|
|
2144
|
-
* | DDD | Weekday, long | Wednesday |
|
|
2145
|
-
* | d | Day of the month, no padding | 3 |
|
|
2146
|
-
* | dd | Day of the month, padded to 2 | 03 |
|
|
2147
|
-
* | M | Month, numeric | 3 |
|
|
2148
|
-
* | MM | Month, 2 digits | 03 |
|
|
2149
|
-
* | MMM | Month, 3 letters | Mar |
|
|
2150
|
-
* | MMMM | Month, long | March |
|
|
2151
|
-
* | y | Year, numeric | 2021 |
|
|
2152
|
-
* | yy | Year, 2 digits | 21 |
|
|
2153
|
-
* | yyyy | Year, numeric | 2021 |
|
|
2154
|
-
* | h | Hours, no padding | 6 |
|
|
2155
|
-
* | hh | Hours, padded to 2 | 06 |
|
|
2156
|
-
* | H | Hours in 24-format, no padding | 18 |
|
|
2157
|
-
* | HH | Hours in 24-format, padded to 2 | 18 |
|
|
2158
|
-
* | m | Minutes, no padding | 7 |
|
|
2159
|
-
* | m | Minutes, padded to 2 | 07 |
|
|
2160
|
-
* | s | Seconds, no padding | 8 |
|
|
2161
|
-
* | ss | Seconds, padded to 2 | 08 |
|
|
2162
|
-
* | S | Milliseconds, no padding | 9 |
|
|
2163
|
-
* | SS | Milliseconds, padded to 2 | 09 |
|
|
2164
|
-
* | SSS | Milliseconds, padded to 3 | 009 |
|
|
2165
|
-
* | G | Era, narrow | A |
|
|
2166
|
-
* | GG | Era, short | AD |
|
|
2167
|
-
* | GGG | Era, long | Anno Domino |
|
|
2168
|
-
* | Z | Time zone, short | GMT+1 |
|
|
2169
|
-
* | ZZ | Time short, long C | entral European Standard Time |
|
|
2170
|
-
* | P | Period of the day, narrow | in the morning |
|
|
2171
|
-
* | PP | Period of the day, short | in the morning |
|
|
2172
|
-
* | PPP | Period of the day, long | in the morning |
|
|
2173
|
-
* | a | Meridiem | pm |
|
|
2174
|
-
* @param pattern The pattern to use. Refer to the token table above for details.
|
|
2175
|
-
* @param config An optional locale and timeZone definition to use during the format.
|
|
2176
|
-
* @returns a string, formatted using the given pattern, at the given timeZone with the given locale.
|
|
2177
|
-
*/
|
|
2178
|
-
format(pattern, config = {}) {
|
|
2179
|
-
return formatTimeInstant(this, pattern, config);
|
|
2180
|
-
}
|
|
2181
|
-
/**
|
|
2182
|
-
* Parses a date string using the given pattern and creates a TimeInstant.
|
|
2183
|
-
* This method is the inverse of format() - parsing a formatted string should recreate the original instant.
|
|
2184
|
-
*
|
|
2185
|
-
* For partial patterns (e.g., time-only or date-only), the missing components are taken from the base instant.
|
|
2186
|
-
* For example, parsing "14:30" with base set to yesterday at midnight will result in yesterday at 14:30.
|
|
2187
|
-
*
|
|
2188
|
-
* Note: Currently performs basic validation (e.g., month 1-12, day 1-31) but does not validate
|
|
2189
|
-
* calendar-specific constraints (e.g., February 30th, April 31st). Invalid dates may be
|
|
2190
|
-
* normalized by the underlying Date constructor.
|
|
2191
|
-
*
|
|
2192
|
-
* @param dateString The date string to parse
|
|
2193
|
-
* @param pattern The pattern used to parse the string (same tokens as format())
|
|
2194
|
-
* @param base The base TimeInstant to use for partial patterns (defaults to now)
|
|
2195
|
-
* @param config An optional locale and timeZone definition to use during parsing
|
|
2196
|
-
* @returns A TimeInstant parsed from the string
|
|
2197
|
-
* @throws Error if the string doesn't match the pattern or contains invalid basic values
|
|
2198
|
-
* @todo Add calendar-aware validation to reject dates like February 30th, April 31st
|
|
2199
|
-
*/
|
|
2200
|
-
static fromString(dateString, pattern, base = _TimeInstant.now(), config = {}) {
|
|
2201
|
-
return parseTimeInstant(dateString, pattern, base, config);
|
|
2202
|
-
}
|
|
2203
|
-
static parse(dateString, pattern, config = {}) {
|
|
2204
|
-
return parseTimeInstantComponents(dateString, pattern, config);
|
|
2205
|
-
}
|
|
2206
|
-
/**
|
|
2207
|
-
* @returns this instant, in ISO 8601 format (eg, 2024-11-01T15:49:22.024Z). The format is meant to always be realiable.
|
|
2208
|
-
*/
|
|
2209
|
-
asIso8601() {
|
|
2210
|
-
return this.format('yyyy-MM-dd"T"HH:mm:ss.SSS"Z"', { timeZone: "UTC" });
|
|
2211
|
-
}
|
|
2212
|
-
/**
|
|
2213
|
-
* @returns this instant, in a human readable format, containing both the date and the time. The format COULD change in the future, do NOT use this method for consistent outputs.
|
|
2214
|
-
*/
|
|
2215
|
-
asHumanTimestamp() {
|
|
2216
|
-
return this.format("yyyy-MM-dd HH:mm:ss");
|
|
2217
|
-
}
|
|
2218
|
-
toUnixTimestamp() {
|
|
2219
|
-
return this.ms;
|
|
2220
|
-
}
|
|
2221
|
-
toDate() {
|
|
2222
|
-
return new Date(this.ms);
|
|
2223
|
-
}
|
|
2224
|
-
toParameters() {
|
|
2225
|
-
const d = this.toDate();
|
|
2226
|
-
return {
|
|
2227
|
-
year: d.getUTCFullYear(),
|
|
2228
|
-
month: d.getUTCMonth() + 1,
|
|
2229
|
-
date: d.getUTCDate(),
|
|
2230
|
-
hours: d.getUTCHours(),
|
|
2231
|
-
minutes: d.getUTCMinutes(),
|
|
2232
|
-
seconds: d.getUTCSeconds(),
|
|
2233
|
-
milliseconds: d.getUTCMilliseconds()
|
|
2234
|
-
};
|
|
2235
|
-
}
|
|
2236
|
-
/**
|
|
2237
|
-
* @returns true if the instant is in the past, false otherwise
|
|
2238
|
-
*/
|
|
2239
|
-
isInThePast() {
|
|
2240
|
-
return this.ms < Date.now();
|
|
2241
|
-
}
|
|
2242
|
-
/**
|
|
2243
|
-
* @returns true if the instant is in the future, false otherwise
|
|
2244
|
-
*/
|
|
2245
|
-
isInTheFuture() {
|
|
2246
|
-
return this.ms > Date.now();
|
|
2247
|
-
}
|
|
2248
|
-
isAfter(other) {
|
|
2249
|
-
return this.ms > other.ms;
|
|
2250
|
-
}
|
|
2251
|
-
isBefore(other) {
|
|
2252
|
-
return this.ms < other.ms;
|
|
2253
|
-
}
|
|
2254
|
-
compareTo(other) {
|
|
2255
|
-
if (this.ms === other.ms) {
|
|
2256
|
-
return 0;
|
|
2257
|
-
} else if (this.isBefore(other)) {
|
|
2258
|
-
return -1;
|
|
2259
|
-
} else {
|
|
2260
|
-
return 1;
|
|
2261
|
-
}
|
|
2262
|
-
}
|
|
2263
|
-
atTime(parameters) {
|
|
2264
|
-
return _TimeInstant.fromParameters(parameters, this);
|
|
2265
|
-
}
|
|
2266
|
-
atDate(parameters) {
|
|
2267
|
-
return _TimeInstant.fromParameters(parameters, this);
|
|
2268
|
-
}
|
|
2269
|
-
at(parameters) {
|
|
2270
|
-
return _TimeInstant.fromParameters(parameters, this);
|
|
2271
|
-
}
|
|
2272
|
-
isBetween(start, end) {
|
|
2273
|
-
return this.ms >= start.ms && this.ms <= end.ms;
|
|
2274
|
-
}
|
|
2275
|
-
static fromDate(date) {
|
|
2276
|
-
return this.fromUnixTimestamp(date.getTime());
|
|
2277
|
-
}
|
|
2278
|
-
static fromUnixTimestamp(unixTimestamp) {
|
|
2279
|
-
return new _TimeInstant(unixTimestamp, TimeUnit.MILLISECONDS);
|
|
2280
|
-
}
|
|
2281
|
-
static fromUnixSeconds(unixSeconds) {
|
|
2282
|
-
return new _TimeInstant(unixSeconds, TimeUnit.SECONDS);
|
|
2283
|
-
}
|
|
2284
|
-
static fromParameters(parameters, referenceDate) {
|
|
2285
|
-
return createTimeInstantFromParameters(parameters, referenceDate);
|
|
2286
|
-
}
|
|
2287
|
-
static highest(instant1, instant2) {
|
|
2288
|
-
return instant1.isAfter(instant2) ? instant1 : instant2;
|
|
2289
|
-
}
|
|
2290
|
-
static lowest(instant1, instant2) {
|
|
2291
|
-
return instant1.isBefore(instant2) ? instant1 : instant2;
|
|
2292
|
-
}
|
|
2293
|
-
static builder() {
|
|
2294
|
-
return timeInstantBuilder();
|
|
2295
|
-
}
|
|
2296
|
-
static fromIso8601(str) {
|
|
2297
|
-
const iso8601Regex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})(Z|[+-]\d{2}:\d{2})?$/;
|
|
2298
|
-
const match = str.match(iso8601Regex);
|
|
2299
|
-
if (!match) {
|
|
2300
|
-
throw new Error("Invalid ISO 8601 date format: " + str);
|
|
2301
|
-
}
|
|
2302
|
-
const [, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr, millisecondStr] = match;
|
|
2303
|
-
const year = parseInt(yearStr, 10);
|
|
2304
|
-
const month = parseInt(monthStr, 10);
|
|
2305
|
-
const date = parseInt(dayStr, 10);
|
|
2306
|
-
const hours = parseInt(hourStr, 10);
|
|
2307
|
-
const minutes = parseInt(minuteStr, 10);
|
|
2308
|
-
const seconds = parseInt(secondStr, 10);
|
|
2309
|
-
const milliseconds = parseInt(millisecondStr, 10);
|
|
2310
|
-
if (!isValidYear(year))
|
|
2311
|
-
throw new Error("Invalid year in: " + str);
|
|
2312
|
-
if (!isValidMonth(month))
|
|
2313
|
-
throw new Error("Invalid month in: " + str);
|
|
2314
|
-
if (!isValidDayOfMonth(date))
|
|
2315
|
-
throw new Error("Invalid day in: " + str);
|
|
2316
|
-
if (!isValidHour(hours))
|
|
2317
|
-
throw new Error("Invalid hour in: " + str);
|
|
2318
|
-
if (!isValidMinute(minutes))
|
|
2319
|
-
throw new Error("Invalid minute in: " + str);
|
|
2320
|
-
if (!isValidSecond(seconds))
|
|
2321
|
-
throw new Error("Invalid second in: " + str);
|
|
2322
|
-
if (!isValidMillisecond(milliseconds))
|
|
2323
|
-
throw new Error("Invalid millisecond in: " + str);
|
|
2324
|
-
return _TimeInstant.fromParameters({
|
|
2325
|
-
year,
|
|
2326
|
-
month,
|
|
2327
|
-
date,
|
|
2328
|
-
hours,
|
|
2329
|
-
minutes,
|
|
2330
|
-
seconds,
|
|
2331
|
-
milliseconds
|
|
2332
|
-
});
|
|
2333
|
-
}
|
|
2334
|
-
/**
|
|
2335
|
-
* @deprecated [2025.10.19]: Use fromIso8601 instead.
|
|
2336
|
-
*/
|
|
2337
|
-
static tryFromIso8601 = this.fromIso8601;
|
|
2338
|
-
static now() {
|
|
2339
|
-
return _TimeInstant.fromUnixTimestamp(Date.now());
|
|
2340
|
-
}
|
|
2341
|
-
static compare(a, b) {
|
|
2342
|
-
return a.compareTo(b);
|
|
2343
|
-
}
|
|
2344
|
-
roundedToStartOf(unit) {
|
|
2345
|
-
return _TimeInstant.fromUnixTimestamp(this.ms - this.ms % unit.multiplier);
|
|
2346
|
-
}
|
|
2347
|
-
roundedToEndOf(unit) {
|
|
2348
|
-
return _TimeInstant.fromUnixTimestamp(this.ms - this.ms % unit.multiplier + unit.toMs(1) - 1);
|
|
2349
|
-
}
|
|
2350
|
-
roundedToNext(unit, factor = 1) {
|
|
2351
|
-
const unitMs = unit.toMs(factor);
|
|
2352
|
-
const timestamp2 = Math.ceil(this.ms / unitMs) * unitMs;
|
|
2353
|
-
return _TimeInstant.fromUnixTimestamp(timestamp2);
|
|
2354
|
-
}
|
|
2355
|
-
roundedToPrevious(unit, factor = 1) {
|
|
2356
|
-
const unitMs = unit.toMs(factor);
|
|
2357
|
-
const timestamp2 = Math.floor(this.ms / unitMs) * unitMs;
|
|
2358
|
-
return _TimeInstant.fromUnixTimestamp(timestamp2);
|
|
2359
|
-
}
|
|
2360
|
-
roundedTo(unit, factor = 1) {
|
|
2361
|
-
const unitMs = unit.toMs(factor);
|
|
2362
|
-
const timestamp2 = Math.round(this.ms / unitMs) * unitMs;
|
|
2363
|
-
return _TimeInstant.fromUnixTimestamp(timestamp2);
|
|
2364
|
-
}
|
|
2365
|
-
/**
|
|
2366
|
-
* Returns the week number represented by this instant, according to the ISO 8601 standard.
|
|
2367
|
-
* Please note that the instant and the week number could be of two different years, eg the friday 1st january is actually part of week 52 of the previous year.
|
|
2368
|
-
*/
|
|
2369
|
-
get weekNumber() {
|
|
2370
|
-
const date = this.toDate();
|
|
2371
|
-
const oneDay = 1e3 * 60 * 60 * 24;
|
|
2372
|
-
const dayOfWeek = date.getUTCDay() || 7;
|
|
2373
|
-
const thursdayMs = this.ms + (4 - dayOfWeek) * oneDay;
|
|
2374
|
-
const thursdayDate = new Date(thursdayMs);
|
|
2375
|
-
const thursdayYear = thursdayDate.getUTCFullYear();
|
|
2376
|
-
const firstOfJanMs = Date.UTC(thursdayYear, 0, 1, 12, 0, 0);
|
|
2377
|
-
const dayOfTheYear = Math.round((thursdayMs - firstOfJanMs) / oneDay);
|
|
2378
|
-
const weekNumber = Math.floor(dayOfTheYear / 7) + 1;
|
|
2379
|
-
return { weekNumber, year: thursdayYear };
|
|
2380
|
-
}
|
|
2381
|
-
/**
|
|
2382
|
-
* This method is used to provide a better DX when inspecting a TimeInstant object in DevTools.
|
|
2383
|
-
*/
|
|
2384
|
-
get _time() {
|
|
2385
|
-
return this.asHumanTimestamp();
|
|
2386
|
-
}
|
|
2387
|
-
toString() {
|
|
2388
|
-
return `${this.constructor.name}[${this.asHumanTimestamp()}]`;
|
|
2389
|
-
}
|
|
2390
|
-
static ZERO = _TimeInstant.fromUnixTimestamp(0);
|
|
2391
|
-
static fromJSON(ms) {
|
|
2392
|
-
return _TimeInstant.fromUnixTimestamp(ms);
|
|
2393
|
-
}
|
|
2394
|
-
};
|
|
2395
|
-
function isTimeInstant(x) {
|
|
2396
|
-
return x !== null && x !== void 0 && x instanceof TimeInstant;
|
|
2397
|
-
}
|
|
2398
|
-
var TimeInstant_default = TimeInstant;
|
|
2399
|
-
function isValidYear(num) {
|
|
2400
|
-
return num >= 0 && num <= 9999;
|
|
2401
|
-
}
|
|
2402
|
-
function isValidMonth(num) {
|
|
2403
|
-
return num >= 1 && num <= 12;
|
|
2404
|
-
}
|
|
2405
|
-
function isValidDayOfMonth(num) {
|
|
2406
|
-
return num >= 1 && num <= 31;
|
|
2407
|
-
}
|
|
2408
|
-
function isValidHour(num) {
|
|
2409
|
-
return num >= 0 && num <= 23;
|
|
2410
|
-
}
|
|
2411
|
-
function isValidMinute(num) {
|
|
2412
|
-
return num >= 0 && num <= 59;
|
|
2413
|
-
}
|
|
2414
|
-
function isValidSecond(num) {
|
|
2415
|
-
return num >= 0 && num <= 59;
|
|
2416
|
-
}
|
|
2417
|
-
function isValidMillisecond(num) {
|
|
2418
|
-
return num >= 0 && num <= 999;
|
|
2419
|
-
}
|
|
2420
|
-
var monthNamesCache = /* @__PURE__ */ new Map();
|
|
2421
|
-
function getMonthNames(locale = "en") {
|
|
2422
|
-
const cached = monthNamesCache.get(locale);
|
|
2423
|
-
if (cached) {
|
|
2424
|
-
return cached;
|
|
2425
|
-
}
|
|
2426
|
-
const shortFormatter = new Intl.DateTimeFormat(locale, { month: "short" });
|
|
2427
|
-
const longFormatter = new Intl.DateTimeFormat(locale, { month: "long" });
|
|
2428
|
-
const short = [];
|
|
2429
|
-
const long = [];
|
|
2430
|
-
for (let month = 0; month < 12; month++) {
|
|
2431
|
-
const date = new Date(2e3, month, 1);
|
|
2432
|
-
short.push(normalizeMonthName(shortFormatter.format(date)));
|
|
2433
|
-
long.push(normalizeMonthName(longFormatter.format(date)));
|
|
2434
|
-
}
|
|
2435
|
-
const result = { short, long };
|
|
2436
|
-
monthNamesCache.set(locale, result);
|
|
2437
|
-
return result;
|
|
2438
|
-
}
|
|
2439
|
-
function normalizeMonthName(name) {
|
|
2440
|
-
return name.replace(/[.,;:!?]/g, "").normalize("NFD").replace(/[\u0300-\u036f]/g, "").trim();
|
|
2441
|
-
}
|
|
2442
|
-
var PATTERN_REGEX = /(M|y|d|D|h|H|m|s|S|G|Z|P|a)+/g;
|
|
2443
|
-
var ESCAPE_REGEX = /\\"|"((?:\\"|[^"])*)"/g;
|
|
2444
|
-
var formatterConfigs = {
|
|
2445
|
-
// Year
|
|
2446
|
-
"y": { options: { year: "numeric" } },
|
|
2447
|
-
"yy": { options: { year: "2-digit" } },
|
|
2448
|
-
"yyyy": { options: { year: "numeric" } },
|
|
2449
|
-
// Month
|
|
2450
|
-
"M": { options: { month: "numeric" } },
|
|
2451
|
-
"MM": { options: { month: "2-digit" } },
|
|
2452
|
-
"MMM": { options: { month: "short" } },
|
|
2453
|
-
"MMMM": { options: { month: "long" } },
|
|
2454
|
-
// Day
|
|
2455
|
-
"d": { options: { day: "numeric" } },
|
|
2456
|
-
"dd": { options: { day: "2-digit" } },
|
|
2457
|
-
// Hours (24-hour) - extract and pad manually due to Intl quirk
|
|
2458
|
-
"H": { options: { hour: "numeric", hour12: false }, extract: { partType: "hour" } },
|
|
2459
|
-
"HH": { options: { hour: "2-digit", hour12: false }, extract: { partType: "hour", transform: (v) => v.padStart(2, "0") } },
|
|
2460
|
-
// Hours (12-hour) - extract and pad manually due to Intl quirk
|
|
2461
|
-
"h": { options: { hour: "numeric", hour12: true }, extract: { partType: "hour" } },
|
|
2462
|
-
"hh": { options: { hour: "2-digit", hour12: true }, extract: { partType: "hour", transform: (v) => v.padStart(2, "0") } },
|
|
2463
|
-
// Minutes - extract and pad manually due to Intl quirk
|
|
2464
|
-
"m": { options: { minute: "numeric" }, extract: { partType: "minute" } },
|
|
2465
|
-
"mm": { options: { minute: "2-digit" }, extract: { partType: "minute", transform: (v) => v.padStart(2, "0") } },
|
|
2466
|
-
// Seconds - extract and pad manually due to Intl quirk
|
|
2467
|
-
"s": { options: { second: "numeric" }, extract: { partType: "second" } },
|
|
2468
|
-
"ss": { options: { second: "2-digit" }, extract: { partType: "second", transform: (v) => v.padStart(2, "0") } },
|
|
2469
|
-
// Milliseconds - extract manually
|
|
2470
|
-
"S": { options: { fractionalSecondDigits: 1 }, extract: { partType: "fractionalSecond" } },
|
|
2471
|
-
"SS": { options: { fractionalSecondDigits: 2 }, extract: { partType: "fractionalSecond" } },
|
|
2472
|
-
"SSS": { options: { fractionalSecondDigits: 3 }, extract: { partType: "fractionalSecond" } },
|
|
2473
|
-
// Weekday
|
|
2474
|
-
"D": { options: { weekday: "narrow" } },
|
|
2475
|
-
"DD": { options: { weekday: "short" } },
|
|
2476
|
-
"DDD": { options: { weekday: "long" } },
|
|
2477
|
-
// Era
|
|
2478
|
-
"G": { options: { era: "narrow" } },
|
|
2479
|
-
"GG": { options: { era: "short" } },
|
|
2480
|
-
"GGG": { options: { era: "long" } },
|
|
2481
|
-
// Timezone
|
|
2482
|
-
"Z": { options: { timeZoneName: "short" } },
|
|
2483
|
-
"ZZ": { options: { timeZoneName: "long" } },
|
|
2484
|
-
// Day period
|
|
2485
|
-
"P": { options: { dayPeriod: "narrow" } },
|
|
2486
|
-
"PP": { options: { dayPeriod: "short" } },
|
|
2487
|
-
"PPP": { options: { dayPeriod: "long" } },
|
|
2488
|
-
// Meridiem - extract dayPeriod and lowercase it
|
|
2489
|
-
"a": { options: { hour: "numeric", hour12: true }, extract: { partType: "dayPeriod", transform: (v) => v.toLowerCase() } }
|
|
2490
|
-
};
|
|
2491
|
-
function formatType(type, length, date, locale = "en", timeZone = "UTC") {
|
|
2492
|
-
const tokenKey = type.repeat(length);
|
|
2493
|
-
const config = formatterConfigs[tokenKey];
|
|
2494
|
-
if (!config) {
|
|
2495
|
-
return void 0;
|
|
2496
|
-
}
|
|
2497
|
-
const formatter = new Intl.DateTimeFormat(locale, { ...config.options, timeZone });
|
|
2498
|
-
if (config.extract) {
|
|
2499
|
-
const part = formatter.formatToParts(date).find((p) => p.type === config.extract.partType);
|
|
2500
|
-
const value = part?.value;
|
|
2501
|
-
if (!value) {
|
|
2502
|
-
return void 0;
|
|
2503
|
-
}
|
|
2504
|
-
return config.extract.transform ? config.extract.transform(value) : value;
|
|
2505
|
-
}
|
|
2506
|
-
return formatter.format(date);
|
|
2507
|
-
}
|
|
2508
|
-
function formatTimeInstant(instant, pattern, config = {}) {
|
|
2509
|
-
const date = instant.toDate();
|
|
2510
|
-
const locale = config.locale || "en";
|
|
2511
|
-
const timeZone = config.timeZone || "UTC";
|
|
2512
|
-
return pattern.split(ESCAPE_REGEX).filter((sub) => sub !== void 0).map((sub, index) => {
|
|
2513
|
-
if (index % 2 !== 0) {
|
|
2514
|
-
return sub;
|
|
2515
|
-
}
|
|
2516
|
-
return sub.replace(PATTERN_REGEX, (match) => {
|
|
2517
|
-
const type = match.charAt(0);
|
|
2518
|
-
const length = match.length;
|
|
2519
|
-
return formatType(type, length, date, locale, timeZone) || match;
|
|
2520
|
-
});
|
|
2521
|
-
}).join("");
|
|
2522
|
-
}
|
|
2523
|
-
function parseTimeInstant(dateString, pattern, base, config = {}) {
|
|
2524
|
-
const parsed = parseTimeInstantComponents(dateString, pattern, config);
|
|
2525
|
-
const params = {
|
|
2526
|
-
...base.toParameters(),
|
|
2527
|
-
...parsed
|
|
2528
|
-
};
|
|
2529
|
-
return TimeInstant.fromParameters(params);
|
|
2530
|
-
}
|
|
2531
|
-
function parseTimeInstantComponents(dateString, pattern, config = {}) {
|
|
2532
|
-
let regexPattern = pattern;
|
|
2533
|
-
const tokens = [];
|
|
2534
|
-
let position = 0;
|
|
2535
|
-
const parts = pattern.split(ESCAPE_REGEX).filter((sub) => sub !== void 0);
|
|
2536
|
-
regexPattern = parts.map((sub, index) => {
|
|
2537
|
-
if (index % 2 !== 0) {
|
|
2538
|
-
return sub.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2539
|
-
}
|
|
2540
|
-
return sub.replace(PATTERN_REGEX, (match) => {
|
|
2541
|
-
const type = match.charAt(0);
|
|
2542
|
-
tokens.push({ type, length: match.length, position: position++ });
|
|
2543
|
-
switch (type) {
|
|
2544
|
-
case "y":
|
|
2545
|
-
return match.length === 2 ? "(\\d{2})" : "(\\d{4})";
|
|
2546
|
-
case "M":
|
|
2547
|
-
if (match.length === 1) return "(\\d{1,2})";
|
|
2548
|
-
if (match.length === 2) return "(\\d{2})";
|
|
2549
|
-
if (match.length === 3) return "([A-Za-z.]{1,7})";
|
|
2550
|
-
return "([A-Za-z]+)";
|
|
2551
|
-
case "d":
|
|
2552
|
-
return match.length === 1 ? "(\\d{1,2})" : "(\\d{2})";
|
|
2553
|
-
case "H":
|
|
2554
|
-
case "h":
|
|
2555
|
-
return match.length === 1 ? "(\\d{1,2})" : "(\\d{2})";
|
|
2556
|
-
case "m":
|
|
2557
|
-
case "s":
|
|
2558
|
-
return match.length === 1 ? "(\\d{1,2})" : "(\\d{2})";
|
|
2559
|
-
case "S":
|
|
2560
|
-
return `(\\d{${match.length}})`;
|
|
2561
|
-
case "a":
|
|
2562
|
-
return "([aApP][mM])";
|
|
2563
|
-
case "D":
|
|
2564
|
-
if (match.length === 1) return "([A-Za-z])";
|
|
2565
|
-
if (match.length === 2) return "([A-Za-z]{3})";
|
|
2566
|
-
return "([A-Za-z]+)";
|
|
2567
|
-
case "G":
|
|
2568
|
-
if (match.length === 1) return "([A-Za-z])";
|
|
2569
|
-
if (match.length === 2) return "([A-Za-z]{2})";
|
|
2570
|
-
return "([A-Za-z\\s]+)";
|
|
2571
|
-
case "Z":
|
|
2572
|
-
return match.length === 1 ? "([A-Za-z0-9+\\-:]+)" : "([A-Za-z\\s]+)";
|
|
2573
|
-
case "P":
|
|
2574
|
-
return "([A-Za-z\\s]+)";
|
|
2575
|
-
default:
|
|
2576
|
-
return match;
|
|
2577
|
-
}
|
|
2578
|
-
});
|
|
2579
|
-
}).join("");
|
|
2580
|
-
const regex = new RegExp("^" + regexPattern + "$");
|
|
2581
|
-
const matches = dateString.match(regex);
|
|
2582
|
-
if (!matches) {
|
|
2583
|
-
throw new Error(`Date string "${dateString}" does not match pattern "${pattern}"`);
|
|
2584
|
-
}
|
|
2585
|
-
const result = {};
|
|
2586
|
-
const locale = config.locale || "en";
|
|
2587
|
-
let isPM = false;
|
|
2588
|
-
let is12Hour = false;
|
|
2589
|
-
let hourValue;
|
|
2590
|
-
tokens.forEach((token, index) => {
|
|
2591
|
-
const value = matches[index + 1];
|
|
2592
|
-
switch (token.type) {
|
|
2593
|
-
case "y":
|
|
2594
|
-
if (token.length === 2) {
|
|
2595
|
-
const shortYear = parseInt(value, 10);
|
|
2596
|
-
result.year = shortYear < 50 ? 2e3 + shortYear : 1900 + shortYear;
|
|
2597
|
-
} else {
|
|
2598
|
-
result.year = parseInt(value, 10);
|
|
2599
|
-
}
|
|
2600
|
-
break;
|
|
2601
|
-
case "M":
|
|
2602
|
-
switch (token.length) {
|
|
2603
|
-
case 1:
|
|
2604
|
-
case 2:
|
|
2605
|
-
result.month = parseInt(value, 10);
|
|
2606
|
-
break;
|
|
2607
|
-
case 3: {
|
|
2608
|
-
const normalizedValue = normalizeMonthName(value);
|
|
2609
|
-
const monthIndex = getMonthNames(locale).short.findIndex(
|
|
2610
|
-
(name) => name.toLowerCase() === normalizedValue.toLowerCase()
|
|
2611
|
-
);
|
|
2612
|
-
if (monthIndex === -1)
|
|
2613
|
-
throw new Error(`Invalid short month name in date string: ${dateString}`);
|
|
2614
|
-
result.month = monthIndex + 1;
|
|
2615
|
-
break;
|
|
2616
|
-
}
|
|
2617
|
-
case 4: {
|
|
2618
|
-
const normalizedValue = normalizeMonthName(value);
|
|
2619
|
-
const monthIndex = getMonthNames(locale).long.findIndex(
|
|
2620
|
-
(name) => name.toLowerCase() === normalizedValue.toLowerCase()
|
|
2621
|
-
);
|
|
2622
|
-
if (monthIndex === -1)
|
|
2623
|
-
throw new Error(`Invalid full month name in date string: ${dateString}`);
|
|
2624
|
-
result.month = monthIndex + 1;
|
|
2625
|
-
break;
|
|
2626
|
-
}
|
|
2627
|
-
default:
|
|
2628
|
-
throw new Error(`Invalid month pattern: ${token}`);
|
|
2629
|
-
}
|
|
2630
|
-
break;
|
|
2631
|
-
case "d":
|
|
2632
|
-
result.date = parseInt(value, 10);
|
|
2633
|
-
break;
|
|
2634
|
-
case "H":
|
|
2635
|
-
result.hours = parseInt(value, 10);
|
|
2636
|
-
break;
|
|
2637
|
-
case "h":
|
|
2638
|
-
hourValue = parseInt(value, 10);
|
|
2639
|
-
is12Hour = true;
|
|
2640
|
-
break;
|
|
2641
|
-
case "m":
|
|
2642
|
-
result.minutes = parseInt(value, 10);
|
|
2643
|
-
break;
|
|
2644
|
-
case "s":
|
|
2645
|
-
result.seconds = parseInt(value, 10);
|
|
2646
|
-
break;
|
|
2647
|
-
case "S":
|
|
2648
|
-
let ms = parseInt(value, 10);
|
|
2649
|
-
if (token.length === 1) ms *= 100;
|
|
2650
|
-
else if (token.length === 2) ms *= 10;
|
|
2651
|
-
result.milliseconds = ms;
|
|
2652
|
-
break;
|
|
2653
|
-
case "a":
|
|
2654
|
-
isPM = value.toLowerCase().includes("p");
|
|
2655
|
-
break;
|
|
2656
|
-
}
|
|
2657
|
-
});
|
|
2658
|
-
if (is12Hour && hourValue !== void 0) {
|
|
2659
|
-
if (isPM && hourValue < 12) {
|
|
2660
|
-
result.hours = hourValue + 12;
|
|
2661
|
-
} else if (!isPM && hourValue === 12) {
|
|
2662
|
-
result.hours = 0;
|
|
2663
|
-
} else {
|
|
2664
|
-
result.hours = hourValue;
|
|
2665
|
-
}
|
|
2666
|
-
}
|
|
2667
|
-
if (typeof result.year === "number" && !isValidYear(result.year)) {
|
|
2668
|
-
throw new Error(`Invalid year in date string: ${dateString}`);
|
|
2669
|
-
}
|
|
2670
|
-
if (typeof result.month === "number" && !isValidMonth(result.month)) {
|
|
2671
|
-
throw new Error(`Invalid month in date string: ${dateString}`);
|
|
2672
|
-
}
|
|
2673
|
-
if (typeof result.date === "number" && !isValidDayOfMonth(result.date)) {
|
|
2674
|
-
throw new Error(`Invalid day in date string: ${dateString}`);
|
|
2675
|
-
}
|
|
2676
|
-
if (typeof result.hours === "number" && !isValidHour(result.hours)) {
|
|
2677
|
-
throw new Error(`Invalid hour in date string: ${dateString}`);
|
|
2678
|
-
}
|
|
2679
|
-
if (typeof result.minutes === "number" && !isValidMinute(result.minutes)) {
|
|
2680
|
-
throw new Error(`Invalid minute in date string: ${dateString}`);
|
|
2681
|
-
}
|
|
2682
|
-
if (typeof result.seconds === "number" && !isValidSecond(result.seconds)) {
|
|
2683
|
-
throw new Error(`Invalid second in date string: ${dateString}`);
|
|
2684
|
-
}
|
|
2685
|
-
if (typeof result.milliseconds === "number" && !isValidMillisecond(result.milliseconds)) {
|
|
2686
|
-
throw new Error(`Invalid millisecond in date string: ${dateString}`);
|
|
2687
|
-
}
|
|
2688
|
-
return result;
|
|
2689
|
-
}
|
|
2690
|
-
|
|
2691
|
-
// src/Logger.ts
|
|
2692
|
-
var LEVELS = ["trace", "log", "debug", "info", "warn", "error"];
|
|
2693
|
-
var timestamp = () => TimeInstant_default.now().format("HH:mm:ss");
|
|
2694
|
-
var Logger = class {
|
|
2695
|
-
constructor(name, args) {
|
|
2696
|
-
this.name = name;
|
|
2697
|
-
this.console = args?.console ?? globalThis.console;
|
|
2698
|
-
this.enabled = args?.enabled ?? true;
|
|
2699
|
-
this.minLevel = LEVELS.indexOf((args?.minLevel ?? "DEBUG").toLowerCase());
|
|
2700
|
-
}
|
|
2701
|
-
enabled = true;
|
|
2702
|
-
console;
|
|
2703
|
-
minLevel;
|
|
2704
|
-
get trace() {
|
|
2705
|
-
return this.logWrap("trace");
|
|
2706
|
-
}
|
|
2707
|
-
get log() {
|
|
2708
|
-
return this.logWrap("log");
|
|
2709
|
-
}
|
|
2710
|
-
get debug() {
|
|
2711
|
-
return this.logWrap("debug");
|
|
2712
|
-
}
|
|
2713
|
-
get info() {
|
|
2714
|
-
return this.logWrap("info");
|
|
2715
|
-
}
|
|
2716
|
-
get warn() {
|
|
2717
|
-
return this.logWrap("warn");
|
|
2718
|
-
}
|
|
2719
|
-
get error() {
|
|
2720
|
-
return this.logWrap("error");
|
|
2721
|
-
}
|
|
2722
|
-
logWrap(methodName) {
|
|
2723
|
-
if (!this.enabled || !this.console || LEVELS.indexOf(methodName) < this.minLevel)
|
|
2724
|
-
return noop;
|
|
2725
|
-
return this.console[methodName].bind(this.console, timestamp(), padRight(methodName.toUpperCase(), 5, " "), this.name);
|
|
2726
|
-
}
|
|
2727
|
-
};
|
|
2728
|
-
|
|
2729
|
-
// src/sorting/Sorter.ts
|
|
2730
|
-
var defaultCompareValuesOptions = {
|
|
2731
|
-
nullsFirst: false
|
|
2732
|
-
};
|
|
2733
|
-
var defaultCompareFunctionOptions = {
|
|
2734
|
-
...defaultCompareValuesOptions,
|
|
2735
|
-
direction: "ASC"
|
|
2736
|
-
};
|
|
2737
|
-
var naturalOrderComparison = (a, b) => a < b ? -1 : 1;
|
|
2738
|
-
function compareValues(a, b, cmp, { nullsFirst }) {
|
|
2739
|
-
if (a === b)
|
|
2740
|
-
return 0;
|
|
2741
|
-
const nullA = isNullOrUndefined(a);
|
|
2742
|
-
const nullB = isNullOrUndefined(b);
|
|
2743
|
-
if (nullA && nullB)
|
|
2744
|
-
return 0;
|
|
2745
|
-
if (nullA !== nullB)
|
|
2746
|
-
return nullA === nullsFirst ? -1 : 1;
|
|
2747
|
-
return cmp(a, b);
|
|
2748
|
-
}
|
|
2749
|
-
function compareFunction(fn, cmp, { nullsFirst, direction }) {
|
|
2750
|
-
return (a, b) => applyDirection(compareValues(fn(a), fn(b), cmp, { nullsFirst }), direction);
|
|
2751
|
-
}
|
|
2752
|
-
function applyDirection(res, direction) {
|
|
2753
|
-
return res * (direction === "ASC" ? 1 : -1);
|
|
2754
|
-
}
|
|
2755
|
-
function combine(f, g) {
|
|
2756
|
-
return (t) => g(f(t));
|
|
2757
|
-
}
|
|
2758
|
-
function reverse2(fn) {
|
|
2759
|
-
return (a, b) => fn(a, b) * -1;
|
|
2760
|
-
}
|
|
2761
|
-
var chain = (fns, cmpFn) => {
|
|
2762
|
-
return doCreateWithFunctions([...fns, cmpFn]);
|
|
2763
|
-
};
|
|
2764
|
-
var transformAndChain = (fns, transform, cmpFn) => {
|
|
2765
|
-
const fn = (a, b) => cmpFn(transform(a), transform(b));
|
|
2766
|
-
return chain(fns, fn);
|
|
2767
|
-
};
|
|
2768
|
-
var compareStrings = (fns, transform, options = {}) => {
|
|
2769
|
-
const { nullsFirst, direction, ignoreCase } = { ...defaultStringComparisonOptions, ...options };
|
|
2770
|
-
if (ignoreCase)
|
|
2771
|
-
transform = combine(transform, (t) => t.toLowerCase());
|
|
2772
|
-
return chain(fns, compareFunction(transform, naturalOrderComparison, { nullsFirst, direction }));
|
|
2773
|
-
};
|
|
2774
|
-
var compareNumbers = (fns, transform, options = {}) => {
|
|
2775
|
-
const { nullsFirst, direction } = { ...defaultNumberComparisonOptions, ...options };
|
|
2776
|
-
return chain(fns, compareFunction(transform, naturalOrderComparison, { nullsFirst, direction }));
|
|
2777
|
-
};
|
|
2778
|
-
var compareDates = (fns, transform, options = {}) => {
|
|
2779
|
-
const { nullsFirst, direction } = { ...defaultDateComparisonOptions, ...options };
|
|
2780
|
-
return chain(fns, compareFunction(transform, naturalOrderComparison, { nullsFirst, direction }));
|
|
2781
|
-
};
|
|
2782
|
-
var compareTimeInstants = (fns, transform, options = {}) => {
|
|
2783
|
-
const { nullsFirst, direction } = { ...defaultTimeInstantComparisonOptions, ...options };
|
|
2784
|
-
return chain(fns, compareFunction(transform, (a, b) => a.isBefore(b) ? -1 : 1, { nullsFirst, direction }));
|
|
2785
|
-
};
|
|
2786
|
-
var compareTimeDurations = (fns, transform, options = {}) => {
|
|
2787
|
-
const { nullsFirst, direction } = { ...defaultTimeDurationComparisonOptions, ...options };
|
|
2788
|
-
return chain(fns, compareFunction(transform, (a, b) => a.isLessThan(b) ? -1 : 1, { nullsFirst, direction }));
|
|
2789
|
-
};
|
|
2790
|
-
var compareBooleans = (fns, transform, options) => {
|
|
2791
|
-
const { nullsFirst, truesFirst } = { ...defaultBooleanComparisonOptions, ...options };
|
|
2792
|
-
return chain(fns, compareFunction(transform, naturalOrderComparison, { nullsFirst, direction: truesFirst ? "DESC" : "ASC" }));
|
|
2793
|
-
};
|
|
2794
|
-
var prioritizeSet = (fns, transform, set, reversed = false) => {
|
|
2795
|
-
return compareBooleans(fns, (t) => {
|
|
2796
|
-
const r = transform(t);
|
|
2797
|
-
return isDefined(r) && set.includes(r);
|
|
2798
|
-
}, { truesFirst: !reversed, nullsFirst: false });
|
|
2799
|
-
};
|
|
2800
|
-
var prioritizeArray = (fns, transform, arr, reversed = false) => {
|
|
2801
|
-
return compareNumbers(fns, (t) => {
|
|
2802
|
-
const r = transform(t);
|
|
2803
|
-
if (!isDefined(r)) return Number.MAX_VALUE;
|
|
2804
|
-
const indexOf = arr.indexOf(r);
|
|
2805
|
-
return indexOf === -1 ? Number.MAX_VALUE : indexOf;
|
|
2806
|
-
}, { direction: reversed ? "DESC" : "ASC", nullsFirst: false });
|
|
2807
|
-
};
|
|
2808
|
-
var next = (fns, transform) => {
|
|
2809
|
-
const using = (transformToX) => {
|
|
2810
|
-
return next(fns, combine(transform, transformToX));
|
|
2811
|
-
};
|
|
2812
|
-
const retAsChainable = {
|
|
2813
|
-
chain(chain2) {
|
|
2814
|
-
return transformAndChain(fns, transform, chain2.compare);
|
|
2815
|
-
},
|
|
2816
|
-
chainFunction(fn) {
|
|
2817
|
-
return transformAndChain(fns, transform, fn);
|
|
2818
|
-
}
|
|
2819
|
-
};
|
|
2820
|
-
const retAsUsing = {
|
|
2821
|
-
using
|
|
2822
|
-
};
|
|
2823
|
-
const retAsPrioritizable = {
|
|
2824
|
-
prioritizing(arr, _options = {}) {
|
|
2825
|
-
return prioritizeArray(fns, transform, arr);
|
|
2826
|
-
},
|
|
2827
|
-
deprioritizing(arr, _options = {}) {
|
|
2828
|
-
return prioritizeArray(fns, transform, arr, true);
|
|
2829
|
-
},
|
|
2830
|
-
prioritizingWithEqualWeight(arr, _options = {}) {
|
|
2831
|
-
return prioritizeSet(fns, transform, arr);
|
|
2832
|
-
},
|
|
2833
|
-
deprioritizingWithEqualWeight(arr, _options = {}) {
|
|
2834
|
-
return prioritizeSet(fns, transform, arr, true);
|
|
2835
|
-
}
|
|
2836
|
-
};
|
|
2837
|
-
const retForRecords = {
|
|
2838
|
-
...retAsUsing,
|
|
2839
|
-
usingStrings: (field) => using((t) => t[field]),
|
|
2840
|
-
usingNumbers: (field) => using((t) => t[field]),
|
|
2841
|
-
usingBooleans: (field) => using((t) => t[field]),
|
|
2842
|
-
usingDates: (field) => using((t) => t[field]),
|
|
2843
|
-
usingTimeInstant: (field) => using((t) => t[field]),
|
|
2844
|
-
usingTimeDuration: (field) => using((t) => t[field])
|
|
2845
|
-
};
|
|
2846
|
-
const retForNumbers = {
|
|
2847
|
-
...retAsUsing,
|
|
2848
|
-
inAscendingOrder(opts = {}) {
|
|
2849
|
-
return compareNumbers(fns, transform, { direction: "ASC", ...opts });
|
|
2850
|
-
},
|
|
2851
|
-
inDescendingOrder(opts = {}) {
|
|
2852
|
-
return compareNumbers(fns, transform, { direction: "DESC", ...opts });
|
|
2853
|
-
}
|
|
2854
|
-
};
|
|
2855
|
-
const retForStringsV1 = {
|
|
2856
|
-
...retAsUsing,
|
|
2857
|
-
inLexographicalOrder(opts = {}) {
|
|
2858
|
-
return compareStrings(fns, transform, { direction: "ASC", ignoreCase: false, ...opts });
|
|
2859
|
-
},
|
|
2860
|
-
inLexographicalOrderIgnoringCase(opts = {}) {
|
|
2861
|
-
return compareStrings(fns, transform, { direction: "ASC", ignoreCase: true, ...opts });
|
|
2862
|
-
},
|
|
2863
|
-
inReverseLexographicalOrder(opts = {}) {
|
|
2864
|
-
return compareStrings(fns, transform, { direction: "DESC", ignoreCase: false, ...opts });
|
|
2865
|
-
},
|
|
2866
|
-
inReverseLexographicalOrderIgnoringCase(opts = {}) {
|
|
2867
|
-
return compareStrings(fns, transform, { direction: "DESC", ignoreCase: true, ...opts });
|
|
2868
|
-
}
|
|
2869
|
-
};
|
|
2870
|
-
const retForStrings = {
|
|
2871
|
-
...retAsUsing,
|
|
2872
|
-
...retForStringsV1,
|
|
2873
|
-
inLexicographicalOrder(opts = {}) {
|
|
2874
|
-
return compareStrings(fns, transform, { direction: "ASC", ignoreCase: false, ...opts });
|
|
2875
|
-
},
|
|
2876
|
-
inLexicographicalOrderIgnoringCase(opts = {}) {
|
|
2877
|
-
return compareStrings(fns, transform, { direction: "ASC", ignoreCase: true, ...opts });
|
|
2878
|
-
},
|
|
2879
|
-
inReverseLexicographicalOrder(opts = {}) {
|
|
2880
|
-
return compareStrings(fns, transform, { direction: "DESC", ignoreCase: false, ...opts });
|
|
2881
|
-
},
|
|
2882
|
-
inReverseLexicographicalOrderIgnoringCase(opts = {}) {
|
|
2883
|
-
return compareStrings(fns, transform, { direction: "DESC", ignoreCase: true, ...opts });
|
|
2884
|
-
}
|
|
2885
|
-
};
|
|
2886
|
-
const retForBooleans = {
|
|
2887
|
-
...retAsUsing,
|
|
2888
|
-
truesFirst(opts = {}) {
|
|
2889
|
-
return compareBooleans(fns, transform, { truesFirst: true, ...opts });
|
|
2890
|
-
},
|
|
2891
|
-
falsesFirst(opts = {}) {
|
|
2892
|
-
return compareBooleans(fns, transform, { truesFirst: false, ...opts });
|
|
2893
|
-
}
|
|
2894
|
-
};
|
|
2895
|
-
const retForDates = {
|
|
2896
|
-
...retAsUsing,
|
|
2897
|
-
oldestDateFirst(opts = {}) {
|
|
2898
|
-
return compareDates(fns, transform, { direction: "ASC", ...opts });
|
|
2899
|
-
},
|
|
2900
|
-
newestDateFirst(opts = {}) {
|
|
2901
|
-
return compareDates(fns, transform, { direction: "DESC", ...opts });
|
|
2902
|
-
}
|
|
2903
|
-
};
|
|
2904
|
-
const retForInstants = {
|
|
2905
|
-
...retAsUsing,
|
|
2906
|
-
oldestFirst(opts = {}) {
|
|
2907
|
-
return compareTimeInstants(fns, transform, { direction: "ASC", ...opts });
|
|
2908
|
-
},
|
|
2909
|
-
newestFirst(opts = {}) {
|
|
2910
|
-
return compareTimeInstants(fns, transform, { direction: "DESC", ...opts });
|
|
2911
|
-
}
|
|
2912
|
-
};
|
|
2913
|
-
const retForDurations = {
|
|
2914
|
-
...retAsUsing,
|
|
2915
|
-
longestFirst(opts = {}) {
|
|
2916
|
-
return compareTimeDurations(fns, transform, { direction: "DESC", ...opts });
|
|
2917
|
-
},
|
|
2918
|
-
shortestFirst(opts = {}) {
|
|
2919
|
-
return compareTimeDurations(fns, transform, { direction: "ASC", ...opts });
|
|
2920
|
-
}
|
|
2921
|
-
};
|
|
2922
|
-
return {
|
|
2923
|
-
...retAsPrioritizable,
|
|
2924
|
-
...retAsChainable,
|
|
2925
|
-
...retForRecords,
|
|
2926
|
-
...retForNumbers,
|
|
2927
|
-
...retForStrings,
|
|
2928
|
-
...retForBooleans,
|
|
2929
|
-
...retForDates,
|
|
2930
|
-
...retForInstants,
|
|
2931
|
-
...retForDurations
|
|
2932
|
-
};
|
|
2933
|
-
};
|
|
2934
|
-
var createComparisonFunction = (fns) => {
|
|
2935
|
-
return (a, b) => {
|
|
2936
|
-
let cmp = 0, i = 0;
|
|
2937
|
-
while (i < fns.length && cmp === 0) {
|
|
2938
|
-
cmp = fns[i++](a, b);
|
|
2939
|
-
}
|
|
2940
|
-
return cmp;
|
|
2941
|
-
};
|
|
2942
|
-
};
|
|
2943
|
-
var compare = (fns, a, b) => {
|
|
2944
|
-
return createComparisonFunction(fns)(a, b);
|
|
2945
|
-
};
|
|
2946
|
-
var sort = (fns, arr) => {
|
|
2947
|
-
const comparisonFn = createComparisonFunction(fns);
|
|
2948
|
-
return [...arr].sort(comparisonFn);
|
|
2949
|
-
};
|
|
2950
|
-
var top = (fns, arr, n) => {
|
|
2951
|
-
return sort(fns, arr).slice(0, Math.max(0, n));
|
|
2952
|
-
};
|
|
2953
|
-
var bottom = (fns, arr, n) => {
|
|
2954
|
-
return sort(fns, arr).slice(-Math.max(0, n)).reverse();
|
|
2955
|
-
};
|
|
2956
|
-
var first2 = (fns, arr) => {
|
|
2957
|
-
return arr.length ? top(fns, arr, 1)[0] : null;
|
|
2958
|
-
};
|
|
2959
|
-
var last2 = (fns, arr) => {
|
|
2960
|
-
return arr.length ? bottom(fns, arr, 1)[0] : null;
|
|
2961
|
-
};
|
|
2962
|
-
function doCreateEmpty() {
|
|
2963
|
-
const base = next([], (x) => x);
|
|
2964
|
-
const ret = {
|
|
2965
|
-
...base,
|
|
2966
|
-
usingFunction(fn) {
|
|
2967
|
-
return doCreateWithFunctions([fn]);
|
|
2968
|
-
}
|
|
2969
|
-
};
|
|
2970
|
-
return ret;
|
|
2971
|
-
}
|
|
2972
|
-
function doCreateWithFunctions(fns) {
|
|
2973
|
-
return {
|
|
2974
|
-
get then() {
|
|
2975
|
-
return next(fns, identity);
|
|
2976
|
-
},
|
|
2977
|
-
reversed() {
|
|
2978
|
-
return doCreateWithFunctions([...fns.map(reverse2)]);
|
|
2979
|
-
},
|
|
2980
|
-
compare: (a, b) => {
|
|
2981
|
-
return compare(fns, a, b);
|
|
2982
|
-
},
|
|
2983
|
-
sort: (arr) => {
|
|
2984
|
-
return sort(fns, arr);
|
|
2985
|
-
},
|
|
2986
|
-
top: (arr, n) => {
|
|
2987
|
-
return top(fns, arr, n);
|
|
2988
|
-
},
|
|
2989
|
-
bottom: (arr, n) => {
|
|
2990
|
-
return bottom(fns, arr, n);
|
|
2991
|
-
},
|
|
2992
|
-
first: (arr) => {
|
|
2993
|
-
return first2(fns, arr);
|
|
2994
|
-
},
|
|
2995
|
-
last: (arr) => {
|
|
2996
|
-
return last2(fns, arr);
|
|
2997
|
-
}
|
|
2998
|
-
};
|
|
2999
|
-
}
|
|
3000
|
-
var defaultStringComparisonOptions = { ...defaultCompareFunctionOptions, ignoreCase: false };
|
|
3001
|
-
var defaultNumberComparisonOptions = { ...defaultCompareFunctionOptions };
|
|
3002
|
-
var defaultDateComparisonOptions = { ...defaultCompareFunctionOptions };
|
|
3003
|
-
var defaultTimeInstantComparisonOptions = { ...defaultCompareFunctionOptions };
|
|
3004
|
-
var defaultTimeDurationComparisonOptions = { ...defaultCompareFunctionOptions };
|
|
3005
|
-
var defaultBooleanComparisonOptions = { ...defaultCompareValuesOptions };
|
|
3006
|
-
var Sorter = {
|
|
3007
|
-
createFor: (_template) => doCreateEmpty(),
|
|
3008
|
-
sort: (arr, builder) => builder(doCreateEmpty()).sort(arr),
|
|
3009
|
-
top: (arr, n, builder) => builder(doCreateEmpty()).top(arr, n),
|
|
3010
|
-
bottom: (arr, n, builder) => builder(doCreateEmpty()).bottom(arr, n),
|
|
3011
|
-
first: (arr, builder) => builder(doCreateEmpty()).first(arr),
|
|
3012
|
-
last: (arr, builder) => builder(doCreateEmpty()).last(arr)
|
|
3013
|
-
};
|
|
3014
|
-
|
|
3015
|
-
// src/time/RandomTimeDuration.ts
|
|
3016
|
-
function randomize(unit) {
|
|
3017
|
-
return (a, b) => {
|
|
3018
|
-
return TimeDuration_default.fromMs(randomNumberInInterval(unit.toMs(a), unit.toMs(b)));
|
|
3019
|
-
};
|
|
3020
|
-
}
|
|
3021
|
-
var RandomTimeDuration = class {
|
|
3022
|
-
constructor() {
|
|
3023
|
-
}
|
|
3024
|
-
static ms = randomize(TimeUnit.MILLISECONDS);
|
|
3025
|
-
static seconds = randomize(TimeUnit.SECONDS);
|
|
3026
|
-
static minutes = randomize(TimeUnit.MINUTES);
|
|
3027
|
-
static hours = randomize(TimeUnit.HOURS);
|
|
3028
|
-
static days = randomize(TimeUnit.DAYS);
|
|
3029
|
-
};
|
|
3030
|
-
|
|
3031
|
-
// src/time/TimeFrequency.ts
|
|
3032
|
-
var TimeFrequency = class _TimeFrequency {
|
|
3033
|
-
constructor(times, period) {
|
|
3034
|
-
this.times = times;
|
|
3035
|
-
this.period = period;
|
|
3036
|
-
if (isNaN(times) || times <= 0)
|
|
3037
|
-
throw new Error();
|
|
3038
|
-
if (period.isEmpty())
|
|
3039
|
-
throw new Error();
|
|
3040
|
-
}
|
|
3041
|
-
get frequency() {
|
|
3042
|
-
return this.period.divideBy(this.times);
|
|
3043
|
-
}
|
|
3044
|
-
static onceEvery(period) {
|
|
3045
|
-
return new _TimeFrequency(1, period);
|
|
3046
|
-
}
|
|
3047
|
-
static twiceEvery(period) {
|
|
3048
|
-
return new _TimeFrequency(2, period);
|
|
3049
|
-
}
|
|
3050
|
-
};
|
|
3051
|
-
|
|
3052
|
-
// src/time/TimeRange.ts
|
|
3053
|
-
var TimeRange = class _TimeRange {
|
|
3054
|
-
constructor(start, duration) {
|
|
3055
|
-
this.start = start;
|
|
3056
|
-
this.duration = duration;
|
|
3057
|
-
this.end = start.addDuration(duration);
|
|
3058
|
-
}
|
|
3059
|
-
end;
|
|
3060
|
-
static fromInstants(a, b) {
|
|
3061
|
-
const lowest = TimeInstant_default.lowest(a, b);
|
|
3062
|
-
const highest = TimeInstant_default.highest(a, b);
|
|
3063
|
-
return new _TimeRange(lowest, lowest.distanceFrom(highest));
|
|
3064
|
-
}
|
|
3065
|
-
static fromInstantWithDuration(a, d) {
|
|
3066
|
-
return new _TimeRange(a, d);
|
|
3067
|
-
}
|
|
3068
|
-
static fromNowWithDuration(d) {
|
|
3069
|
-
return new _TimeRange(TimeInstant_default.now(), d);
|
|
3070
|
-
}
|
|
3071
|
-
};
|
|
3072
|
-
|
|
3073
|
-
// src/upgrade/errors.ts
|
|
3074
|
-
var UnavailableUpgradeError = class extends Error {
|
|
3075
|
-
constructor(data, from, to) {
|
|
3076
|
-
super(`No transition found to upgrade data from version ${from} to version ${to}.`);
|
|
3077
|
-
this.data = data;
|
|
3078
|
-
this.from = from;
|
|
3079
|
-
this.to = to;
|
|
3080
|
-
}
|
|
3081
|
-
};
|
|
3082
|
-
var EmptyUpgradeError = class extends Error {
|
|
3083
|
-
constructor(data, from, to) {
|
|
3084
|
-
super(`Transition from version ${from} to version ${to} resulted in empty data`);
|
|
3085
|
-
this.data = data;
|
|
3086
|
-
this.from = from;
|
|
3087
|
-
this.to = to;
|
|
3088
|
-
}
|
|
3089
|
-
};
|
|
3090
|
-
|
|
3091
|
-
// src/upgrade/getTransitionsPath.ts
|
|
3092
|
-
var DEBUG_ENABLED = false;
|
|
3093
|
-
function getTransitionsPath(matrix, from, to) {
|
|
3094
|
-
return internalGetTransitionsPath(matrix, from, to, 0, DEBUG_ENABLED);
|
|
3095
|
-
}
|
|
3096
|
-
function internalGetTransitionsPath(matrix, from, to, depth, debug) {
|
|
3097
|
-
debugLog(debug, depth, from, to, "Search started.");
|
|
3098
|
-
const transitionsTo = matrix[to];
|
|
3099
|
-
if (!transitionsTo) {
|
|
3100
|
-
debugLog(debug, depth, from, to, "No transitions available to this version");
|
|
3101
|
-
return null;
|
|
3102
|
-
}
|
|
3103
|
-
const exactTransition = transitionsTo[from];
|
|
3104
|
-
if (exactTransition) {
|
|
3105
|
-
debugLog(debug, depth, from, to, "Found exact transition.");
|
|
3106
|
-
return [exactTransition];
|
|
3107
|
-
}
|
|
3108
|
-
const keys = Object.keys(transitionsTo);
|
|
3109
|
-
return keys.reduce((curBestPath, transitionKey) => {
|
|
3110
|
-
const transition = transitionsTo[transitionKey];
|
|
3111
|
-
const path = internalGetTransitionsPath(matrix, from, transition.from, depth + 1, debug);
|
|
3112
|
-
if (path === null) {
|
|
3113
|
-
debugLog(debug, depth + 1, from, transition.from, "No path found.");
|
|
3114
|
-
return curBestPath;
|
|
3115
|
-
} else {
|
|
3116
|
-
if (curBestPath === null || curBestPath.length > path.length + 1) {
|
|
3117
|
-
debugLog(debug, depth + 1, from, transition.from, "New best path found: " + printTransitions(path));
|
|
3118
|
-
return [...path, transition];
|
|
3119
|
-
} else {
|
|
3120
|
-
return curBestPath;
|
|
3121
|
-
}
|
|
3122
|
-
}
|
|
3123
|
-
}, null);
|
|
3124
|
-
}
|
|
3125
|
-
function debugLog(enabled, depth, from, to, message) {
|
|
3126
|
-
if (!enabled)
|
|
3127
|
-
return;
|
|
3128
|
-
console.info(
|
|
3129
|
-
"#".repeat(depth + 1),
|
|
3130
|
-
from + " -> " + to,
|
|
3131
|
-
message
|
|
3132
|
-
);
|
|
3133
|
-
}
|
|
3134
|
-
function printTransitions(transitions) {
|
|
3135
|
-
return transitions.reduce((ret, cur) => {
|
|
3136
|
-
return ret + " -> " + cur.to;
|
|
3137
|
-
}, "" + transitions[0].from);
|
|
3138
|
-
}
|
|
3139
|
-
|
|
3140
|
-
// src/upgrade/DataUpgrader.ts
|
|
3141
|
-
var VERSION_FIELD = "$version";
|
|
3142
|
-
var DataUpgrader = class _DataUpgrader {
|
|
3143
|
-
constructor(latestVersion) {
|
|
3144
|
-
this.latestVersion = latestVersion;
|
|
3145
|
-
}
|
|
3146
|
-
transitions = {};
|
|
3147
|
-
addTransition(from, to, apply) {
|
|
3148
|
-
if (from === void 0 || from < 0)
|
|
3149
|
-
throw new Error(`Invalid argument from, non-negative number expected, got: ${from}`);
|
|
3150
|
-
if (to === void 0 || to <= 0)
|
|
3151
|
-
throw new Error(`Invalid argument to, positive number expected, got: ${to}`);
|
|
3152
|
-
if (to <= from)
|
|
3153
|
-
throw new Error(`Invalid argument to, expected number greater than ${from}, got: ${to}`);
|
|
3154
|
-
if (to > this.latestVersion)
|
|
3155
|
-
throw new Error(`Invalid argument to, expected number lower than ${this.latestVersion}, got: ${to}`);
|
|
3156
|
-
if (this.transitions[to]?.[from])
|
|
3157
|
-
throw new Error(`Invalid transition arguments, duplicated transition found from ${from} to ${to}`);
|
|
3158
|
-
const toMatrix = this.transitions[to] ?? {};
|
|
3159
|
-
toMatrix[from] = { from, to, apply };
|
|
3160
|
-
this.transitions[to] = toMatrix;
|
|
3161
|
-
return this;
|
|
3162
|
-
}
|
|
3163
|
-
async upgrade(data) {
|
|
3164
|
-
if (!data || typeof data !== "object")
|
|
3165
|
-
throw new Error(`Invalid argument data, object expected, got: ${typeof data}.`);
|
|
3166
|
-
if (data.$version === null || data.$version === void 0 || typeof data.$version !== "number")
|
|
3167
|
-
throw new Error(`Invalid argument data, version metadata required, got: ${data.$version}`);
|
|
3168
|
-
if (data.$version > this.latestVersion)
|
|
3169
|
-
throw new Error(`Invalid argument data, version metadata is in the future: ${data.$version}, current: ${this.latestVersion}`);
|
|
3170
|
-
if (this.isLatestVersion(data))
|
|
3171
|
-
return data;
|
|
3172
|
-
const from = data.$version;
|
|
3173
|
-
const to = this.latestVersion;
|
|
3174
|
-
const path = getTransitionsPath(this.transitions, from, to);
|
|
3175
|
-
if (path === null)
|
|
3176
|
-
throw new UnavailableUpgradeError(data, from, to);
|
|
3177
|
-
let current = jsonCloneDeep(data);
|
|
3178
|
-
for (const transition of path) {
|
|
3179
|
-
const next2 = await transition.apply(current);
|
|
3180
|
-
if (next2 === null || next2 === void 0)
|
|
3181
|
-
throw new EmptyUpgradeError(current, from, to);
|
|
3182
|
-
current = next2;
|
|
3183
|
-
}
|
|
3184
|
-
return current;
|
|
3185
|
-
}
|
|
3186
|
-
isLatestVersion(data) {
|
|
3187
|
-
return data.$version === this.latestVersion;
|
|
3188
|
-
}
|
|
3189
|
-
static create(latest) {
|
|
3190
|
-
return new _DataUpgrader(latest);
|
|
3191
|
-
}
|
|
3192
|
-
static Errors = {
|
|
3193
|
-
EmptyUpgrade: EmptyUpgradeError,
|
|
3194
|
-
UnavailableUpgrade: UnavailableUpgradeError
|
|
3195
|
-
};
|
|
3196
|
-
};
|
|
3197
|
-
function isUpgradable(obj) {
|
|
3198
|
-
return isDefined(obj) && typeof obj === "object" && VERSION_FIELD in obj && isNumber(obj.$version) && isPositiveNumber(obj.$version);
|
|
3199
|
-
}
|
|
3200
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
3201
|
-
0 && (module.exports = {
|
|
3202
|
-
DataUpgrader,
|
|
3203
|
-
Deferred,
|
|
3204
|
-
DeferredCanceledError,
|
|
3205
|
-
ErrorCannotInstantiatePresentOptionalWithEmptyValue,
|
|
3206
|
-
ErrorGetEmptyOptional,
|
|
3207
|
-
ErrorSetEmptyOptional,
|
|
3208
|
-
Lazy,
|
|
3209
|
-
LazyAsync,
|
|
3210
|
-
LazyDictionary,
|
|
3211
|
-
Logger,
|
|
3212
|
-
NEVER,
|
|
3213
|
-
NonExhaustiveSwitchError,
|
|
3214
|
-
Operation,
|
|
3215
|
-
Optional,
|
|
3216
|
-
PredicateBuilder,
|
|
3217
|
-
RandomTimeDuration,
|
|
3218
|
-
RateThrottler,
|
|
3219
|
-
Semaphore,
|
|
3220
|
-
Sorter,
|
|
3221
|
-
StringParts,
|
|
3222
|
-
TimeDuration,
|
|
3223
|
-
TimeFrequency,
|
|
3224
|
-
TimeInstant,
|
|
3225
|
-
TimeRange,
|
|
3226
|
-
TimeUnit,
|
|
3227
|
-
TimeoutError,
|
|
3228
|
-
alwaysFalse,
|
|
3229
|
-
alwaysTrue,
|
|
3230
|
-
and,
|
|
3231
|
-
arrayGet,
|
|
3232
|
-
asError,
|
|
3233
|
-
asPromise,
|
|
3234
|
-
average,
|
|
3235
|
-
awaitAtMost,
|
|
3236
|
-
capitalizeWord,
|
|
3237
|
-
clamp,
|
|
3238
|
-
clampInt0_100,
|
|
3239
|
-
constant,
|
|
3240
|
-
constantFalse,
|
|
3241
|
-
constantNull,
|
|
3242
|
-
constantOne,
|
|
3243
|
-
constantTrue,
|
|
3244
|
-
constantUndefined,
|
|
3245
|
-
constantZero,
|
|
3246
|
-
cssDeclarationRulesDictionaryToCss,
|
|
3247
|
-
decrement,
|
|
3248
|
-
decrementBy,
|
|
3249
|
-
delayPromise,
|
|
3250
|
-
dictToEntries,
|
|
3251
|
-
dictToList,
|
|
3252
|
-
divideBy,
|
|
3253
|
-
ellipsis,
|
|
3254
|
-
ensureArray,
|
|
3255
|
-
ensureDefined,
|
|
3256
|
-
ensureNegativeNumber,
|
|
3257
|
-
ensureNonNegativeNumber,
|
|
3258
|
-
ensureNonPositiveNumber,
|
|
3259
|
-
ensurePositiveNumber,
|
|
3260
|
-
ensureReadableArray,
|
|
3261
|
-
entriesToDict,
|
|
3262
|
-
entriesToEntries,
|
|
3263
|
-
entriesToList,
|
|
3264
|
-
extendArray,
|
|
3265
|
-
extendArrayWith,
|
|
3266
|
-
fill,
|
|
3267
|
-
filterMap,
|
|
3268
|
-
filterMapReduce,
|
|
3269
|
-
filterWithTypePredicate,
|
|
3270
|
-
findInArray,
|
|
3271
|
-
findIndexInArray,
|
|
3272
|
-
first,
|
|
3273
|
-
flatMapTruthys,
|
|
3274
|
-
getCauseMessageFromError,
|
|
3275
|
-
getCauseStackFromError,
|
|
3276
|
-
getMessageFromError,
|
|
3277
|
-
getStackFromError,
|
|
3278
|
-
groupByBoolean,
|
|
3279
|
-
groupByBooleanWith,
|
|
3280
|
-
groupByNumber,
|
|
3281
|
-
groupByNumberWith,
|
|
3282
|
-
groupByString,
|
|
3283
|
-
groupByStringWith,
|
|
3284
|
-
groupBySymbol,
|
|
3285
|
-
groupBySymbolWith,
|
|
3286
|
-
hashCode,
|
|
3287
|
-
head,
|
|
3288
|
-
identity,
|
|
3289
|
-
ifDefined,
|
|
3290
|
-
ifNullOrUndefined,
|
|
3291
|
-
iff,
|
|
3292
|
-
includes,
|
|
3293
|
-
increment,
|
|
3294
|
-
incrementBy,
|
|
3295
|
-
indexByNumber,
|
|
3296
|
-
indexByNumberWith,
|
|
3297
|
-
indexByString,
|
|
3298
|
-
indexByStringWith,
|
|
3299
|
-
indexBySymbol,
|
|
3300
|
-
indexBySymbolWith,
|
|
3301
|
-
isAllowedTimeDuration,
|
|
3302
|
-
isArray,
|
|
3303
|
-
isDefined,
|
|
3304
|
-
isEmpty,
|
|
3305
|
-
isError,
|
|
3306
|
-
isFalse,
|
|
3307
|
-
isFunction,
|
|
3308
|
-
isNegativeNumber,
|
|
3309
|
-
isNullOrUndefined,
|
|
3310
|
-
isNullOrUndefinedOrEmpty,
|
|
3311
|
-
isNumber,
|
|
3312
|
-
isPositiveNumber,
|
|
3313
|
-
isString,
|
|
3314
|
-
isTimeInstant,
|
|
3315
|
-
isTrue,
|
|
3316
|
-
isUpgradable,
|
|
3317
|
-
isZero,
|
|
3318
|
-
jsonCloneDeep,
|
|
3319
|
-
last,
|
|
3320
|
-
listToDict,
|
|
3321
|
-
mapDefined,
|
|
3322
|
-
mapEntries,
|
|
3323
|
-
mapFirstTruthy,
|
|
3324
|
-
mapTruthys,
|
|
3325
|
-
max,
|
|
3326
|
-
maxBy,
|
|
3327
|
-
min,
|
|
3328
|
-
minBy,
|
|
3329
|
-
multiplyBy,
|
|
3330
|
-
noop,
|
|
3331
|
-
not,
|
|
3332
|
-
omitFromJsonObject,
|
|
3333
|
-
or,
|
|
3334
|
-
pad,
|
|
3335
|
-
padLeft,
|
|
3336
|
-
padRight,
|
|
3337
|
-
parseJson,
|
|
3338
|
-
partition,
|
|
3339
|
-
pick,
|
|
3340
|
-
pipedInvoke,
|
|
3341
|
-
pipedInvokeFromArray,
|
|
3342
|
-
pluralize,
|
|
3343
|
-
promiseSequence,
|
|
3344
|
-
randomId,
|
|
3345
|
-
randomNumberInInterval,
|
|
3346
|
-
range,
|
|
3347
|
-
repeat,
|
|
3348
|
-
reverse,
|
|
3349
|
-
round,
|
|
3350
|
-
roundAwayFromZero,
|
|
3351
|
-
roundToLower,
|
|
3352
|
-
roundToNearest,
|
|
3353
|
-
roundToUpper,
|
|
3354
|
-
roundTowardsZero,
|
|
3355
|
-
shallowArrayEquals,
|
|
3356
|
-
shallowRecordEquals,
|
|
3357
|
-
sortedArray,
|
|
3358
|
-
splitWords,
|
|
3359
|
-
stringToNumber,
|
|
3360
|
-
stringifyJson,
|
|
3361
|
-
sum,
|
|
3362
|
-
sumBy,
|
|
3363
|
-
tail,
|
|
3364
|
-
throttle,
|
|
3365
|
-
throwIfNullOrUndefined,
|
|
3366
|
-
transformCssDictionary,
|
|
3367
|
-
tryToParseJson,
|
|
3368
|
-
tryToParseNumber,
|
|
3369
|
-
uniq,
|
|
3370
|
-
uniqBy,
|
|
3371
|
-
uniqByKey,
|
|
3372
|
-
unzip,
|
|
3373
|
-
upsert,
|
|
3374
|
-
withTryCatch,
|
|
3375
|
-
withTryCatchAsync,
|
|
3376
|
-
wrapWithString,
|
|
3377
|
-
xor,
|
|
3378
|
-
zip
|
|
3379
|
-
});
|
|
3380
|
-
//# sourceMappingURL=index.cjs.map
|