@develia/commons 0.3.19 → 0.3.21

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.
package/src/utilities.ts CHANGED
@@ -2,67 +2,158 @@
2
2
 
3
3
 
4
4
  import Pair from "./pair";
5
- import {Nullable, Provider} from "./types";
6
- import Type from "./type";
7
-
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
+ */
8
14
  export function isIterable(obj: any): boolean {
9
15
  return obj[Symbol.iterator] === 'function';
10
16
  }
11
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
+ */
12
24
  export function isString(value: any): value is string {
13
25
  return typeof value === 'string';
14
26
  }
15
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
+ */
16
34
  export function isNumber(value: any): value is number {
17
35
  return typeof value === 'number';
18
36
  }
19
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
+ */
20
45
  export function isBoolean(value: any): value is boolean {
21
46
  return typeof value === 'boolean';
22
47
  }
23
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
+ */
24
54
  export function isObject(value: any): value is object {
25
- return value !== null && typeof value === 'object' && !Array.isArray(value);
55
+ return value != null && typeof value === 'object' && !Array.isArray(value);
26
56
  }
27
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
+ */
28
65
  export function isArray(value: any): value is any[] {
29
66
  return Array.isArray(value);
30
67
  }
31
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
+ */
32
75
  export function isFunction(value: any): value is Function {
33
76
  return typeof value === 'function';
34
77
  }
35
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
+ */
36
85
  export function isUndefined(value: any): value is undefined {
37
86
  return typeof value === 'undefined';
38
87
  }
39
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
+ */
40
96
  export function isDefined(value: any): value is undefined {
41
97
  return typeof value !== 'undefined';
42
98
  }
43
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
+ */
44
106
  export function isNull(value: any): value is null {
45
107
  return value === null;
46
108
  }
47
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
+ */
48
115
  export function isBigInt(value: any): value is bigint {
49
116
  return typeof value === 'bigint';
50
117
  }
51
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
+ */
52
125
  export function isSymbol(value: any): value is symbol {
53
126
  return typeof value === 'symbol';
54
127
  }
55
128
 
56
- export function isNullOrUndefined(value: any) {
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 {
57
136
  return value === null || typeof value === 'undefined';
58
137
  }
59
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
+ */
60
145
  export function isEmpty(value: any): boolean {
61
146
  return (Array.isArray(value) && value.length === 0) ||
62
147
  (typeof value === 'string' && value === '') ||
63
148
  value === null || typeof value === 'undefined';
64
149
  }
65
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
+ */
66
157
  export function isEmptyOrWhitespace(value: any): boolean {
67
158
  return (Array.isArray(value) && value.length === 0) ||
68
159
  (typeof value === 'string' && value.trim() === '') ||
@@ -70,6 +161,13 @@ export function isEmptyOrWhitespace(value: any): boolean {
70
161
  }
71
162
 
72
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
+ */
73
171
  export async function ajaxSubmit(selectorOrElement: HTMLFormElement | string): Promise<Response> {
74
172
 
75
173
  const form = typeof selectorOrElement === 'string'
@@ -87,7 +185,13 @@ export async function ajaxSubmit(selectorOrElement: HTMLFormElement | string): P
87
185
  });
88
186
  }
89
187
 
90
- export function toPairs(obj: any): Pair<any, any>[] {
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>[] {
91
195
 
92
196
  let output = [];
93
197
  for (const key in obj) {
@@ -97,6 +201,12 @@ export function toPairs(obj: any): Pair<any, any>[] {
97
201
 
98
202
  }
99
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
+ */
100
210
  export function promisify<T>(thing: any): Promise<T> {
101
211
 
102
212
  if (thing instanceof Promise)
@@ -117,9 +227,10 @@ export function promisify<T>(thing: any): Promise<T> {
117
227
  return Promise.resolve(thing);
118
228
  }
119
229
 
230
+
120
231
  export function ajaxSubmission(selectorOrElement: HTMLFormElement | string,
121
- onSuccess: Nullable<Provider<Nullable<any>>> = null,
122
- onFailure: Nullable<Provider<Nullable<any>>> = null) {
232
+ onSuccess: Optional<Provider<any>> = undefined,
233
+ onFailure: Optional<Provider<any>> = undefined) : void {
123
234
 
124
235
  const form = typeof selectorOrElement === 'string'
125
236
  ? document.querySelector(selectorOrElement)
@@ -145,6 +256,12 @@ export function ajaxSubmission(selectorOrElement: HTMLFormElement | string,
145
256
  }
146
257
 
147
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
+ */
148
265
  export function deepClone<T>(obj: T): T {
149
266
  if (obj === null || typeof obj !== 'object') {
150
267
  return obj;
@@ -167,10 +284,25 @@ export function deepClone<T>(obj: T): T {
167
284
  return copy as T;
168
285
  }
169
286
 
170
- function getType(value: any): Type {
171
- return Type[typeof value as keyof typeof Type];
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];
172
296
  }
173
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
+ */
174
306
  function _objectToFormData(data: Record<string, any>, formData: FormData, parentKey: string = ''): FormData {
175
307
 
176
308
  for (const key in data) {
@@ -201,6 +333,12 @@ function _objectToFormData(data: Record<string, any>, formData: FormData, parent
201
333
  return formData;
202
334
  }
203
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
+ */
204
342
  export function objectToFormData(data: Record<string, any>): FormData {
205
343
  let formData = new FormData();
206
344
  return _objectToFormData(data, formData);
package/src/strings.ts DELETED
@@ -1,21 +0,0 @@
1
- /**
2
- * Ensure prefix of a string
3
- *
4
- * @category String
5
- */
6
- export function ensurePrefix(str: string, prefix: string) {
7
- if (!str.startsWith(prefix))
8
- return prefix + str
9
- return str
10
- }
11
-
12
- /**
13
- * Ensure suffix of a string
14
- *
15
- * @category String
16
- */
17
- export function ensureSuffix(str: string, suffix: string) {
18
- if (!str.endsWith(suffix))
19
- return str + suffix
20
- return str
21
- }