@valkyriestudios/utils 12.36.0 → 12.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/array/groupBy.d.ts +3 -3
- package/array/join.d.ts +2 -2
- package/array/split.d.ts +3 -3
- package/caching/LRU.d.ts +16 -10
- package/{array → cjs/array}/groupBy.js +16 -4
- package/{array → cjs/array}/join.js +15 -3
- package/{array → cjs/array}/mapFnAsMap.js +1 -1
- package/{array → cjs/array}/split.js +16 -7
- package/cjs/caching/LRU.js +123 -0
- package/{caching → cjs/caching}/memoize.js +11 -5
- package/{date → cjs/date}/format.js +7 -7
- package/{date → cjs/date}/isFormat.js +5 -2
- package/cjs/formdata/toObject.js +92 -0
- package/{object → cjs/object}/pick.js +2 -2
- package/{string → cjs/string}/humanizeNumber.js +2 -2
- package/cjs/string/isNotEmpty.js +23 -0
- package/{string → cjs/string}/shorten.js +1 -1
- package/esm/array/dedupe.js +52 -0
- package/esm/array/groupBy.js +43 -0
- package/esm/array/index.js +14 -0
- package/esm/array/is.js +4 -0
- package/esm/array/isNotEmpty.js +4 -0
- package/esm/array/join.js +58 -0
- package/esm/array/mapFn.js +22 -0
- package/esm/array/mapFnAsMap.js +21 -0
- package/esm/array/mapKey.js +22 -0
- package/esm/array/mapKeyAsMap.js +22 -0
- package/esm/array/mapPrimitive.js +33 -0
- package/esm/array/shuffle.js +10 -0
- package/esm/array/sort.js +137 -0
- package/esm/array/split.js +43 -0
- package/esm/boolean/index.js +1 -0
- package/esm/boolean/is.js +4 -0
- package/esm/caching/LRU.js +116 -0
- package/esm/caching/index.js +3 -0
- package/esm/caching/memoize.js +37 -0
- package/esm/date/addUTC.js +41 -0
- package/esm/date/convertToDate.js +12 -0
- package/esm/date/diff.js +34 -0
- package/esm/date/endOfUTC.js +50 -0
- package/esm/date/format.js +241 -0
- package/esm/date/index.js +15 -0
- package/esm/date/is.js +4 -0
- package/esm/date/isFormat.js +118 -0
- package/esm/date/isLeap.js +9 -0
- package/esm/date/nowUnix.js +4 -0
- package/esm/date/nowUnixMs.js +4 -0
- package/esm/date/setTimeUTC.js +10 -0
- package/esm/date/startOfUTC.js +50 -0
- package/esm/date/toUTC.js +7 -0
- package/esm/date/toUnix.js +7 -0
- package/esm/deep/freeze.js +21 -0
- package/esm/deep/get.js +84 -0
- package/esm/deep/index.js +8 -0
- package/esm/deep/seal.js +21 -0
- package/esm/deep/set.js +50 -0
- package/esm/equal.js +67 -0
- package/esm/formdata/index.js +2 -0
- package/esm/formdata/is.js +4 -0
- package/esm/formdata/toObject.js +89 -0
- package/esm/function/debounce.js +39 -0
- package/esm/function/index.js +9 -0
- package/esm/function/is.js +4 -0
- package/esm/function/isAsync.js +4 -0
- package/esm/function/noop.js +2 -0
- package/esm/function/noopresolve.js +4 -0
- package/esm/function/noopreturn.js +4 -0
- package/esm/function/sleep.js +6 -0
- package/esm/hash/fnv1A.js +51 -0
- package/esm/hash/guid.js +31 -0
- package/esm/hash/index.js +2 -0
- package/esm/is.js +63 -0
- package/esm/modules/PubSub.js +187 -0
- package/esm/modules/Scheduler.js +343 -0
- package/esm/modules/index.js +2 -0
- package/esm/number/index.js +37 -0
- package/esm/number/is.js +4 -0
- package/esm/number/isAbove.js +4 -0
- package/esm/number/isAboveOrEqual.js +4 -0
- package/esm/number/isBelow.js +4 -0
- package/esm/number/isBelowOrEqual.js +4 -0
- package/esm/number/isBetween.js +7 -0
- package/esm/number/isInteger.js +4 -0
- package/esm/number/isIntegerAbove.js +4 -0
- package/esm/number/isIntegerAboveOrEqual.js +4 -0
- package/esm/number/isIntegerBelow.js +4 -0
- package/esm/number/isIntegerBelowOrEqual.js +4 -0
- package/esm/number/isIntegerBetween.js +7 -0
- package/esm/number/isNumericalNaN.js +4 -0
- package/esm/number/randomBetween.js +7 -0
- package/esm/number/randomIntBetween.js +7 -0
- package/esm/number/round.js +10 -0
- package/esm/number/toPercentage.js +9 -0
- package/esm/object/define.js +7 -0
- package/esm/object/index.js +7 -0
- package/esm/object/is.js +4 -0
- package/esm/object/isNotEmpty.js +9 -0
- package/esm/object/merge.js +31 -0
- package/esm/object/omit.js +30 -0
- package/esm/object/pick.js +33 -0
- package/esm/regexp/index.js +3 -0
- package/esm/regexp/is.js +4 -0
- package/esm/regexp/sanitize.js +7 -0
- package/esm/string/humanizeBytes.js +15 -0
- package/esm/string/humanizeNumber.js +40 -0
- package/esm/string/index.js +7 -0
- package/esm/string/is.js +4 -0
- package/esm/string/isBetween.js +12 -0
- package/esm/string/isNotEmpty.js +20 -0
- package/esm/string/shorten.js +18 -0
- package/formdata/toObject.d.ts +4 -0
- package/index.d.ts +20 -15
- package/package.json +193 -851
- package/caching/LRU.js +0 -72
- package/formdata/toObject.js +0 -70
- package/string/isNotEmpty.js +0 -9
- /package/{array → cjs/array}/dedupe.js +0 -0
- /package/{array → cjs/array}/index.js +0 -0
- /package/{array → cjs/array}/is.js +0 -0
- /package/{array → cjs/array}/isNotEmpty.js +0 -0
- /package/{array → cjs/array}/mapFn.js +0 -0
- /package/{array → cjs/array}/mapKey.js +0 -0
- /package/{array → cjs/array}/mapKeyAsMap.js +0 -0
- /package/{array → cjs/array}/mapPrimitive.js +0 -0
- /package/{array → cjs/array}/shuffle.js +0 -0
- /package/{array → cjs/array}/sort.js +0 -0
- /package/{boolean → cjs/boolean}/index.js +0 -0
- /package/{boolean → cjs/boolean}/is.js +0 -0
- /package/{caching → cjs/caching}/index.js +0 -0
- /package/{date → cjs/date}/addUTC.js +0 -0
- /package/{date → cjs/date}/convertToDate.js +0 -0
- /package/{date → cjs/date}/diff.js +0 -0
- /package/{date → cjs/date}/endOfUTC.js +0 -0
- /package/{date → cjs/date}/index.js +0 -0
- /package/{date → cjs/date}/is.js +0 -0
- /package/{date → cjs/date}/isLeap.js +0 -0
- /package/{date → cjs/date}/nowUnix.js +0 -0
- /package/{date → cjs/date}/nowUnixMs.js +0 -0
- /package/{date → cjs/date}/setTimeUTC.js +0 -0
- /package/{date → cjs/date}/startOfUTC.js +0 -0
- /package/{date → cjs/date}/toUTC.js +0 -0
- /package/{date → cjs/date}/toUnix.js +0 -0
- /package/{deep → cjs/deep}/freeze.js +0 -0
- /package/{deep → cjs/deep}/get.js +0 -0
- /package/{deep → cjs/deep}/index.js +0 -0
- /package/{deep → cjs/deep}/seal.js +0 -0
- /package/{deep → cjs/deep}/set.js +0 -0
- /package/{equal.js → cjs/equal.js} +0 -0
- /package/{formdata → cjs/formdata}/index.js +0 -0
- /package/{formdata → cjs/formdata}/is.js +0 -0
- /package/{function → cjs/function}/debounce.js +0 -0
- /package/{function → cjs/function}/index.js +0 -0
- /package/{function → cjs/function}/is.js +0 -0
- /package/{function → cjs/function}/isAsync.js +0 -0
- /package/{function → cjs/function}/noop.js +0 -0
- /package/{function → cjs/function}/noopresolve.js +0 -0
- /package/{function → cjs/function}/noopreturn.js +0 -0
- /package/{function → cjs/function}/sleep.js +0 -0
- /package/{hash → cjs/hash}/fnv1A.js +0 -0
- /package/{hash → cjs/hash}/guid.js +0 -0
- /package/{hash → cjs/hash}/index.js +0 -0
- /package/{is.js → cjs/is.js} +0 -0
- /package/{modules → cjs/modules}/PubSub.js +0 -0
- /package/{modules → cjs/modules}/Scheduler.js +0 -0
- /package/{modules → cjs/modules}/index.js +0 -0
- /package/{number → cjs/number}/index.js +0 -0
- /package/{number → cjs/number}/is.js +0 -0
- /package/{number → cjs/number}/isAbove.js +0 -0
- /package/{number → cjs/number}/isAboveOrEqual.js +0 -0
- /package/{number → cjs/number}/isBelow.js +0 -0
- /package/{number → cjs/number}/isBelowOrEqual.js +0 -0
- /package/{number → cjs/number}/isBetween.js +0 -0
- /package/{number → cjs/number}/isInteger.js +0 -0
- /package/{number → cjs/number}/isIntegerAbove.js +0 -0
- /package/{number → cjs/number}/isIntegerAboveOrEqual.js +0 -0
- /package/{number → cjs/number}/isIntegerBelow.js +0 -0
- /package/{number → cjs/number}/isIntegerBelowOrEqual.js +0 -0
- /package/{number → cjs/number}/isIntegerBetween.js +0 -0
- /package/{number → cjs/number}/isNumericalNaN.js +0 -0
- /package/{number → cjs/number}/randomBetween.js +0 -0
- /package/{number → cjs/number}/randomIntBetween.js +0 -0
- /package/{number → cjs/number}/round.js +0 -0
- /package/{number → cjs/number}/toPercentage.js +0 -0
- /package/{object → cjs/object}/define.js +0 -0
- /package/{object → cjs/object}/index.js +0 -0
- /package/{object → cjs/object}/is.js +0 -0
- /package/{object → cjs/object}/isNotEmpty.js +0 -0
- /package/{object → cjs/object}/merge.js +0 -0
- /package/{object → cjs/object}/omit.js +0 -0
- /package/{regexp → cjs/regexp}/index.js +0 -0
- /package/{regexp → cjs/regexp}/is.js +0 -0
- /package/{regexp → cjs/regexp}/sanitize.js +0 -0
- /package/{string → cjs/string}/humanizeBytes.js +0 -0
- /package/{string → cjs/string}/index.js +0 -0
- /package/{string → cjs/string}/is.js +0 -0
- /package/{string → cjs/string}/isBetween.js +0 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Scheduler_jobs, _Scheduler_name, _Scheduler_log, _Scheduler_timeZone, _Scheduler_parallel, _Scheduler_timer;
|
|
13
|
+
import { format } from '../date/format';
|
|
14
|
+
import { toUTC } from '../date/toUTC';
|
|
15
|
+
import { isNotEmptyString } from '../string/isNotEmpty';
|
|
16
|
+
import { isFunction } from '../function/is';
|
|
17
|
+
import { isIntegerAbove, isIntegerBetween } from '../number';
|
|
18
|
+
import { isObject, pick } from '../object';
|
|
19
|
+
import { noop } from '../function';
|
|
20
|
+
import { isBoolean } from '../boolean';
|
|
21
|
+
import { split } from '../array';
|
|
22
|
+
const RGX_DIGITS = /^\d+$/;
|
|
23
|
+
const LIMITS = {
|
|
24
|
+
minute: [0, 59],
|
|
25
|
+
hour: [0, 23],
|
|
26
|
+
day_of_month: [1, 31],
|
|
27
|
+
month: [1, 12],
|
|
28
|
+
day_of_week: [0, 6],
|
|
29
|
+
};
|
|
30
|
+
function convertPart(part, min, max) {
|
|
31
|
+
if (part === '*')
|
|
32
|
+
return '*';
|
|
33
|
+
const set = new Set();
|
|
34
|
+
if (part.indexOf('/') > -1) {
|
|
35
|
+
const [base, raw_step] = part.split('/', 2);
|
|
36
|
+
const step = parseInt(raw_step, 10);
|
|
37
|
+
let start;
|
|
38
|
+
let end = max;
|
|
39
|
+
if (base === '*') {
|
|
40
|
+
start = min;
|
|
41
|
+
}
|
|
42
|
+
else if (base.indexOf('-') > -1) {
|
|
43
|
+
const chunks = base.split('-', 2);
|
|
44
|
+
start = parseInt(chunks[0], 10);
|
|
45
|
+
end = parseInt(chunks[1], 10);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
start = parseInt(base, 10);
|
|
49
|
+
}
|
|
50
|
+
for (let i = start; i <= end; i += step)
|
|
51
|
+
set.add(i);
|
|
52
|
+
}
|
|
53
|
+
else if (part.indexOf('-') > -1) {
|
|
54
|
+
const chunks = part.split('-', 2);
|
|
55
|
+
const start = parseInt(chunks[0], 10);
|
|
56
|
+
const end = parseInt(chunks[1], 10);
|
|
57
|
+
for (let i = start; i <= end; i++)
|
|
58
|
+
set.add(i);
|
|
59
|
+
}
|
|
60
|
+
else if (part.indexOf(',') > -1) {
|
|
61
|
+
const chunks = part.split(',');
|
|
62
|
+
for (let i = 0; i < chunks.length; i++)
|
|
63
|
+
set.add(parseInt(chunks[i], 10));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
set.add(parseInt(part, 10));
|
|
67
|
+
}
|
|
68
|
+
return set;
|
|
69
|
+
}
|
|
70
|
+
function isCronSubpart(part, min, max) {
|
|
71
|
+
if (!RGX_DIGITS.test(part))
|
|
72
|
+
return null;
|
|
73
|
+
const normalized = parseFloat(part);
|
|
74
|
+
if (!isIntegerBetween(normalized, min, max))
|
|
75
|
+
return null;
|
|
76
|
+
return normalized;
|
|
77
|
+
}
|
|
78
|
+
function isCronPart(part, min, max) {
|
|
79
|
+
if (part === '*') {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
else if (part.indexOf('/') > -1) {
|
|
83
|
+
const [base, stepStr] = part.split('/', 2);
|
|
84
|
+
const step = parseFloat(stepStr);
|
|
85
|
+
if (!isIntegerAbove(step, 0) || !isIntegerBetween(step, min, max))
|
|
86
|
+
return false;
|
|
87
|
+
if (base === '*')
|
|
88
|
+
return true;
|
|
89
|
+
let start = min;
|
|
90
|
+
let end = max;
|
|
91
|
+
if (base.indexOf('-') > -1) {
|
|
92
|
+
const chunks = base.split('-');
|
|
93
|
+
if (chunks.length !== 2)
|
|
94
|
+
return false;
|
|
95
|
+
start = isCronSubpart(chunks[0], min, max);
|
|
96
|
+
end = isCronSubpart(chunks[1], min, max);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
start = isCronSubpart(base, min, max);
|
|
100
|
+
}
|
|
101
|
+
if (start === null || end === null)
|
|
102
|
+
return false;
|
|
103
|
+
if (start > end)
|
|
104
|
+
return false;
|
|
105
|
+
if (step > (end - start))
|
|
106
|
+
return false;
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
else if (part.indexOf('-') > -1) {
|
|
110
|
+
const chunks = part.split('-');
|
|
111
|
+
if (chunks.length !== 2)
|
|
112
|
+
return false;
|
|
113
|
+
const start = isCronSubpart(chunks[0], min, max);
|
|
114
|
+
const end = isCronSubpart(chunks[1], min, max);
|
|
115
|
+
if (start === null || end === null)
|
|
116
|
+
return false;
|
|
117
|
+
return start < end;
|
|
118
|
+
}
|
|
119
|
+
else if (part.indexOf(',') > -1) {
|
|
120
|
+
const chunks = part.split(',');
|
|
121
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
122
|
+
if (isCronSubpart(chunks[i], min, max) === null)
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
return isCronSubpart(part, min, max) !== null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
class Scheduler {
|
|
132
|
+
constructor(options = {}) {
|
|
133
|
+
_Scheduler_jobs.set(this, []);
|
|
134
|
+
_Scheduler_name.set(this, 'Scheduler');
|
|
135
|
+
_Scheduler_log.set(this, noop);
|
|
136
|
+
_Scheduler_timeZone.set(this, null);
|
|
137
|
+
_Scheduler_parallel.set(this, true);
|
|
138
|
+
_Scheduler_timer.set(this, null);
|
|
139
|
+
if (!isObject(options))
|
|
140
|
+
throw new Error('Scheduler@ctor: options should be an object');
|
|
141
|
+
if ('logger' in options) {
|
|
142
|
+
if (!isFunction(options.logger))
|
|
143
|
+
throw new Error('Scheduler@ctor: logger should be a function');
|
|
144
|
+
__classPrivateFieldSet(this, _Scheduler_log, options.logger, "f");
|
|
145
|
+
}
|
|
146
|
+
if ('name' in options) {
|
|
147
|
+
if (!isNotEmptyString(options.name))
|
|
148
|
+
throw new Error('Scheduler@ctor: name should be a non-empty string');
|
|
149
|
+
__classPrivateFieldSet(this, _Scheduler_name, options.name.trim(), "f");
|
|
150
|
+
}
|
|
151
|
+
if ('timeZone' in options) {
|
|
152
|
+
if (options.timeZone !== null &&
|
|
153
|
+
!isNotEmptyString(options.timeZone))
|
|
154
|
+
throw new Error('Scheduler@ctor: timeZone should be null or a non-empty string');
|
|
155
|
+
__classPrivateFieldSet(this, _Scheduler_timeZone, options.timeZone, "f");
|
|
156
|
+
}
|
|
157
|
+
if ('parallel' in options) {
|
|
158
|
+
if (!isBoolean(options.parallel) &&
|
|
159
|
+
!isIntegerAbove(options.parallel, 0))
|
|
160
|
+
throw new Error('Scheduler@ctor: parallel should be passed as a boolean or int above 0');
|
|
161
|
+
__classPrivateFieldSet(this, _Scheduler_parallel, options.parallel, "f");
|
|
162
|
+
}
|
|
163
|
+
if (options.auto === true)
|
|
164
|
+
this.startAutomaticRun();
|
|
165
|
+
}
|
|
166
|
+
get name() {
|
|
167
|
+
return __classPrivateFieldGet(this, _Scheduler_name, "f");
|
|
168
|
+
}
|
|
169
|
+
get timeZone() {
|
|
170
|
+
return __classPrivateFieldGet(this, _Scheduler_timeZone, "f");
|
|
171
|
+
}
|
|
172
|
+
get parallel() {
|
|
173
|
+
return __classPrivateFieldGet(this, _Scheduler_parallel, "f");
|
|
174
|
+
}
|
|
175
|
+
get isAutomatic() {
|
|
176
|
+
return __classPrivateFieldGet(this, _Scheduler_timer, "f") !== null;
|
|
177
|
+
}
|
|
178
|
+
get jobs() {
|
|
179
|
+
const acc = [];
|
|
180
|
+
for (let i = 0; i < __classPrivateFieldGet(this, _Scheduler_jobs, "f").length; i++) {
|
|
181
|
+
const { name, schedule, timeZone, data = null } = __classPrivateFieldGet(this, _Scheduler_jobs, "f")[i];
|
|
182
|
+
acc.push({
|
|
183
|
+
name,
|
|
184
|
+
schedule,
|
|
185
|
+
timeZone,
|
|
186
|
+
...data !== null && { data: { ...data } },
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return acc;
|
|
190
|
+
}
|
|
191
|
+
add(job) {
|
|
192
|
+
try {
|
|
193
|
+
if (!Scheduler.isCronSchedule(job?.schedule))
|
|
194
|
+
throw new Error(`${__classPrivateFieldGet(this, _Scheduler_name, "f")}@add: Invalid cron schedule`);
|
|
195
|
+
if (!isFunction(job.fn))
|
|
196
|
+
throw new Error(`${__classPrivateFieldGet(this, _Scheduler_name, "f")}@add: Invalid function for job`);
|
|
197
|
+
if (!isNotEmptyString(job.name))
|
|
198
|
+
throw new Error(`${__classPrivateFieldGet(this, _Scheduler_name, "f")}@add: Invalid name for job`);
|
|
199
|
+
if ('data' in job && !isObject(job.data))
|
|
200
|
+
throw new Error(`${__classPrivateFieldGet(this, _Scheduler_name, "f")}@add: Job data should be an object`);
|
|
201
|
+
__classPrivateFieldGet(this, _Scheduler_jobs, "f").push({
|
|
202
|
+
name: job.name,
|
|
203
|
+
schedule: job.schedule,
|
|
204
|
+
fn: job.fn,
|
|
205
|
+
timeZone: isNotEmptyString(job.timeZone) ? job.timeZone : __classPrivateFieldGet(this, _Scheduler_timeZone, "f"),
|
|
206
|
+
map: Scheduler.convertToMap(job.schedule),
|
|
207
|
+
...job.data ? { data: job.data } : {},
|
|
208
|
+
});
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
catch (err) {
|
|
212
|
+
__classPrivateFieldGet(this, _Scheduler_log, "f").call(this, {
|
|
213
|
+
name: __classPrivateFieldGet(this, _Scheduler_name, "f"),
|
|
214
|
+
msg: err.message,
|
|
215
|
+
on: new Date(),
|
|
216
|
+
data: job,
|
|
217
|
+
err: err,
|
|
218
|
+
});
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
remove(name) {
|
|
223
|
+
const names = new Set(isNotEmptyString(name) ? [name] : Array.isArray(name) ? name : []);
|
|
224
|
+
const jobs = [];
|
|
225
|
+
for (let i = 0; i < __classPrivateFieldGet(this, _Scheduler_jobs, "f").length; i++) {
|
|
226
|
+
const el = __classPrivateFieldGet(this, _Scheduler_jobs, "f")[i];
|
|
227
|
+
if (!names.has(el.name))
|
|
228
|
+
jobs.push(el);
|
|
229
|
+
}
|
|
230
|
+
__classPrivateFieldSet(this, _Scheduler_jobs, jobs, "f");
|
|
231
|
+
}
|
|
232
|
+
async run() {
|
|
233
|
+
const promises = [];
|
|
234
|
+
for (let i = 0; i < __classPrivateFieldGet(this, _Scheduler_jobs, "f").length; i++) {
|
|
235
|
+
const job = __classPrivateFieldGet(this, _Scheduler_jobs, "f")[i];
|
|
236
|
+
const time = Scheduler.getTimeParts(new Date(), job.timeZone);
|
|
237
|
+
if (Scheduler.checkTimeAgainstMap(job.map, time)) {
|
|
238
|
+
try {
|
|
239
|
+
if (__classPrivateFieldGet(this, _Scheduler_parallel, "f")) {
|
|
240
|
+
promises.push(job);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
await job.fn(job.data);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
catch (err) {
|
|
247
|
+
__classPrivateFieldGet(this, _Scheduler_log, "f").call(this, {
|
|
248
|
+
name: __classPrivateFieldGet(this, _Scheduler_name, "f"),
|
|
249
|
+
msg: `${job.name}: ${err.message}`,
|
|
250
|
+
on: new Date(),
|
|
251
|
+
data: pick(job, ['schedule', 'timeZone', 'name', 'data']),
|
|
252
|
+
err,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (__classPrivateFieldGet(this, _Scheduler_parallel, "f") && promises.length) {
|
|
258
|
+
const batches = __classPrivateFieldGet(this, _Scheduler_parallel, "f") === true
|
|
259
|
+
? [promises]
|
|
260
|
+
: split(promises, __classPrivateFieldGet(this, _Scheduler_parallel, "f"));
|
|
261
|
+
for (const batch of batches) {
|
|
262
|
+
await Promise.allSettled(batch.map(el => (async () => {
|
|
263
|
+
try {
|
|
264
|
+
await el.fn(el.data);
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
__classPrivateFieldGet(this, _Scheduler_log, "f").call(this, {
|
|
268
|
+
name: __classPrivateFieldGet(this, _Scheduler_name, "f"),
|
|
269
|
+
msg: `${el.name}: ${err.message}`,
|
|
270
|
+
on: new Date(),
|
|
271
|
+
data: pick(el, ['schedule', 'timeZone', 'name', 'data']),
|
|
272
|
+
err: err,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
})()));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
stopAutomaticRun() {
|
|
280
|
+
if (!__classPrivateFieldGet(this, _Scheduler_timer, "f"))
|
|
281
|
+
return;
|
|
282
|
+
clearInterval(__classPrivateFieldGet(this, _Scheduler_timer, "f"));
|
|
283
|
+
__classPrivateFieldSet(this, _Scheduler_timer, null, "f");
|
|
284
|
+
}
|
|
285
|
+
startAutomaticRun() {
|
|
286
|
+
this.stopAutomaticRun();
|
|
287
|
+
__classPrivateFieldSet(this, _Scheduler_timer, setInterval(this.run.bind(this), 60000), "f");
|
|
288
|
+
}
|
|
289
|
+
static convertToMap(schedule) {
|
|
290
|
+
const parts = schedule.split(' ');
|
|
291
|
+
return {
|
|
292
|
+
minute: convertPart(parts[0], LIMITS.minute[0], LIMITS.minute[1]),
|
|
293
|
+
hour: convertPart(parts[1], LIMITS.hour[0], LIMITS.hour[1]),
|
|
294
|
+
day_of_month: convertPart(parts[2], LIMITS.day_of_month[0], LIMITS.day_of_month[1]),
|
|
295
|
+
month: convertPart(parts[3], LIMITS.month[0], LIMITS.month[1]),
|
|
296
|
+
day_of_week: convertPart(parts[4], LIMITS.day_of_week[0], LIMITS.day_of_week[1]),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
static getTimeParts(date, timeZone) {
|
|
300
|
+
const now = toUTC(timeZone !== null
|
|
301
|
+
? new Date(format(date, 'ISO', 'en-US', timeZone))
|
|
302
|
+
: date);
|
|
303
|
+
return {
|
|
304
|
+
minute: now.getUTCMinutes(),
|
|
305
|
+
hour: now.getUTCHours(),
|
|
306
|
+
day_of_month: now.getUTCDate(),
|
|
307
|
+
month: now.getUTCMonth() + 1,
|
|
308
|
+
day_of_week: now.getUTCDay(),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
static checkTimeAgainstMap(map, time) {
|
|
312
|
+
const { minute, hour, day_of_month, month, day_of_week } = map;
|
|
313
|
+
if (minute !== '*' && !minute.has(time.minute))
|
|
314
|
+
return false;
|
|
315
|
+
if (hour !== '*' && !hour.has(time.hour))
|
|
316
|
+
return false;
|
|
317
|
+
if (day_of_month !== '*' && !day_of_month.has(time.day_of_month))
|
|
318
|
+
return false;
|
|
319
|
+
if (month !== '*' && !month.has(time.month))
|
|
320
|
+
return false;
|
|
321
|
+
if (day_of_week !== '*' && !day_of_week.has(time.day_of_week))
|
|
322
|
+
return false;
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
static isCronSchedule(raw) {
|
|
326
|
+
if (!isNotEmptyString(raw))
|
|
327
|
+
return false;
|
|
328
|
+
const parts = raw.split(' ');
|
|
329
|
+
return (parts.length === 5 &&
|
|
330
|
+
isCronPart(parts[0], LIMITS.minute[0], LIMITS.minute[1]) &&
|
|
331
|
+
isCronPart(parts[1], LIMITS.hour[0], LIMITS.hour[1]) &&
|
|
332
|
+
isCronPart(parts[2], LIMITS.day_of_month[0], LIMITS.day_of_month[1]) &&
|
|
333
|
+
isCronPart(parts[3], LIMITS.month[0], LIMITS.month[1]) &&
|
|
334
|
+
isCronPart(parts[4], LIMITS.day_of_week[0], LIMITS.day_of_week[1]));
|
|
335
|
+
}
|
|
336
|
+
static cronShouldRun(schedule, timeZone = null) {
|
|
337
|
+
if (!Scheduler.isCronSchedule(schedule))
|
|
338
|
+
return false;
|
|
339
|
+
return Scheduler.checkTimeAgainstMap(Scheduler.convertToMap(schedule), Scheduler.getTimeParts(new Date(), isNotEmptyString(timeZone) ? timeZone : null));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
_Scheduler_jobs = new WeakMap(), _Scheduler_name = new WeakMap(), _Scheduler_log = new WeakMap(), _Scheduler_timeZone = new WeakMap(), _Scheduler_parallel = new WeakMap(), _Scheduler_timer = new WeakMap();
|
|
343
|
+
export { Scheduler, Scheduler as default };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export { isNumber } from './is';
|
|
2
|
+
export { isNumber as isNum } from './is';
|
|
3
|
+
export { isNumberAbove } from './isAbove';
|
|
4
|
+
export { isNumberAbove as isNumGt } from './isAbove';
|
|
5
|
+
export { isNumberAbove as isGt } from './isAbove';
|
|
6
|
+
export { isNumberAboveOrEqual } from './isAboveOrEqual';
|
|
7
|
+
export { isNumberAboveOrEqual as isNumGte } from './isAboveOrEqual';
|
|
8
|
+
export { isNumberAboveOrEqual as isGte } from './isAboveOrEqual';
|
|
9
|
+
export { isNumberBelow } from './isBelow';
|
|
10
|
+
export { isNumberBelow as isNumLt } from './isBelow';
|
|
11
|
+
export { isNumberBelow as isLt } from './isBelow';
|
|
12
|
+
export { isNumberBelowOrEqual } from './isBelowOrEqual';
|
|
13
|
+
export { isNumberBelowOrEqual as isNumLte } from './isBelowOrEqual';
|
|
14
|
+
export { isNumberBelowOrEqual as isLte } from './isBelowOrEqual';
|
|
15
|
+
export { isNumberBetween } from './isBetween';
|
|
16
|
+
export { isNumberBetween as isNumBetween } from './isBetween';
|
|
17
|
+
export { isNumberBetween as isBetween } from './isBetween';
|
|
18
|
+
export { isInteger } from './isInteger';
|
|
19
|
+
export { isInteger as isInt } from './isInteger';
|
|
20
|
+
export { isIntegerAbove } from './isIntegerAbove';
|
|
21
|
+
export { isIntegerAbove as isIntGt } from './isIntegerAbove';
|
|
22
|
+
export { isIntegerAboveOrEqual } from './isIntegerAboveOrEqual';
|
|
23
|
+
export { isIntegerAboveOrEqual as isIntGte } from './isIntegerAboveOrEqual';
|
|
24
|
+
export { isIntegerBelow } from './isIntegerBelow';
|
|
25
|
+
export { isIntegerBelow as isIntLt } from './isIntegerBelow';
|
|
26
|
+
export { isIntegerBelowOrEqual } from './isIntegerBelowOrEqual';
|
|
27
|
+
export { isIntegerBelowOrEqual as isIntLte } from './isIntegerBelowOrEqual';
|
|
28
|
+
export { isIntegerBetween } from './isIntegerBetween';
|
|
29
|
+
export { isIntegerBetween as isIntBetween } from './isIntegerBetween';
|
|
30
|
+
export { isNumericalNaN } from './isNumericalNaN';
|
|
31
|
+
export { randomBetween } from './randomBetween';
|
|
32
|
+
export { randomBetween as randBetween } from './randomBetween';
|
|
33
|
+
export { randomIntBetween } from './randomIntBetween';
|
|
34
|
+
export { randomIntBetween as randIntBetween } from './randomIntBetween';
|
|
35
|
+
export { round } from './round';
|
|
36
|
+
export { toPercentage } from './toPercentage';
|
|
37
|
+
export { toPercentage as toPct } from './toPercentage';
|
package/esm/number/is.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function randomIntBetween(min = 0, max = 10) {
|
|
2
|
+
if (!Number.isFinite(min) ||
|
|
3
|
+
!Number.isFinite(max))
|
|
4
|
+
throw new TypeError('Min/Max should be numeric');
|
|
5
|
+
return ((Math.random() * (max - min)) + min) | 0;
|
|
6
|
+
}
|
|
7
|
+
export { randomIntBetween, randomIntBetween as default };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const ROUND_EPSILON = 1 + Number.EPSILON;
|
|
2
|
+
function round(val, precision = 0) {
|
|
3
|
+
if (!Number.isFinite(val))
|
|
4
|
+
throw new TypeError('Value should be numeric');
|
|
5
|
+
if (!Number.isInteger(precision) || precision <= 0)
|
|
6
|
+
return Math.round(val * ROUND_EPSILON);
|
|
7
|
+
const exp = Math.pow(10, precision);
|
|
8
|
+
return Math.round((val * exp) * ROUND_EPSILON) / exp;
|
|
9
|
+
}
|
|
10
|
+
export { round, round as default };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { round } from './round';
|
|
2
|
+
function toPercentage(val, precision = 0, min = 0, max = 1) {
|
|
3
|
+
if (!Number.isFinite(val) ||
|
|
4
|
+
!Number.isFinite(min) ||
|
|
5
|
+
!Number.isFinite(max))
|
|
6
|
+
throw new TypeError('value/min/max should be numeric');
|
|
7
|
+
return round(((val - min) / (max - min)) * 100, precision);
|
|
8
|
+
}
|
|
9
|
+
export { toPercentage, toPercentage as default };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function define(props, obj = {}) {
|
|
2
|
+
if (Object.prototype.toString.call(props) !== '[object Object]' ||
|
|
3
|
+
Object.prototype.toString.call(obj) !== '[object Object]')
|
|
4
|
+
throw new TypeError('Please pass an object as the value for props and obj');
|
|
5
|
+
return Object.defineProperties(obj, props);
|
|
6
|
+
}
|
|
7
|
+
export { define, define as default };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { define } from './define';
|
|
2
|
+
export { merge } from './merge';
|
|
3
|
+
export { pick } from './pick';
|
|
4
|
+
export { omit } from './omit';
|
|
5
|
+
export { isObject } from './is';
|
|
6
|
+
export { isNotEmptyObject } from './isNotEmpty';
|
|
7
|
+
export { isNotEmptyObject as isNeObject } from './isNotEmpty';
|
package/esm/object/is.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const PROTO_OBJ = '[object Object]';
|
|
2
|
+
function innerMerge(target, source, UNION) {
|
|
3
|
+
const origin = UNION ? source : target;
|
|
4
|
+
for (const key in origin) {
|
|
5
|
+
const t_key = target[key];
|
|
6
|
+
const s_key = source[key];
|
|
7
|
+
if (Object.prototype.toString.call(t_key) === PROTO_OBJ &&
|
|
8
|
+
Object.prototype.toString.call(s_key) === PROTO_OBJ) {
|
|
9
|
+
target[key] = innerMerge({ ...t_key }, s_key, UNION);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
target[key] = s_key !== undefined ? s_key : t_key;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return target;
|
|
16
|
+
}
|
|
17
|
+
function merge(target, source, opts = {}) {
|
|
18
|
+
if (Object.prototype.toString.call(target) !== PROTO_OBJ)
|
|
19
|
+
throw new Error('object/merge: Please ensure valid target/source is passed');
|
|
20
|
+
const union = opts?.union === true;
|
|
21
|
+
const sources = Array.isArray(source) ? source : [source];
|
|
22
|
+
let acc = { ...target };
|
|
23
|
+
for (let i = 0; i < sources.length; i++) {
|
|
24
|
+
const el = sources[i];
|
|
25
|
+
if (!el || Object.prototype.toString.call(el) !== PROTO_OBJ)
|
|
26
|
+
continue;
|
|
27
|
+
acc = innerMerge(acc, el, union);
|
|
28
|
+
}
|
|
29
|
+
return acc;
|
|
30
|
+
}
|
|
31
|
+
export { merge, merge as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
function innerOmit(obj, keys) {
|
|
2
|
+
const result = { ...obj };
|
|
3
|
+
const groups = {};
|
|
4
|
+
for (let i = 0; i < keys.length; i++) {
|
|
5
|
+
if (typeof keys[i] === 'string') {
|
|
6
|
+
const [root, ...rest] = keys[i].split('.');
|
|
7
|
+
if (rest.length) {
|
|
8
|
+
if (!groups[root])
|
|
9
|
+
groups[root] = [];
|
|
10
|
+
groups[root].push(rest.join('.'));
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
delete result[root];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
for (const root in groups) {
|
|
18
|
+
if (typeof result[root] === 'object' &&
|
|
19
|
+
result[root] !== null)
|
|
20
|
+
result[root] = innerOmit(result[root], groups[root]);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
function omit(obj, keys) {
|
|
25
|
+
if (Object.prototype.toString.call(obj) !== '[object Object]' ||
|
|
26
|
+
!Array.isArray(keys))
|
|
27
|
+
throw new TypeError('Please pass an object to omit from and a keys array');
|
|
28
|
+
return innerOmit(obj, keys);
|
|
29
|
+
}
|
|
30
|
+
export { omit, omit as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { deepGet } from '../deep/get';
|
|
2
|
+
function pick(obj, keys) {
|
|
3
|
+
if (Object.prototype.toString.call(obj) !== '[object Object]' ||
|
|
4
|
+
!Array.isArray(keys))
|
|
5
|
+
throw new TypeError('Please pass an object to pick from and a keys array');
|
|
6
|
+
const map = {};
|
|
7
|
+
let val;
|
|
8
|
+
for (let i = 0; i < keys.length; i++) {
|
|
9
|
+
const key = keys[i];
|
|
10
|
+
if (typeof key !== 'string' || !key)
|
|
11
|
+
continue;
|
|
12
|
+
if (key.indexOf('.') >= 0) {
|
|
13
|
+
val = deepGet(obj, key);
|
|
14
|
+
if (val === undefined)
|
|
15
|
+
continue;
|
|
16
|
+
const parts = key.split('.');
|
|
17
|
+
const parts_len = parts.length;
|
|
18
|
+
let cursor = map;
|
|
19
|
+
for (let y = 0; y < parts_len - 1; y++) {
|
|
20
|
+
const part = parts[y];
|
|
21
|
+
if (!(part in cursor))
|
|
22
|
+
cursor[part] = {};
|
|
23
|
+
cursor = cursor[part];
|
|
24
|
+
}
|
|
25
|
+
cursor[parts[parts_len - 1]] = val;
|
|
26
|
+
}
|
|
27
|
+
else if (key in obj) {
|
|
28
|
+
map[key] = obj[key];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return map;
|
|
32
|
+
}
|
|
33
|
+
export { pick, pick as default };
|
package/esm/regexp/is.js
ADDED