@kwiz/common 1.0.1

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 (310) hide show
  1. package/LICENSE +21 -0
  2. package/dist/_dependencies.d.ts +1 -0
  3. package/dist/_dependencies.js +8 -0
  4. package/dist/_dependencies.js.map +1 -0
  5. package/dist/helpers/Guid.d.ts +119 -0
  6. package/dist/helpers/Guid.js +183 -0
  7. package/dist/helpers/Guid.js.map +1 -0
  8. package/dist/helpers/_dependencies.d.ts +2 -0
  9. package/dist/helpers/_dependencies.js +21 -0
  10. package/dist/helpers/_dependencies.js.map +1 -0
  11. package/dist/helpers/base64.d.ts +8 -0
  12. package/dist/helpers/base64.js +178 -0
  13. package/dist/helpers/base64.js.map +1 -0
  14. package/dist/helpers/browser.d.ts +144 -0
  15. package/dist/helpers/browser.js +1200 -0
  16. package/dist/helpers/browser.js.map +1 -0
  17. package/dist/helpers/browserinfo.d.ts +23 -0
  18. package/dist/helpers/browserinfo.js +215 -0
  19. package/dist/helpers/browserinfo.js.map +1 -0
  20. package/dist/helpers/collections.base.d.ts +128 -0
  21. package/dist/helpers/collections.base.js +430 -0
  22. package/dist/helpers/collections.base.js.map +1 -0
  23. package/dist/helpers/collections.d.ts +18 -0
  24. package/dist/helpers/collections.js +99 -0
  25. package/dist/helpers/collections.js.map +1 -0
  26. package/dist/helpers/color.d.ts +11 -0
  27. package/dist/helpers/color.js +57 -0
  28. package/dist/helpers/color.js.map +1 -0
  29. package/dist/helpers/cookies.d.ts +6 -0
  30. package/dist/helpers/cookies.js +56 -0
  31. package/dist/helpers/cookies.js.map +1 -0
  32. package/dist/helpers/date.d.ts +43 -0
  33. package/dist/helpers/date.js +158 -0
  34. package/dist/helpers/date.js.map +1 -0
  35. package/dist/helpers/debug.d.ts +33 -0
  36. package/dist/helpers/debug.js +177 -0
  37. package/dist/helpers/debug.js.map +1 -0
  38. package/dist/helpers/emails.d.ts +2 -0
  39. package/dist/helpers/emails.js +10 -0
  40. package/dist/helpers/emails.js.map +1 -0
  41. package/dist/helpers/eval.d.ts +1 -0
  42. package/dist/helpers/eval.js +10 -0
  43. package/dist/helpers/eval.js.map +1 -0
  44. package/dist/helpers/file.d.ts +11 -0
  45. package/dist/helpers/file.js +55 -0
  46. package/dist/helpers/file.js.map +1 -0
  47. package/dist/helpers/flatted.d.ts +20 -0
  48. package/dist/helpers/flatted.js +135 -0
  49. package/dist/helpers/flatted.js.map +1 -0
  50. package/dist/helpers/functions.d.ts +4 -0
  51. package/dist/helpers/functions.js +17 -0
  52. package/dist/helpers/functions.js.map +1 -0
  53. package/dist/helpers/graph/calendar.types.d.ts +3 -0
  54. package/dist/helpers/graph/calendar.types.js +15 -0
  55. package/dist/helpers/graph/calendar.types.js.map +1 -0
  56. package/dist/helpers/http.d.ts +25 -0
  57. package/dist/helpers/http.js +70 -0
  58. package/dist/helpers/http.js.map +1 -0
  59. package/dist/helpers/images.d.ts +16 -0
  60. package/dist/helpers/images.js +21 -0
  61. package/dist/helpers/images.js.map +1 -0
  62. package/dist/helpers/index.d.ts +29 -0
  63. package/dist/helpers/index.js +46 -0
  64. package/dist/helpers/index.js.map +1 -0
  65. package/dist/helpers/json.d.ts +3 -0
  66. package/dist/helpers/json.js +44 -0
  67. package/dist/helpers/json.js.map +1 -0
  68. package/dist/helpers/md5.d.ts +5 -0
  69. package/dist/helpers/md5.js +172 -0
  70. package/dist/helpers/md5.js.map +1 -0
  71. package/dist/helpers/objects.d.ts +49 -0
  72. package/dist/helpers/objects.js +271 -0
  73. package/dist/helpers/objects.js.map +1 -0
  74. package/dist/helpers/promises.d.ts +14 -0
  75. package/dist/helpers/promises.js +112 -0
  76. package/dist/helpers/promises.js.map +1 -0
  77. package/dist/helpers/random.d.ts +2 -0
  78. package/dist/helpers/random.js +32 -0
  79. package/dist/helpers/random.js.map +1 -0
  80. package/dist/helpers/scheduler/scheduler.d.ts +39 -0
  81. package/dist/helpers/scheduler/scheduler.js +109 -0
  82. package/dist/helpers/scheduler/scheduler.js.map +1 -0
  83. package/dist/helpers/sharepoint.d.ts +112 -0
  84. package/dist/helpers/sharepoint.js +629 -0
  85. package/dist/helpers/sharepoint.js.map +1 -0
  86. package/dist/helpers/strings.d.ts +56 -0
  87. package/dist/helpers/strings.js +282 -0
  88. package/dist/helpers/strings.js.map +1 -0
  89. package/dist/helpers/typecheckers.d.ts +56 -0
  90. package/dist/helpers/typecheckers.js +274 -0
  91. package/dist/helpers/typecheckers.js.map +1 -0
  92. package/dist/helpers/url.d.ts +46 -0
  93. package/dist/helpers/url.js +199 -0
  94. package/dist/helpers/url.js.map +1 -0
  95. package/dist/helpers/urlhelper.d.ts +16 -0
  96. package/dist/helpers/urlhelper.js +96 -0
  97. package/dist/helpers/urlhelper.js.map +1 -0
  98. package/dist/index.d.ts +15 -0
  99. package/dist/index.js +37 -0
  100. package/dist/index.js.map +1 -0
  101. package/dist/types/common.types.d.ts +19 -0
  102. package/dist/types/common.types.js +4 -0
  103. package/dist/types/common.types.js.map +1 -0
  104. package/dist/types/flatted.types.d.ts +49 -0
  105. package/dist/types/flatted.types.js +3 -0
  106. package/dist/types/flatted.types.js.map +1 -0
  107. package/dist/types/globals.types.d.ts +6 -0
  108. package/dist/types/globals.types.js +3 -0
  109. package/dist/types/globals.types.js.map +1 -0
  110. package/dist/types/graph/calendar.types.d.ts +79 -0
  111. package/dist/types/graph/calendar.types.js +3 -0
  112. package/dist/types/graph/calendar.types.js.map +1 -0
  113. package/dist/types/graph/index.d.ts +1 -0
  114. package/dist/types/graph/index.js +18 -0
  115. package/dist/types/graph/index.js.map +1 -0
  116. package/dist/types/index.d.ts +11 -0
  117. package/dist/types/index.js +28 -0
  118. package/dist/types/index.js.map +1 -0
  119. package/dist/types/knownscript.types.d.ts +22 -0
  120. package/dist/types/knownscript.types.js +3 -0
  121. package/dist/types/knownscript.types.js.map +1 -0
  122. package/dist/types/libs/datajs.types.d.ts +29 -0
  123. package/dist/types/libs/datajs.types.js +3 -0
  124. package/dist/types/libs/datajs.types.js.map +1 -0
  125. package/dist/types/libs/ics.types.d.ts +29 -0
  126. package/dist/types/libs/ics.types.js +3 -0
  127. package/dist/types/libs/ics.types.js.map +1 -0
  128. package/dist/types/libs/index.d.ts +3 -0
  129. package/dist/types/libs/index.js +20 -0
  130. package/dist/types/libs/index.js.map +1 -0
  131. package/dist/types/libs/msal.types.d.ts +33 -0
  132. package/dist/types/libs/msal.types.js +63 -0
  133. package/dist/types/libs/msal.types.js.map +1 -0
  134. package/dist/types/locales.d.ts +122 -0
  135. package/dist/types/locales.js +1 -0
  136. package/dist/types/locales.js.map +1 -0
  137. package/dist/types/localstoragecache.types.d.ts +8 -0
  138. package/dist/types/localstoragecache.types.js +3 -0
  139. package/dist/types/localstoragecache.types.js.map +1 -0
  140. package/dist/types/moment.d.ts +6 -0
  141. package/dist/types/moment.js +1 -0
  142. package/dist/types/moment.js.map +1 -0
  143. package/dist/types/regex.types.d.ts +14 -0
  144. package/dist/types/regex.types.js +3 -0
  145. package/dist/types/regex.types.js.map +1 -0
  146. package/dist/types/rest.types.d.ts +81 -0
  147. package/dist/types/rest.types.js +24 -0
  148. package/dist/types/rest.types.js.map +1 -0
  149. package/dist/types/sharepoint.types.d.ts +1432 -0
  150. package/dist/types/sharepoint.types.js +629 -0
  151. package/dist/types/sharepoint.types.js.map +1 -0
  152. package/dist/types/sharepoint.utils.types.d.ts +246 -0
  153. package/dist/types/sharepoint.utils.types.js +26 -0
  154. package/dist/types/sharepoint.utils.types.js.map +1 -0
  155. package/dist/utils/_dependencies.d.ts +3 -0
  156. package/dist/utils/_dependencies.js +24 -0
  157. package/dist/utils/_dependencies.js.map +1 -0
  158. package/dist/utils/base64.d.ts +2 -0
  159. package/dist/utils/base64.js +41 -0
  160. package/dist/utils/base64.js.map +1 -0
  161. package/dist/utils/consolelogger.d.ts +73 -0
  162. package/dist/utils/consolelogger.js +281 -0
  163. package/dist/utils/consolelogger.js.map +1 -0
  164. package/dist/utils/date.d.ts +10 -0
  165. package/dist/utils/date.js +30 -0
  166. package/dist/utils/date.js.map +1 -0
  167. package/dist/utils/emails.d.ts +2 -0
  168. package/dist/utils/emails.js +24 -0
  169. package/dist/utils/emails.js.map +1 -0
  170. package/dist/utils/index.d.ts +13 -0
  171. package/dist/utils/index.js +31 -0
  172. package/dist/utils/index.js.map +1 -0
  173. package/dist/utils/knownscript.d.ts +99 -0
  174. package/dist/utils/knownscript.js +261 -0
  175. package/dist/utils/knownscript.js.map +1 -0
  176. package/dist/utils/localstoragecache.d.ts +19 -0
  177. package/dist/utils/localstoragecache.js +372 -0
  178. package/dist/utils/localstoragecache.js.map +1 -0
  179. package/dist/utils/rest.d.ts +17 -0
  180. package/dist/utils/rest.js +402 -0
  181. package/dist/utils/rest.js.map +1 -0
  182. package/dist/utils/script.d.ts +33 -0
  183. package/dist/utils/script.js +169 -0
  184. package/dist/utils/script.js.map +1 -0
  185. package/dist/utils/sharepoint.rest/common.d.ts +32 -0
  186. package/dist/utils/sharepoint.rest/common.js +146 -0
  187. package/dist/utils/sharepoint.rest/common.js.map +1 -0
  188. package/dist/utils/sharepoint.rest/date.d.ts +2 -0
  189. package/dist/utils/sharepoint.rest/date.js +57 -0
  190. package/dist/utils/sharepoint.rest/date.js.map +1 -0
  191. package/dist/utils/sharepoint.rest/file.folder.d.ts +58 -0
  192. package/dist/utils/sharepoint.rest/file.folder.js +305 -0
  193. package/dist/utils/sharepoint.rest/file.folder.js.map +1 -0
  194. package/dist/utils/sharepoint.rest/index.d.ts +11 -0
  195. package/dist/utils/sharepoint.rest/index.js +28 -0
  196. package/dist/utils/sharepoint.rest/index.js.map +1 -0
  197. package/dist/utils/sharepoint.rest/item.d.ts +81 -0
  198. package/dist/utils/sharepoint.rest/item.js +444 -0
  199. package/dist/utils/sharepoint.rest/item.js.map +1 -0
  200. package/dist/utils/sharepoint.rest/list.d.ts +228 -0
  201. package/dist/utils/sharepoint.rest/list.js +868 -0
  202. package/dist/utils/sharepoint.rest/list.js.map +1 -0
  203. package/dist/utils/sharepoint.rest/listutils/GetListItemsByCaml.d.ts +19 -0
  204. package/dist/utils/sharepoint.rest/listutils/GetListItemsByCaml.js +629 -0
  205. package/dist/utils/sharepoint.rest/listutils/GetListItemsByCaml.js.map +1 -0
  206. package/dist/utils/sharepoint.rest/listutils/GetListItemsById.d.ts +14 -0
  207. package/dist/utils/sharepoint.rest/listutils/GetListItemsById.js +222 -0
  208. package/dist/utils/sharepoint.rest/listutils/GetListItemsById.js.map +1 -0
  209. package/dist/utils/sharepoint.rest/listutils/common.d.ts +3 -0
  210. package/dist/utils/sharepoint.rest/listutils/common.js +198 -0
  211. package/dist/utils/sharepoint.rest/listutils/common.js.map +1 -0
  212. package/dist/utils/sharepoint.rest/user-search.d.ts +76 -0
  213. package/dist/utils/sharepoint.rest/user-search.js +239 -0
  214. package/dist/utils/sharepoint.rest/user-search.js.map +1 -0
  215. package/dist/utils/sharepoint.rest/user.d.ts +71 -0
  216. package/dist/utils/sharepoint.rest/user.js +404 -0
  217. package/dist/utils/sharepoint.rest/user.js.map +1 -0
  218. package/dist/utils/sharepoint.rest/web.d.ts +107 -0
  219. package/dist/utils/sharepoint.rest/web.js +929 -0
  220. package/dist/utils/sharepoint.rest/web.js.map +1 -0
  221. package/dist/utils/sod.d.ts +29 -0
  222. package/dist/utils/sod.js +177 -0
  223. package/dist/utils/sod.js.map +1 -0
  224. package/package.json +60 -0
  225. package/readme.md +13 -0
  226. package/src/_dependencies.ts +2 -0
  227. package/src/helpers/Guid.ts +182 -0
  228. package/src/helpers/_dependencies.ts +3 -0
  229. package/src/helpers/base64.ts +174 -0
  230. package/src/helpers/browser.test.js +9 -0
  231. package/src/helpers/browser.ts +1299 -0
  232. package/src/helpers/browserinfo.ts +293 -0
  233. package/src/helpers/collections.base.test.js +26 -0
  234. package/src/helpers/collections.base.ts +439 -0
  235. package/src/helpers/collections.ts +108 -0
  236. package/src/helpers/color.ts +55 -0
  237. package/src/helpers/cookies.ts +55 -0
  238. package/src/helpers/date.test.js +97 -0
  239. package/src/helpers/date.ts +163 -0
  240. package/src/helpers/debug.ts +187 -0
  241. package/src/helpers/emails.ts +7 -0
  242. package/src/helpers/eval.ts +5 -0
  243. package/src/helpers/file.test.js +51 -0
  244. package/src/helpers/file.ts +59 -0
  245. package/src/helpers/flatted.ts +150 -0
  246. package/src/helpers/functions.ts +17 -0
  247. package/src/helpers/graph/calendar.types.ts +11 -0
  248. package/src/helpers/http.ts +70 -0
  249. package/src/helpers/images.ts +23 -0
  250. package/src/helpers/index.ts +29 -0
  251. package/src/helpers/json.ts +39 -0
  252. package/src/helpers/md5.ts +190 -0
  253. package/src/helpers/objects.test.js +27 -0
  254. package/src/helpers/objects.ts +256 -0
  255. package/src/helpers/promises.test.js +18 -0
  256. package/src/helpers/promises.ts +102 -0
  257. package/src/helpers/random.ts +27 -0
  258. package/src/helpers/scheduler/scheduler.test.js +104 -0
  259. package/src/helpers/scheduler/scheduler.ts +132 -0
  260. package/src/helpers/sharepoint.ts +681 -0
  261. package/src/helpers/strings.test.js +43 -0
  262. package/src/helpers/strings.ts +288 -0
  263. package/src/helpers/typecheckers.test.js +35 -0
  264. package/src/helpers/typecheckers.ts +263 -0
  265. package/src/helpers/url.test.js +18 -0
  266. package/src/helpers/url.ts +202 -0
  267. package/src/helpers/urlhelper.ts +104 -0
  268. package/src/index.ts +24 -0
  269. package/src/types/common.types.ts +16 -0
  270. package/src/types/flatted.types.ts +60 -0
  271. package/src/types/globals.types.ts +7 -0
  272. package/src/types/graph/calendar.types.ts +81 -0
  273. package/src/types/graph/index.ts +1 -0
  274. package/src/types/index.ts +12 -0
  275. package/src/types/knownscript.types.ts +19 -0
  276. package/src/types/libs/datajs.types.ts +29 -0
  277. package/src/types/libs/ics.types.ts +31 -0
  278. package/src/types/libs/index.ts +4 -0
  279. package/src/types/libs/msal.types.ts +79 -0
  280. package/src/types/locales.ts +125 -0
  281. package/src/types/localstoragecache.types.ts +9 -0
  282. package/src/types/moment.ts +7 -0
  283. package/src/types/regex.types.ts +17 -0
  284. package/src/types/rest.types.ts +94 -0
  285. package/src/types/sharepoint.types.ts +1413 -0
  286. package/src/types/sharepoint.utils.types.ts +246 -0
  287. package/src/utils/_dependencies.ts +4 -0
  288. package/src/utils/base64.ts +27 -0
  289. package/src/utils/consolelogger.ts +315 -0
  290. package/src/utils/date.ts +36 -0
  291. package/src/utils/emails.ts +25 -0
  292. package/src/utils/index.ts +13 -0
  293. package/src/utils/knownscript.ts +280 -0
  294. package/src/utils/localstoragecache.ts +419 -0
  295. package/src/utils/rest.ts +465 -0
  296. package/src/utils/script.ts +168 -0
  297. package/src/utils/sharepoint.rest/common.ts +149 -0
  298. package/src/utils/sharepoint.rest/date.ts +61 -0
  299. package/src/utils/sharepoint.rest/file.folder.ts +368 -0
  300. package/src/utils/sharepoint.rest/index.ts +11 -0
  301. package/src/utils/sharepoint.rest/item.ts +456 -0
  302. package/src/utils/sharepoint.rest/list.ts +1144 -0
  303. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +750 -0
  304. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +265 -0
  305. package/src/utils/sharepoint.rest/listutils/common.ts +202 -0
  306. package/src/utils/sharepoint.rest/user-search.ts +254 -0
  307. package/src/utils/sharepoint.rest/user.ts +447 -0
  308. package/src/utils/sharepoint.rest/web.ts +1031 -0
  309. package/src/utils/sod.ts +193 -0
  310. package/tsconfig.json +21 -0
@@ -0,0 +1,256 @@
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
+ }
@@ -0,0 +1,18 @@
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
+ });
@@ -0,0 +1,102 @@
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
+ }
@@ -0,0 +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
+ }
@@ -0,0 +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));
104
+ });
@@ -0,0 +1,132 @@
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
+ }