@kwiz/common 1.0.129 → 1.0.132

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.
Files changed (100) hide show
  1. package/.github/workflows/npm-publish.yml +24 -24
  2. package/LICENSE +21 -21
  3. package/lib/cjs/config.js +2 -0
  4. package/lib/cjs/config.js.map +1 -1
  5. package/lib/cjs/types/libs/msal.types.js +26 -26
  6. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  7. package/lib/esm/config.js +2 -0
  8. package/lib/esm/config.js.map +1 -1
  9. package/lib/esm/types/libs/msal.types.js +26 -26
  10. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  11. package/lib/types/config.d.ts +2 -0
  12. package/package.json +81 -81
  13. package/readme.md +17 -17
  14. package/src/_dependencies.ts +12 -12
  15. package/src/config.ts +19 -17
  16. package/src/helpers/Guid.ts +181 -181
  17. package/src/helpers/base64.ts +173 -173
  18. package/src/helpers/browser.test.js +13 -13
  19. package/src/helpers/browser.ts +1504 -1504
  20. package/src/helpers/browserinfo.ts +292 -292
  21. package/src/helpers/collections.base.test.js +25 -25
  22. package/src/helpers/collections.base.ts +437 -437
  23. package/src/helpers/collections.ts +107 -107
  24. package/src/helpers/color.ts +54 -54
  25. package/src/helpers/cookies.ts +59 -59
  26. package/src/helpers/date.test.js +119 -119
  27. package/src/helpers/date.ts +188 -188
  28. package/src/helpers/debug.ts +186 -186
  29. package/src/helpers/diagrams.ts +43 -43
  30. package/src/helpers/emails.ts +6 -6
  31. package/src/helpers/eval.ts +5 -5
  32. package/src/helpers/file.test.js +50 -50
  33. package/src/helpers/file.ts +63 -63
  34. package/src/helpers/flatted.ts +149 -149
  35. package/src/helpers/functions.ts +16 -16
  36. package/src/helpers/graph/calendar.types.ts +10 -10
  37. package/src/helpers/http.ts +69 -69
  38. package/src/helpers/images.ts +22 -22
  39. package/src/helpers/json.ts +44 -44
  40. package/src/helpers/md5.ts +189 -189
  41. package/src/helpers/objects.test.js +33 -33
  42. package/src/helpers/objects.ts +274 -274
  43. package/src/helpers/promises.test.js +37 -37
  44. package/src/helpers/promises.ts +165 -165
  45. package/src/helpers/random.ts +27 -27
  46. package/src/helpers/scheduler/scheduler.test.js +103 -103
  47. package/src/helpers/scheduler/scheduler.ts +131 -131
  48. package/src/helpers/sharepoint.ts +796 -796
  49. package/src/helpers/strings.test.js +122 -122
  50. package/src/helpers/strings.ts +337 -337
  51. package/src/helpers/typecheckers.test.js +34 -34
  52. package/src/helpers/typecheckers.ts +266 -266
  53. package/src/helpers/url.test.js +43 -43
  54. package/src/helpers/url.ts +207 -207
  55. package/src/helpers/urlhelper.ts +111 -111
  56. package/src/index.ts +6 -6
  57. package/src/types/auth.ts +62 -62
  58. package/src/types/common.types.ts +15 -15
  59. package/src/types/flatted.types.ts +59 -59
  60. package/src/types/globals.types.ts +6 -6
  61. package/src/types/graph/calendar.types.ts +80 -80
  62. package/src/types/knownscript.types.ts +18 -18
  63. package/src/types/libs/datajs.types.ts +28 -28
  64. package/src/types/libs/ics.types.ts +30 -30
  65. package/src/types/libs/msal.types.ts +57 -57
  66. package/src/types/locales.ts +125 -125
  67. package/src/types/localstoragecache.types.ts +8 -8
  68. package/src/types/location.types.ts +27 -27
  69. package/src/types/moment.ts +11 -11
  70. package/src/types/regex.types.ts +16 -16
  71. package/src/types/rest.types.ts +95 -95
  72. package/src/types/sharepoint.types.ts +1466 -1466
  73. package/src/types/sharepoint.utils.types.ts +306 -306
  74. package/src/utils/auth/common.ts +118 -118
  75. package/src/utils/auth/discovery.test.js +12 -12
  76. package/src/utils/auth/discovery.ts +132 -132
  77. package/src/utils/base64.ts +27 -27
  78. package/src/utils/consolelogger.ts +333 -333
  79. package/src/utils/date.ts +172 -172
  80. package/src/utils/emails.ts +24 -24
  81. package/src/utils/knownscript.ts +286 -286
  82. package/src/utils/localstoragecache.ts +446 -446
  83. package/src/utils/rest.ts +501 -501
  84. package/src/utils/script.ts +170 -170
  85. package/src/utils/sharepoint.rest/common.ts +159 -159
  86. package/src/utils/sharepoint.rest/date.ts +62 -62
  87. package/src/utils/sharepoint.rest/file.folder.ts +685 -685
  88. package/src/utils/sharepoint.rest/item.ts +547 -547
  89. package/src/utils/sharepoint.rest/list.ts +1572 -1572
  90. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  91. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  92. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  93. package/src/utils/sharepoint.rest/location.ts +141 -141
  94. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  95. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  96. package/src/utils/sharepoint.rest/user.ts +558 -558
  97. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  98. package/src/utils/sod.ts +194 -194
  99. package/.madgerc +0 -3
  100. package/fix-folder-imports.js +0 -27
@@ -1,166 +1,166 @@
1
- import { IDictionary } from "../types/common.types";
2
- import { getGlobal, hasOwnProperty } from "./objects";
3
- import { isFunction, isNullOrUndefined, isNumber } from "./typecheckers";
4
-
5
- function _getGlobal() {
6
- let _global = getGlobal<{ promises: IDictionary<Promise<any>> }>("helpers_promises",
7
- {
8
- promises: {}
9
- });
10
- return _global;
11
- }
12
-
13
- /**
14
- * Lock all concurrent calls to a resource to one promise for a set duration of time.
15
- * @param {string} key - Unique key to identify the promise.
16
- * @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
17
- * @param {number} duration - Duration to hold on to the promise result. (default=1)
18
- * @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
19
- * @example
20
- * // returns Promise<string>
21
- * var initTests = await promiseLock<string>("initTests", async () => { ... }, 2);
22
- */
23
- export async function promiseLock<T>(key: string, promiseFunc: () => Promise<T>, duration = 1): Promise<T> {
24
- return promiseOnce(key, promiseFunc).then((result) => {
25
- (globalThis || window).setTimeout(() => {
26
- _deletePromiseByKey(key);
27
- }, isNumber(duration) && duration >= 1 ? duration : 1);
28
- return result;
29
- });
30
- }
31
- /**
32
- * Ensures that a promise runs only once
33
- * @param {string} key - Unique key to identify the promise.
34
- * @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
35
- * @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
36
- * by the promise is valid and should be kepy in memory.
37
- * @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
38
- * @example
39
- * // returns Promise<string>
40
- * var initTests = await promiseOnce<string>("initTests", async () => { ... });
41
- */
42
- export async function promiseOnce<T>(key: string, promiseFunc: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T> {
43
- let _global = _getGlobal();
44
- let promises = _global.promises;
45
-
46
- if (hasOwnProperty(promises, key) && isFunction(isValidResult)) {
47
- //we have en existing pending promise...
48
- let queuedResult: T = null;
49
- try { queuedResult = await promises[key]; } catch (e) { }
50
- if ((await isValidResult(queuedResult)) !== true) {
51
- _deletePromiseByKey(key);
52
- }
53
- }
54
-
55
- if (!hasOwnProperty(promises, key)) {
56
- promises[key] = promiseFunc();
57
- }
58
-
59
- return promises[key];
60
- }
61
-
62
- /**
63
- * Runs all promises in sequential order.
64
- * @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
65
- * @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
66
- * provided promise functions
67
- */
68
- export function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]> {
69
- if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
70
- return Promise.resolve([]);
71
- }
72
- return asyncFuncs.reduce((promiseChain, currentTaskcurrentTask) => (
73
- promiseChain.then((result) => {
74
- let taskResult = currentTaskcurrentTask();
75
- if (isNullOrUndefined(taskResult) || !isFunction(taskResult.then))//culprit - found one that did not return a promise?! make one.
76
- taskResult = Promise.resolve() as any as Promise<T>;
77
-
78
- return taskResult.then(Array.prototype.concat.bind(result));
79
- })
80
- ), Promise.resolve([]));
81
- }
82
-
83
- /**
84
- * Runs N promises in parallel.
85
- * @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
86
- * @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
87
- * @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
88
- * provided promise functions
89
- */
90
- export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel: number = 8): Promise<T[]> {
91
- if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
92
- return Promise.resolve([]);
93
- }
94
-
95
- let startChain = () => {
96
- let chainData = [];
97
-
98
- if (asyncFuncs.length) {
99
- let next = (data: T) => {
100
- chainData.push(data);
101
- return asyncFuncs.length ? (asyncFuncs.shift())().then(next) : chainData;
102
- };
103
- return (asyncFuncs.shift())().then(next);
104
- } else {
105
- return Promise.resolve(chainData);
106
- }
107
- };
108
-
109
- let chains = [];
110
- for (let k = 0; k < maxParallel; k += 1) {
111
- chains.push(startChain());
112
- }
113
-
114
- return Promise.all(chains).then(d => {
115
- //flatten results
116
- return d.reduce((acc, val) => acc.concat(val), []);
117
- });
118
- }
119
-
120
- /**
121
- * Provides an asnyc sleep function that allows you to delay async/wait calls.
122
- * @param {number} [seconds] - Time to sleep in seconds.
123
- */
124
- export function sleepAsync(seconds?: number): Promise<void> {
125
- return new Promise(resolve => {
126
- (globalThis || window).setTimeout(() => resolve(), seconds > 0 ? seconds * 1000 : 3000);
127
- });
128
- }
129
-
130
- /**
131
- * Provides the ability to retry an async function n times with a optional delay between calls
132
- * @param {(...args) => Promise<T>} fn - Function to retry,
133
- * @param {number} numberOfRetries - Number of times to retry.
134
- * @param {number} [seconds] - Delay between retries in seconds (default=1).
135
- */
136
- export async function retryAsync<T>(fn: (...args) => Promise<T>, numberOfRetries: number, seconds = 1) {
137
- let error: Error = null;
138
-
139
- for (let i = 0; i < numberOfRetries; i++) {
140
- try {
141
- error = null;
142
- await sleepAsync(i === 0 ? 0 : seconds);
143
- return await fn();
144
- } catch (ex) {
145
- error = ex;
146
- }
147
- }
148
-
149
- if (error) {
150
- throw error;
151
- }
152
- throw new Error(`Failed retrying ${numberOfRetries} times`);
153
- }
154
-
155
- function _deletePromiseByKey(key: string) {
156
- let _global = _getGlobal();
157
- let promises = _global.promises;
158
- if (hasOwnProperty(promises, key)) {
159
- try {
160
- delete promises[key];
161
- return true;
162
- } catch {
163
- return false;
164
- }
165
- }
1
+ import { IDictionary } from "../types/common.types";
2
+ import { getGlobal, hasOwnProperty } from "./objects";
3
+ import { isFunction, isNullOrUndefined, isNumber } from "./typecheckers";
4
+
5
+ function _getGlobal() {
6
+ let _global = getGlobal<{ promises: IDictionary<Promise<any>> }>("helpers_promises",
7
+ {
8
+ promises: {}
9
+ });
10
+ return _global;
11
+ }
12
+
13
+ /**
14
+ * Lock all concurrent calls to a resource to one promise for a set duration of time.
15
+ * @param {string} key - Unique key to identify the promise.
16
+ * @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
17
+ * @param {number} duration - Duration to hold on to the promise result. (default=1)
18
+ * @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
19
+ * @example
20
+ * // returns Promise<string>
21
+ * var initTests = await promiseLock<string>("initTests", async () => { ... }, 2);
22
+ */
23
+ export async function promiseLock<T>(key: string, promiseFunc: () => Promise<T>, duration = 1): Promise<T> {
24
+ return promiseOnce(key, promiseFunc).then((result) => {
25
+ (globalThis || window).setTimeout(() => {
26
+ _deletePromiseByKey(key);
27
+ }, isNumber(duration) && duration >= 1 ? duration : 1);
28
+ return result;
29
+ });
30
+ }
31
+ /**
32
+ * Ensures that a promise runs only once
33
+ * @param {string} key - Unique key to identify the promise.
34
+ * @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
35
+ * @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
36
+ * by the promise is valid and should be kepy in memory.
37
+ * @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
38
+ * @example
39
+ * // returns Promise<string>
40
+ * var initTests = await promiseOnce<string>("initTests", async () => { ... });
41
+ */
42
+ export async function promiseOnce<T>(key: string, promiseFunc: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T> {
43
+ let _global = _getGlobal();
44
+ let promises = _global.promises;
45
+
46
+ if (hasOwnProperty(promises, key) && isFunction(isValidResult)) {
47
+ //we have en existing pending promise...
48
+ let queuedResult: T = null;
49
+ try { queuedResult = await promises[key]; } catch (e) { }
50
+ if ((await isValidResult(queuedResult)) !== true) {
51
+ _deletePromiseByKey(key);
52
+ }
53
+ }
54
+
55
+ if (!hasOwnProperty(promises, key)) {
56
+ promises[key] = promiseFunc();
57
+ }
58
+
59
+ return promises[key];
60
+ }
61
+
62
+ /**
63
+ * Runs all promises in sequential order.
64
+ * @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
65
+ * @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
66
+ * provided promise functions
67
+ */
68
+ export function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]> {
69
+ if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
70
+ return Promise.resolve([]);
71
+ }
72
+ return asyncFuncs.reduce((promiseChain, currentTaskcurrentTask) => (
73
+ promiseChain.then((result) => {
74
+ let taskResult = currentTaskcurrentTask();
75
+ if (isNullOrUndefined(taskResult) || !isFunction(taskResult.then))//culprit - found one that did not return a promise?! make one.
76
+ taskResult = Promise.resolve() as any as Promise<T>;
77
+
78
+ return taskResult.then(Array.prototype.concat.bind(result));
79
+ })
80
+ ), Promise.resolve([]));
81
+ }
82
+
83
+ /**
84
+ * Runs N promises in parallel.
85
+ * @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
86
+ * @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
87
+ * @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
88
+ * provided promise functions
89
+ */
90
+ export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel: number = 8): Promise<T[]> {
91
+ if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
92
+ return Promise.resolve([]);
93
+ }
94
+
95
+ let startChain = () => {
96
+ let chainData = [];
97
+
98
+ if (asyncFuncs.length) {
99
+ let next = (data: T) => {
100
+ chainData.push(data);
101
+ return asyncFuncs.length ? (asyncFuncs.shift())().then(next) : chainData;
102
+ };
103
+ return (asyncFuncs.shift())().then(next);
104
+ } else {
105
+ return Promise.resolve(chainData);
106
+ }
107
+ };
108
+
109
+ let chains = [];
110
+ for (let k = 0; k < maxParallel; k += 1) {
111
+ chains.push(startChain());
112
+ }
113
+
114
+ return Promise.all(chains).then(d => {
115
+ //flatten results
116
+ return d.reduce((acc, val) => acc.concat(val), []);
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Provides an asnyc sleep function that allows you to delay async/wait calls.
122
+ * @param {number} [seconds] - Time to sleep in seconds.
123
+ */
124
+ export function sleepAsync(seconds?: number): Promise<void> {
125
+ return new Promise(resolve => {
126
+ (globalThis || window).setTimeout(() => resolve(), seconds > 0 ? seconds * 1000 : 3000);
127
+ });
128
+ }
129
+
130
+ /**
131
+ * Provides the ability to retry an async function n times with a optional delay between calls
132
+ * @param {(...args) => Promise<T>} fn - Function to retry,
133
+ * @param {number} numberOfRetries - Number of times to retry.
134
+ * @param {number} [seconds] - Delay between retries in seconds (default=1).
135
+ */
136
+ export async function retryAsync<T>(fn: (...args) => Promise<T>, numberOfRetries: number, seconds = 1) {
137
+ let error: Error = null;
138
+
139
+ for (let i = 0; i < numberOfRetries; i++) {
140
+ try {
141
+ error = null;
142
+ await sleepAsync(i === 0 ? 0 : seconds);
143
+ return await fn();
144
+ } catch (ex) {
145
+ error = ex;
146
+ }
147
+ }
148
+
149
+ if (error) {
150
+ throw error;
151
+ }
152
+ throw new Error(`Failed retrying ${numberOfRetries} times`);
153
+ }
154
+
155
+ function _deletePromiseByKey(key: string) {
156
+ let _global = _getGlobal();
157
+ let promises = _global.promises;
158
+ if (hasOwnProperty(promises, key)) {
159
+ try {
160
+ delete promises[key];
161
+ return true;
162
+ } catch {
163
+ return false;
164
+ }
165
+ }
166
166
  }
@@ -1,27 +1,27 @@
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
+ 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 +1,104 @@
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));
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
104
  });