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