@develia/commons 0.3.17 → 0.3.20

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.
@@ -0,0 +1,345 @@
1
+ // noinspection JSUnusedGlobalSymbols
2
+
3
+
4
+ import Pair from "./pair";
5
+ import {Optional, Provider} from "./types";
6
+ import {Type} from "./typing";
7
+
8
+ /**
9
+ * Checks if an object is iterable.
10
+ *
11
+ * @param {any} obj - The object to check.
12
+ * @return {boolean} - Returns true if the object is iterable, otherwise false.
13
+ */
14
+ export function isIterable(obj: any): boolean {
15
+ return obj[Symbol.iterator] === 'function';
16
+ }
17
+
18
+ /**
19
+ * Checks if a given value is a string.
20
+ *
21
+ * @param {*} value - The value to check.
22
+ * @return {boolean} - Returns true if the value is a string, otherwise returns false.
23
+ */
24
+ export function isString(value: any): value is string {
25
+ return typeof value === 'string';
26
+ }
27
+
28
+ /**
29
+ * Checks if a value is a number.
30
+ *
31
+ * @param {any} value - The value to check.
32
+ * @return {boolean} - Returns true if the value is a number, otherwise false.
33
+ */
34
+ export function isNumber(value: any): value is number {
35
+ return typeof value === 'number';
36
+ }
37
+
38
+ /**
39
+ * Checks if a given value is a boolean.
40
+ *
41
+ * @param {any} value - The value to be checked.
42
+ *
43
+ * @return {boolean} - Returns true if the value is a boolean, otherwise false.
44
+ */
45
+ export function isBoolean(value: any): value is boolean {
46
+ return typeof value === 'boolean';
47
+ }
48
+
49
+ /**
50
+ * Checks if a value is an object.
51
+ * @param {any} value - The value to be checked.
52
+ * @returns {boolean} - Returns true if the value is an object, otherwise returns false.
53
+ */
54
+ export function isObject(value: any): value is object {
55
+ return value != null && typeof value === 'object' && !Array.isArray(value);
56
+ }
57
+
58
+ /**
59
+ * Determines if a value is an array.
60
+ *
61
+ * @param value - The value to be checked.
62
+ *
63
+ * @return Whether the value is an array.
64
+ */
65
+ export function isArray(value: any): value is any[] {
66
+ return Array.isArray(value);
67
+ }
68
+
69
+ /**
70
+ * Checks if a value is a function.
71
+ *
72
+ * @param {any} value - The value to be checked.
73
+ * @return {boolean} - Returns true if the value is a function, otherwise returns false.
74
+ */
75
+ export function isFunction(value: any): value is Function {
76
+ return typeof value === 'function';
77
+ }
78
+
79
+ /**
80
+ * Checks if a value is undefined.
81
+ *
82
+ * @param {any} value - The value to check.
83
+ * @returns {boolean} - True if the value is undefined, false otherwise.
84
+ */
85
+ export function isUndefined(value: any): value is undefined {
86
+ return typeof value === 'undefined';
87
+ }
88
+
89
+ /**
90
+ * Checks if a value is defined or not.
91
+ *
92
+ * @param {any} value - The value to be checked.
93
+ *
94
+ * @return {boolean} - True if the value is defined, false otherwise.
95
+ */
96
+ export function isDefined(value: any): value is undefined {
97
+ return typeof value !== 'undefined';
98
+ }
99
+
100
+ /**
101
+ * Checks if a given value is null.
102
+ *
103
+ * @param {any} value - The value to check for null.
104
+ * @return {boolean} - Returns true if the value is null, otherwise returns false.
105
+ */
106
+ export function isNull(value: any): value is null {
107
+ return value === null;
108
+ }
109
+
110
+ /**
111
+ * Determines whether the given value is of type bigint.
112
+ * @param {any} value - The value to be checked.
113
+ * @return {boolean} - Returns true if the value is of type bigint, false otherwise.
114
+ */
115
+ export function isBigInt(value: any): value is bigint {
116
+ return typeof value === 'bigint';
117
+ }
118
+
119
+ /**
120
+ * Checks if a given value is a symbol.
121
+ *
122
+ * @param {any} value - The value to be checked.
123
+ * @return {boolean} - Returns true if the value is a symbol, false otherwise.
124
+ */
125
+ export function isSymbol(value: any): value is symbol {
126
+ return typeof value === 'symbol';
127
+ }
128
+
129
+ /**
130
+ * Checks if a value is null or undefined.
131
+ *
132
+ * @param {any} value - The value to check.
133
+ * @return {boolean} - Returns true if the value is null or undefined, false otherwise.
134
+ */
135
+ export function isNullOrUndefined(value: any): boolean {
136
+ return value === null || typeof value === 'undefined';
137
+ }
138
+
139
+ /**
140
+ * Checks if a given value is empty.
141
+ *
142
+ * @param {any} value - The value to check.
143
+ * @return {boolean} - Returns true if the value is empty, otherwise returns false.
144
+ */
145
+ export function isEmpty(value: any): boolean {
146
+ return (Array.isArray(value) && value.length === 0) ||
147
+ (typeof value === 'string' && value === '') ||
148
+ value === null || typeof value === 'undefined';
149
+ }
150
+
151
+ /**
152
+ * Check if a value is empty or contains only whitespace characters.
153
+ *
154
+ * @param {any} value - The value to check.
155
+ * @return {boolean} Returns true if the value is empty or contains only whitespace characters, otherwise returns false.
156
+ */
157
+ export function isEmptyOrWhitespace(value: any): boolean {
158
+ return (Array.isArray(value) && value.length === 0) ||
159
+ (typeof value === 'string' && value.trim() === '') ||
160
+ value === null || typeof value === 'undefined';
161
+ }
162
+
163
+
164
+ /**
165
+ * Submits a form via AJAX and returns a Promise that resolves to the Response object.
166
+ *
167
+ * @param {HTMLFormElement | string} selectorOrElement - The form element or selector.
168
+ * @return {Promise<Response>} A Promise that resolves to the Response object.
169
+ * @throws {Error} If the element is invalid.
170
+ */
171
+ export async function ajaxSubmit(selectorOrElement: HTMLFormElement | string): Promise<Response> {
172
+
173
+ const form = typeof selectorOrElement === 'string'
174
+ ? document.querySelector(selectorOrElement)
175
+ : selectorOrElement;
176
+
177
+ if (!(form instanceof HTMLFormElement)) {
178
+ throw new Error("Invalid element.");
179
+ }
180
+
181
+ // Crear un objeto FormData a partir del formulario
182
+ return await fetch(form.action, {
183
+ method: form.method,
184
+ body: new FormData(form),
185
+ });
186
+ }
187
+
188
+ /**
189
+ * Converts an object into an array of key-value pairs.
190
+ *
191
+ * @param {Record<string, any>} obj - The object to convert.
192
+ * @return {Pair<string, any>[]} - The array of key-value pairs.
193
+ */
194
+ export function toPairs(obj: Record<string, any>): Pair<string, any>[] {
195
+
196
+ let output = [];
197
+ for (const key in obj) {
198
+ output.push(new Pair(key, obj[key]));
199
+ }
200
+ return output;
201
+
202
+ }
203
+
204
+ /**
205
+ * Converts a given thing into a Promise.
206
+ * @template T
207
+ * @param {any} thing - The thing to be converted into a Promise.
208
+ * @returns {Promise<T>} - A Promise representing the given thing.
209
+ */
210
+ export function promisify<T>(thing: any): Promise<T> {
211
+
212
+ if (thing instanceof Promise)
213
+ return thing;
214
+
215
+
216
+ if (isFunction(thing)) {
217
+ return new Promise<T>((resolve, reject) => {
218
+ try {
219
+ const result = thing();
220
+ resolve(result);
221
+ } catch (error) {
222
+ reject(error);
223
+ }
224
+ });
225
+ }
226
+
227
+ return Promise.resolve(thing);
228
+ }
229
+
230
+
231
+ export function ajaxSubmission(selectorOrElement: HTMLFormElement | string,
232
+ onSuccess: Optional<Provider<any>> = undefined,
233
+ onFailure: Optional<Provider<any>> = undefined) : void {
234
+
235
+ const form = typeof selectorOrElement === 'string'
236
+ ? document.querySelector(selectorOrElement)
237
+ : selectorOrElement;
238
+
239
+ if (!(form instanceof HTMLFormElement)) {
240
+ return;
241
+ }
242
+
243
+ form.addEventListener('submit', async (event) => {
244
+ event.preventDefault();
245
+ let promise = ajaxSubmit(form);
246
+ if (promise) {
247
+ if (onSuccess)
248
+ promise = promise.then(onSuccess);
249
+ if (onFailure)
250
+ promise.catch(onFailure);
251
+ }
252
+
253
+ });
254
+
255
+
256
+ }
257
+
258
+
259
+ /**
260
+ * Creates a deep clone of the given object.
261
+ * @template T
262
+ * @param {T} obj - The object to clone.
263
+ * @return {T} - The deep clone of the given object.
264
+ */
265
+ export function deepClone<T>(obj: T): T {
266
+ if (obj === null || typeof obj !== 'object') {
267
+ return obj;
268
+ }
269
+
270
+ if (Array.isArray(obj)) {
271
+ const copy: any[] = [];
272
+ for (const element of obj) {
273
+ copy.push(deepClone(element));
274
+ }
275
+ return copy as T;
276
+ }
277
+
278
+ const copy: { [key: string]: any } = {};
279
+ for (const key in obj) {
280
+ if (obj.hasOwnProperty(key)) {
281
+ copy[key] = deepClone((obj as { [key: string]: any })[key]);
282
+ }
283
+ }
284
+ return copy as T;
285
+ }
286
+
287
+ /**
288
+ * Returns the type of the given value.
289
+ *
290
+ * @param {any} value - The value to determine the type of.
291
+ * @return {Type} - The type of the given value.
292
+ */
293
+ export function getType(value: any): Type {
294
+
295
+ return value === null ? Type.Null : Type[typeof value as keyof typeof Type];
296
+ }
297
+
298
+ /**
299
+ * Converts an object to `FormData` format recursively.
300
+ *
301
+ * @param {Record<string, any>} data - The object data to convert.
302
+ * @param {FormData} formData - The `FormData` instance to append data to.
303
+ * @param {string} parentKey - The parent key to append to child keys in the `FormData`.
304
+ * @returns {FormData} - The `FormData` instance with the converted data.
305
+ */
306
+ function _objectToFormData(data: Record<string, any>, formData: FormData, parentKey: string = ''): FormData {
307
+
308
+ for (const key in data) {
309
+ if (data.hasOwnProperty(key)) {
310
+ const value = data[key];
311
+
312
+ if (value instanceof Date) {
313
+ formData.append(parentKey ? `${parentKey}[${key}]` : key, value.toISOString());
314
+ } else if (value instanceof File) {
315
+ formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
316
+ } else if (typeof value === 'object' && !Array.isArray(value)) {
317
+ _objectToFormData(value, formData, parentKey ? `${parentKey}[${key}]` : key);
318
+ } else if (Array.isArray(value)) {
319
+ value.forEach((item, index) => {
320
+ const arrayKey = `${parentKey ? `${parentKey}[${key}]` : key}[${index}]`;
321
+ if (typeof item === 'object' && !Array.isArray(item)) {
322
+ _objectToFormData(item, formData, arrayKey);
323
+ } else {
324
+ formData.append(arrayKey, item);
325
+ }
326
+ });
327
+ } else {
328
+ formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
329
+ }
330
+ }
331
+ }
332
+
333
+ return formData;
334
+ }
335
+
336
+ /**
337
+ * Converts an object into FormData.
338
+ *
339
+ * @param {Record<string, any>} data - The object to be converted into FormData.
340
+ * @return {FormData} - The FormData object representing the converted data.
341
+ */
342
+ export function objectToFormData(data: Record<string, any>): FormData {
343
+ let formData = new FormData();
344
+ return _objectToFormData(data, formData);
345
+ }
package/src/functions.ts DELETED
@@ -1,247 +0,0 @@
1
- // noinspection JSUnusedGlobalSymbols
2
-
3
-
4
- import Pair from "./pair";
5
- import {Nullable, Provider} from "./types";
6
-
7
- export function isIterable(obj: any): boolean {
8
- return obj[Symbol.iterator] === 'function';
9
- }
10
-
11
- export function clamp(n: number, min: number, max: number) {
12
- if (n <= min)
13
- return min;
14
- if (n >= max)
15
- return max;
16
- return n
17
- }
18
-
19
- /**
20
- * Linearly remaps a value from its source range [`inMin`, `inMax`] to the destination range [`outMin`, `outMax`]
21
- *
22
- * @category Math
23
- * @example
24
- * ```
25
- * const value = remap(0.5, 0, 1, 200, 400) // value will be 300
26
- * ```
27
- */
28
- export function lerp(n: number, inMin: number, inMax: number, outMin: number, outMax: number) {
29
-
30
-
31
- return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin))
32
-
33
-
34
- }
35
-
36
-
37
- /**
38
- * Ensure prefix of a string
39
- *
40
- * @category String
41
- */
42
- export function ensurePrefix(prefix: string, str: string) {
43
- if (!str.startsWith(prefix))
44
- return prefix + str
45
- return str
46
- }
47
-
48
- /**
49
- * Ensure suffix of a string
50
- *
51
- * @category String
52
- */
53
- export function ensureSuffix(suffix: string, str: string) {
54
- if (!str.endsWith(suffix))
55
- return str + suffix
56
- return str
57
- }
58
-
59
- export function isString(value: any): value is string {
60
- return typeof value === 'string';
61
- }
62
-
63
- export function isNumber(value: any): value is number {
64
- return typeof value === 'number';
65
- }
66
-
67
- export function isBoolean(value: any): value is boolean {
68
- return typeof value === 'boolean';
69
- }
70
-
71
- export function isObject(value: any): value is object {
72
- return value !== null && typeof value === 'object' && !Array.isArray(value);
73
- }
74
-
75
- export function isArray(value: any): value is any[] {
76
- return Array.isArray(value);
77
- }
78
-
79
- export function isFunction(value: any): value is Function {
80
- return typeof value === 'function';
81
- }
82
-
83
- export function isUndefined(value: any): value is undefined {
84
- return typeof value === 'undefined';
85
- }
86
-
87
- export function isNull(value: any): value is null {
88
- return value === null;
89
- }
90
-
91
- export function isBigInt(value: any): value is bigint {
92
- return typeof value === 'bigint';
93
- }
94
-
95
- export function isSymbol(value: any): value is symbol {
96
- return typeof value === 'symbol';
97
- }
98
-
99
- export function isNullOrUndefined(value: any) {
100
- return value === null || typeof value === 'undefined';
101
- }
102
-
103
- export function isEmpty(value: any): boolean {
104
- return (Array.isArray(value) && value.length === 0) ||
105
- (typeof value === 'string' && value === '') ||
106
- value === null || typeof value === 'undefined';
107
- }
108
-
109
- export function isEmptyOrWhitespace(value: any): boolean {
110
- return (Array.isArray(value) && value.length === 0) ||
111
- (typeof value === 'string' && value.trim() === '') ||
112
- value === null || typeof value === 'undefined';
113
- }
114
-
115
-
116
- export async function ajaxSubmit(selectorOrElement: HTMLFormElement | string): Promise<Response> {
117
-
118
- const form = typeof selectorOrElement === 'string'
119
- ? document.querySelector(selectorOrElement)
120
- : selectorOrElement;
121
-
122
- if (!(form instanceof HTMLFormElement)) {
123
- throw new Error("Invalid element.");
124
- }
125
-
126
- // Crear un objeto FormData a partir del formulario
127
- return await fetch(form.action, {
128
- method: form.method,
129
- body: new FormData(form),
130
- });
131
- }
132
-
133
- export function toPairs(obj: any): Pair<any, any>[] {
134
-
135
- let output = [];
136
- for (const key in obj) {
137
- output.push(new Pair(key, obj[key]));
138
- }
139
- return output;
140
-
141
- }
142
-
143
- export function promisify<T>(thing: any): Promise<T> {
144
-
145
- if (thing instanceof Promise)
146
- return thing;
147
-
148
-
149
- if (isFunction(thing)) {
150
- return new Promise<T>((resolve, reject) => {
151
- try {
152
- const result = thing();
153
- resolve(result);
154
- } catch (error) {
155
- reject(error);
156
- }
157
- });
158
- }
159
-
160
- return Promise.resolve(thing);
161
- }
162
-
163
- export function ajaxSubmission(selectorOrElement: HTMLFormElement | string,
164
- onSuccess: Nullable<Provider<Nullable<any>>> = null,
165
- onFailure: Nullable<Provider<Nullable<any>>> = null) {
166
-
167
- const form = typeof selectorOrElement === 'string'
168
- ? document.querySelector(selectorOrElement)
169
- : selectorOrElement;
170
-
171
- if (!(form instanceof HTMLFormElement)) {
172
- return;
173
- }
174
-
175
- form.addEventListener('submit', async (event) => {
176
- event.preventDefault();
177
- let promise = ajaxSubmit(form);
178
- if (promise) {
179
- if (onSuccess)
180
- promise = promise.then(onSuccess);
181
- if (onFailure)
182
- promise.catch(onFailure);
183
- }
184
-
185
- });
186
-
187
-
188
- }
189
-
190
-
191
- export function deepClone<T>(obj: T): T {
192
- if (obj === null || typeof obj !== 'object') {
193
- return obj;
194
- }
195
-
196
- if (Array.isArray(obj)) {
197
- const copy: any[] = [];
198
- for (const element of obj) {
199
- copy.push(deepClone(element));
200
- }
201
- return copy as T;
202
- }
203
-
204
- const copy: { [key: string]: any } = {};
205
- for (const key in obj) {
206
- if (obj.hasOwnProperty(key)) {
207
- copy[key] = deepClone((obj as { [key: string]: any })[key]);
208
- }
209
- }
210
- return copy as T;
211
- }
212
-
213
- function _objectToFormData(data: Record<string, any>, formData: FormData, parentKey: string = ''): FormData {
214
-
215
-
216
- for (const key in data) {
217
- if (data.hasOwnProperty(key)) {
218
- const value = data[key];
219
-
220
- if (value instanceof Date) {
221
- formData.append(parentKey ? `${parentKey}[${key}]` : key, value.toISOString());
222
- } else if (value instanceof File) {
223
- formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
224
- } else if (typeof value === 'object' && !Array.isArray(value)) {
225
- _objectToFormData(value, formData, parentKey ? `${parentKey}[${key}]` : key);
226
- } else if (Array.isArray(value)) {
227
- value.forEach((item, index) => {
228
- const arrayKey = `${parentKey ? `${parentKey}[${key}]` : key}[${index}]`;
229
- if (typeof item === 'object' && !Array.isArray(item)) {
230
- _objectToFormData(item, formData, arrayKey);
231
- } else {
232
- formData.append(arrayKey, item);
233
- }
234
- });
235
- } else {
236
- formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
237
- }
238
- }
239
- }
240
-
241
- return formData;
242
- }
243
-
244
- export function objectToFormData(data: Record<string, any>): FormData {
245
- let formData = new FormData();
246
- return _objectToFormData(data, formData);
247
- }