@develia/commons 0.4.16 → 0.4.18

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 (207) hide show
  1. package/dist/cjs/array.js +22 -2
  2. package/dist/cjs/array.js.map +1 -1
  3. package/dist/cjs/async-pool-executor.js +26 -11
  4. package/dist/cjs/async-pool-executor.js.map +1 -1
  5. package/dist/cjs/browser/form-utilities.js +35 -34
  6. package/dist/cjs/browser/form-utilities.js.map +1 -1
  7. package/dist/cjs/browser/geo-location.js +1 -1
  8. package/dist/cjs/browser/geo-location.js.map +1 -1
  9. package/dist/cjs/browser/native-bridge.js +3 -1
  10. package/dist/cjs/browser/native-bridge.js.map +1 -1
  11. package/dist/cjs/cancellable-promise.js +34 -13
  12. package/dist/cjs/cancellable-promise.js.map +1 -1
  13. package/dist/cjs/clean-url-search-params.js +26 -3
  14. package/dist/cjs/clean-url-search-params.js.map +1 -1
  15. package/dist/cjs/collections/cache-dictionary.js +3 -3
  16. package/dist/cjs/collections/cache-dictionary.js.map +1 -1
  17. package/dist/cjs/collections/default-map.js.map +1 -1
  18. package/dist/cjs/collections/default-weak-map.js.map +1 -1
  19. package/dist/cjs/collections/from.js +3 -3
  20. package/dist/cjs/collections/from.js.map +1 -1
  21. package/dist/cjs/collections/index.js.map +1 -1
  22. package/dist/cjs/draft.js +46 -11
  23. package/dist/cjs/draft.js.map +1 -1
  24. package/dist/cjs/event-bus.js +1 -1
  25. package/dist/cjs/event-bus.js.map +1 -1
  26. package/dist/cjs/functions.js +348 -0
  27. package/dist/cjs/functions.js.map +1 -0
  28. package/dist/cjs/index.js +1 -0
  29. package/dist/cjs/index.js.map +1 -1
  30. package/dist/cjs/key-value-pair.js.map +1 -1
  31. package/dist/cjs/lazy.js.map +1 -1
  32. package/dist/cjs/math.js +3 -0
  33. package/dist/cjs/math.js.map +1 -1
  34. package/dist/cjs/order.js.map +1 -1
  35. package/dist/cjs/parse.js +10 -1
  36. package/dist/cjs/parse.js.map +1 -1
  37. package/dist/cjs/patch.js.map +1 -1
  38. package/dist/cjs/stopwatch.js.map +1 -1
  39. package/dist/cjs/string.js +8 -5
  40. package/dist/cjs/string.js.map +1 -1
  41. package/dist/cjs/timer.js +1 -1
  42. package/dist/cjs/timer.js.map +1 -1
  43. package/dist/cjs/timespan.js +21 -8
  44. package/dist/cjs/timespan.js.map +1 -1
  45. package/dist/cjs/type-code.js.map +1 -1
  46. package/dist/cjs/type.js +20 -19
  47. package/dist/cjs/type.js.map +1 -1
  48. package/dist/cjs/types.js.map +1 -1
  49. package/dist/cjs/utilities.js +175 -39
  50. package/dist/cjs/utilities.js.map +1 -1
  51. package/dist/esm/array.js +21 -2
  52. package/dist/esm/array.js.map +1 -1
  53. package/dist/esm/async-pool-executor.js +26 -11
  54. package/dist/esm/async-pool-executor.js.map +1 -1
  55. package/dist/esm/browser/form-utilities.js +35 -34
  56. package/dist/esm/browser/form-utilities.js.map +1 -1
  57. package/dist/esm/browser/geo-location.js +1 -1
  58. package/dist/esm/browser/geo-location.js.map +1 -1
  59. package/dist/esm/browser/native-bridge.js +3 -1
  60. package/dist/esm/browser/native-bridge.js.map +1 -1
  61. package/dist/esm/cancellable-promise.js +34 -13
  62. package/dist/esm/cancellable-promise.js.map +1 -1
  63. package/dist/esm/clean-url-search-params.js +26 -3
  64. package/dist/esm/clean-url-search-params.js.map +1 -1
  65. package/dist/esm/collections/cache-dictionary.js +3 -3
  66. package/dist/esm/collections/cache-dictionary.js.map +1 -1
  67. package/dist/esm/collections/default-map.js.map +1 -1
  68. package/dist/esm/collections/default-weak-map.js.map +1 -1
  69. package/dist/esm/collections/from.js +3 -3
  70. package/dist/esm/collections/from.js.map +1 -1
  71. package/dist/esm/collections/index.js.map +1 -1
  72. package/dist/esm/draft.js +46 -11
  73. package/dist/esm/draft.js.map +1 -1
  74. package/dist/esm/event-bus.js +1 -1
  75. package/dist/esm/event-bus.js.map +1 -1
  76. package/dist/esm/functions.js +336 -0
  77. package/dist/esm/functions.js.map +1 -0
  78. package/dist/esm/index.js +1 -0
  79. package/dist/esm/index.js.map +1 -1
  80. package/dist/esm/key-value-pair.js.map +1 -1
  81. package/dist/esm/lazy.js.map +1 -1
  82. package/dist/esm/math.js +3 -0
  83. package/dist/esm/math.js.map +1 -1
  84. package/dist/esm/order.js.map +1 -1
  85. package/dist/esm/parse.js +10 -1
  86. package/dist/esm/parse.js.map +1 -1
  87. package/dist/esm/patch.js.map +1 -1
  88. package/dist/esm/stopwatch.js.map +1 -1
  89. package/dist/esm/string.js +8 -5
  90. package/dist/esm/string.js.map +1 -1
  91. package/dist/esm/timer.js +1 -1
  92. package/dist/esm/timer.js.map +1 -1
  93. package/dist/esm/timespan.js +21 -8
  94. package/dist/esm/timespan.js.map +1 -1
  95. package/dist/esm/type-code.js.map +1 -1
  96. package/dist/esm/type.js +20 -19
  97. package/dist/esm/type.js.map +1 -1
  98. package/dist/esm/types.js.map +1 -1
  99. package/dist/esm/utilities.js +174 -39
  100. package/dist/esm/utilities.js.map +1 -1
  101. package/dist/types/array.d.ts +20 -0
  102. package/dist/types/array.d.ts.map +1 -1
  103. package/dist/types/async-pool-executor.d.ts +2 -2
  104. package/dist/types/async-pool-executor.d.ts.map +1 -1
  105. package/dist/types/browser/form-utilities.d.ts.map +1 -1
  106. package/dist/types/browser/native-bridge.d.ts.map +1 -1
  107. package/dist/types/cancellable-promise.d.ts.map +1 -1
  108. package/dist/types/clean-url-search-params.d.ts +3 -1
  109. package/dist/types/clean-url-search-params.d.ts.map +1 -1
  110. package/dist/types/collections/from.d.ts +1 -1
  111. package/dist/types/draft.d.ts.map +1 -1
  112. package/dist/types/functions.d.ts +231 -0
  113. package/dist/types/functions.d.ts.map +1 -0
  114. package/dist/types/index.d.ts +1 -0
  115. package/dist/types/index.d.ts.map +1 -1
  116. package/dist/types/math.d.ts.map +1 -1
  117. package/dist/types/parse.d.ts.map +1 -1
  118. package/dist/types/string.d.ts.map +1 -1
  119. package/dist/types/timer.d.ts +1 -1
  120. package/dist/types/timespan.d.ts.map +1 -1
  121. package/dist/types/type.d.ts +2 -0
  122. package/dist/types/type.d.ts.map +1 -1
  123. package/dist/types/types.d.ts +1 -1
  124. package/dist/types/types.d.ts.map +1 -1
  125. package/dist/types/utilities.d.ts +21 -3
  126. package/dist/types/utilities.d.ts.map +1 -1
  127. package/dist/web/array.js +21 -2
  128. package/dist/web/array.js.map +1 -1
  129. package/dist/web/async-pool-executor.js +26 -11
  130. package/dist/web/async-pool-executor.js.map +1 -1
  131. package/dist/web/browser/form-utilities.js +35 -34
  132. package/dist/web/browser/form-utilities.js.map +1 -1
  133. package/dist/web/browser/geo-location.js +1 -1
  134. package/dist/web/browser/geo-location.js.map +1 -1
  135. package/dist/web/browser/native-bridge.js +3 -1
  136. package/dist/web/browser/native-bridge.js.map +1 -1
  137. package/dist/web/cancellable-promise.js +34 -13
  138. package/dist/web/cancellable-promise.js.map +1 -1
  139. package/dist/web/clean-url-search-params.js +26 -3
  140. package/dist/web/clean-url-search-params.js.map +1 -1
  141. package/dist/web/collections/cache-dictionary.js +3 -3
  142. package/dist/web/collections/cache-dictionary.js.map +1 -1
  143. package/dist/web/collections/default-map.js.map +1 -1
  144. package/dist/web/collections/default-weak-map.js.map +1 -1
  145. package/dist/web/collections/from.js +3 -3
  146. package/dist/web/collections/from.js.map +1 -1
  147. package/dist/web/collections/index.js.map +1 -1
  148. package/dist/web/draft.js +46 -11
  149. package/dist/web/draft.js.map +1 -1
  150. package/dist/web/event-bus.js +1 -1
  151. package/dist/web/event-bus.js.map +1 -1
  152. package/dist/web/functions.js +336 -0
  153. package/dist/web/functions.js.map +1 -0
  154. package/dist/web/index.js +1 -0
  155. package/dist/web/index.js.map +1 -1
  156. package/dist/web/key-value-pair.js.map +1 -1
  157. package/dist/web/lazy.js.map +1 -1
  158. package/dist/web/math.js +3 -0
  159. package/dist/web/math.js.map +1 -1
  160. package/dist/web/order.js.map +1 -1
  161. package/dist/web/parse.js +10 -1
  162. package/dist/web/parse.js.map +1 -1
  163. package/dist/web/patch.js.map +1 -1
  164. package/dist/web/stopwatch.js.map +1 -1
  165. package/dist/web/string.js +8 -5
  166. package/dist/web/string.js.map +1 -1
  167. package/dist/web/timer.js +1 -1
  168. package/dist/web/timer.js.map +1 -1
  169. package/dist/web/timespan.js +21 -8
  170. package/dist/web/timespan.js.map +1 -1
  171. package/dist/web/type-code.js.map +1 -1
  172. package/dist/web/type.js +20 -19
  173. package/dist/web/type.js.map +1 -1
  174. package/dist/web/types.js.map +1 -1
  175. package/dist/web/utilities.js +174 -39
  176. package/dist/web/utilities.js.map +1 -1
  177. package/package.json +66 -60
  178. package/src/array.ts +116 -84
  179. package/src/async-pool-executor.ts +107 -85
  180. package/src/browser/form-utilities.ts +32 -31
  181. package/src/browser/geo-location.ts +113 -113
  182. package/src/browser/native-bridge.ts +118 -116
  183. package/src/cancellable-promise.ts +102 -76
  184. package/src/clean-url-search-params.ts +36 -11
  185. package/src/collections/cache-dictionary.ts +115 -115
  186. package/src/collections/default-map.ts +48 -48
  187. package/src/collections/default-weak-map.ts +51 -51
  188. package/src/collections/from.ts +859 -859
  189. package/src/collections/index.ts +3 -3
  190. package/src/draft.ts +55 -16
  191. package/src/event-bus.ts +83 -83
  192. package/src/functions.ts +496 -0
  193. package/src/index.ts +1 -0
  194. package/src/key-value-pair.ts +24 -24
  195. package/src/lazy.ts +32 -32
  196. package/src/math.ts +33 -30
  197. package/src/order.ts +5 -5
  198. package/src/parse.ts +251 -240
  199. package/src/patch.ts +79 -79
  200. package/src/stopwatch.ts +43 -43
  201. package/src/string.ts +8 -5
  202. package/src/timer.ts +67 -67
  203. package/src/timespan.ts +412 -398
  204. package/src/type-code.ts +15 -15
  205. package/src/type.ts +372 -371
  206. package/src/types.ts +30 -30
  207. package/src/utilities.ts +653 -480
package/src/utilities.ts CHANGED
@@ -1,480 +1,653 @@
1
- // noinspection JSUnusedGlobalSymbols
2
-
3
-
4
- import KeyValuePair from "./key-value-pair.js";
5
- import type {Resolvable} from "./types.js";
6
- import Type from "./type.js";
7
-
8
- export function lazy<T extends object>(factory: () => T): T {
9
- let instance: T | null = null;
10
-
11
- const getInstance = (): T => {
12
- if (!instance) {
13
- instance = factory();
14
- }
15
- return instance;
16
- };
17
-
18
- return new Proxy({} as T, {
19
- get(_, prop, receiver) {
20
- return Reflect.get(getInstance(), prop, receiver);
21
- },
22
- set(_, prop, value, receiver) {
23
- return Reflect.set(getInstance(), prop, value, receiver);
24
- },
25
- has(_, prop) {
26
- return Reflect.has(getInstance(), prop);
27
- },
28
- deleteProperty(_, prop) {
29
- return Reflect.deleteProperty(getInstance(), prop);
30
- },
31
- ownKeys() {
32
- return Reflect.ownKeys(getInstance());
33
- },
34
- getOwnPropertyDescriptor(_, prop) {
35
- return Reflect.getOwnPropertyDescriptor(getInstance(), prop);
36
- },
37
- getPrototypeOf() {
38
- return Reflect.getPrototypeOf(getInstance());
39
- },
40
- });
41
- }
42
-
43
- /**
44
- * Checks if an object is iterable.
45
- *
46
- * @param {any} obj - The object to check.
47
- * @return {boolean} - Returns true if the object is iterable, otherwise false.
48
- */
49
- export function isIterable(obj: any): boolean {
50
- return obj[Symbol.iterator] === 'function';
51
- }
52
-
53
- /**
54
- * Checks if a given value is a string.
55
- *
56
- * @param {*} value - The value to check.
57
- * @return {boolean} - Returns true if the value is a string, otherwise returns false.
58
- */
59
- export function isString(value: any): value is string {
60
- return typeof value === 'string';
61
- }
62
-
63
- /**
64
- * Checks if a value is a number.
65
- *
66
- * @param {any} value - The value to check.
67
- * @return {boolean} - Returns true if the value is a number, otherwise false.
68
- */
69
- export function isNumber(value: any): value is number {
70
- return typeof value === 'number';
71
- }
72
-
73
- /**
74
- * Checks if a given value is a boolean.
75
- *
76
- * @param {any} value - The value to be checked.
77
- *
78
- * @return {boolean} - Returns true if the value is a boolean, otherwise false.
79
- */
80
- export function isBoolean(value: any): value is boolean {
81
- return typeof value === 'boolean';
82
- }
83
-
84
- /**
85
- * Checks if a value is an object.
86
- * @param {any} value - The value to be checked.
87
- * @returns {boolean} - Returns true if the value is an object, otherwise returns false.
88
- */
89
- export function isObject(value: any): value is object {
90
- return value != null && typeof value === 'object' && !Array.isArray(value);
91
- }
92
-
93
- /**
94
- * Determines if a value is an array.
95
- *
96
- * @param value - The value to be checked.
97
- *
98
- * @return Whether the value is an array.
99
- */
100
- export function isArray(value: any): value is any[] {
101
- return Array.isArray(value);
102
- }
103
-
104
- /**
105
- * Checks if a value is a function.
106
- *
107
- * @param {any} value - The value to be checked.
108
- * @return {boolean} - Returns true if the value is a function, otherwise returns false.
109
- */
110
- export function isFunction(value: any): value is Function {
111
- return typeof value === 'function';
112
- }
113
-
114
-
115
- /**
116
- * Structural deep equality check.
117
- *
118
- * Compares primitives by value, and objects/arrays/Maps/Sets/Dates/RegExps
119
- * by structure. Handles circular references.
120
- *
121
- * @param a - First value.
122
- * @param b - Second value.
123
- * @returns `true` if `a` and `b` are structurally equal.
124
- */
125
- export function deepEqual(a: unknown, b: unknown): boolean {
126
- return _deepEqual(a, b);
127
- }
128
-
129
- /**
130
- * Internal implementation of deep equality check.
131
- * @param a - First value.
132
- * @param b - Second value.
133
- * @param seen - Internal map used to handle circular references.
134
- */
135
- function _deepEqual(
136
- a: unknown,
137
- b: unknown,
138
- seen: WeakMap<object, unknown> = new WeakMap()
139
- ): boolean {
140
- if (a === b) return true;
141
- if (a === null || b === null) return false;
142
- if (typeof a !== typeof b) return false;
143
- if (typeof a !== "object") return false;
144
-
145
- const objA = a as object;
146
- if (seen.has(objA)) return seen.get(objA) === b;
147
- seen.set(objA, b);
148
-
149
- if (a instanceof Date) return b instanceof Date && a.getTime() === b.getTime();
150
- if (a instanceof RegExp) return b instanceof RegExp && a.toString() === b.toString();
151
- if (Array.isArray(a) !== Array.isArray(b)) return false;
152
-
153
- if (a instanceof Map) {
154
- if (!(b instanceof Map) || a.size !== b.size) return false;
155
- for (const [k, v] of a) {
156
- if (!b.has(k) || !_deepEqual(v, b.get(k), seen)) return false;
157
- }
158
- return true;
159
- }
160
-
161
- if (a instanceof Set) {
162
- if (!(b instanceof Set) || a.size !== b.size) return false;
163
- for (const v of a) if (!b.has(v)) return false;
164
- return true;
165
- }
166
-
167
- const keysA = Reflect.ownKeys(a as object);
168
- const keysB = Reflect.ownKeys(b as object);
169
- if (keysA.length !== keysB.length) return false;
170
-
171
- for (const key of keysA) {
172
- if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
173
- if (!_deepEqual((a as any)[key], (b as any)[key], seen)) return false;
174
- }
175
-
176
- return true;
177
- }
178
-
179
-
180
- /**
181
- * Submits an HTML form programmatically using the specified HTTP method (default is POST),
182
- * without using AJAX. Supports nested objects and arrays using bracket notation (e.g., `user[name]`, `tags[]`).
183
- *
184
- * @param url - The destination URL where the form will be submitted.
185
- * @param data - An object containing key-value pairs. Supports nested structures and arrays.
186
- * @param method - HTTP method to use: 'POST' or 'GET' (default: 'POST').
187
- * @param target - Optional parameter specifying the target for the form submission (e.g., '_blank', '_self', '_parent', '_top').
188
- */
189
-
190
-
191
- /**
192
- * Compares two objects or arrays to check if they have the same immediate fields and values,
193
- * or the same immediate elements if they are arrays (shallow equality check).
194
- *
195
- * @param {any} a - The first object or array.
196
- * @param {any} b - The second object or array.
197
- * @return {boolean} - Returns true if the objects/arrays are shallowly equal, otherwise false.
198
- */
199
- export function shallowEqual(a: any, b: any): boolean {
200
- if (a === b) {
201
- return true;
202
- }
203
-
204
- if (a === null || b === null || typeof a !== typeof b) {
205
- return false;
206
- }
207
-
208
- if (Array.isArray(a) && Array.isArray(b)) {
209
- if (a.length !== b.length) {
210
- return false;
211
- }
212
- return a.every((value, index) => value === b[index]);
213
- }
214
-
215
- if (typeof a === "object" && typeof b === "object") {
216
- const keysA = Object.keys(a);
217
- const keysB = Object.keys(b);
218
-
219
- if (keysA.length !== keysB.length) {
220
- return false;
221
- }
222
- return keysA.every(key => b.hasOwnProperty(key) && a[key] === b[key]);
223
- }
224
-
225
- return false;
226
- }
227
-
228
-
229
- /**
230
- * Pauses the execution of an asynchronous function for a specified duration.
231
- *
232
- * @param {number} ms - The delay duration in milliseconds.
233
- * @return {Promise<void>} A promise that resolves after the specified delay.
234
- */
235
- export async function delay(ms: number): Promise<void> {
236
- await new Promise(resolve => setTimeout(resolve, ms));
237
- }
238
-
239
- /**
240
- * Checks if a value is undefined.
241
- *
242
- * @param {any} value - The value to check.
243
- * @returns {boolean} - True if the value is undefined, false otherwise.
244
- */
245
- export function isUndefined(value: any): value is undefined {
246
- return typeof value === 'undefined';
247
- }
248
-
249
- /**
250
- * Checks if a value is defined or not.
251
- *
252
- * @param {any} value - The value to be checked.
253
- *
254
- * @return {boolean} - True if the value is defined, false otherwise.
255
- */
256
- export function isDefined(value: any): value is undefined {
257
- return typeof value !== 'undefined';
258
- }
259
-
260
- /**
261
- * Checks if a given value is null.
262
- *
263
- * @param {any} value - The value to check for null.
264
- * @return {boolean} - Returns true if the value is null, otherwise returns false.
265
- */
266
- export function isNull(value: any): value is null {
267
- return value === null;
268
- }
269
-
270
-
271
- /**
272
- * Determines whether the given value is of type bigint.
273
- * @param {any} value - The value to be checked.
274
- * @return {boolean} - Returns true if the value is of type bigint, false otherwise.
275
- */
276
- export function isBigInt(value: any): value is bigint {
277
- return typeof value === 'bigint';
278
- }
279
-
280
- /**
281
- * Checks if a given value is a symbol.
282
- *
283
- * @param {any} value - The value to be checked.
284
- * @return {boolean} - Returns true if the value is a symbol, false otherwise.
285
- */
286
- export function isSymbol(value: any): value is symbol {
287
- return typeof value === 'symbol';
288
- }
289
-
290
- /**
291
- * Checks if a value is null or undefined.
292
- *
293
- * @param {any} value - The value to check.
294
- * @return {boolean} - Returns true if the value is null or undefined, false otherwise.
295
- * @deprecated
296
- */
297
- export function isNullOrUndefined(value: any): boolean {
298
- return value === null || typeof value === 'undefined';
299
- }
300
-
301
- /**
302
- * Checks if a value is null or undefined.
303
- *
304
- * @param {any} value - The value to check.
305
- * @return {boolean} - Returns true if the value is null or undefined, false otherwise.
306
-
307
- */
308
- export function isNullish(value: any): boolean {
309
- return value === null || typeof value === 'undefined';
310
- }
311
-
312
- /**
313
- * Checks if a given value is empty.
314
- *
315
- * @param {any} value - The value to check.
316
- * @return {boolean} - Returns true if the value is empty, otherwise returns false.
317
- */
318
- export function isEmpty(value: any): boolean {
319
- return (Array.isArray(value) && value.length === 0) ||
320
- (typeof value === 'string' && value === '') ||
321
- value === null || typeof value === 'undefined';
322
- }
323
-
324
- /**
325
- * Check if a value is empty or contains only whitespace characters.
326
- *
327
- * @param {any} value - The value to check.
328
- * @return {boolean} Returns true if the value is empty or contains only whitespace characters, otherwise returns false.
329
- */
330
- export function isEmptyOrWhitespace(value: any): boolean {
331
- return (Array.isArray(value) && value.length === 0) ||
332
- (typeof value === 'string' && value.trim() === '') ||
333
- value === null || typeof value === 'undefined';
334
- }
335
-
336
-
337
- /**
338
- * Converts an object into an array of key-value pairs.
339
- *
340
- * @param {Record<string, any>} obj - The object to convert.
341
- * @return {KeyValuePair<string, any>[]} - The array of key-value pairs.
342
- */
343
- export function toPairs(obj: Record<string, any>): KeyValuePair<string, any>[] {
344
-
345
- let output = [];
346
- for (const key in obj) {
347
- output.push(new KeyValuePair(key, obj[key]));
348
- }
349
- return output;
350
-
351
- }
352
-
353
- /**
354
- * Creates an object composed of the picked object properties.
355
- *
356
- * @template T - The type of the source object.
357
- * @template K - The type of the keys to pick.
358
- * @param obj - The source object.
359
- * @param props - The property names to pick.
360
- * @returns A new object with the picked properties.
361
- */
362
- export function pick<T extends object, K extends keyof T>(obj: T, props: K[]): Pick<T, K> {
363
- const result = {} as Pick<T, K>;
364
- if (obj === null || typeof obj !== 'object') {
365
- return result;
366
- }
367
-
368
- for (const key of props) {
369
- if (key in obj) {
370
- result[key] = obj[key];
371
- }
372
- }
373
- return result;
374
- }
375
-
376
- /**
377
- * Converts a given thing into a Promise.
378
- * @template T
379
- * @param {any} thing - The thing to be converted into a Promise.
380
- * @returns {Promise<T>} - A Promise representing the given thing.
381
- */
382
- export function promisify<T>(thing: Resolvable<T>): Promise<T> {
383
-
384
- if (thing instanceof Promise)
385
- return thing;
386
-
387
-
388
- if (isFunction(thing)) {
389
- return new Promise<T>((resolve, reject) => {
390
- try {
391
- const result = thing();
392
- resolve(result);
393
- } catch (error) {
394
- reject(error);
395
- }
396
- });
397
- }
398
-
399
- return Promise.resolve(thing);
400
- }
401
-
402
-
403
- /**
404
- * Create a deep, independent copy of a value.
405
- *
406
- * Supports primitives, plain objects, arrays, `Date`, `RegExp`, `Map`, `Set`,
407
- * symbol-keyed properties and circular references. Functions are copied by
408
- * reference. Class instances preserve their prototype.
409
- *
410
- * @template T - The value's type.
411
- * @param value - The value to clone.
412
- * @returns A deep clone of `value`.
413
- */
414
- export function deepClone<T>(value: T): T {
415
- return _deepClone(value);
416
- }
417
-
418
- /**
419
- * Internal implementation of deep clone.
420
- * @param value - The value to clone.
421
- * @param seen - Internal map used to handle circular references.
422
- */
423
- function _deepClone<T>(
424
- value: T,
425
- seen: WeakMap<object, unknown> = new WeakMap()
426
- ): T {
427
- if (value === null || typeof value !== "object") return value;
428
-
429
- const obj = value as unknown as object;
430
- if (seen.has(obj)) return seen.get(obj) as T;
431
-
432
- if (value instanceof Date) return new Date(value) as T;
433
- if (value instanceof RegExp) return new RegExp(value) as T;
434
-
435
- if (value instanceof Map) {
436
- const copy = new Map();
437
- seen.set(value, copy);
438
- for (const [k, v] of value) {
439
- copy.set(_deepClone(k, seen), _deepClone(v, seen));
440
- }
441
- return copy as T;
442
- }
443
-
444
- if (value instanceof Set) {
445
- const copy = new Set();
446
- seen.set(value, copy);
447
- for (const v of value) {
448
- copy.add(_deepClone(v, seen));
449
- }
450
- return copy as T;
451
- }
452
-
453
- const copy: any = Array.isArray(value)
454
- ? []
455
- : Object.create(Object.getPrototypeOf(value));
456
-
457
- seen.set(obj, copy);
458
-
459
- for (const key of Reflect.ownKeys(obj)) {
460
- copy[key] = _deepClone((value as any)[key], seen);
461
- }
462
-
463
- return copy as T;
464
- }
465
-
466
-
467
- /**
468
- * Returns the type of the given value.
469
- *
470
- * @param {any} value - The value to determine the type of.
471
- * @return {Type} - The type of the given value.
472
- * @deprecated use Type.of(...)
473
- */
474
- export function getType(value: any): Type {
475
-
476
- return Type.of(value);
477
- }
478
-
479
-
480
-
1
+ // noinspection JSUnusedGlobalSymbols
2
+
3
+
4
+ import KeyValuePair from "./key-value-pair.js";
5
+ import type {Resolvable, Constructor} from "./types.js";
6
+ import Type from "./type.js";
7
+
8
+ export function lazy<T extends object>(factory: () => T): T {
9
+ let instance: T | null = null;
10
+
11
+ const getInstance = (): T => {
12
+ if (!instance) {
13
+ instance = factory();
14
+ }
15
+ return instance;
16
+ };
17
+
18
+ return new Proxy({} as T, {
19
+ get(_, prop, receiver) {
20
+ return Reflect.get(getInstance(), prop, receiver);
21
+ },
22
+ set(_, prop, value, receiver) {
23
+ return Reflect.set(getInstance(), prop, value, receiver);
24
+ },
25
+ has(_, prop) {
26
+ return Reflect.has(getInstance(), prop);
27
+ },
28
+ deleteProperty(_, prop) {
29
+ return Reflect.deleteProperty(getInstance(), prop);
30
+ },
31
+ ownKeys() {
32
+ return Reflect.ownKeys(getInstance());
33
+ },
34
+ getOwnPropertyDescriptor(_, prop) {
35
+ return Reflect.getOwnPropertyDescriptor(getInstance(), prop);
36
+ },
37
+ getPrototypeOf() {
38
+ return Reflect.getPrototypeOf(getInstance());
39
+ },
40
+ });
41
+ }
42
+
43
+ /**
44
+ * Checks if an object is iterable.
45
+ *
46
+ * @param {any} obj - The object to check.
47
+ * @return {boolean} - Returns true if the object is iterable, otherwise false.
48
+ */
49
+ export function isIterable(obj: any): boolean {
50
+ return obj != null && typeof obj[Symbol.iterator] === 'function';
51
+ }
52
+
53
+ /**
54
+ * Checks if a given value is a string.
55
+ *
56
+ * @param {*} value - The value to check.
57
+ * @return {boolean} - Returns true if the value is a string, otherwise returns false.
58
+ */
59
+ export function isString(value: any): value is string {
60
+ return typeof value === 'string';
61
+ }
62
+
63
+ /**
64
+ * Checks if a value is a number.
65
+ *
66
+ * @param {any} value - The value to check.
67
+ * @return {boolean} - Returns true if the value is a number, otherwise false.
68
+ */
69
+ export function isNumber(value: any): value is number {
70
+ return typeof value === 'number';
71
+ }
72
+
73
+ /**
74
+ * Checks if a given value is a boolean.
75
+ *
76
+ * @param {any} value - The value to be checked.
77
+ *
78
+ * @return {boolean} - Returns true if the value is a boolean, otherwise false.
79
+ */
80
+ export function isBoolean(value: any): value is boolean {
81
+ return typeof value === 'boolean';
82
+ }
83
+
84
+ /**
85
+ * Checks if a value is an object.
86
+ * @param {any} value - The value to be checked.
87
+ * @returns {boolean} - Returns true if the value is an object, otherwise returns false.
88
+ */
89
+ export function isObject(value: any): value is object {
90
+ return value != null && typeof value === 'object' && !Array.isArray(value);
91
+ }
92
+
93
+ /**
94
+ * Determines if a value is an array.
95
+ *
96
+ * @param value - The value to be checked.
97
+ *
98
+ * @return Whether the value is an array.
99
+ */
100
+ export function isArray(value: any): value is any[] {
101
+ return Array.isArray(value);
102
+ }
103
+
104
+ /**
105
+ * Checks if a value is a function.
106
+ *
107
+ * @param {any} value - The value to be checked.
108
+ * @return {boolean} - Returns true if the value is a function, otherwise returns false.
109
+ */
110
+ export function isFunction(value: any): value is Function {
111
+ return typeof value === 'function';
112
+ }
113
+
114
+
115
+ /**
116
+ * Structural deep equality check.
117
+ *
118
+ * Compares primitives by value, and objects/arrays/Maps/Sets/Dates/RegExps
119
+ * by structure. Handles circular references.
120
+ *
121
+ * @param a - First value.
122
+ * @param b - Second value.
123
+ * @param includeInherited - Whether inherited enumerable properties are compared. Defaults to `true`.
124
+ * @returns `true` if `a` and `b` are structurally equal.
125
+ */
126
+ export function deepEqual(a: unknown, b: unknown, includeInherited = true): boolean {
127
+ return _deepEqual(a, b, includeInherited);
128
+ }
129
+
130
+ /**
131
+ * Internal implementation of deep equality check.
132
+ * @param a - First value.
133
+ * @param b - Second value.
134
+ * @param includeInherited - Whether inherited enumerable properties are compared.
135
+ * @param seen - Internal map used to handle circular references.
136
+ */
137
+ function _deepEqual(
138
+ a: unknown,
139
+ b: unknown,
140
+ includeInherited: boolean,
141
+ seen: Map<object, unknown> = new Map()
142
+ ): boolean {
143
+ if (a === b) return true;
144
+ if (a === null || b === null) return false;
145
+ if (typeof a !== typeof b) return false;
146
+ if (typeof a !== "object") return false;
147
+
148
+ if (a instanceof Date || b instanceof Date) {
149
+ return a instanceof Date && b instanceof Date && a.getTime() === b.getTime();
150
+ }
151
+
152
+ if (a instanceof RegExp || b instanceof RegExp) {
153
+ return a instanceof RegExp && b instanceof RegExp && a.toString() === b.toString();
154
+ }
155
+
156
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
157
+
158
+ if (a instanceof Map || b instanceof Map) {
159
+ if (!(a instanceof Map) || !(b instanceof Map)) return false;
160
+ }
161
+
162
+ if (a instanceof Set || b instanceof Set) {
163
+ if (!(a instanceof Set) || !(b instanceof Set)) return false;
164
+ }
165
+
166
+ const objA = a as object;
167
+ if (seen.has(objA)) return seen.get(objA) === b;
168
+ seen.set(objA, b);
169
+
170
+ if (a instanceof Map && b instanceof Map) {
171
+ return _deepEqualMap(a, b, includeInherited, seen);
172
+ }
173
+
174
+ if (a instanceof Set && b instanceof Set) {
175
+ return _deepEqualSet(a, b, includeInherited, seen);
176
+ }
177
+
178
+ const objB = b as object;
179
+ const keysA = _deepEqualKeys(objA, includeInherited);
180
+ const keysB = _deepEqualKeys(objB, includeInherited);
181
+ if (keysA.length !== keysB.length) return false;
182
+
183
+ for (const key of keysA) {
184
+ if (!_deepEqualHasKey(objB, key, includeInherited)) return false;
185
+ if (!_deepEqual((a as any)[key], (b as any)[key], includeInherited, seen)) return false;
186
+ }
187
+
188
+ return true;
189
+ }
190
+
191
+ function _deepEqualMap(
192
+ a: Map<unknown, unknown>,
193
+ b: Map<unknown, unknown>,
194
+ includeInherited: boolean,
195
+ seen: Map<object, unknown>
196
+ ): boolean {
197
+ if (a.size !== b.size) return false;
198
+
199
+ const entriesB = Array.from(b.entries());
200
+ const matched = new Set<number>();
201
+
202
+ for (const [keyA, valueA] of a) {
203
+ let found = false;
204
+
205
+ for (let i = 0; i < entriesB.length; i++) {
206
+ if (matched.has(i)) continue;
207
+
208
+ const [keyB, valueB] = entriesB[i];
209
+ const candidateSeen = new Map(seen);
210
+ if (
211
+ _deepEqual(keyA, keyB, includeInherited, candidateSeen) &&
212
+ _deepEqual(valueA, valueB, includeInherited, candidateSeen)
213
+ ) {
214
+ matched.add(i);
215
+ _mergeDeepEqualSeen(candidateSeen, seen);
216
+ found = true;
217
+ break;
218
+ }
219
+ }
220
+
221
+ if (!found) return false;
222
+ }
223
+
224
+ return true;
225
+ }
226
+
227
+ function _deepEqualSet(
228
+ a: Set<unknown>,
229
+ b: Set<unknown>,
230
+ includeInherited: boolean,
231
+ seen: Map<object, unknown>
232
+ ): boolean {
233
+ if (a.size !== b.size) return false;
234
+
235
+ const valuesB = Array.from(b.values());
236
+ const matched = new Set<number>();
237
+
238
+ for (const valueA of a) {
239
+ let found = false;
240
+
241
+ for (let i = 0; i < valuesB.length; i++) {
242
+ if (matched.has(i)) continue;
243
+
244
+ const candidateSeen = new Map(seen);
245
+ if (_deepEqual(valueA, valuesB[i], includeInherited, candidateSeen)) {
246
+ matched.add(i);
247
+ _mergeDeepEqualSeen(candidateSeen, seen);
248
+ found = true;
249
+ break;
250
+ }
251
+ }
252
+
253
+ if (!found) return false;
254
+ }
255
+
256
+ return true;
257
+ }
258
+
259
+ function _deepEqualKeys(value: object, includeInherited: boolean): PropertyKey[] {
260
+ const keys: PropertyKey[] = [];
261
+ const seen = new Set<PropertyKey>();
262
+ const addKey = (key: PropertyKey) => {
263
+ if (seen.has(key)) return;
264
+ seen.add(key);
265
+ keys.push(key);
266
+ };
267
+
268
+ for (const key of Reflect.ownKeys(value)) {
269
+ addKey(key);
270
+ }
271
+
272
+ if (!includeInherited) return keys;
273
+
274
+ let prototype = Object.getPrototypeOf(value);
275
+ while (prototype !== null) {
276
+ for (const key of Reflect.ownKeys(prototype)) {
277
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, key);
278
+ if (descriptor?.enumerable) addKey(key);
279
+ }
280
+ prototype = Object.getPrototypeOf(prototype);
281
+ }
282
+
283
+ return keys;
284
+ }
285
+
286
+ function _deepEqualHasKey(value: object, key: PropertyKey, includeInherited: boolean): boolean {
287
+ return includeInherited
288
+ ? key in value
289
+ : Object.prototype.hasOwnProperty.call(value, key);
290
+ }
291
+
292
+ function _mergeDeepEqualSeen(source: Map<object, unknown>, target: Map<object, unknown>): void {
293
+ for (const [key, value] of source) {
294
+ target.set(key, value);
295
+ }
296
+ }
297
+
298
+
299
+ /**
300
+ * Submits an HTML form programmatically using the specified HTTP method (default is POST),
301
+ * without using AJAX. Supports nested objects and arrays using bracket notation (e.g., `user[name]`, `tags[]`).
302
+ *
303
+ * @param url - The destination URL where the form will be submitted.
304
+ * @param data - An object containing key-value pairs. Supports nested structures and arrays.
305
+ * @param method - HTTP method to use: 'POST' or 'GET' (default: 'POST').
306
+ * @param target - Optional parameter specifying the target for the form submission (e.g., '_blank', '_self', '_parent', '_top').
307
+ */
308
+
309
+
310
+ /**
311
+ * Compares two objects or arrays to check if they have the same immediate fields and values,
312
+ * or the same immediate elements if they are arrays (shallow equality check).
313
+ *
314
+ * @param {any} a - The first object or array.
315
+ * @param {any} b - The second object or array.
316
+ * @return {boolean} - Returns true if the objects/arrays are shallowly equal, otherwise false.
317
+ */
318
+ export function shallowEqual(a: any, b: any): boolean {
319
+ if (a === b) {
320
+ return true;
321
+ }
322
+
323
+ if (a === null || b === null || typeof a !== typeof b) {
324
+ return false;
325
+ }
326
+
327
+ if (Array.isArray(a) && Array.isArray(b)) {
328
+ if (a.length !== b.length) {
329
+ return false;
330
+ }
331
+ return a.every((value, index) => value === b[index]);
332
+ }
333
+
334
+ if (typeof a === "object" && typeof b === "object") {
335
+ const keysA = Object.keys(a);
336
+ const keysB = Object.keys(b);
337
+
338
+ if (keysA.length !== keysB.length) {
339
+ return false;
340
+ }
341
+ return keysA.every(key => Object.prototype.hasOwnProperty.call(b, key) && a[key] === b[key]);
342
+ }
343
+
344
+ return false;
345
+ }
346
+
347
+
348
+ /**
349
+ * Pauses the execution of an asynchronous function for a specified duration.
350
+ *
351
+ * @param {number} ms - The delay duration in milliseconds.
352
+ * @return {Promise<void>} A promise that resolves after the specified delay.
353
+ */
354
+ export async function delay(ms: number): Promise<void> {
355
+ await new Promise(resolve => setTimeout(resolve, ms));
356
+ }
357
+
358
+ /**
359
+ * Checks if a value is undefined.
360
+ *
361
+ * @param {any} value - The value to check.
362
+ * @returns {boolean} - True if the value is undefined, false otherwise.
363
+ */
364
+ export function isUndefined(value: any): value is undefined {
365
+ return typeof value === 'undefined';
366
+ }
367
+
368
+ /**
369
+ * Checks if a value is defined or not.
370
+ *
371
+ * @param {any} value - The value to be checked.
372
+ *
373
+ * @return {boolean} - True if the value is defined, false otherwise.
374
+ */
375
+ export function isDefined<T>(value: T): value is Exclude<T, undefined> {
376
+ return typeof value !== 'undefined';
377
+ }
378
+
379
+ /**
380
+ * Checks if a given value is null.
381
+ *
382
+ * @param {any} value - The value to check for null.
383
+ * @return {boolean} - Returns true if the value is null, otherwise returns false.
384
+ */
385
+ export function isNull(value: any): value is null {
386
+ return value === null;
387
+ }
388
+
389
+
390
+ /**
391
+ * Determines whether the given value is of type bigint.
392
+ * @param {any} value - The value to be checked.
393
+ * @return {boolean} - Returns true if the value is of type bigint, false otherwise.
394
+ */
395
+ export function isBigInt(value: any): value is bigint {
396
+ return typeof value === 'bigint';
397
+ }
398
+
399
+ /**
400
+ * Checks if a given value is a symbol.
401
+ *
402
+ * @param {any} value - The value to be checked.
403
+ * @return {boolean} - Returns true if the value is a symbol, false otherwise.
404
+ */
405
+ export function isSymbol(value: any): value is symbol {
406
+ return typeof value === 'symbol';
407
+ }
408
+
409
+ /**
410
+ * Checks if a value is null or undefined.
411
+ *
412
+ * @param {any} value - The value to check.
413
+ * @return {boolean} - Returns true if the value is null or undefined, false otherwise.
414
+ * @deprecated
415
+ */
416
+ export function isNullOrUndefined(value: any): boolean {
417
+ return isNullish(value);
418
+ }
419
+
420
+ /**
421
+ * Checks if a value is null or undefined.
422
+ *
423
+ * @param {any} value - The value to check.
424
+ * @return {boolean} - Returns true if the value is null or undefined, false otherwise.
425
+
426
+ */
427
+ export function isNullish(value: any): boolean {
428
+ return value === null || typeof value === 'undefined';
429
+ }
430
+
431
+ /**
432
+ * Checks if a given value is empty.
433
+ *
434
+ * @param {any} value - The value to check.
435
+ * @return {boolean} - Returns true if the value is empty, otherwise returns false.
436
+ */
437
+ export function isEmpty(value: any): boolean {
438
+ if (isNullish(value)) return true;
439
+ if (Array.isArray(value)) return value.length === 0;
440
+ if (typeof value === 'string') return value === '';
441
+ return false;
442
+ }
443
+
444
+ /**
445
+ * Check if a value is empty or contains only whitespace characters.
446
+ *
447
+ * @param {any} value - The value to check.
448
+ * @return {boolean} Returns true if the value is empty or contains only whitespace characters, otherwise returns false.
449
+ */
450
+ export function isEmptyOrWhitespace(value: any): boolean {
451
+ if (typeof value === 'string') return value.trim() === '';
452
+ return isEmpty(value);
453
+ }
454
+
455
+
456
+ /**
457
+ * Converts an object into an array of key-value pairs.
458
+ *
459
+ * @param {Record<string, any>} obj - The object to convert.
460
+ * @return {KeyValuePair<string, any>[]} - The array of key-value pairs.
461
+ */
462
+ export function toPairs(obj: Record<string, any>): KeyValuePair<string, any>[] {
463
+
464
+ let output = [];
465
+ for (const key in obj) {
466
+ output.push(new KeyValuePair(key, obj[key]));
467
+ }
468
+ return output;
469
+
470
+ }
471
+
472
+ /**
473
+ * Creates an object composed of the picked object properties.
474
+ *
475
+ * @template T - The type of the source object.
476
+ * @template K - The type of the keys to pick.
477
+ * @param obj - The source object.
478
+ * @param props - The property names to pick.
479
+ * @returns A new object with the picked properties.
480
+ */
481
+ export function pick<T extends object, K extends keyof T>(obj: T, props: K[]): Pick<T, K> {
482
+ const result = {} as Pick<T, K>;
483
+ if (obj === null || typeof obj !== 'object') {
484
+ return result;
485
+ }
486
+
487
+ for (const key of props) {
488
+ if (key in obj) {
489
+ result[key] = obj[key];
490
+ }
491
+ }
492
+ return result;
493
+ }
494
+
495
+ /**
496
+ * Converts a given thing into a Promise.
497
+ * @template T
498
+ * @param {any} thing - The thing to be converted into a Promise.
499
+ * @returns {Promise<T>} - A Promise representing the given thing.
500
+ */
501
+ export function promisify<T>(thing: Resolvable<T>): Promise<T> {
502
+
503
+ if (thing instanceof Promise)
504
+ return thing;
505
+
506
+
507
+ if (isFunction(thing)) {
508
+ return new Promise<T>((resolve, reject) => {
509
+ try {
510
+ const result = thing();
511
+ resolve(result);
512
+ } catch (error) {
513
+ reject(error);
514
+ }
515
+ });
516
+ }
517
+
518
+ return Promise.resolve(thing);
519
+ }
520
+
521
+
522
+ /**
523
+ * Create a deep, independent copy of a value.
524
+ *
525
+ * Supports primitives, plain objects, arrays, `Date`, `RegExp`, `Map`, `Set`,
526
+ * symbol-keyed properties and circular references. Functions are copied by
527
+ * reference. Class instances preserve their prototype.
528
+ *
529
+ * @template T - The value's type.
530
+ * @param value - The value to clone.
531
+ * @returns A deep clone of `value`.
532
+ */
533
+ export function deepClone<T>(value: T): T {
534
+ return _deepClone(value);
535
+ }
536
+
537
+ /**
538
+ * Internal implementation of deep clone.
539
+ * @param value - The value to clone.
540
+ * @param seen - Internal map used to handle circular references.
541
+ */
542
+ function _deepClone<T>(
543
+ value: T,
544
+ seen: WeakMap<object, unknown> = new WeakMap()
545
+ ): T {
546
+ if (value === null || typeof value !== "object") return value;
547
+
548
+ const obj = value as unknown as object;
549
+ if (seen.has(obj)) return seen.get(obj) as T;
550
+
551
+ if (value instanceof Date) return new Date(value) as T;
552
+ if (value instanceof RegExp) {
553
+ const copy = new RegExp(value);
554
+ copy.lastIndex = value.lastIndex;
555
+ return copy as T;
556
+ }
557
+
558
+ if (value instanceof Map) {
559
+ const copy = new Map();
560
+ seen.set(value, copy);
561
+ for (const [k, v] of value) {
562
+ copy.set(_deepClone(k, seen), _deepClone(v, seen));
563
+ }
564
+ return copy as T;
565
+ }
566
+
567
+ if (value instanceof Set) {
568
+ const copy = new Set();
569
+ seen.set(value, copy);
570
+ for (const v of value) {
571
+ copy.add(_deepClone(v, seen));
572
+ }
573
+ return copy as T;
574
+ }
575
+
576
+ const copy: any = Array.isArray(value)
577
+ ? []
578
+ : Object.create(Object.getPrototypeOf(value));
579
+
580
+ seen.set(obj, copy);
581
+
582
+ for (const key of Reflect.ownKeys(obj)) {
583
+ copy[key] = _deepClone((value as any)[key], seen);
584
+ }
585
+
586
+ return copy as T;
587
+ }
588
+
589
+
590
+ /**
591
+ * Returns the type of the given value.
592
+ *
593
+ * @param {any} value - The value to determine the type of.
594
+ * @return {Type} - The type of the given value.
595
+ * @deprecated use Type.of(...)
596
+ */
597
+ export function getType(value: any): Type {
598
+
599
+ return Type.of(value);
600
+ }
601
+
602
+ /**
603
+ * Patches a method onto a class prototype.
604
+ *
605
+ * This function allows adding new methods to existing classes (monkey-patching).
606
+ * If the method name already exists on the prototype, a warning is issued
607
+ * and the method is not overwritten.
608
+ *
609
+ * If `method` is a function, its name is extracted (if not provided) and
610
+ * it is wrapped to pass the instance (`this`) as the first argument.
611
+ *
612
+ * @template T - The type of the class constructor.
613
+ * @param {T} target - The class constructor whose prototype will be patched.
614
+ * @param {any} method - The implementation or value to patch.
615
+ * @param {string | symbol} [name] - Optional name for the method. If not provided and method is a function, method.name is used.
616
+ * @returns {boolean} Returns `true` if the method was successfully added, or `false` if it already existed.
617
+ */
618
+ export function monkeyPatch<T extends Constructor>(
619
+ target: T,
620
+ method: any,
621
+ name?: string | symbol
622
+ ): boolean {
623
+
624
+ let methodName;
625
+ let methodValue = method;
626
+
627
+ if (typeof method === 'function') {
628
+ methodName = name ?? (method as Function).name;
629
+ methodValue = function (this: any, ...args: any[]) {
630
+ return method.apply(this, [this, ...args]);
631
+ };
632
+ } else {
633
+ methodName = name;
634
+ }
635
+
636
+ if (!methodName) {
637
+ throw new Error("Monkey patch failed: No name provided and could not be extracted from function.");
638
+ }
639
+
640
+ if (methodName in target.prototype) {
641
+ console.warn(`Method '${String(methodName)}' already exists on ${target.name}`);
642
+ return false;
643
+ }
644
+
645
+ Object.defineProperty(target.prototype, methodName, {
646
+ value: methodValue,
647
+ writable: true,
648
+ configurable: true,
649
+ enumerable: false
650
+ });
651
+
652
+ return true;
653
+ }