@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/dist/index.cjs.js CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var Type;
5
+ /**
6
+ * Represents different types in JavaScript.
7
+ * @enum {string}
8
+ */
9
+ exports.Type = void 0;
6
10
  (function (Type) {
7
11
  Type["Undefined"] = "undefined";
8
12
  Type["Number"] = "number";
@@ -12,9 +16,15 @@ var Type;
12
16
  Type["Function"] = "function";
13
17
  Type["Symbol"] = "symbol";
14
18
  Type["BigInt"] = "bigint";
15
- })(Type || (Type = {}));
16
- var Type$1 = Type;
19
+ Type["Null"] = "null";
20
+ })(exports.Type || (exports.Type = {}));
17
21
 
22
+ /**
23
+ * Represents a pair of key and value.
24
+ *
25
+ * @template TKey The type of the key.
26
+ * @template TValue The type of the value.
27
+ */
18
28
  class Pair {
19
29
  get value() {
20
30
  return this._value;
@@ -29,55 +39,153 @@ class Pair {
29
39
  }
30
40
 
31
41
  // noinspection JSUnusedGlobalSymbols
42
+ /**
43
+ * Checks if an object is iterable.
44
+ *
45
+ * @param {any} obj - The object to check.
46
+ * @return {boolean} - Returns true if the object is iterable, otherwise false.
47
+ */
32
48
  function isIterable(obj) {
33
49
  return obj[Symbol.iterator] === 'function';
34
50
  }
51
+ /**
52
+ * Checks if a given value is a string.
53
+ *
54
+ * @param {*} value - The value to check.
55
+ * @return {boolean} - Returns true if the value is a string, otherwise returns false.
56
+ */
35
57
  function isString(value) {
36
58
  return typeof value === 'string';
37
59
  }
60
+ /**
61
+ * Checks if a value is a number.
62
+ *
63
+ * @param {any} value - The value to check.
64
+ * @return {boolean} - Returns true if the value is a number, otherwise false.
65
+ */
38
66
  function isNumber(value) {
39
67
  return typeof value === 'number';
40
68
  }
69
+ /**
70
+ * Checks if a given value is a boolean.
71
+ *
72
+ * @param {any} value - The value to be checked.
73
+ *
74
+ * @return {boolean} - Returns true if the value is a boolean, otherwise false.
75
+ */
41
76
  function isBoolean(value) {
42
77
  return typeof value === 'boolean';
43
78
  }
79
+ /**
80
+ * Checks if a value is an object.
81
+ * @param {any} value - The value to be checked.
82
+ * @returns {boolean} - Returns true if the value is an object, otherwise returns false.
83
+ */
44
84
  function isObject(value) {
45
- return value !== null && typeof value === 'object' && !Array.isArray(value);
85
+ return value != null && typeof value === 'object' && !Array.isArray(value);
46
86
  }
87
+ /**
88
+ * Determines if a value is an array.
89
+ *
90
+ * @param value - The value to be checked.
91
+ *
92
+ * @return Whether the value is an array.
93
+ */
47
94
  function isArray(value) {
48
95
  return Array.isArray(value);
49
96
  }
97
+ /**
98
+ * Checks if a value is a function.
99
+ *
100
+ * @param {any} value - The value to be checked.
101
+ * @return {boolean} - Returns true if the value is a function, otherwise returns false.
102
+ */
50
103
  function isFunction(value) {
51
104
  return typeof value === 'function';
52
105
  }
106
+ /**
107
+ * Checks if a value is undefined.
108
+ *
109
+ * @param {any} value - The value to check.
110
+ * @returns {boolean} - True if the value is undefined, false otherwise.
111
+ */
53
112
  function isUndefined(value) {
54
113
  return typeof value === 'undefined';
55
114
  }
115
+ /**
116
+ * Checks if a value is defined or not.
117
+ *
118
+ * @param {any} value - The value to be checked.
119
+ *
120
+ * @return {boolean} - True if the value is defined, false otherwise.
121
+ */
56
122
  function isDefined(value) {
57
123
  return typeof value !== 'undefined';
58
124
  }
125
+ /**
126
+ * Checks if a given value is null.
127
+ *
128
+ * @param {any} value - The value to check for null.
129
+ * @return {boolean} - Returns true if the value is null, otherwise returns false.
130
+ */
59
131
  function isNull(value) {
60
132
  return value === null;
61
133
  }
134
+ /**
135
+ * Determines whether the given value is of type bigint.
136
+ * @param {any} value - The value to be checked.
137
+ * @return {boolean} - Returns true if the value is of type bigint, false otherwise.
138
+ */
62
139
  function isBigInt(value) {
63
140
  return typeof value === 'bigint';
64
141
  }
142
+ /**
143
+ * Checks if a given value is a symbol.
144
+ *
145
+ * @param {any} value - The value to be checked.
146
+ * @return {boolean} - Returns true if the value is a symbol, false otherwise.
147
+ */
65
148
  function isSymbol(value) {
66
149
  return typeof value === 'symbol';
67
150
  }
151
+ /**
152
+ * Checks if a value is null or undefined.
153
+ *
154
+ * @param {any} value - The value to check.
155
+ * @return {boolean} - Returns true if the value is null or undefined, false otherwise.
156
+ */
68
157
  function isNullOrUndefined(value) {
69
158
  return value === null || typeof value === 'undefined';
70
159
  }
160
+ /**
161
+ * Checks if a given value is empty.
162
+ *
163
+ * @param {any} value - The value to check.
164
+ * @return {boolean} - Returns true if the value is empty, otherwise returns false.
165
+ */
71
166
  function isEmpty(value) {
72
167
  return (Array.isArray(value) && value.length === 0) ||
73
168
  (typeof value === 'string' && value === '') ||
74
169
  value === null || typeof value === 'undefined';
75
170
  }
171
+ /**
172
+ * Check if a value is empty or contains only whitespace characters.
173
+ *
174
+ * @param {any} value - The value to check.
175
+ * @return {boolean} Returns true if the value is empty or contains only whitespace characters, otherwise returns false.
176
+ */
76
177
  function isEmptyOrWhitespace(value) {
77
178
  return (Array.isArray(value) && value.length === 0) ||
78
179
  (typeof value === 'string' && value.trim() === '') ||
79
180
  value === null || typeof value === 'undefined';
80
181
  }
182
+ /**
183
+ * Submits a form via AJAX and returns a Promise that resolves to the Response object.
184
+ *
185
+ * @param {HTMLFormElement | string} selectorOrElement - The form element or selector.
186
+ * @return {Promise<Response>} A Promise that resolves to the Response object.
187
+ * @throws {Error} If the element is invalid.
188
+ */
81
189
  async function ajaxSubmit(selectorOrElement) {
82
190
  const form = typeof selectorOrElement === 'string'
83
191
  ? document.querySelector(selectorOrElement)
@@ -91,6 +199,12 @@ async function ajaxSubmit(selectorOrElement) {
91
199
  body: new FormData(form),
92
200
  });
93
201
  }
202
+ /**
203
+ * Converts an object into an array of key-value pairs.
204
+ *
205
+ * @param {Record<string, any>} obj - The object to convert.
206
+ * @return {Pair<string, any>[]} - The array of key-value pairs.
207
+ */
94
208
  function toPairs(obj) {
95
209
  let output = [];
96
210
  for (const key in obj) {
@@ -98,6 +212,12 @@ function toPairs(obj) {
98
212
  }
99
213
  return output;
100
214
  }
215
+ /**
216
+ * Converts a given thing into a Promise.
217
+ * @template T
218
+ * @param {any} thing - The thing to be converted into a Promise.
219
+ * @returns {Promise<T>} - A Promise representing the given thing.
220
+ */
101
221
  function promisify(thing) {
102
222
  if (thing instanceof Promise)
103
223
  return thing;
@@ -114,7 +234,7 @@ function promisify(thing) {
114
234
  }
115
235
  return Promise.resolve(thing);
116
236
  }
117
- function ajaxSubmission(selectorOrElement, onSuccess = null, onFailure = null) {
237
+ function ajaxSubmission(selectorOrElement, onSuccess = undefined, onFailure = undefined) {
118
238
  const form = typeof selectorOrElement === 'string'
119
239
  ? document.querySelector(selectorOrElement)
120
240
  : selectorOrElement;
@@ -132,6 +252,12 @@ function ajaxSubmission(selectorOrElement, onSuccess = null, onFailure = null) {
132
252
  }
133
253
  });
134
254
  }
255
+ /**
256
+ * Creates a deep clone of the given object.
257
+ * @template T
258
+ * @param {T} obj - The object to clone.
259
+ * @return {T} - The deep clone of the given object.
260
+ */
135
261
  function deepClone(obj) {
136
262
  if (obj === null || typeof obj !== 'object') {
137
263
  return obj;
@@ -151,6 +277,23 @@ function deepClone(obj) {
151
277
  }
152
278
  return copy;
153
279
  }
280
+ /**
281
+ * Returns the type of the given value.
282
+ *
283
+ * @param {any} value - The value to determine the type of.
284
+ * @return {Type} - The type of the given value.
285
+ */
286
+ function getType(value) {
287
+ return value === null ? exports.Type.Null : exports.Type[typeof value];
288
+ }
289
+ /**
290
+ * Converts an object to `FormData` format recursively.
291
+ *
292
+ * @param {Record<string, any>} data - The object data to convert.
293
+ * @param {FormData} formData - The `FormData` instance to append data to.
294
+ * @param {string} parentKey - The parent key to append to child keys in the `FormData`.
295
+ * @returns {FormData} - The `FormData` instance with the converted data.
296
+ */
154
297
  function _objectToFormData(data, formData, parentKey = '') {
155
298
  for (const key in data) {
156
299
  if (data.hasOwnProperty(key)) {
@@ -182,44 +325,84 @@ function _objectToFormData(data, formData, parentKey = '') {
182
325
  }
183
326
  return formData;
184
327
  }
328
+ /**
329
+ * Converts an object into FormData.
330
+ *
331
+ * @param {Record<string, any>} data - The object to be converted into FormData.
332
+ * @return {FormData} - The FormData object representing the converted data.
333
+ */
185
334
  function objectToFormData(data) {
186
335
  let formData = new FormData();
187
336
  return _objectToFormData(data, formData);
188
337
  }
189
338
 
190
339
  /**
191
- * Ensure prefix of a string
340
+ * Ensures that a given string has a specific prefix.
192
341
  *
193
- * @category String
342
+ * @param {string} str - The string to ensure the prefix on.
343
+ * @param {string} prefix - The prefix to ensure on the string.
344
+ * @return {string} - The resulting string with the ensured prefix.
194
345
  */
195
- function ensurePrefix(prefix, str) {
346
+ function ensurePrefix(str, prefix) {
196
347
  if (!str.startsWith(prefix))
197
348
  return prefix + str;
198
349
  return str;
199
350
  }
200
351
  /**
201
- * Ensure suffix of a string
352
+ * Ensures that a string ends with a specified suffix.
202
353
  *
203
- * @category String
354
+ * @param {string} str - The original string.
355
+ * @param {string} suffix - The suffix to ensure.
356
+ * @return {string} - The modified string with the suffix added if it was not already present.
204
357
  */
205
- function ensureSuffix(suffix, str) {
358
+ function ensureSuffix(str, suffix) {
206
359
  if (!str.endsWith(suffix))
207
360
  return str + suffix;
208
361
  return str;
209
362
  }
363
+ /**
364
+ * Formats a string using a template and provided arguments.
365
+ *
366
+ * @param {string} template - The template string containing placeholders.
367
+ * @param {any[]} args - Optional arguments to replace the placeholders in the template.
368
+ * @return {string} The formatted string.
369
+ */
370
+ function format(template, ...args) {
371
+ let regex;
372
+ if (args.length === 1 && typeof args[0] === 'object') {
373
+ args = args[0];
374
+ regex = /{(.+?)}/g;
375
+ }
376
+ else {
377
+ regex = /{(\d+?)}/g;
378
+ }
379
+ return template.replace(regex, (match, key) => {
380
+ return typeof args[key] !== 'undefined' ? args[key] : "";
381
+ });
382
+ }
210
383
 
211
384
  /**
212
- * Linearly remaps a value from its source range [`inMin`, `inMax`] to the destination range [`outMin`, `outMax`]
385
+ * Linearly interpolates a number between two ranges.
386
+ *
387
+ * @param {number} n - The number to interpolate.
388
+ * @param {number} inMin - The minimum value of the input range.
389
+ * @param {number} inMax - The maximum value of the input range.
390
+ * @param {number} outMin - The minimum value of the output range.
391
+ * @param {number} outMax - The maximum value of the output range.
213
392
  *
214
- * @category Math
215
- * @example
216
- * ```
217
- * const value = remap(0.5, 0, 1, 200, 400) // value will be 300
218
- * ```
393
+ * @return {number} - The interpolated value.
219
394
  */
220
395
  function lerp(n, inMin, inMax, outMin, outMax) {
221
396
  return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin));
222
397
  }
398
+ /**
399
+ * Clamps a number between a minimum and maximum value.
400
+ *
401
+ * @param {number} n - The value to be clamped.
402
+ * @param {number} min - The minimum value.
403
+ * @param {number} max - The maximum value.
404
+ * @return {number} The clamped value.
405
+ */
223
406
  function clamp(n, min, max) {
224
407
  if (n <= min)
225
408
  return min;
@@ -362,14 +545,26 @@ class From {
362
545
  }
363
546
  return true;
364
547
  }
365
- any(predicate) {
548
+ any(predicate = undefined) {
366
549
  for (let item of this) {
367
- if (predicate(item)) {
550
+ if (predicate == null || predicate(item)) {
368
551
  return true;
369
552
  }
370
553
  }
371
554
  return false;
372
555
  }
556
+ get length() {
557
+ return this.count();
558
+ }
559
+ count(predicate = undefined) {
560
+ let output = 0;
561
+ for (let item of this) {
562
+ if (predicate == null || predicate(item)) {
563
+ output++;
564
+ }
565
+ }
566
+ return output;
567
+ }
373
568
  filter(predicate) {
374
569
  const self = this;
375
570
  return From.fn(function* () {
@@ -735,6 +930,9 @@ class Grouping extends From {
735
930
  }
736
931
  }
737
932
 
933
+ /**
934
+ * A class representing a timer that executes a callback function at a specified interval.
935
+ */
738
936
  class Timer {
739
937
  /**
740
938
  * @param callback Callback
@@ -786,6 +984,9 @@ class Timer {
786
984
  }
787
985
  }
788
986
 
987
+ /**
988
+ * Represents a duration of time in milliseconds.
989
+ */
789
990
  class TimeSpan {
790
991
  constructor(milliseconds) {
791
992
  this.milliseconds = milliseconds;
@@ -954,6 +1155,10 @@ class TimeSpan {
954
1155
  TimeSpan.INFINITE = new TimeSpan(Number.POSITIVE_INFINITY);
955
1156
  TimeSpan.NEGATIVE_INFINITE = new TimeSpan(Number.NEGATIVE_INFINITY);
956
1157
 
1158
+ /**
1159
+ * Represents a lazy value that is created only when it is accessed for the first time.
1160
+ * @template T The type of the lazy value.
1161
+ */
957
1162
  class Lazy {
958
1163
  constructor(getter) {
959
1164
  this._valueCreated = false;
@@ -963,9 +1168,9 @@ class Lazy {
963
1168
  get valueCreated() {
964
1169
  return this._valueCreated;
965
1170
  }
966
- async getValue() {
1171
+ get value() {
967
1172
  if (!this._valueCreated) {
968
- this._value = await promisify(this._factoryMethod);
1173
+ this._value = this._factoryMethod();
969
1174
  this._valueCreated = true;
970
1175
  }
971
1176
  return this._value;
@@ -976,6 +1181,11 @@ class Lazy {
976
1181
  }
977
1182
  }
978
1183
 
1184
+ /**
1185
+ * Represents a class for manipulating an array of items.
1186
+ *
1187
+ * @template T - The type of items in the array.
1188
+ */
979
1189
  class ArrayManipulator {
980
1190
  constructor(array) {
981
1191
  this._array = array;
@@ -983,12 +1193,29 @@ class ArrayManipulator {
983
1193
  [Symbol.iterator]() {
984
1194
  return this._array[Symbol.iterator]();
985
1195
  }
1196
+ /**
1197
+ * Retrieves or sets the value at the specified index in the array.
1198
+ * If `item` is not provided, returns the value at index `n`.
1199
+ * If `item` is provided, sets the value at index `n` to the provided value.
1200
+ *
1201
+ * @param {number} n - The index at which to retrieve or set the value.
1202
+ * @param {T | undefined} [item] - The value to set at index `n`, if provided.
1203
+ *
1204
+ * @return {T | void} - If `item` is not provided, returns the value at index `n`.
1205
+ * - If `item` is provided, does not return anything.
1206
+ */
986
1207
  at(n, item = undefined) {
987
1208
  if (item === undefined)
988
1209
  return this._array[n];
989
1210
  else
990
1211
  this._array[n] = item;
991
1212
  }
1213
+ /**
1214
+ * Removes the specified item from the array.
1215
+ *
1216
+ * @param {T} item - The item to be removed.
1217
+ * @returns {ArrayManipulator<T>} - The updated ArrayManipulator instance.
1218
+ */
992
1219
  remove(item) {
993
1220
  const index = this._array.indexOf(item);
994
1221
  if (index !== -1) {
@@ -996,12 +1223,25 @@ class ArrayManipulator {
996
1223
  }
997
1224
  return this;
998
1225
  }
1226
+ /**
1227
+ * Applies a mapping function to each element in the array, transforming it into a new array.
1228
+ * @template R
1229
+ * @param {UnaryFunction<T, R>} mapFn - The function to be applied to each element.
1230
+ * @return {ArrayManipulator<R>} - The new ArrayManipulator instance with the mapped elements.
1231
+ */
999
1232
  map(mapFn) {
1000
1233
  for (let i = 0; i < this._array.length; i++) {
1001
1234
  this._array[i] = mapFn(this._array[i]);
1002
1235
  }
1003
1236
  return this;
1004
1237
  }
1238
+ /**
1239
+ * Filters the elements of the array based on a given filter function.
1240
+ * The filter function should return a boolean value, indicating whether the element should be included in the filtered array or not.
1241
+ *
1242
+ * @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.
1243
+ * @returns {ArrayManipulator<T>} - The filtered ArrayManipulator instance with the elements that passed the filter.
1244
+ */
1005
1245
  filter(filterFn) {
1006
1246
  for (let i = 0; i < this._array.length; i++) {
1007
1247
  if (!filterFn(this._array[i])) {
@@ -1038,10 +1278,22 @@ class ArrayManipulator {
1038
1278
  this._array.splice(0, start);
1039
1279
  return this;
1040
1280
  }
1281
+ /**
1282
+ * Appends one or more items to the array.
1283
+ *
1284
+ * @param {...T} items - The items to be appended to the array.
1285
+ * @return {ArrayManipulator<T>} - The ArrayManipulator instance.
1286
+ */
1041
1287
  append(...items) {
1042
1288
  this._array.push(...items);
1043
1289
  return this;
1044
1290
  }
1291
+ /**
1292
+ * Prepend items to the beginning of the array.
1293
+ *
1294
+ * @param {...T} items - The items to be prepended.
1295
+ * @return {ArrayManipulator<T>} The ArrayManipulator instance.
1296
+ */
1045
1297
  prepend(...items) {
1046
1298
  this._array.unshift(...items);
1047
1299
  return this;
@@ -1050,6 +1302,12 @@ class ArrayManipulator {
1050
1302
  return this._array;
1051
1303
  }
1052
1304
  }
1305
+ /**
1306
+ * Creates an instance of ArrayManipulator with the given array.
1307
+ * @template T
1308
+ * @param {T[]} array - The input array.
1309
+ * @return {ArrayManipulator<T>} - An instance of the ArrayManipulator class.
1310
+ */
1053
1311
  function array(array) {
1054
1312
  return new ArrayManipulator(array);
1055
1313
  }
@@ -1061,7 +1319,6 @@ exports.Lazy = Lazy;
1061
1319
  exports.Pair = Pair;
1062
1320
  exports.TimeSpan = TimeSpan;
1063
1321
  exports.Timer = Timer;
1064
- exports.Type = Type$1;
1065
1322
  exports.ajaxSubmission = ajaxSubmission;
1066
1323
  exports.ajaxSubmit = ajaxSubmit;
1067
1324
  exports.array = array;
@@ -1069,7 +1326,9 @@ exports.clamp = clamp;
1069
1326
  exports.deepClone = deepClone;
1070
1327
  exports.ensurePrefix = ensurePrefix;
1071
1328
  exports.ensureSuffix = ensureSuffix;
1329
+ exports.format = format;
1072
1330
  exports.from = from;
1331
+ exports.getType = getType;
1073
1332
  exports.isArray = isArray;
1074
1333
  exports.isBigInt = isBigInt;
1075
1334
  exports.isBoolean = isBoolean;