@kwiz/common 1.0.78 → 1.0.79

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