@kwiz/common 1.0.102 → 1.0.103

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 (95) hide show
  1. package/.github/workflows/npm-publish.yml +24 -24
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  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/types/libs/msal.types.js +26 -26
  8. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  9. package/package.json +81 -81
  10. package/readme.md +17 -17
  11. package/src/_dependencies.ts +12 -12
  12. package/src/config.ts +17 -17
  13. package/src/helpers/Guid.ts +181 -181
  14. package/src/helpers/base64.ts +173 -173
  15. package/src/helpers/browser.test.js +13 -13
  16. package/src/helpers/browser.ts +1399 -1399
  17. package/src/helpers/browserinfo.ts +292 -292
  18. package/src/helpers/collections.base.test.js +25 -25
  19. package/src/helpers/collections.base.ts +437 -437
  20. package/src/helpers/collections.ts +107 -107
  21. package/src/helpers/color.ts +54 -54
  22. package/src/helpers/cookies.ts +59 -59
  23. package/src/helpers/date.test.js +119 -119
  24. package/src/helpers/date.ts +188 -188
  25. package/src/helpers/debug.ts +186 -186
  26. package/src/helpers/diagrams.ts +43 -43
  27. package/src/helpers/emails.ts +6 -6
  28. package/src/helpers/eval.ts +5 -5
  29. package/src/helpers/file.test.js +50 -50
  30. package/src/helpers/file.ts +63 -63
  31. package/src/helpers/flatted.ts +149 -149
  32. package/src/helpers/functions.ts +16 -16
  33. package/src/helpers/graph/calendar.types.ts +10 -10
  34. package/src/helpers/http.ts +69 -69
  35. package/src/helpers/images.ts +22 -22
  36. package/src/helpers/json.ts +44 -44
  37. package/src/helpers/md5.ts +189 -189
  38. package/src/helpers/objects.test.js +33 -33
  39. package/src/helpers/objects.ts +274 -274
  40. package/src/helpers/promises.test.js +37 -37
  41. package/src/helpers/promises.ts +165 -165
  42. package/src/helpers/random.ts +27 -27
  43. package/src/helpers/scheduler/scheduler.test.js +103 -103
  44. package/src/helpers/scheduler/scheduler.ts +131 -131
  45. package/src/helpers/sharepoint.ts +785 -785
  46. package/src/helpers/strings.test.js +122 -122
  47. package/src/helpers/strings.ts +337 -337
  48. package/src/helpers/typecheckers.test.js +34 -34
  49. package/src/helpers/typecheckers.ts +266 -266
  50. package/src/helpers/url.test.js +43 -43
  51. package/src/helpers/url.ts +207 -207
  52. package/src/helpers/urlhelper.ts +111 -111
  53. package/src/index.ts +6 -6
  54. package/src/types/auth.ts +54 -54
  55. package/src/types/common.types.ts +15 -15
  56. package/src/types/flatted.types.ts +59 -59
  57. package/src/types/globals.types.ts +6 -6
  58. package/src/types/graph/calendar.types.ts +80 -80
  59. package/src/types/knownscript.types.ts +18 -18
  60. package/src/types/libs/datajs.types.ts +28 -28
  61. package/src/types/libs/ics.types.ts +30 -30
  62. package/src/types/libs/msal.types.ts +49 -49
  63. package/src/types/locales.ts +124 -124
  64. package/src/types/localstoragecache.types.ts +8 -8
  65. package/src/types/location.types.ts +27 -27
  66. package/src/types/moment.ts +11 -11
  67. package/src/types/regex.types.ts +16 -16
  68. package/src/types/rest.types.ts +95 -95
  69. package/src/types/sharepoint.types.ts +1466 -1466
  70. package/src/types/sharepoint.utils.types.ts +287 -287
  71. package/src/utils/auth/common.ts +74 -74
  72. package/src/utils/auth/discovery.test.js +12 -12
  73. package/src/utils/auth/discovery.ts +132 -132
  74. package/src/utils/base64.ts +27 -27
  75. package/src/utils/consolelogger.ts +320 -320
  76. package/src/utils/date.ts +35 -35
  77. package/src/utils/emails.ts +24 -24
  78. package/src/utils/knownscript.ts +286 -286
  79. package/src/utils/localstoragecache.ts +446 -446
  80. package/src/utils/rest.ts +501 -501
  81. package/src/utils/script.ts +170 -170
  82. package/src/utils/sharepoint.rest/common.ts +154 -154
  83. package/src/utils/sharepoint.rest/date.ts +62 -62
  84. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  85. package/src/utils/sharepoint.rest/item.ts +547 -547
  86. package/src/utils/sharepoint.rest/list.ts +1481 -1481
  87. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  88. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  89. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  90. package/src/utils/sharepoint.rest/location.ts +141 -141
  91. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  92. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  93. package/src/utils/sharepoint.rest/user.ts +491 -491
  94. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  95. package/src/utils/sod.ts +194 -194
@@ -1,275 +1,275 @@
1
- import { makeUniqueArray } from "./collections.base";
2
- import { jsonParse } from "./json";
3
- import { isDate, isFunction, isNotEmptyArray, isNullOrEmptyString, isNullOrUndefined, isObject, isPrimitiveValue, isString, 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
- location: {
10
- href: "", host: ""
11
- }
12
- } as any as Window : window;
13
-
14
- /** wrapper for hasOwnProperty that satisfies https://eslint.org/docs/latest/rules/no-prototype-builtins */
15
- export function hasOwnProperty(obj: any, prop: string) {
16
- if (!isNullOrUndefined(obj)) {
17
- return Object.prototype.hasOwnProperty.call(obj, prop);
18
- }
19
- return false;
20
- }
21
-
22
- /** empty async function */
23
- /* eslint-disable-next-line @typescript-eslint/no-empty-function */
24
- export var noop = async () => { };
25
- /** empty sync function */
26
- /* eslint-disable-next-line @typescript-eslint/no-empty-function */
27
- export var noops = () => { };
28
-
29
- /** get or create kwizcom object from top window or current window, set allowFromTop if you want to try to get from window.top */
30
- export function getKWizComGlobal(allowFromTop?: boolean) {
31
- if (allowFromTop) {
32
- try {
33
- $w.top["kwizcom"] = $w.top["kwizcom"] || {};
34
- return $w.top["kwizcom"] as IKWizComGlobals;
35
- } catch (ex) { }
36
- }
37
- $w["kwizcom"] = $w["kwizcom"] || {};
38
- return $w["kwizcom"] as IKWizComGlobals;
39
- }
40
- /** 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 */
41
- export function getGlobal<T>(name: string, defaults?: T, notFromTop?: boolean) {
42
- var kGlobal = getKWizComGlobal(notFromTop !== true);
43
- kGlobal.globals = kGlobal.globals || {};
44
-
45
- if (!kGlobal.globals[name]) {
46
- if (isObject(defaults)) {
47
- // eslint-disable-next-line @typescript-eslint/ban-types
48
- kGlobal.globals[name] = { ...(defaults as object) };
49
- } else {
50
- kGlobal.globals[name] = {};
51
- }
52
- }
53
- return kGlobal.globals[name] as T;
54
- }
55
-
56
- /**
57
- * Automatically bind all functions of instance to instance
58
- * Note: if you use knockout, you should skip ko.isObservable
59
- * @param instance
60
- */
61
- //eslint-disable-next-line @typescript-eslint/ban-types
62
- export function autoBind(instance: any, skip?: (name: string, f: Function) => boolean) {
63
- let funcitonNames = getAllFunctionNames(instance, 1);
64
- funcitonNames.forEach(prop => {
65
- const val = instance[prop];
66
- if (!isFunction(skip) || !skip(prop, val))
67
- instance[prop] = val.bind(instance);
68
- });
69
- }
70
-
71
- /** Implements Object.assign which does not exist in IE
72
- * Copies properties over from overrides into original object
73
- * Merge default and override settings: var merged = assign({},defaults,props)
74
- * Create deep copy of object by var copy = assign({},obj) */
75
- export function assign<T>(original: Partial<T>, ...overrides: Partial<T>[]): T {
76
- if (original === undefined || original === null) {
77
- throw new TypeError('Cannot convert first argument to object');
78
- }
79
-
80
- var to = Object(original);
81
- if (overrides && overrides.length > 0)
82
- overrides.forEach(o => {
83
- if (!isNullOrUndefined(o)) {
84
- var keysArray = Object.keys(Object(o));
85
- for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
86
- var nextKey = keysArray[nextIndex];
87
- var desc = Object.getOwnPropertyDescriptor(o, nextKey);
88
- if (typeof (desc.value) === "undefined" || desc.value === null) {
89
- to[nextKey] = desc.value;
90
- }
91
- else if (typeof (desc.value) === "object") {
92
- //reference type, call assign recursively. fixed problem that object value types from overrides were modified when the returnd object was modified.
93
- if (!Array.isArray(desc.value)) {
94
- to[nextKey] = assign({}, desc.value);
95
- } else {
96
- to[nextKey] = assign([], desc.value);
97
- }
98
- } else {
99
- if (desc !== undefined && desc.enumerable) {
100
- to[nextKey] = desc.value;//this will make a shallow copy, altering the override object o[nextKey];
101
- }
102
- }
103
- }
104
- }
105
- });
106
- return to as T;
107
- }
108
-
109
- export function primitivesEqual(o1: primitiveTypes, o2: primitiveTypes) {
110
- let normalize = (oo: any) => {
111
- if (isNullOrUndefined(oo))
112
- return null;
113
- else if (isDate(oo))
114
- return oo.getTime();
115
- return oo;
116
- };
117
- o1 = normalize(o1);
118
- o2 = normalize(o2);
119
-
120
- return o1 === o2;
121
- }
122
- export function objectsEqual<T extends object>(o1: T, o2: T, ignoreKeys?: string[]) {
123
- let isPrimitive1 = isPrimitiveValue(o1);
124
- let isPrimitive2 = isPrimitiveValue(o2);
125
-
126
- if (isPrimitive1 !== isPrimitive2) return false;//one primitive other not?
127
- if (isPrimitive1)
128
- return primitivesEqual(o1 as unknown as primitiveTypes, o2 as unknown as primitiveTypes);
129
-
130
- if (isFunction(o1) || isFunction(o2)) {
131
- try {
132
- return o1.toString() === o2.toString();
133
- } catch (e) {
134
- return false;
135
- }
136
- }
137
-
138
- ignoreKeys = ignoreKeys || [];
139
- let allKeys = makeUniqueArray(Object.keys(o1).concat(Object.keys(o2))).filter(key => !ignoreKeys.includes(key));
140
- for (let i = 0; i < allKeys.length; i++) {
141
- let key = allKeys[i];
142
- if (!objectsEqual(o1[key], o2[key], ignoreKeys))
143
- return false;
144
- }
145
- return true;
146
- }
147
- export function jsonClone<T>(obj: T): T {
148
- //todo: check if assign utility method is faster
149
- if (isNullOrUndefined(obj)) return null;
150
- let result = obj;
151
- try { result = jsonParse<T>(JSON.stringify(obj)); }
152
- catch (e) {
153
- if (isNotEmptyArray(obj))
154
- result = (obj as any).slice();
155
- else
156
- result = obj;
157
- }
158
-
159
- //clone date objects
160
- try { cloneDatesOnObjectRecursivily(obj, result); } catch (e) { }
161
- return result;
162
- }
163
-
164
- function cloneDatesOnObjectRecursivily(obj1, obj2) {
165
- Object.keys(obj1).forEach(key => {
166
- let v = obj1[key];
167
- if (v === null)
168
- obj2[key] = null;
169
- else if (isDate(v))
170
- obj2[key] = new Date(v.getTime());
171
- else if (isObject(v) && Object.keys(v).length) {
172
- let v2 = obj2[key];
173
- if (isObject(v2))
174
- cloneDatesOnObjectRecursivily(v, v2);
175
- }
176
- });
177
- }
178
-
179
- /** if an object in this path doesnt exist under parent - creates it.*/
180
- export function ensureObjectPath(objectPath: string, defaultValue: any = {}, parent: any = $w) {
181
- if (isNullOrEmptyString(objectPath)) return;
182
- let parts = objectPath.split('.');
183
- for (let i = 0; i < parts.length; i++) {
184
- let partName = parts[i];
185
- if (i === parts.length - 1)//last
186
- {
187
- if (isNullOrUndefined(parent[partName]))
188
- parent[partName] = defaultValue;
189
- }
190
- else {
191
- if (isNullOrUndefined(parent[partName]))
192
- parent[partName] = {};
193
- parent = parent[partName];
194
- }
195
- }
196
- }
197
-
198
- /** If o has propb and not propa - will copy propb into propa and remove propb */
199
- export function keepOne(o: any, propa: string, propb: string) {
200
- /* using the unkown type doesn't work in modern apps project */
201
- if (isObject(o)) {
202
- if (!hasOwnProperty(o, propa) && hasOwnProperty(o, propb)) {
203
- o[propa] = o[propb];
204
- delete o[propb];
205
- }
206
- }
207
- }
208
-
209
- /**return all members and functions of an object, including inherited ones from its base class, excluding the constructor
210
- * send prototypeLevels to limit the number of prototype climbs to get functions from. 0 means unlimited.
211
- */
212
- export function getAllMemberNames(instance: any, prototypeLevels: number): string[] {
213
- let props: string[] = [];
214
- let obj = instance;
215
- let level = 0;
216
- let unlimitedLevels = prototypeLevels < 0;
217
-
218
- do {
219
- props.push(...Object.getOwnPropertyNames(obj));
220
- obj = Object.getPrototypeOf(obj);
221
- level++;
222
- }
223
- while (unlimitedLevels ? !!obj : !!obj && level <= prototypeLevels);
224
-
225
- return makeUniqueArray(props.filter(p => p !== 'constructor' && p !== 'dispose'));
226
- }
227
- /**return all functions of an object, including inherited ones from its base class, excluding the constructor
228
- * send prototypeLevels to limit the number of prototype climbs to get functions from. 0 means unlimited.
229
- */
230
- export function getAllFunctionNames(instance: any, prototypeLevels: number): string[] {
231
- return getAllMemberNames(instance, prototypeLevels).filter(p => isFunction(instance[p]));
232
- }
233
-
234
- /** generic implementation of Object.values */
235
- export function objectValues<T = any>(obj: any): T[] {
236
- return Object.keys(obj).map((key) => {
237
- return obj[key];
238
- }) as T[];
239
- }
240
-
241
- export class DefaultProp<T> {
242
- private _value: T;
243
- private _defaultValue: T | (() => T);
244
- private isValid: (value: T) => boolean;
245
- public set value(newValue: T) { this._value = newValue; }
246
- public get value() {
247
- if (!this.isValid(this._value)) {
248
- this._value = isFunction(this._defaultValue)
249
- ? (this._defaultValue as () => T)()
250
- : this._defaultValue;
251
- }
252
- return this._value;
253
- }
254
- public constructor(defaultValue: T | (() => T), initialValue?: T, isValid?: (value: T) => boolean) {
255
- this._defaultValue = defaultValue;
256
- this._value = initialValue;
257
- this.isValid = isFunction(isValid) ? isValid : v => !isNullOrUndefined(v);
258
- }
259
- }
260
- /** creates a safe property, if the value is null/undefined or empty string - it will return the default value. */
261
- export function GetDefaultProp<T>(defaultValue: T | (() => T), initialValue?: T, isValid?: (value: T) => boolean) {
262
- return new DefaultProp(defaultValue, initialValue, isValid);
263
- }
264
-
265
- /** Get string error message from an error object */
266
- export function GetError(error: any, defaultError: string = "Unknown error"): string {
267
- const err = isNullOrUndefined(error)
268
- ? defaultError
269
- : isString(error)
270
- ? error
271
- : isString((error as Error).message)
272
- ? error.message
273
- : defaultError;
274
- return err.length > 0 ? err : defaultError;
1
+ import { makeUniqueArray } from "./collections.base";
2
+ import { jsonParse } from "./json";
3
+ import { isDate, isFunction, isNotEmptyArray, isNullOrEmptyString, isNullOrUndefined, isObject, isPrimitiveValue, isString, 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
+ location: {
10
+ href: "", host: ""
11
+ }
12
+ } as any as Window : window;
13
+
14
+ /** wrapper for hasOwnProperty that satisfies https://eslint.org/docs/latest/rules/no-prototype-builtins */
15
+ export function hasOwnProperty(obj: any, prop: string) {
16
+ if (!isNullOrUndefined(obj)) {
17
+ return Object.prototype.hasOwnProperty.call(obj, prop);
18
+ }
19
+ return false;
20
+ }
21
+
22
+ /** empty async function */
23
+ /* eslint-disable-next-line @typescript-eslint/no-empty-function */
24
+ export var noop = async () => { };
25
+ /** empty sync function */
26
+ /* eslint-disable-next-line @typescript-eslint/no-empty-function */
27
+ export var noops = () => { };
28
+
29
+ /** get or create kwizcom object from top window or current window, set allowFromTop if you want to try to get from window.top */
30
+ export function getKWizComGlobal(allowFromTop?: boolean) {
31
+ if (allowFromTop) {
32
+ try {
33
+ $w.top["kwizcom"] = $w.top["kwizcom"] || {};
34
+ return $w.top["kwizcom"] as IKWizComGlobals;
35
+ } catch (ex) { }
36
+ }
37
+ $w["kwizcom"] = $w["kwizcom"] || {};
38
+ return $w["kwizcom"] as IKWizComGlobals;
39
+ }
40
+ /** 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 */
41
+ export function getGlobal<T>(name: string, defaults?: T, notFromTop?: boolean) {
42
+ var kGlobal = getKWizComGlobal(notFromTop !== true);
43
+ kGlobal.globals = kGlobal.globals || {};
44
+
45
+ if (!kGlobal.globals[name]) {
46
+ if (isObject(defaults)) {
47
+ // eslint-disable-next-line @typescript-eslint/ban-types
48
+ kGlobal.globals[name] = { ...(defaults as object) };
49
+ } else {
50
+ kGlobal.globals[name] = {};
51
+ }
52
+ }
53
+ return kGlobal.globals[name] as T;
54
+ }
55
+
56
+ /**
57
+ * Automatically bind all functions of instance to instance
58
+ * Note: if you use knockout, you should skip ko.isObservable
59
+ * @param instance
60
+ */
61
+ //eslint-disable-next-line @typescript-eslint/ban-types
62
+ export function autoBind(instance: any, skip?: (name: string, f: Function) => boolean) {
63
+ let funcitonNames = getAllFunctionNames(instance, 1);
64
+ funcitonNames.forEach(prop => {
65
+ const val = instance[prop];
66
+ if (!isFunction(skip) || !skip(prop, val))
67
+ instance[prop] = val.bind(instance);
68
+ });
69
+ }
70
+
71
+ /** Implements Object.assign which does not exist in IE
72
+ * Copies properties over from overrides into original object
73
+ * Merge default and override settings: var merged = assign({},defaults,props)
74
+ * Create deep copy of object by var copy = assign({},obj) */
75
+ export function assign<T>(original: Partial<T>, ...overrides: Partial<T>[]): T {
76
+ if (original === undefined || original === null) {
77
+ throw new TypeError('Cannot convert first argument to object');
78
+ }
79
+
80
+ var to = Object(original);
81
+ if (overrides && overrides.length > 0)
82
+ overrides.forEach(o => {
83
+ if (!isNullOrUndefined(o)) {
84
+ var keysArray = Object.keys(Object(o));
85
+ for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
86
+ var nextKey = keysArray[nextIndex];
87
+ var desc = Object.getOwnPropertyDescriptor(o, nextKey);
88
+ if (typeof (desc.value) === "undefined" || desc.value === null) {
89
+ to[nextKey] = desc.value;
90
+ }
91
+ else if (typeof (desc.value) === "object") {
92
+ //reference type, call assign recursively. fixed problem that object value types from overrides were modified when the returnd object was modified.
93
+ if (!Array.isArray(desc.value)) {
94
+ to[nextKey] = assign({}, desc.value);
95
+ } else {
96
+ to[nextKey] = assign([], desc.value);
97
+ }
98
+ } else {
99
+ if (desc !== undefined && desc.enumerable) {
100
+ to[nextKey] = desc.value;//this will make a shallow copy, altering the override object o[nextKey];
101
+ }
102
+ }
103
+ }
104
+ }
105
+ });
106
+ return to as T;
107
+ }
108
+
109
+ export function primitivesEqual(o1: primitiveTypes, o2: primitiveTypes) {
110
+ let normalize = (oo: any) => {
111
+ if (isNullOrUndefined(oo))
112
+ return null;
113
+ else if (isDate(oo))
114
+ return oo.getTime();
115
+ return oo;
116
+ };
117
+ o1 = normalize(o1);
118
+ o2 = normalize(o2);
119
+
120
+ return o1 === o2;
121
+ }
122
+ export function objectsEqual<T extends object>(o1: T, o2: T, ignoreKeys?: string[]) {
123
+ let isPrimitive1 = isPrimitiveValue(o1);
124
+ let isPrimitive2 = isPrimitiveValue(o2);
125
+
126
+ if (isPrimitive1 !== isPrimitive2) return false;//one primitive other not?
127
+ if (isPrimitive1)
128
+ return primitivesEqual(o1 as unknown as primitiveTypes, o2 as unknown as primitiveTypes);
129
+
130
+ if (isFunction(o1) || isFunction(o2)) {
131
+ try {
132
+ return o1.toString() === o2.toString();
133
+ } catch (e) {
134
+ return false;
135
+ }
136
+ }
137
+
138
+ ignoreKeys = ignoreKeys || [];
139
+ let allKeys = makeUniqueArray(Object.keys(o1).concat(Object.keys(o2))).filter(key => !ignoreKeys.includes(key));
140
+ for (let i = 0; i < allKeys.length; i++) {
141
+ let key = allKeys[i];
142
+ if (!objectsEqual(o1[key], o2[key], ignoreKeys))
143
+ return false;
144
+ }
145
+ return true;
146
+ }
147
+ export function jsonClone<T>(obj: T): T {
148
+ //todo: check if assign utility method is faster
149
+ if (isNullOrUndefined(obj)) return null;
150
+ let result = obj;
151
+ try { result = jsonParse<T>(JSON.stringify(obj)); }
152
+ catch (e) {
153
+ if (isNotEmptyArray(obj))
154
+ result = (obj as any).slice();
155
+ else
156
+ result = obj;
157
+ }
158
+
159
+ //clone date objects
160
+ try { cloneDatesOnObjectRecursivily(obj, result); } catch (e) { }
161
+ return result;
162
+ }
163
+
164
+ function cloneDatesOnObjectRecursivily(obj1, obj2) {
165
+ Object.keys(obj1).forEach(key => {
166
+ let v = obj1[key];
167
+ if (v === null)
168
+ obj2[key] = null;
169
+ else if (isDate(v))
170
+ obj2[key] = new Date(v.getTime());
171
+ else if (isObject(v) && Object.keys(v).length) {
172
+ let v2 = obj2[key];
173
+ if (isObject(v2))
174
+ cloneDatesOnObjectRecursivily(v, v2);
175
+ }
176
+ });
177
+ }
178
+
179
+ /** if an object in this path doesnt exist under parent - creates it.*/
180
+ export function ensureObjectPath(objectPath: string, defaultValue: any = {}, parent: any = $w) {
181
+ if (isNullOrEmptyString(objectPath)) return;
182
+ let parts = objectPath.split('.');
183
+ for (let i = 0; i < parts.length; i++) {
184
+ let partName = parts[i];
185
+ if (i === parts.length - 1)//last
186
+ {
187
+ if (isNullOrUndefined(parent[partName]))
188
+ parent[partName] = defaultValue;
189
+ }
190
+ else {
191
+ if (isNullOrUndefined(parent[partName]))
192
+ parent[partName] = {};
193
+ parent = parent[partName];
194
+ }
195
+ }
196
+ }
197
+
198
+ /** If o has propb and not propa - will copy propb into propa and remove propb */
199
+ export function keepOne(o: any, propa: string, propb: string) {
200
+ /* using the unkown type doesn't work in modern apps project */
201
+ if (isObject(o)) {
202
+ if (!hasOwnProperty(o, propa) && hasOwnProperty(o, propb)) {
203
+ o[propa] = o[propb];
204
+ delete o[propb];
205
+ }
206
+ }
207
+ }
208
+
209
+ /**return all members and functions of an object, including inherited ones from its base class, excluding the constructor
210
+ * send prototypeLevels to limit the number of prototype climbs to get functions from. 0 means unlimited.
211
+ */
212
+ export function getAllMemberNames(instance: any, prototypeLevels: number): string[] {
213
+ let props: string[] = [];
214
+ let obj = instance;
215
+ let level = 0;
216
+ let unlimitedLevels = prototypeLevels < 0;
217
+
218
+ do {
219
+ props.push(...Object.getOwnPropertyNames(obj));
220
+ obj = Object.getPrototypeOf(obj);
221
+ level++;
222
+ }
223
+ while (unlimitedLevels ? !!obj : !!obj && level <= prototypeLevels);
224
+
225
+ return makeUniqueArray(props.filter(p => p !== 'constructor' && p !== 'dispose'));
226
+ }
227
+ /**return all functions of an object, including inherited ones from its base class, excluding the constructor
228
+ * send prototypeLevels to limit the number of prototype climbs to get functions from. 0 means unlimited.
229
+ */
230
+ export function getAllFunctionNames(instance: any, prototypeLevels: number): string[] {
231
+ return getAllMemberNames(instance, prototypeLevels).filter(p => isFunction(instance[p]));
232
+ }
233
+
234
+ /** generic implementation of Object.values */
235
+ export function objectValues<T = any>(obj: any): T[] {
236
+ return Object.keys(obj).map((key) => {
237
+ return obj[key];
238
+ }) as T[];
239
+ }
240
+
241
+ export class DefaultProp<T> {
242
+ private _value: T;
243
+ private _defaultValue: T | (() => T);
244
+ private isValid: (value: T) => boolean;
245
+ public set value(newValue: T) { this._value = newValue; }
246
+ public get value() {
247
+ if (!this.isValid(this._value)) {
248
+ this._value = isFunction(this._defaultValue)
249
+ ? (this._defaultValue as () => T)()
250
+ : this._defaultValue;
251
+ }
252
+ return this._value;
253
+ }
254
+ public constructor(defaultValue: T | (() => T), initialValue?: T, isValid?: (value: T) => boolean) {
255
+ this._defaultValue = defaultValue;
256
+ this._value = initialValue;
257
+ this.isValid = isFunction(isValid) ? isValid : v => !isNullOrUndefined(v);
258
+ }
259
+ }
260
+ /** creates a safe property, if the value is null/undefined or empty string - it will return the default value. */
261
+ export function GetDefaultProp<T>(defaultValue: T | (() => T), initialValue?: T, isValid?: (value: T) => boolean) {
262
+ return new DefaultProp(defaultValue, initialValue, isValid);
263
+ }
264
+
265
+ /** Get string error message from an error object */
266
+ export function GetError(error: any, defaultError: string = "Unknown error"): string {
267
+ const err = isNullOrUndefined(error)
268
+ ? defaultError
269
+ : isString(error)
270
+ ? error
271
+ : isString((error as Error).message)
272
+ ? error.message
273
+ : defaultError;
274
+ return err.length > 0 ? err : defaultError;
275
275
  }
@@ -1,38 +1,38 @@
1
- import assert from 'assert/strict';
2
- import test from 'node:test';
3
- import { promiseLock, promiseOnce, sleepAsync } from './promises';
4
-
5
-
6
- test('promiseOnce', async t => {
7
- let 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
- });
19
-
20
- test('promisLock', async t => {
21
-
22
- let promiseLockTester = async() => {
23
- return new Date().getTime();
24
- };
25
-
26
- let p1 = promiseLock("promiseLockTest", promiseLockTester);
27
- let p2 = promiseLock("promiseLockTest", promiseLockTester);
28
-
29
- let v1 = await p1;
30
- let v2 = await p2;
31
-
32
- await sleepAsync(1);
33
-
34
- let v3 = await promiseLock("promiseLockTest", promiseLockTester);
35
-
36
- await t.test("During lock", t => assert.strictEqual(v1 === v2, true));
37
- await t.test("After lock", t => assert.strictEqual(v1 === v3, false));
1
+ import assert from 'assert/strict';
2
+ import test from 'node:test';
3
+ import { promiseLock, promiseOnce, sleepAsync } from './promises';
4
+
5
+
6
+ test('promiseOnce', async t => {
7
+ let 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
+ });
19
+
20
+ test('promisLock', async t => {
21
+
22
+ let promiseLockTester = async() => {
23
+ return new Date().getTime();
24
+ };
25
+
26
+ let p1 = promiseLock("promiseLockTest", promiseLockTester);
27
+ let p2 = promiseLock("promiseLockTest", promiseLockTester);
28
+
29
+ let v1 = await p1;
30
+ let v2 = await p2;
31
+
32
+ await sleepAsync(1);
33
+
34
+ let v3 = await promiseLock("promiseLockTest", promiseLockTester);
35
+
36
+ await t.test("During lock", t => assert.strictEqual(v1 === v2, true));
37
+ await t.test("After lock", t => assert.strictEqual(v1 === v3, false));
38
38
  });