@kwiz/common 1.0.2 → 1.0.3
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/package.json +1 -1
- package/src/_dependencies.ts +0 -2
- package/src/helpers/Guid.ts +0 -182
- package/src/helpers/_dependencies.ts +0 -3
- package/src/helpers/base64.ts +0 -174
- package/src/helpers/browser.test.js +0 -9
- package/src/helpers/browser.ts +0 -1299
- package/src/helpers/browserinfo.ts +0 -293
- package/src/helpers/collections.base.test.js +0 -26
- package/src/helpers/collections.base.ts +0 -439
- package/src/helpers/collections.ts +0 -108
- package/src/helpers/color.ts +0 -55
- package/src/helpers/cookies.ts +0 -55
- package/src/helpers/date.test.js +0 -97
- package/src/helpers/date.ts +0 -163
- package/src/helpers/debug.ts +0 -187
- package/src/helpers/emails.ts +0 -7
- package/src/helpers/eval.ts +0 -5
- package/src/helpers/file.test.js +0 -51
- package/src/helpers/file.ts +0 -59
- package/src/helpers/flatted.ts +0 -150
- package/src/helpers/functions.ts +0 -17
- package/src/helpers/graph/calendar.types.ts +0 -11
- package/src/helpers/http.ts +0 -70
- package/src/helpers/images.ts +0 -23
- package/src/helpers/index.ts +0 -29
- package/src/helpers/json.ts +0 -39
- package/src/helpers/md5.ts +0 -190
- package/src/helpers/objects.test.js +0 -27
- package/src/helpers/objects.ts +0 -256
- package/src/helpers/promises.test.js +0 -18
- package/src/helpers/promises.ts +0 -102
- package/src/helpers/random.ts +0 -27
- package/src/helpers/scheduler/scheduler.test.js +0 -104
- package/src/helpers/scheduler/scheduler.ts +0 -132
- package/src/helpers/sharepoint.ts +0 -681
- package/src/helpers/strings.test.js +0 -43
- package/src/helpers/strings.ts +0 -288
- package/src/helpers/typecheckers.test.js +0 -35
- package/src/helpers/typecheckers.ts +0 -263
- package/src/helpers/url.test.js +0 -18
- package/src/helpers/url.ts +0 -202
- package/src/helpers/urlhelper.ts +0 -104
- package/src/index.ts +0 -24
- package/src/types/common.types.ts +0 -16
- package/src/types/flatted.types.ts +0 -60
- package/src/types/globals.types.ts +0 -7
- package/src/types/graph/calendar.types.ts +0 -81
- package/src/types/graph/index.ts +0 -1
- package/src/types/index.ts +0 -12
- package/src/types/knownscript.types.ts +0 -19
- package/src/types/libs/datajs.types.ts +0 -29
- package/src/types/libs/ics.types.ts +0 -31
- package/src/types/libs/index.ts +0 -4
- package/src/types/libs/msal.types.ts +0 -79
- package/src/types/locales.ts +0 -125
- package/src/types/localstoragecache.types.ts +0 -9
- package/src/types/moment.ts +0 -7
- package/src/types/regex.types.ts +0 -17
- package/src/types/rest.types.ts +0 -94
- package/src/types/sharepoint.types.ts +0 -1413
- package/src/types/sharepoint.utils.types.ts +0 -246
- package/src/utils/_dependencies.ts +0 -4
- package/src/utils/base64.ts +0 -27
- package/src/utils/consolelogger.ts +0 -315
- package/src/utils/date.ts +0 -36
- package/src/utils/emails.ts +0 -25
- package/src/utils/index.ts +0 -13
- package/src/utils/knownscript.ts +0 -280
- package/src/utils/localstoragecache.ts +0 -419
- package/src/utils/rest.ts +0 -465
- package/src/utils/script.ts +0 -168
- package/src/utils/sharepoint.rest/common.ts +0 -149
- package/src/utils/sharepoint.rest/date.ts +0 -61
- package/src/utils/sharepoint.rest/file.folder.ts +0 -368
- package/src/utils/sharepoint.rest/index.ts +0 -11
- package/src/utils/sharepoint.rest/item.ts +0 -456
- package/src/utils/sharepoint.rest/list.ts +0 -1144
- package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +0 -750
- package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +0 -265
- package/src/utils/sharepoint.rest/listutils/common.ts +0 -202
- package/src/utils/sharepoint.rest/user-search.ts +0 -254
- package/src/utils/sharepoint.rest/user.ts +0 -447
- package/src/utils/sharepoint.rest/web.ts +0 -1031
- package/src/utils/sod.ts +0 -193
- package/tsconfig.json +0 -21
package/src/helpers/objects.ts
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
import { makeUniqueArray } from "./collections.base";
|
|
2
|
-
import { jsonParse } from "./json";
|
|
3
|
-
import { isDate, isFunction, isNotEmptyArray, isNullOrEmptyString, isNullOrUndefined, isObject, isPrimitiveValue, primitiveTypes } from "./typecheckers";
|
|
4
|
-
|
|
5
|
-
/** global window, safe for testing and environments without a browser */
|
|
6
|
-
export var $w = typeof window === "undefined" ? {
|
|
7
|
-
setTimeout: setTimeout,
|
|
8
|
-
clearTimeout: clearTimeout
|
|
9
|
-
} as any as Window : window;
|
|
10
|
-
|
|
11
|
-
/** wrapper for hasOwnProperty that satisfies https://eslint.org/docs/latest/rules/no-prototype-builtins */
|
|
12
|
-
export function hasOwnProperty(obj: any, prop: string) {
|
|
13
|
-
if (!isNullOrUndefined(obj)) {
|
|
14
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
15
|
-
}
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
|
|
20
|
-
export var noop = async () => { };
|
|
21
|
-
|
|
22
|
-
/** get or create kwizcom object from top window or current window, set allowFromTop if you want to try to get from window.top */
|
|
23
|
-
export function getKWizComGlobal(allowFromTop?: boolean) {
|
|
24
|
-
if (allowFromTop) {
|
|
25
|
-
try {
|
|
26
|
-
$w.top["kwizcom"] = $w.top["kwizcom"] || {};
|
|
27
|
-
return $w.top["kwizcom"] as IKWizComGlobals;
|
|
28
|
-
} catch (ex) { }
|
|
29
|
-
}
|
|
30
|
-
$w["kwizcom"] = $w["kwizcom"] || {};
|
|
31
|
-
return $w["kwizcom"] as IKWizComGlobals;
|
|
32
|
-
}
|
|
33
|
-
/** get or create kwizcom.globals dictionary from top window or current window. Add or return key:name initialize as defaults or blank object if does not already exist */
|
|
34
|
-
export function getGlobal<T>(name: string, defaults?: T, notFromTop?: boolean) {
|
|
35
|
-
var kGlobal = getKWizComGlobal(notFromTop !== true);
|
|
36
|
-
kGlobal.globals = kGlobal.globals || {};
|
|
37
|
-
|
|
38
|
-
if (!kGlobal.globals[name]) {
|
|
39
|
-
if (isObject(defaults)) {
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
41
|
-
kGlobal.globals[name] = { ...(defaults as object) };
|
|
42
|
-
} else {
|
|
43
|
-
kGlobal.globals[name] = {};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return kGlobal.globals[name] as T;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Automatically bind all functions of instance to instance
|
|
51
|
-
* Note: if you use knockout, you should skip ko.isObservable
|
|
52
|
-
* @param instance
|
|
53
|
-
*/
|
|
54
|
-
//eslint-disable-next-line @typescript-eslint/ban-types
|
|
55
|
-
export function autoBind(instance: any, skip?: (name: string, f: Function) => boolean) {
|
|
56
|
-
let funcitonNames = getAllFunctionNames(instance, 1);
|
|
57
|
-
funcitonNames.forEach(prop => {
|
|
58
|
-
const val = instance[prop];
|
|
59
|
-
if (!isFunction(skip) || !skip(prop, val))
|
|
60
|
-
instance[prop] = val.bind(instance);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Implements Object.assign which does not exist in IE
|
|
65
|
-
* Copies properties over from overrides into original object
|
|
66
|
-
* Merge default and override settings: var merged = assign({},defaults,props)
|
|
67
|
-
* Create deep copy of object by var copy = assign({},obj) */
|
|
68
|
-
export function assign<T>(original: Partial<T>, ...overrides: Partial<T>[]): T {
|
|
69
|
-
if (original === undefined || original === null) {
|
|
70
|
-
throw new TypeError('Cannot convert first argument to object');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
var to = Object(original);
|
|
74
|
-
if (overrides && overrides.length > 0)
|
|
75
|
-
overrides.forEach(o => {
|
|
76
|
-
if (!isNullOrUndefined(o)) {
|
|
77
|
-
var keysArray = Object.keys(Object(o));
|
|
78
|
-
for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
|
|
79
|
-
var nextKey = keysArray[nextIndex];
|
|
80
|
-
var desc = Object.getOwnPropertyDescriptor(o, nextKey);
|
|
81
|
-
if (typeof (desc.value) === "undefined" || desc.value === null) {
|
|
82
|
-
to[nextKey] = desc.value;
|
|
83
|
-
}
|
|
84
|
-
else if (typeof (desc.value) === "object") {
|
|
85
|
-
//reference type, call assign recursively. fixed problem that object value types from overrides were modified when the returnd object was modified.
|
|
86
|
-
if (!Array.isArray(desc.value)) {
|
|
87
|
-
to[nextKey] = assign({}, desc.value);
|
|
88
|
-
} else {
|
|
89
|
-
to[nextKey] = assign([], desc.value);
|
|
90
|
-
}
|
|
91
|
-
} else {
|
|
92
|
-
if (desc !== undefined && desc.enumerable) {
|
|
93
|
-
to[nextKey] = desc.value;//this will make a shallow copy, altering the override object o[nextKey];
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
return to as T;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function primitivesEqual(o1: primitiveTypes, o2: primitiveTypes) {
|
|
103
|
-
let normalize = (oo: any) => {
|
|
104
|
-
if (isNullOrEmptyString(oo))
|
|
105
|
-
return null;
|
|
106
|
-
else if (isDate(oo))
|
|
107
|
-
return oo.getTime();
|
|
108
|
-
return oo;
|
|
109
|
-
};
|
|
110
|
-
o1 = normalize(o1);
|
|
111
|
-
o2 = normalize(o2);
|
|
112
|
-
|
|
113
|
-
return o1 === o2;
|
|
114
|
-
}
|
|
115
|
-
export function objectsEqual<T extends object>(o1: T, o2: T, ignoreKeys?: string[]) {
|
|
116
|
-
let isPrimitive1 = isPrimitiveValue(o1);
|
|
117
|
-
let isPrimitive2 = isPrimitiveValue(o2);
|
|
118
|
-
|
|
119
|
-
if (isPrimitive1 !== isPrimitive2) return false;//one primitive other not?
|
|
120
|
-
if (isPrimitive1)
|
|
121
|
-
return primitivesEqual(o1 as unknown as primitiveTypes, o2 as unknown as primitiveTypes);
|
|
122
|
-
|
|
123
|
-
if (isFunction(o1) || isFunction(o2)) {
|
|
124
|
-
try {
|
|
125
|
-
return o1.toString() === o2.toString();
|
|
126
|
-
} catch (e) {
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
ignoreKeys = ignoreKeys || [];
|
|
132
|
-
let allKeys = makeUniqueArray(Object.keys(o1).concat(Object.keys(o2))).filter(key => !ignoreKeys.includes(key));
|
|
133
|
-
for (let i = 0; i < allKeys.length; i++) {
|
|
134
|
-
let key = allKeys[i];
|
|
135
|
-
if (!objectsEqual(o1[key], o2[key], ignoreKeys))
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
export function jsonClone<T>(obj: T): T {
|
|
141
|
-
//todo: check if assign utility method is faster
|
|
142
|
-
if (isNullOrUndefined(obj)) return null;
|
|
143
|
-
let result = obj;
|
|
144
|
-
try { result = jsonParse<T>(JSON.stringify(obj)); }
|
|
145
|
-
catch (e) {
|
|
146
|
-
if (isNotEmptyArray(obj))
|
|
147
|
-
result = (obj as any).slice();
|
|
148
|
-
else
|
|
149
|
-
result = obj;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
//clone date objects
|
|
153
|
-
try { cloneDatesOnObjectRecursivily(obj, result); } catch (e) { }
|
|
154
|
-
return result;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function cloneDatesOnObjectRecursivily(obj1, obj2) {
|
|
158
|
-
Object.keys(obj1).forEach(key => {
|
|
159
|
-
let v = obj1[key];
|
|
160
|
-
if (v === null)
|
|
161
|
-
obj2[key] = null;
|
|
162
|
-
else if (isDate(v))
|
|
163
|
-
obj2[key] = new Date(v.getTime());
|
|
164
|
-
else if (isObject(v) && Object.keys(v).length) {
|
|
165
|
-
let v2 = obj2[key];
|
|
166
|
-
if (isObject(v2))
|
|
167
|
-
cloneDatesOnObjectRecursivily(v, v2);
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** if an object in this path doesnt exist under parent - creates it.*/
|
|
173
|
-
export function ensureObjectPath(objectPath: string, defaultValue: any = {}, parent: any = $w) {
|
|
174
|
-
if (isNullOrEmptyString(objectPath)) return;
|
|
175
|
-
let parts = objectPath.split('.');
|
|
176
|
-
for (let i = 0; i < parts.length; i++) {
|
|
177
|
-
let partName = parts[i];
|
|
178
|
-
if (i === parts.length - 1)//last
|
|
179
|
-
{
|
|
180
|
-
if (isNullOrUndefined(parent[partName]))
|
|
181
|
-
parent[partName] = defaultValue;
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
if (isNullOrUndefined(parent[partName]))
|
|
185
|
-
parent[partName] = {};
|
|
186
|
-
parent = parent[partName];
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/** If o has propb and not propa - will copy propb into propa and remove propb */
|
|
192
|
-
export function keepOne(o: any, propa: string, propb: string) {
|
|
193
|
-
/* using the unkown type doesn't work in modern apps project */
|
|
194
|
-
if (isObject(o)) {
|
|
195
|
-
if (!hasOwnProperty(o, propa) && hasOwnProperty(o, propb)) {
|
|
196
|
-
o[propa] = o[propb];
|
|
197
|
-
delete o[propb];
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**return all members and functions of an object, including inherited ones from its base class, excluding the constructor
|
|
203
|
-
* send prototypeLevels to limit the number of prototype climbs to get functions from. 0 means unlimited.
|
|
204
|
-
*/
|
|
205
|
-
export function getAllMemberNames(instance: any, prototypeLevels: number): string[] {
|
|
206
|
-
let props: string[] = [];
|
|
207
|
-
let obj = instance;
|
|
208
|
-
let level = 0;
|
|
209
|
-
let unlimitedLevels = prototypeLevels < 0;
|
|
210
|
-
|
|
211
|
-
do {
|
|
212
|
-
props.push(...Object.getOwnPropertyNames(obj));
|
|
213
|
-
obj = Object.getPrototypeOf(obj);
|
|
214
|
-
level++;
|
|
215
|
-
}
|
|
216
|
-
while (unlimitedLevels ? !!obj : !!obj && level <= prototypeLevels);
|
|
217
|
-
|
|
218
|
-
return makeUniqueArray(props.filter(p => p !== 'constructor' && p !== 'dispose'));
|
|
219
|
-
}
|
|
220
|
-
/**return all functions of an object, including inherited ones from its base class, excluding the constructor
|
|
221
|
-
* send prototypeLevels to limit the number of prototype climbs to get functions from. 0 means unlimited.
|
|
222
|
-
*/
|
|
223
|
-
export function getAllFunctionNames(instance: any, prototypeLevels: number): string[] {
|
|
224
|
-
return getAllMemberNames(instance, prototypeLevels).filter(p => isFunction(instance[p]));
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** generic implementation of Object.values */
|
|
228
|
-
export function objectValues<T = any>(obj: any): T[] {
|
|
229
|
-
return Object.keys(obj).map((key) => {
|
|
230
|
-
return obj[key];
|
|
231
|
-
}) as T[];
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
class DefaultProp<T>{
|
|
235
|
-
private _value: T;
|
|
236
|
-
private _defaultValue: T | (() => T);
|
|
237
|
-
private isValid: (value: T) => boolean;
|
|
238
|
-
public set value(newValue: T) { this._value = newValue; }
|
|
239
|
-
public get value() {
|
|
240
|
-
if (!this.isValid(this._value)) {
|
|
241
|
-
this._value = isFunction(this._defaultValue)
|
|
242
|
-
? (this._defaultValue as () => T)()
|
|
243
|
-
: this._defaultValue;
|
|
244
|
-
}
|
|
245
|
-
return this._value;
|
|
246
|
-
}
|
|
247
|
-
public constructor(defaultValue: T | (() => T), initialValue?: T, isValid?: (value: T) => boolean) {
|
|
248
|
-
this._defaultValue = defaultValue;
|
|
249
|
-
this._value = initialValue;
|
|
250
|
-
this.isValid = isFunction(isValid) ? isValid : v => !isNullOrUndefined(v);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
/** creates a safe property, if the value is null/undefined or empty string - it will return the default value. */
|
|
254
|
-
export function GetDefaultProp<T>(defaultValue: T | (() => T), initialValue?: T, isValid?: (value: T) => boolean) {
|
|
255
|
-
return new DefaultProp(defaultValue, initialValue, isValid);
|
|
256
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import assert from 'assert/strict';
|
|
2
|
-
import test from 'node:test';
|
|
3
|
-
import { promiseOnce } from './promises';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
test('promiseOnce', async t => {
|
|
7
|
-
var promiseOnceTester = promiseOnce("promiseOnceTester", async () => {
|
|
8
|
-
return Date.now();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
let value = await promiseOnceTester;
|
|
12
|
-
await t.test("First call", t => assert.strictEqual(value > 0, true));
|
|
13
|
-
//check cache
|
|
14
|
-
let value2 = await promiseOnceTester;
|
|
15
|
-
await t.test("Second call", t => assert.strictEqual(value === value2, true));
|
|
16
|
-
let value3 = await promiseOnceTester;
|
|
17
|
-
await t.test("Third call", t => assert.strictEqual(value === value3, true));
|
|
18
|
-
});
|
package/src/helpers/promises.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { IDictionary } from "./_dependencies";
|
|
2
|
-
import { hasOwnProperty } from "./objects";
|
|
3
|
-
import { isFunction, isNullOrUndefined, isNumeric } from "./typecheckers";
|
|
4
|
-
|
|
5
|
-
var promises: IDictionary<Promise<any>> = {};
|
|
6
|
-
/** key has to be unique for the promise name + its parameters
|
|
7
|
-
* Usage: export var initTests = promiseOnce("initTests", async () => { ... });
|
|
8
|
-
* Usage with variables:
|
|
9
|
-
* function DiscoverTenantInfo(hostName: string) {
|
|
10
|
-
* return promiseOnce(`DiscoverTenantInfo|${hostName}`, async () => {...});
|
|
11
|
-
* }
|
|
12
|
-
*/
|
|
13
|
-
export async function promiseOnce<T>(key: string, promise: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T> {
|
|
14
|
-
if (hasOwnProperty(promises, key) && isFunction(isValidResult)) {
|
|
15
|
-
//we have en existing pending promise...
|
|
16
|
-
let queuedResult: T = null;
|
|
17
|
-
try { queuedResult = await promises[key]; } catch (e) { }
|
|
18
|
-
if ((await isValidResult(queuedResult)) !== true)
|
|
19
|
-
delete promises[key];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (!hasOwnProperty(promises, key)) {
|
|
24
|
-
promises[key] = promise();
|
|
25
|
-
}
|
|
26
|
-
return promises[key];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]> {
|
|
30
|
-
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
31
|
-
return Promise.resolve([]);
|
|
32
|
-
}
|
|
33
|
-
return asyncFuncs.reduce((promiseChain, currentTaskcurrentTask) => (
|
|
34
|
-
promiseChain.then((result) => {
|
|
35
|
-
let taskResult = currentTaskcurrentTask();
|
|
36
|
-
if (isNullOrUndefined(taskResult) || !isFunction(taskResult.then))//culprit - found one that did not return a promise?! make one.
|
|
37
|
-
taskResult = Promise.resolve() as any as Promise<T>;
|
|
38
|
-
|
|
39
|
-
return taskResult.then(Array.prototype.concat.bind(result));
|
|
40
|
-
})
|
|
41
|
-
), Promise.resolve([]));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel?: number): Promise<T[]> {
|
|
45
|
-
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
46
|
-
return Promise.resolve([]);
|
|
47
|
-
}
|
|
48
|
-
if (!isNumeric(maxParallel)) {
|
|
49
|
-
maxParallel = asyncFuncs.length;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
var startChain = () => {
|
|
53
|
-
var chainData = [];
|
|
54
|
-
|
|
55
|
-
if (asyncFuncs.length) {
|
|
56
|
-
let next = (data: T) => {
|
|
57
|
-
chainData.push(data);
|
|
58
|
-
return asyncFuncs.length ? (asyncFuncs.shift())().then(next) : chainData;
|
|
59
|
-
};
|
|
60
|
-
return (asyncFuncs.shift())().then(next);
|
|
61
|
-
} else {
|
|
62
|
-
return Promise.resolve(chainData);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
var chains = [];
|
|
67
|
-
for (var k = 0; k < maxParallel; k += 1) {
|
|
68
|
-
chains.push(startChain());
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return Promise.all(chains).then(d => {
|
|
72
|
-
//flatten results
|
|
73
|
-
return d.reduce((acc, val) => acc.concat(val), []);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** provides an asnyc sleep function that allows you to delay async/wait calls */
|
|
78
|
-
export function sleepAsync(seconds?: number): Promise<void> {
|
|
79
|
-
return new Promise(resolve => {
|
|
80
|
-
window.setTimeout(() => resolve(), seconds > 0 ? seconds * 1000 : 3000);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** provides the ability to retry an async function n times with a optional delay between calls */
|
|
85
|
-
export async function retryAsync<T>(fn: (...args) => Promise<T>, numberOfRetries: number, seconds = 1) {
|
|
86
|
-
let error: Error = null;
|
|
87
|
-
|
|
88
|
-
for (let i = 0; i < numberOfRetries; i++) {
|
|
89
|
-
try {
|
|
90
|
-
error = null;
|
|
91
|
-
await sleepAsync(i === 0 ? 0 : seconds);
|
|
92
|
-
return await fn();
|
|
93
|
-
} catch (ex) {
|
|
94
|
-
error = ex;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (error) {
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
throw new Error(`Failed retrying ${numberOfRetries} times`);
|
|
102
|
-
}
|
package/src/helpers/random.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export function getRandomId() {
|
|
2
|
-
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function getUniqueId() {
|
|
6
|
-
var _s_hexcode = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
|
|
7
|
-
for (var a = "", c = 0; c < 32; c++) {
|
|
8
|
-
var b = Math.floor(Math.random() * 16);
|
|
9
|
-
switch (c) {
|
|
10
|
-
case 8:
|
|
11
|
-
a += "-";
|
|
12
|
-
break;
|
|
13
|
-
case 12:
|
|
14
|
-
b = 4;
|
|
15
|
-
a += "-";
|
|
16
|
-
break;
|
|
17
|
-
case 16:
|
|
18
|
-
b = b & 3 | 8;
|
|
19
|
-
a += "-";
|
|
20
|
-
break;
|
|
21
|
-
case 20:
|
|
22
|
-
a += "-";
|
|
23
|
-
}
|
|
24
|
-
a += _s_hexcode[b];
|
|
25
|
-
}
|
|
26
|
-
return a;
|
|
27
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import assert from 'assert/strict';
|
|
2
|
-
import test from 'node:test';
|
|
3
|
-
import { isValidSchedule, getNextUTC } from "./scheduler";
|
|
4
|
-
import { ScheduleTypes } from './scheduler';
|
|
5
|
-
|
|
6
|
-
var startDate = "2020-01-01T05:00Z";
|
|
7
|
-
var invalidOutput = "9999999999";
|
|
8
|
-
var schedules = [
|
|
9
|
-
{ schedule: {}, output: invalidOutput, valid: false },
|
|
10
|
-
{ schedule: { ScheduleType: "x" }, output: invalidOutput, valid: false },
|
|
11
|
-
{
|
|
12
|
-
schedule: {
|
|
13
|
-
ScheduleType: ScheduleTypes.hourly,
|
|
14
|
-
interval: 24
|
|
15
|
-
}, output: invalidOutput, valid: false
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
name: "move 2 hours",
|
|
19
|
-
schedule: {
|
|
20
|
-
ScheduleType: ScheduleTypes.hourly,
|
|
21
|
-
interval: 2
|
|
22
|
-
}, output: "2020010107", valid: true
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
schedule: {
|
|
26
|
-
ScheduleType: ScheduleTypes.daily,
|
|
27
|
-
hours: []
|
|
28
|
-
}, output: invalidOutput, valid: false
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
schedule: {
|
|
32
|
-
ScheduleType: ScheduleTypes.daily,
|
|
33
|
-
hours: [5, 2, 9, 26]
|
|
34
|
-
}, output: invalidOutput, valid: false
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
name: "move to next hour in same day",
|
|
38
|
-
schedule: {
|
|
39
|
-
ScheduleType: ScheduleTypes.daily,
|
|
40
|
-
hours: [5, 2, 9, 22]
|
|
41
|
-
}, output: "2020010109", valid: true
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
name: "passed last hour, move to next day",
|
|
45
|
-
schedule: {
|
|
46
|
-
ScheduleType: ScheduleTypes.daily,
|
|
47
|
-
hours: [3, 1, 5, 2]
|
|
48
|
-
}, output: "2020010201", valid: true
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
schedule: {
|
|
52
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
53
|
-
hours: []
|
|
54
|
-
}, output: invalidOutput, valid: false
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
schedule: {
|
|
58
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
59
|
-
hours: [5, 2, 9, 26],
|
|
60
|
-
days: [2],
|
|
61
|
-
}, output: invalidOutput, valid: false
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
schedule: {
|
|
65
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
66
|
-
hours: [5, 2, 9, 22],
|
|
67
|
-
days: [5, 2, 7]
|
|
68
|
-
}, output: invalidOutput, valid: false
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: "day is no ok, move to next day first hour",
|
|
72
|
-
schedule: {
|
|
73
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
74
|
-
hours: [5, 2, 9, 22],
|
|
75
|
-
days: [5, 2, 4]
|
|
76
|
-
}, output: "2020010202", valid: true
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
name: "day is not ok, move to first day in next week",
|
|
80
|
-
schedule: {
|
|
81
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
82
|
-
hours: [5, 2, 9, 22],
|
|
83
|
-
days: [2, 0, 1]
|
|
84
|
-
}, output: "2020010502", valid: true
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
name: "day is ok, move to next hour",
|
|
88
|
-
schedule: {
|
|
89
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
90
|
-
hours: [5, 2, 9, 22],
|
|
91
|
-
days: [5, 2, 3, 4]
|
|
92
|
-
}, output: "2020010109", valid: true
|
|
93
|
-
}
|
|
94
|
-
];
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
test('isValidSchedule', t => {
|
|
98
|
-
schedules.forEach(s => assert.strictEqual(isValidSchedule(s.schedule), s.valid));
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
test('getNextUTC', async t => {
|
|
102
|
-
let date = new Date(startDate);
|
|
103
|
-
schedules.forEach(s => assert.strictEqual(getNextUTC(date, s.schedule), s.output));
|
|
104
|
-
});
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { firstOrNull, sortNumberArrayAsc } from "../collections.base";
|
|
2
|
-
import { cloneDate } from "../date";
|
|
3
|
-
import { padLeft } from "../strings";
|
|
4
|
-
import { isNotEmptyArray, isNullOrUndefined } from "../typecheckers";
|
|
5
|
-
|
|
6
|
-
export type hourlyInterval = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
|
|
7
|
-
export type hours = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23;
|
|
8
|
-
export type days = 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
9
|
-
|
|
10
|
-
export enum ScheduleTypes {
|
|
11
|
-
hourly = "hourly",
|
|
12
|
-
daily = "daily",
|
|
13
|
-
weekly = "weekly"
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const ScheduleTypesDefinitions: { [Name: string]: { title: string, value: ScheduleTypes }; } = {
|
|
17
|
-
weekly: {
|
|
18
|
-
value: ScheduleTypes.weekly,
|
|
19
|
-
title: "Run on specific days, at specific times"
|
|
20
|
-
},
|
|
21
|
-
daily: {
|
|
22
|
-
value: ScheduleTypes.daily,
|
|
23
|
-
title: "Run every day, at specific times"
|
|
24
|
-
},
|
|
25
|
-
hourly: {
|
|
26
|
-
value: ScheduleTypes.hourly,
|
|
27
|
-
title: "Run every few hours"
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export interface IScheduleHourly {
|
|
32
|
-
ScheduleType: ScheduleTypes.hourly,
|
|
33
|
-
/** run every x number of hours */
|
|
34
|
-
interval: hourlyInterval;
|
|
35
|
-
}
|
|
36
|
-
export interface IScheduleDaily {
|
|
37
|
-
ScheduleType: ScheduleTypes.daily,
|
|
38
|
-
/** run every day, at these hours */
|
|
39
|
-
hours: hours[];
|
|
40
|
-
}
|
|
41
|
-
export interface IScheduleWeekly {
|
|
42
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
43
|
-
/** run in the following days */
|
|
44
|
-
days: days[];
|
|
45
|
-
/** run at these hours */
|
|
46
|
-
hours: hours[];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export type TypeSchedule = IScheduleHourly | IScheduleDaily | IScheduleWeekly;
|
|
50
|
-
|
|
51
|
-
export const defaultWeeklySchedule: IScheduleWeekly = {
|
|
52
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
53
|
-
days: [1, 2, 3, 4, 5],
|
|
54
|
-
hours: [0]
|
|
55
|
-
};
|
|
56
|
-
export const defaultDailySchedule: IScheduleDaily = {
|
|
57
|
-
ScheduleType: ScheduleTypes.daily,
|
|
58
|
-
hours: [0, 12]
|
|
59
|
-
};
|
|
60
|
-
export const defaultHourlySchedule: IScheduleHourly = {
|
|
61
|
-
ScheduleType: ScheduleTypes.hourly,
|
|
62
|
-
interval: 4
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export function isValidSchedule(schedule: TypeSchedule) {
|
|
66
|
-
if (isNullOrUndefined(schedule)) return false;
|
|
67
|
-
if (schedule.ScheduleType === ScheduleTypes.hourly)
|
|
68
|
-
return schedule.interval > 0 && schedule.interval <= 12;
|
|
69
|
-
else if (schedule.ScheduleType === ScheduleTypes.daily) {
|
|
70
|
-
return isNotEmptyArray(schedule.hours) && schedule.hours.every(h => h >= 0 && h <= 23);
|
|
71
|
-
}
|
|
72
|
-
else if (schedule.ScheduleType === ScheduleTypes.weekly) {
|
|
73
|
-
return isNotEmptyArray(schedule.hours) && schedule.hours.every(h => h >= 0 && h <= 23)
|
|
74
|
-
&& isNotEmptyArray(schedule.days) && schedule.days.every(d => d >= 0 && d <= 6);
|
|
75
|
-
}
|
|
76
|
-
else return false;
|
|
77
|
-
}
|
|
78
|
-
/** returns a yyyyMMddHH for the next time this schedule needs to run after the currentDate */
|
|
79
|
-
export function getNextUTC(currentDate: Date, schedule: TypeSchedule): string {
|
|
80
|
-
if (!isValidSchedule(schedule)) return "9999999999";
|
|
81
|
-
|
|
82
|
-
currentDate = cloneDate(currentDate);
|
|
83
|
-
|
|
84
|
-
let date = currentDate.getUTCDate();
|
|
85
|
-
let hour = currentDate.getUTCHours();
|
|
86
|
-
let day = currentDate.getUTCDay() as days;
|
|
87
|
-
|
|
88
|
-
//adjust date based on selected schedule
|
|
89
|
-
if (schedule.ScheduleType === ScheduleTypes.hourly) {
|
|
90
|
-
hour += schedule.interval;
|
|
91
|
-
currentDate.setUTCHours(hour);
|
|
92
|
-
}
|
|
93
|
-
else if (schedule.ScheduleType === ScheduleTypes.daily) {
|
|
94
|
-
//get current hour, see if the next run is today or tomorrow
|
|
95
|
-
let sortedHours = schedule.hours.sort(sortNumberArrayAsc);
|
|
96
|
-
let nextHour = firstOrNull(sortedHours.filter(h => h > hour));
|
|
97
|
-
if (nextHour !== null) {
|
|
98
|
-
currentDate.setUTCHours(nextHour);
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
//tomorrow at the first scheduled hour
|
|
102
|
-
currentDate.setUTCHours(sortedHours[0]);
|
|
103
|
-
currentDate.setUTCDate(date + 1);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
//implement weekly
|
|
108
|
-
let sortedDays = schedule.days.sort(sortNumberArrayAsc);
|
|
109
|
-
let sortedHours = schedule.hours.sort(sortNumberArrayAsc);
|
|
110
|
-
let nextHour = firstOrNull(sortedHours.filter(h => h > hour));
|
|
111
|
-
let nextDay = firstOrNull(sortedDays.filter(h => h > day));
|
|
112
|
-
|
|
113
|
-
if (sortedDays.includes(day) && nextHour !== null)//today is allowed
|
|
114
|
-
{
|
|
115
|
-
currentDate.setUTCHours(nextHour);
|
|
116
|
-
}
|
|
117
|
-
else//move to next allowed day, at first hour allowed
|
|
118
|
-
{
|
|
119
|
-
let daysToAdd = nextDay === null
|
|
120
|
-
? 7 - day + sortedDays[0]
|
|
121
|
-
: nextDay - day;
|
|
122
|
-
currentDate.setUTCHours(sortedHours[0]);
|
|
123
|
-
currentDate.setUTCDate(date + daysToAdd);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return getUTCDateMarker(currentDate);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function getUTCDateMarker(date: Date) {
|
|
131
|
-
return `${date.getUTCFullYear()}${padLeft(date.getUTCMonth() + 1, 2)}${padLeft(date.getUTCDate(), 2)}${padLeft(date.getUTCHours(), 2)}`;
|
|
132
|
-
}
|