@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.
package/dist/index.esm.js CHANGED
@@ -1,3 +1,26 @@
1
+ /**
2
+ * Represents different types in JavaScript.
3
+ * @enum {string}
4
+ */
5
+ var Type;
6
+ (function (Type) {
7
+ Type["Undefined"] = "undefined";
8
+ Type["Number"] = "number";
9
+ Type["String"] = "string";
10
+ Type["Boolean"] = "boolean";
11
+ Type["Object"] = "object";
12
+ Type["Function"] = "function";
13
+ Type["Symbol"] = "symbol";
14
+ Type["BigInt"] = "bigint";
15
+ Type["Null"] = "null";
16
+ })(Type || (Type = {}));
17
+
18
+ /**
19
+ * Represents a pair of key and value.
20
+ *
21
+ * @template TKey The type of the key.
22
+ * @template TValue The type of the value.
23
+ */
1
24
  class Pair {
2
25
  get value() {
3
26
  return this._value;
@@ -12,91 +35,153 @@ class Pair {
12
35
  }
13
36
 
14
37
  // noinspection JSUnusedGlobalSymbols
15
- function isIterable(obj) {
16
- return obj[Symbol.iterator] === 'function';
17
- }
18
- function clamp(n, min, max) {
19
- if (n <= min)
20
- return min;
21
- if (n >= max)
22
- return max;
23
- return n;
24
- }
25
38
  /**
26
- * Linearly remaps a value from its source range [`inMin`, `inMax`] to the destination range [`outMin`, `outMax`]
39
+ * Checks if an object is iterable.
27
40
  *
28
- * @category Math
29
- * @example
30
- * ```
31
- * const value = remap(0.5, 0, 1, 200, 400) // value will be 300
32
- * ```
41
+ * @param {any} obj - The object to check.
42
+ * @return {boolean} - Returns true if the object is iterable, otherwise false.
33
43
  */
34
- function lerp(n, inMin, inMax, outMin, outMax) {
35
- return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin));
44
+ function isIterable(obj) {
45
+ return obj[Symbol.iterator] === 'function';
36
46
  }
37
47
  /**
38
- * Ensure prefix of a string
48
+ * Checks if a given value is a string.
39
49
  *
40
- * @category String
50
+ * @param {*} value - The value to check.
51
+ * @return {boolean} - Returns true if the value is a string, otherwise returns false.
41
52
  */
42
- function ensurePrefix(prefix, str) {
43
- if (!str.startsWith(prefix))
44
- return prefix + str;
45
- return str;
53
+ function isString(value) {
54
+ return typeof value === 'string';
46
55
  }
47
56
  /**
48
- * Ensure suffix of a string
57
+ * Checks if a value is a number.
49
58
  *
50
- * @category String
59
+ * @param {any} value - The value to check.
60
+ * @return {boolean} - Returns true if the value is a number, otherwise false.
51
61
  */
52
- function ensureSuffix(suffix, str) {
53
- if (!str.endsWith(suffix))
54
- return str + suffix;
55
- return str;
56
- }
57
- function isString(value) {
58
- return typeof value === 'string';
59
- }
60
62
  function isNumber(value) {
61
63
  return typeof value === 'number';
62
64
  }
65
+ /**
66
+ * Checks if a given value is a boolean.
67
+ *
68
+ * @param {any} value - The value to be checked.
69
+ *
70
+ * @return {boolean} - Returns true if the value is a boolean, otherwise false.
71
+ */
63
72
  function isBoolean(value) {
64
73
  return typeof value === 'boolean';
65
74
  }
75
+ /**
76
+ * Checks if a value is an object.
77
+ * @param {any} value - The value to be checked.
78
+ * @returns {boolean} - Returns true if the value is an object, otherwise returns false.
79
+ */
66
80
  function isObject(value) {
67
- return value !== null && typeof value === 'object' && !Array.isArray(value);
81
+ return value != null && typeof value === 'object' && !Array.isArray(value);
68
82
  }
83
+ /**
84
+ * Determines if a value is an array.
85
+ *
86
+ * @param value - The value to be checked.
87
+ *
88
+ * @return Whether the value is an array.
89
+ */
69
90
  function isArray(value) {
70
91
  return Array.isArray(value);
71
92
  }
93
+ /**
94
+ * Checks if a value is a function.
95
+ *
96
+ * @param {any} value - The value to be checked.
97
+ * @return {boolean} - Returns true if the value is a function, otherwise returns false.
98
+ */
72
99
  function isFunction(value) {
73
100
  return typeof value === 'function';
74
101
  }
102
+ /**
103
+ * Checks if a value is undefined.
104
+ *
105
+ * @param {any} value - The value to check.
106
+ * @returns {boolean} - True if the value is undefined, false otherwise.
107
+ */
75
108
  function isUndefined(value) {
76
109
  return typeof value === 'undefined';
77
110
  }
111
+ /**
112
+ * Checks if a value is defined or not.
113
+ *
114
+ * @param {any} value - The value to be checked.
115
+ *
116
+ * @return {boolean} - True if the value is defined, false otherwise.
117
+ */
118
+ function isDefined(value) {
119
+ return typeof value !== 'undefined';
120
+ }
121
+ /**
122
+ * Checks if a given value is null.
123
+ *
124
+ * @param {any} value - The value to check for null.
125
+ * @return {boolean} - Returns true if the value is null, otherwise returns false.
126
+ */
78
127
  function isNull(value) {
79
128
  return value === null;
80
129
  }
130
+ /**
131
+ * Determines whether the given value is of type bigint.
132
+ * @param {any} value - The value to be checked.
133
+ * @return {boolean} - Returns true if the value is of type bigint, false otherwise.
134
+ */
81
135
  function isBigInt(value) {
82
136
  return typeof value === 'bigint';
83
137
  }
138
+ /**
139
+ * Checks if a given value is a symbol.
140
+ *
141
+ * @param {any} value - The value to be checked.
142
+ * @return {boolean} - Returns true if the value is a symbol, false otherwise.
143
+ */
84
144
  function isSymbol(value) {
85
145
  return typeof value === 'symbol';
86
146
  }
147
+ /**
148
+ * Checks if a value is null or undefined.
149
+ *
150
+ * @param {any} value - The value to check.
151
+ * @return {boolean} - Returns true if the value is null or undefined, false otherwise.
152
+ */
87
153
  function isNullOrUndefined(value) {
88
154
  return value === null || typeof value === 'undefined';
89
155
  }
156
+ /**
157
+ * Checks if a given value is empty.
158
+ *
159
+ * @param {any} value - The value to check.
160
+ * @return {boolean} - Returns true if the value is empty, otherwise returns false.
161
+ */
90
162
  function isEmpty(value) {
91
163
  return (Array.isArray(value) && value.length === 0) ||
92
164
  (typeof value === 'string' && value === '') ||
93
165
  value === null || typeof value === 'undefined';
94
166
  }
167
+ /**
168
+ * Check if a value is empty or contains only whitespace characters.
169
+ *
170
+ * @param {any} value - The value to check.
171
+ * @return {boolean} Returns true if the value is empty or contains only whitespace characters, otherwise returns false.
172
+ */
95
173
  function isEmptyOrWhitespace(value) {
96
174
  return (Array.isArray(value) && value.length === 0) ||
97
175
  (typeof value === 'string' && value.trim() === '') ||
98
176
  value === null || typeof value === 'undefined';
99
177
  }
178
+ /**
179
+ * Submits a form via AJAX and returns a Promise that resolves to the Response object.
180
+ *
181
+ * @param {HTMLFormElement | string} selectorOrElement - The form element or selector.
182
+ * @return {Promise<Response>} A Promise that resolves to the Response object.
183
+ * @throws {Error} If the element is invalid.
184
+ */
100
185
  async function ajaxSubmit(selectorOrElement) {
101
186
  const form = typeof selectorOrElement === 'string'
102
187
  ? document.querySelector(selectorOrElement)
@@ -110,6 +195,12 @@ async function ajaxSubmit(selectorOrElement) {
110
195
  body: new FormData(form),
111
196
  });
112
197
  }
198
+ /**
199
+ * Converts an object into an array of key-value pairs.
200
+ *
201
+ * @param {Record<string, any>} obj - The object to convert.
202
+ * @return {Pair<string, any>[]} - The array of key-value pairs.
203
+ */
113
204
  function toPairs(obj) {
114
205
  let output = [];
115
206
  for (const key in obj) {
@@ -117,6 +208,12 @@ function toPairs(obj) {
117
208
  }
118
209
  return output;
119
210
  }
211
+ /**
212
+ * Converts a given thing into a Promise.
213
+ * @template T
214
+ * @param {any} thing - The thing to be converted into a Promise.
215
+ * @returns {Promise<T>} - A Promise representing the given thing.
216
+ */
120
217
  function promisify(thing) {
121
218
  if (thing instanceof Promise)
122
219
  return thing;
@@ -133,7 +230,7 @@ function promisify(thing) {
133
230
  }
134
231
  return Promise.resolve(thing);
135
232
  }
136
- function ajaxSubmission(selectorOrElement, onSuccess = null, onFailure = null) {
233
+ function ajaxSubmission(selectorOrElement, onSuccess = undefined, onFailure = undefined) {
137
234
  const form = typeof selectorOrElement === 'string'
138
235
  ? document.querySelector(selectorOrElement)
139
236
  : selectorOrElement;
@@ -151,6 +248,12 @@ function ajaxSubmission(selectorOrElement, onSuccess = null, onFailure = null) {
151
248
  }
152
249
  });
153
250
  }
251
+ /**
252
+ * Creates a deep clone of the given object.
253
+ * @template T
254
+ * @param {T} obj - The object to clone.
255
+ * @return {T} - The deep clone of the given object.
256
+ */
154
257
  function deepClone(obj) {
155
258
  if (obj === null || typeof obj !== 'object') {
156
259
  return obj;
@@ -170,6 +273,23 @@ function deepClone(obj) {
170
273
  }
171
274
  return copy;
172
275
  }
276
+ /**
277
+ * Returns the type of the given value.
278
+ *
279
+ * @param {any} value - The value to determine the type of.
280
+ * @return {Type} - The type of the given value.
281
+ */
282
+ function getType(value) {
283
+ return value === null ? Type.Null : Type[typeof value];
284
+ }
285
+ /**
286
+ * Converts an object to `FormData` format recursively.
287
+ *
288
+ * @param {Record<string, any>} data - The object data to convert.
289
+ * @param {FormData} formData - The `FormData` instance to append data to.
290
+ * @param {string} parentKey - The parent key to append to child keys in the `FormData`.
291
+ * @returns {FormData} - The `FormData` instance with the converted data.
292
+ */
173
293
  function _objectToFormData(data, formData, parentKey = '') {
174
294
  for (const key in data) {
175
295
  if (data.hasOwnProperty(key)) {
@@ -201,11 +321,92 @@ function _objectToFormData(data, formData, parentKey = '') {
201
321
  }
202
322
  return formData;
203
323
  }
324
+ /**
325
+ * Converts an object into FormData.
326
+ *
327
+ * @param {Record<string, any>} data - The object to be converted into FormData.
328
+ * @return {FormData} - The FormData object representing the converted data.
329
+ */
204
330
  function objectToFormData(data) {
205
331
  let formData = new FormData();
206
332
  return _objectToFormData(data, formData);
207
333
  }
208
334
 
335
+ /**
336
+ * Ensures that a given string has a specific prefix.
337
+ *
338
+ * @param {string} str - The string to ensure the prefix on.
339
+ * @param {string} prefix - The prefix to ensure on the string.
340
+ * @return {string} - The resulting string with the ensured prefix.
341
+ */
342
+ function ensurePrefix(str, prefix) {
343
+ if (!str.startsWith(prefix))
344
+ return prefix + str;
345
+ return str;
346
+ }
347
+ /**
348
+ * Ensures that a string ends with a specified suffix.
349
+ *
350
+ * @param {string} str - The original string.
351
+ * @param {string} suffix - The suffix to ensure.
352
+ * @return {string} - The modified string with the suffix added if it was not already present.
353
+ */
354
+ function ensureSuffix(str, suffix) {
355
+ if (!str.endsWith(suffix))
356
+ return str + suffix;
357
+ return str;
358
+ }
359
+ /**
360
+ * Formats a string using a template and provided arguments.
361
+ *
362
+ * @param {string} template - The template string containing placeholders.
363
+ * @param {any[]} args - Optional arguments to replace the placeholders in the template.
364
+ * @return {string} The formatted string.
365
+ */
366
+ function format(template, ...args) {
367
+ let regex;
368
+ if (args.length === 1 && typeof args[0] === 'object') {
369
+ args = args[0];
370
+ regex = /{(.+?)}/g;
371
+ }
372
+ else {
373
+ regex = /{(\d+?)}/g;
374
+ }
375
+ return template.replace(regex, (match, key) => {
376
+ return typeof args[key] !== 'undefined' ? args[key] : "";
377
+ });
378
+ }
379
+
380
+ /**
381
+ * Linearly interpolates a number between two ranges.
382
+ *
383
+ * @param {number} n - The number to interpolate.
384
+ * @param {number} inMin - The minimum value of the input range.
385
+ * @param {number} inMax - The maximum value of the input range.
386
+ * @param {number} outMin - The minimum value of the output range.
387
+ * @param {number} outMax - The maximum value of the output range.
388
+ *
389
+ * @return {number} - The interpolated value.
390
+ */
391
+ function lerp(n, inMin, inMax, outMin, outMax) {
392
+ return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin));
393
+ }
394
+ /**
395
+ * Clamps a number between a minimum and maximum value.
396
+ *
397
+ * @param {number} n - The value to be clamped.
398
+ * @param {number} min - The minimum value.
399
+ * @param {number} max - The maximum value.
400
+ * @return {number} The clamped value.
401
+ */
402
+ function clamp(n, min, max) {
403
+ if (n <= min)
404
+ return min;
405
+ if (n >= max)
406
+ return max;
407
+ return n;
408
+ }
409
+
209
410
  class CacheDictionary {
210
411
  constructor(fallback = null, defaultDuration = null) {
211
412
  this.fallback = fallback;
@@ -713,6 +914,9 @@ class Grouping extends From {
713
914
  }
714
915
  }
715
916
 
917
+ /**
918
+ * A class representing a timer that executes a callback function at a specified interval.
919
+ */
716
920
  class Timer {
717
921
  /**
718
922
  * @param callback Callback
@@ -764,6 +968,9 @@ class Timer {
764
968
  }
765
969
  }
766
970
 
971
+ /**
972
+ * Represents a duration of time in milliseconds.
973
+ */
767
974
  class TimeSpan {
768
975
  constructor(milliseconds) {
769
976
  this.milliseconds = milliseconds;
@@ -932,6 +1139,10 @@ class TimeSpan {
932
1139
  TimeSpan.INFINITE = new TimeSpan(Number.POSITIVE_INFINITY);
933
1140
  TimeSpan.NEGATIVE_INFINITE = new TimeSpan(Number.NEGATIVE_INFINITY);
934
1141
 
1142
+ /**
1143
+ * Represents a lazy value that is created only when it is accessed for the first time.
1144
+ * @template T The type of the lazy value.
1145
+ */
935
1146
  class Lazy {
936
1147
  constructor(getter) {
937
1148
  this._valueCreated = false;
@@ -941,9 +1152,9 @@ class Lazy {
941
1152
  get valueCreated() {
942
1153
  return this._valueCreated;
943
1154
  }
944
- async getValue() {
1155
+ get value() {
945
1156
  if (!this._valueCreated) {
946
- this._value = await promisify(this._factoryMethod);
1157
+ this._value = this._factoryMethod();
947
1158
  this._valueCreated = true;
948
1159
  }
949
1160
  return this._value;
@@ -954,5 +1165,136 @@ class Lazy {
954
1165
  }
955
1166
  }
956
1167
 
957
- export { CacheDictionary, From, Lazy, Pair, TimeSpan, Timer, ajaxSubmission, ajaxSubmit, clamp, deepClone, ensurePrefix, ensureSuffix, from, isArray, isBigInt, isBoolean, isEmpty, isEmptyOrWhitespace, isFunction, isIterable, isNull, isNullOrUndefined, isNumber, isObject, isString, isSymbol, isUndefined, lerp, objectToFormData, promisify, toPairs };
1168
+ /**
1169
+ * Represents a class for manipulating an array of items.
1170
+ *
1171
+ * @template T - The type of items in the array.
1172
+ */
1173
+ class ArrayManipulator {
1174
+ constructor(array) {
1175
+ this._array = array;
1176
+ }
1177
+ [Symbol.iterator]() {
1178
+ return this._array[Symbol.iterator]();
1179
+ }
1180
+ /**
1181
+ * Retrieves or sets the value at the specified index in the array.
1182
+ * If `item` is not provided, returns the value at index `n`.
1183
+ * If `item` is provided, sets the value at index `n` to the provided value.
1184
+ *
1185
+ * @param {number} n - The index at which to retrieve or set the value.
1186
+ * @param {T | undefined} [item] - The value to set at index `n`, if provided.
1187
+ *
1188
+ * @return {T | void} - If `item` is not provided, returns the value at index `n`.
1189
+ * - If `item` is provided, does not return anything.
1190
+ */
1191
+ at(n, item = undefined) {
1192
+ if (item === undefined)
1193
+ return this._array[n];
1194
+ else
1195
+ this._array[n] = item;
1196
+ }
1197
+ /**
1198
+ * Removes the specified item from the array.
1199
+ *
1200
+ * @param {T} item - The item to be removed.
1201
+ * @returns {ArrayManipulator<T>} - The updated ArrayManipulator instance.
1202
+ */
1203
+ remove(item) {
1204
+ const index = this._array.indexOf(item);
1205
+ if (index !== -1) {
1206
+ this._array.splice(index, 1);
1207
+ }
1208
+ return this;
1209
+ }
1210
+ /**
1211
+ * Applies a mapping function to each element in the array, transforming it into a new array.
1212
+ * @template R
1213
+ * @param {UnaryFunction<T, R>} mapFn - The function to be applied to each element.
1214
+ * @return {ArrayManipulator<R>} - The new ArrayManipulator instance with the mapped elements.
1215
+ */
1216
+ map(mapFn) {
1217
+ for (let i = 0; i < this._array.length; i++) {
1218
+ this._array[i] = mapFn(this._array[i]);
1219
+ }
1220
+ return this;
1221
+ }
1222
+ /**
1223
+ * Filters the elements of the array based on a given filter function.
1224
+ * The filter function should return a boolean value, indicating whether the element should be included in the filtered array or not.
1225
+ *
1226
+ * @param {Predicate<T>} filterFn - The function that will be used to filter the elements. It should accept an element of type T and return a boolean value.
1227
+ * @returns {ArrayManipulator<T>} - The filtered ArrayManipulator instance with the elements that passed the filter.
1228
+ */
1229
+ filter(filterFn) {
1230
+ for (let i = 0; i < this._array.length; i++) {
1231
+ if (!filterFn(this._array[i])) {
1232
+ this._array.splice(i, 1);
1233
+ i--;
1234
+ }
1235
+ }
1236
+ return this;
1237
+ }
1238
+ head(n) {
1239
+ this._array.splice(n);
1240
+ return this;
1241
+ }
1242
+ slice(start, count = undefined) {
1243
+ this._array.splice(0, start); // Eliminar los primeros elementos hasta `start`
1244
+ if (count !== undefined) {
1245
+ this._array.splice(count); // Mantener solo `count` elementos restantes
1246
+ }
1247
+ return this;
1248
+ }
1249
+ mapMany(mapper) {
1250
+ let i = 0;
1251
+ while (i < this._array.length) {
1252
+ let mappedItems = mapper(this._array[i]);
1253
+ if (!Array.isArray(mappedItems))
1254
+ mappedItems = Array.from(mappedItems);
1255
+ this._array.splice(i, 1, ...mappedItems);
1256
+ i += mappedItems.length;
1257
+ }
1258
+ return this;
1259
+ }
1260
+ tail(n) {
1261
+ const start = this._array.length - n;
1262
+ this._array.splice(0, start);
1263
+ return this;
1264
+ }
1265
+ /**
1266
+ * Appends one or more items to the array.
1267
+ *
1268
+ * @param {...T} items - The items to be appended to the array.
1269
+ * @return {ArrayManipulator<T>} - The ArrayManipulator instance.
1270
+ */
1271
+ append(...items) {
1272
+ this._array.push(...items);
1273
+ return this;
1274
+ }
1275
+ /**
1276
+ * Prepend items to the beginning of the array.
1277
+ *
1278
+ * @param {...T} items - The items to be prepended.
1279
+ * @return {ArrayManipulator<T>} The ArrayManipulator instance.
1280
+ */
1281
+ prepend(...items) {
1282
+ this._array.unshift(...items);
1283
+ return this;
1284
+ }
1285
+ get array() {
1286
+ return this._array;
1287
+ }
1288
+ }
1289
+ /**
1290
+ * Creates an instance of ArrayManipulator with the given array.
1291
+ * @template T
1292
+ * @param {T[]} array - The input array.
1293
+ * @return {ArrayManipulator<T>} - An instance of the ArrayManipulator class.
1294
+ */
1295
+ function array(array) {
1296
+ return new ArrayManipulator(array);
1297
+ }
1298
+
1299
+ export { ArrayManipulator, CacheDictionary, From, Lazy, Pair, TimeSpan, Timer, Type, ajaxSubmission, ajaxSubmit, array, clamp, deepClone, ensurePrefix, ensureSuffix, format, from, getType, isArray, isBigInt, isBoolean, isDefined, isEmpty, isEmptyOrWhitespace, isFunction, isIterable, isNull, isNullOrUndefined, isNumber, isObject, isString, isSymbol, isUndefined, lerp, objectToFormData, promisify, toPairs };
958
1300
  //# sourceMappingURL=index.esm.js.map