@kwiz/common 1.0.96 → 1.0.97

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/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/strings.js +20 -1
  6. package/lib/cjs/helpers/strings.js.map +1 -1
  7. package/lib/cjs/types/libs/msal.types.js +26 -26
  8. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  9. package/lib/esm/helpers/strings.js +19 -1
  10. package/lib/esm/helpers/strings.js.map +1 -1
  11. package/lib/esm/types/libs/msal.types.js +26 -26
  12. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  13. package/lib/types/helpers/strings.d.ts +5 -0
  14. package/package.json +81 -81
  15. package/readme.md +17 -17
  16. package/src/_dependencies.ts +12 -12
  17. package/src/config.ts +17 -17
  18. package/src/helpers/Guid.ts +181 -181
  19. package/src/helpers/base64.ts +173 -173
  20. package/src/helpers/browser.test.js +13 -13
  21. package/src/helpers/browser.ts +1399 -1399
  22. package/src/helpers/browserinfo.ts +292 -292
  23. package/src/helpers/collections.base.test.js +25 -25
  24. package/src/helpers/collections.base.ts +437 -437
  25. package/src/helpers/collections.ts +107 -107
  26. package/src/helpers/color.ts +54 -54
  27. package/src/helpers/cookies.ts +59 -59
  28. package/src/helpers/date.test.js +119 -119
  29. package/src/helpers/date.ts +188 -188
  30. package/src/helpers/debug.ts +186 -186
  31. package/src/helpers/diagrams.ts +43 -43
  32. package/src/helpers/emails.ts +6 -6
  33. package/src/helpers/eval.ts +5 -5
  34. package/src/helpers/file.test.js +50 -50
  35. package/src/helpers/file.ts +60 -60
  36. package/src/helpers/flatted.ts +149 -149
  37. package/src/helpers/functions.ts +16 -16
  38. package/src/helpers/graph/calendar.types.ts +10 -10
  39. package/src/helpers/http.ts +69 -69
  40. package/src/helpers/images.ts +22 -22
  41. package/src/helpers/json.ts +38 -38
  42. package/src/helpers/md5.ts +189 -189
  43. package/src/helpers/objects.test.js +33 -33
  44. package/src/helpers/objects.ts +274 -274
  45. package/src/helpers/promises.test.js +37 -37
  46. package/src/helpers/promises.ts +165 -165
  47. package/src/helpers/random.ts +27 -27
  48. package/src/helpers/scheduler/scheduler.test.js +103 -103
  49. package/src/helpers/scheduler/scheduler.ts +131 -131
  50. package/src/helpers/sharepoint.ts +776 -776
  51. package/src/helpers/strings.test.js +122 -101
  52. package/src/helpers/strings.ts +337 -317
  53. package/src/helpers/typecheckers.test.js +34 -34
  54. package/src/helpers/typecheckers.ts +266 -266
  55. package/src/helpers/url.test.js +43 -43
  56. package/src/helpers/url.ts +207 -207
  57. package/src/helpers/urlhelper.ts +111 -111
  58. package/src/index.ts +6 -6
  59. package/src/types/auth.ts +54 -54
  60. package/src/types/common.types.ts +15 -15
  61. package/src/types/flatted.types.ts +59 -59
  62. package/src/types/globals.types.ts +6 -6
  63. package/src/types/graph/calendar.types.ts +80 -80
  64. package/src/types/knownscript.types.ts +18 -18
  65. package/src/types/libs/datajs.types.ts +28 -28
  66. package/src/types/libs/ics.types.ts +30 -30
  67. package/src/types/libs/msal.types.ts +49 -49
  68. package/src/types/locales.ts +124 -124
  69. package/src/types/localstoragecache.types.ts +8 -8
  70. package/src/types/location.types.ts +27 -27
  71. package/src/types/moment.ts +11 -11
  72. package/src/types/regex.types.ts +16 -16
  73. package/src/types/rest.types.ts +95 -95
  74. package/src/types/sharepoint.types.ts +1465 -1465
  75. package/src/types/sharepoint.utils.types.ts +287 -287
  76. package/src/utils/auth/common.ts +74 -74
  77. package/src/utils/auth/discovery.test.js +12 -12
  78. package/src/utils/auth/discovery.ts +132 -132
  79. package/src/utils/base64.ts +27 -27
  80. package/src/utils/consolelogger.ts +320 -320
  81. package/src/utils/date.ts +35 -35
  82. package/src/utils/emails.ts +24 -24
  83. package/src/utils/knownscript.ts +286 -286
  84. package/src/utils/localstoragecache.ts +441 -441
  85. package/src/utils/rest.ts +501 -501
  86. package/src/utils/script.ts +170 -170
  87. package/src/utils/sharepoint.rest/common.ts +154 -154
  88. package/src/utils/sharepoint.rest/date.ts +62 -62
  89. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  90. package/src/utils/sharepoint.rest/item.ts +547 -547
  91. package/src/utils/sharepoint.rest/list.ts +1480 -1480
  92. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  93. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  94. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  95. package/src/utils/sharepoint.rest/location.ts +141 -141
  96. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  97. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  98. package/src/utils/sharepoint.rest/user.ts +491 -491
  99. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  100. 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
  });