@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 @@
1
+ !function(t){"use strict";var e;t.Type=void 0,(e=t.Type||(t.Type={})).Undefined="undefined",e.Number="number",e.String="string",e.Boolean="boolean",e.Object="object",e.Function="function",e.Symbol="symbol",e.BigInt="bigint",e.Null="null";class r{get value(){return this._value}get key(){return this._key}constructor(t,e){this._key=t,this._value=e}}function n(t){return"function"==typeof t}async function i(t){const e="string"==typeof t?document.querySelector(t):t;if(!(e instanceof HTMLFormElement))throw new Error("Invalid element.");return await fetch(e.action,{method:e.method,body:new FormData(e)})}function s(t){let e=[];for(const n in t)e.push(new r(n,t[n]));return e}function o(t,e,r=""){for(const n in t)if(t.hasOwnProperty(n)){const i=t[n];i instanceof Date?e.append(r?`${r}[${n}]`:n,i.toISOString()):i instanceof File?e.append(r?`${r}[${n}]`:n,i):"object"!=typeof i||Array.isArray(i)?Array.isArray(i)?i.forEach(((t,i)=>{const s=`${r?`${r}[${n}]`:n}[${i}]`;"object"!=typeof t||Array.isArray(t)?e.append(s,t):o(t,e,s)})):e.append(r?`${r}[${n}]`:n,i):o(i,e,r?`${r}[${n}]`:n)}return e}class l{static object(t){return new l((function(){return s(t)}))}static _shallowEqual(t,e){const r=Object.keys(t),n=Object.keys(e);if(r.length!==n.length)return!1;for(let n of r)if(t[n]!==e[n])return!1;return!0}constructor(t){this._fn=t}static fn(t){return new l(t)}collect(){const t=Array.from(this);return l.iterable(t)}static iterable(t){return l.fn((function*(){for(const e of t)yield e}))}map(t){const e=this;return l.fn((function*(){for(let r of e)yield t(r)}))}*[Symbol.iterator](){yield*this._fn()}all(t){for(let e of this._fn())if(!t(e))return!1;return!0}any(t){for(let e of this)if(t(e))return!0;return!1}filter(t){const e=this;return l.fn((function*(){for(let r of e)t(r)&&(yield r)}))}contains(t){for(let e of this)if(e===t)return!0;return!1}first(t){if(t){for(let e of this)if(!t||t(e))return e}else for(let t of this)return t}append(t){const e=this;return l.fn((function*(){for(let t of e)yield t;yield t}))}prepend(t){const e=this;return l.fn((function*(){yield t;for(let t of e)yield t}))}at(t){let e=0;for(let r of this)if(e++===t)return r}last(t){let e;if(t)for(let r of this)t&&!t(r)||(e=r);else for(let t of this)e=t;return e}mapMany(t){const e=this;return l.fn((function*(){for(const r of e){const e=t(r);for(const t of e)yield t}}))}flatten(){const t=this;return l.fn((function*(){for(let e of t){let t=e;for(let e of t)yield e}}))}sum(t){let e=0;if(t)for(let r of this)e+=t(r);else for(let t of this)e+=t;return e}avg(t){let e=0,r=0;if(t)for(let n of this)e+=t(n),r++;else for(let t of this)e+=t,r++;return r>0?e/r:0}max(t){let e=-1/0;for(let r of this){let n=t(r);e=n>e?n:e}return e}min(t){let e=1/0;for(let r of this){let n=t(r);e=n<e?n:e}return e}maxBy(t){let e,r=-1/0;for(let n of this){let i=t(n);i>r&&(r=i,e=n)}return e}minBy(t){let e,r=1/0;for(let n of this){let i=t(n);i<r&&(r=i,e=n)}return e}orderBy(t){const e=Array.from(this._fn());return e.sort(((e,r)=>{const n=t(e),i=t(r);return n>i?1:n<i?-1:0})),l.iterable(e)}groupBy(t,e){e=null==e?l._shallowEqual:e;const r=this;return l.fn((function*(){const n=[];for(let i of r){const r=t(i);let s=!1;for(let[t,o]of n)if(e(r,t)){o.push(i),s=!0;break}s||n.push([r,[i]])}yield*n.map((([t,e])=>new a(t,e)))}))}head(t){const e=this;return l.fn((function*(){let r=0;for(let n of e){if(!(r++<t))return;yield n}}))}tail(t){const e=this;return l.fn((function*(){let r=[];for(let n of e)r.push(n),r.length>t&&r.shift();yield*r}))}forEach(t){for(let e of this)t(e)}toArray(){return Array.from(this)}instancesOf(t){return this.filter((e=>typeof e===t))}allInstanceOf(t){for(let e of this)if(!(e instanceof t))return!1;return!0}distinct(t){null==t&&(t=l._shallowEqual);const e=this;return l.fn((function*(){const r=[];for(let n of e)l.iterable(r).any((e=>t(e,n)))||(r.push(n),yield n)}))}insert(t,e){const r=this;return l.fn((function*(){let n=0,i=!1;for(let s of r)n===e&&(yield t,i=!0),yield s,n++;i||(yield t)}))}skip(t){const e=this;return l.fn((function*(){let r=0;for(let n of e)r>=t&&(yield n),r++}))}union(t){const e=this;return l.fn((function*(){yield*e,yield*t}))}innerJoin(t,e,r,n){const i=this;return l.fn((()=>{const s=new Map;for(let e of t)s.set(r(e),e);return Array.from(i).filter((t=>s.has(e(t)))).map((t=>n(t,s.get(e(t)))))}))}leftJoin(t,e,r,n){const i=this;return l.fn((()=>{const s=new Map;for(let e of t)s.set(r(e),e);return Array.from(i).map((t=>n(t,s.get(e(t)))))}))}leftGroupJoin(t,e,r,n){const i=this;return l.fn((()=>{const s=new Map;for(let e of t){const t=r(e);s.has(t)||s.set(t,[]),s.get(t).push(e)}return Array.from(i).map((t=>n(t,s.get(e(t))||[])))}))}rightGroupJoin(t,e,r,n){const i=this;return l.fn((()=>{const s=new Map;for(let t of i){const r=e(t);s.has(r)||s.set(r,[]),s.get(r).push(t)}return Array.from(t).map((t=>n(s.get(r(t))||[],t)))}))}rightJoin(t,e,r,n){const i=this;return l.fn((()=>{const s=new Map;for(let t of i)s.set(e(t),t);return Array.from(t).map((t=>n(s.get(r(t)),t)))}))}}class a extends l{get key(){return this._key}constructor(t,e){super((()=>e)),this._key=t}}class u{constructor(t){this.milliseconds=t}seconds(){return this.milliseconds/1e3}minutes(){return this.milliseconds/6e4}hours(){return this.milliseconds/36e5}days(){return this.milliseconds/864e5}weeks(){return this.milliseconds/6048e5}static fromMilliseconds(t){return new u(t)}static fromSeconds(t){return new u(1e3*t)}static fromMinutes(t){return new u(1e3*t*60)}static fromHours(t){return new u(1e3*t*60*60)}static fromDays(t){return new u(1e3*t*60*60*24)}static fromWeeks(t){return new u(1e3*t*60*60*24*7)}addMilliseconds(t){return new u(this.milliseconds+t)}addSeconds(t){return this.addMilliseconds(1e3*t)}addMinutes(t){return this.addMilliseconds(1e3*t*60)}addHours(t){return this.addMilliseconds(1e3*t*60*60)}addDays(t){return this.addMilliseconds(1e3*t*60*60*24)}addWeeks(t){return this.addMilliseconds(1e3*t*60*60*24*7)}subtractMilliseconds(t){return new u(this.milliseconds-t)}subtractSeconds(t){return this.subtractMilliseconds(1e3*t)}subtractMinutes(t){return this.subtractMilliseconds(1e3*t*60)}subtractHours(t){return this.subtractMilliseconds(1e3*t*60*60)}subtractDays(t){return this.subtractMilliseconds(1e3*t*60*60*24)}subtractWeeks(t){return this.subtractMilliseconds(1e3*t*60*60*24*7)}add(t){return new u(this.milliseconds+t.milliseconds)}subtract(t){return new u(this.milliseconds-t.milliseconds)}addTo(t){return new Date(t.getTime()+this.milliseconds)}subtractFrom(t){return new Date(t.getTime()-this.milliseconds)}static fromDifference(t,e){return new u(e.getTime()-t.getTime())}format(t="hh:mm:ss"){const e=t.toLowerCase(),r=e.includes("h"),n=e.includes("m");let i=0,s=0,o=Math.floor(this.milliseconds/1e3);r&&(i=Math.floor(o/3600),o-=3600*i),n&&(s=Math.floor(o/60),o-=60*s);const l=String(i).padStart(2,"0"),a=String(s).padStart(2,"0"),u=String(o).padStart(2,"0");return e.replace("hh",l).replace("h",i.toString()).replace("mm",a).replace("m",s.toString()).replace("ss",u).replace("s",o.toString())}eq(t){return this.milliseconds===t.milliseconds}le(t){return this.milliseconds<=t.milliseconds}lt(t){return this.milliseconds<t.milliseconds}ge(t){return this.milliseconds>=t.milliseconds}gt(t){return this.milliseconds>t.milliseconds}multiply(t){return new u(this.milliseconds*t)}divide(t){return new u(this.milliseconds/t)}abs(){return new u(Math.abs(this.milliseconds))}isInfinite(){return this.milliseconds===Number.POSITIVE_INFINITY||this.milliseconds===Number.NEGATIVE_INFINITY}isPositiveInfinite(){return this.milliseconds===Number.POSITIVE_INFINITY}isNegativeInfinite(){return this.milliseconds===Number.NEGATIVE_INFINITY}}u.INFINITE=new u(Number.POSITIVE_INFINITY),u.NEGATIVE_INFINITE=new u(Number.NEGATIVE_INFINITY);class c{constructor(t){this._array=t}[Symbol.iterator](){return this._array[Symbol.iterator]()}at(t,e=void 0){if(void 0===e)return this._array[t];this._array[t]=e}remove(t){const e=this._array.indexOf(t);return-1!==e&&this._array.splice(e,1),this}map(t){for(let e=0;e<this._array.length;e++)this._array[e]=t(this._array[e]);return this}filter(t){for(let e=0;e<this._array.length;e++)t(this._array[e])||(this._array.splice(e,1),e--);return this}head(t){return this._array.splice(t),this}slice(t,e=void 0){return this._array.splice(0,t),void 0!==e&&this._array.splice(e),this}mapMany(t){let e=0;for(;e<this._array.length;){let r=t(this._array[e]);Array.isArray(r)||(r=Array.from(r)),this._array.splice(e,1,...r),e+=r.length}return this}tail(t){const e=this._array.length-t;return this._array.splice(0,e),this}append(...t){return this._array.push(...t),this}prepend(...t){return this._array.unshift(...t),this}get array(){return this._array}}t.ArrayManipulator=c,t.CacheDictionary=class{constructor(t=null,e=null){this.fallback=t,this.cache={},this.defaultDuration=e,this.expiration={}}getExpiration(t){return this.expiration[t]}setExpiration(t,e){this.expiration[t]=e}setDuration(t,e){if(null===e)delete this.expiration[t];else{const r=new Date;r.setSeconds(r.getSeconds()+e),this.expiration[t]=r}}get(t){if(!this.cache.hasOwnProperty(t)){if(!this.fallback)return null;this.cache[t]=this.fallback(t),this.expiration[t]=this.calculateExpiration(this.defaultDuration)}return this.isExpired(t)?(this.remove(t),null):this.cache[t]}set(t,e,r=null){this.cache[t]=e,this.expiration[t]=this.calculateExpiration(r)}remove(t){delete this.cache[t],delete this.expiration[t]}isExpired(t){const e=this.expiration[t];return e instanceof Date&&e<new Date}calculateExpiration(t){if(null===t)return;const e=new Date;return e.setSeconds(e.getSeconds()+t),e}},t.From=l,t.Lazy=class{constructor(t){this._valueCreated=!1,this._value=null,this._factoryMethod=t}get valueCreated(){return this._valueCreated}get value(){return this._valueCreated||(this._value=this._factoryMethod(),this._valueCreated=!0),this._value}reset(){this._valueCreated=!1,this._value=null}},t.Pair=r,t.TimeSpan=u,t.Timer=class{constructor(t,e){this._callback=t,this._interval=e,this._intervalId=null}get running(){return null!==this._intervalId&&void 0!==this._intervalId}get interval(){return this._interval}set interval(t){t!=this._interval&&(this._interval=t,this.running&&this.restart())}get callback(){return this._callback}set callback(t){t!=this._callback&&(this._callback=t)}start(){null===this._intervalId&&(this._intervalId=setInterval((()=>{null!=this._callback&&this._callback()}),this._interval))}stop(){null!==this._intervalId&&(clearInterval(this._intervalId),this._intervalId=null)}restart(){this.stop(),this.start()}},t.ajaxSubmission=function(t,e=void 0,r=void 0){const n="string"==typeof t?document.querySelector(t):t;n instanceof HTMLFormElement&&n.addEventListener("submit",(async t=>{t.preventDefault();let s=i(n);s&&(e&&(s=s.then(e)),r&&s.catch(r))}))},t.ajaxSubmit=i,t.array=function(t){return new c(t)},t.clamp=function(t,e,r){return t<=e?e:t>=r?r:t},t.deepClone=function t(e){if(null===e||"object"!=typeof e)return e;if(Array.isArray(e)){const r=[];for(const n of e)r.push(t(n));return r}const r={};for(const n in e)e.hasOwnProperty(n)&&(r[n]=t(e[n]));return r},t.ensurePrefix=function(t,e){return t.startsWith(e)?t:e+t},t.ensureSuffix=function(t,e){return t.endsWith(e)?t:t+e},t.format=function(t,...e){let r;return 1===e.length&&"object"==typeof e[0]?(e=e[0],r=/{(.+?)}/g):r=/{(\d+?)}/g,t.replace(r,((t,r)=>void 0!==e[r]?e[r]:""))},t.from=function(t){if(null==t)throw"Source is null.";if("function"==typeof t)return l.fn(t);if("function"==typeof t[Symbol.iterator])return l.iterable(t);if("object"==typeof t)return l.object(t);throw"Invalid source."},t.getType=function(e){return null===e?t.Type.Null:t.Type[typeof e]},t.isArray=function(t){return Array.isArray(t)},t.isBigInt=function(t){return"bigint"==typeof t},t.isBoolean=function(t){return"boolean"==typeof t},t.isDefined=function(t){return void 0!==t},t.isEmpty=function(t){return Array.isArray(t)&&0===t.length||"string"==typeof t&&""===t||null==t},t.isEmptyOrWhitespace=function(t){return Array.isArray(t)&&0===t.length||"string"==typeof t&&""===t.trim()||null==t},t.isFunction=n,t.isIterable=function(t){return"function"===t[Symbol.iterator]},t.isNull=function(t){return null===t},t.isNullOrUndefined=function(t){return null==t},t.isNumber=function(t){return"number"==typeof t},t.isObject=function(t){return null!=t&&"object"==typeof t&&!Array.isArray(t)},t.isString=function(t){return"string"==typeof t},t.isSymbol=function(t){return"symbol"==typeof t},t.isUndefined=function(t){return void 0===t},t.lerp=function(t,e,r,n,i){return n+(t-e)/(r-e)*(i-n)},t.objectToFormData=function(t){return o(t,new FormData)},t.promisify=function(t){return t instanceof Promise?t:n(t)?new Promise(((e,r)=>{try{e(t())}catch(t){r(t)}})):Promise.resolve(t)},t.toPairs=s}(this.window=this.window||{});
package/dist/index.cjs.js CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ /**
6
+ * Represents different types in JavaScript.
7
+ * @enum {string}
8
+ */
9
+ exports.Type = void 0;
10
+ (function (Type) {
11
+ Type["Undefined"] = "undefined";
12
+ Type["Number"] = "number";
13
+ Type["String"] = "string";
14
+ Type["Boolean"] = "boolean";
15
+ Type["Object"] = "object";
16
+ Type["Function"] = "function";
17
+ Type["Symbol"] = "symbol";
18
+ Type["BigInt"] = "bigint";
19
+ Type["Null"] = "null";
20
+ })(exports.Type || (exports.Type = {}));
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
+ */
5
28
  class Pair {
6
29
  get value() {
7
30
  return this._value;
@@ -16,91 +39,153 @@ class Pair {
16
39
  }
17
40
 
18
41
  // noinspection JSUnusedGlobalSymbols
19
- function isIterable(obj) {
20
- return obj[Symbol.iterator] === 'function';
21
- }
22
- function clamp(n, min, max) {
23
- if (n <= min)
24
- return min;
25
- if (n >= max)
26
- return max;
27
- return n;
28
- }
29
42
  /**
30
- * Linearly remaps a value from its source range [`inMin`, `inMax`] to the destination range [`outMin`, `outMax`]
43
+ * Checks if an object is iterable.
31
44
  *
32
- * @category Math
33
- * @example
34
- * ```
35
- * const value = remap(0.5, 0, 1, 200, 400) // value will be 300
36
- * ```
45
+ * @param {any} obj - The object to check.
46
+ * @return {boolean} - Returns true if the object is iterable, otherwise false.
37
47
  */
38
- function lerp(n, inMin, inMax, outMin, outMax) {
39
- return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin));
48
+ function isIterable(obj) {
49
+ return obj[Symbol.iterator] === 'function';
40
50
  }
41
51
  /**
42
- * Ensure prefix of a string
52
+ * Checks if a given value is a string.
43
53
  *
44
- * @category String
54
+ * @param {*} value - The value to check.
55
+ * @return {boolean} - Returns true if the value is a string, otherwise returns false.
45
56
  */
46
- function ensurePrefix(prefix, str) {
47
- if (!str.startsWith(prefix))
48
- return prefix + str;
49
- return str;
57
+ function isString(value) {
58
+ return typeof value === 'string';
50
59
  }
51
60
  /**
52
- * Ensure suffix of a string
61
+ * Checks if a value is a number.
53
62
  *
54
- * @category String
63
+ * @param {any} value - The value to check.
64
+ * @return {boolean} - Returns true if the value is a number, otherwise false.
55
65
  */
56
- function ensureSuffix(suffix, str) {
57
- if (!str.endsWith(suffix))
58
- return str + suffix;
59
- return str;
60
- }
61
- function isString(value) {
62
- return typeof value === 'string';
63
- }
64
66
  function isNumber(value) {
65
67
  return typeof value === 'number';
66
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
+ */
67
76
  function isBoolean(value) {
68
77
  return typeof value === 'boolean';
69
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
+ */
70
84
  function isObject(value) {
71
- return value !== null && typeof value === 'object' && !Array.isArray(value);
85
+ return value != null && typeof value === 'object' && !Array.isArray(value);
72
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
+ */
73
94
  function isArray(value) {
74
95
  return Array.isArray(value);
75
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
+ */
76
103
  function isFunction(value) {
77
104
  return typeof value === 'function';
78
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
+ */
79
112
  function isUndefined(value) {
80
113
  return typeof value === 'undefined';
81
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
+ */
122
+ function isDefined(value) {
123
+ return typeof value !== 'undefined';
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
+ */
82
131
  function isNull(value) {
83
132
  return value === null;
84
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
+ */
85
139
  function isBigInt(value) {
86
140
  return typeof value === 'bigint';
87
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
+ */
88
148
  function isSymbol(value) {
89
149
  return typeof value === 'symbol';
90
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
+ */
91
157
  function isNullOrUndefined(value) {
92
158
  return value === null || typeof value === 'undefined';
93
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
+ */
94
166
  function isEmpty(value) {
95
167
  return (Array.isArray(value) && value.length === 0) ||
96
168
  (typeof value === 'string' && value === '') ||
97
169
  value === null || typeof value === 'undefined';
98
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
+ */
99
177
  function isEmptyOrWhitespace(value) {
100
178
  return (Array.isArray(value) && value.length === 0) ||
101
179
  (typeof value === 'string' && value.trim() === '') ||
102
180
  value === null || typeof value === 'undefined';
103
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
+ */
104
189
  async function ajaxSubmit(selectorOrElement) {
105
190
  const form = typeof selectorOrElement === 'string'
106
191
  ? document.querySelector(selectorOrElement)
@@ -114,6 +199,12 @@ async function ajaxSubmit(selectorOrElement) {
114
199
  body: new FormData(form),
115
200
  });
116
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
+ */
117
208
  function toPairs(obj) {
118
209
  let output = [];
119
210
  for (const key in obj) {
@@ -121,6 +212,12 @@ function toPairs(obj) {
121
212
  }
122
213
  return output;
123
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
+ */
124
221
  function promisify(thing) {
125
222
  if (thing instanceof Promise)
126
223
  return thing;
@@ -137,7 +234,7 @@ function promisify(thing) {
137
234
  }
138
235
  return Promise.resolve(thing);
139
236
  }
140
- function ajaxSubmission(selectorOrElement, onSuccess = null, onFailure = null) {
237
+ function ajaxSubmission(selectorOrElement, onSuccess = undefined, onFailure = undefined) {
141
238
  const form = typeof selectorOrElement === 'string'
142
239
  ? document.querySelector(selectorOrElement)
143
240
  : selectorOrElement;
@@ -155,6 +252,12 @@ function ajaxSubmission(selectorOrElement, onSuccess = null, onFailure = null) {
155
252
  }
156
253
  });
157
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
+ */
158
261
  function deepClone(obj) {
159
262
  if (obj === null || typeof obj !== 'object') {
160
263
  return obj;
@@ -174,6 +277,23 @@ function deepClone(obj) {
174
277
  }
175
278
  return copy;
176
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
+ */
177
297
  function _objectToFormData(data, formData, parentKey = '') {
178
298
  for (const key in data) {
179
299
  if (data.hasOwnProperty(key)) {
@@ -205,11 +325,92 @@ function _objectToFormData(data, formData, parentKey = '') {
205
325
  }
206
326
  return formData;
207
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
+ */
208
334
  function objectToFormData(data) {
209
335
  let formData = new FormData();
210
336
  return _objectToFormData(data, formData);
211
337
  }
212
338
 
339
+ /**
340
+ * Ensures that a given string has a specific prefix.
341
+ *
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.
345
+ */
346
+ function ensurePrefix(str, prefix) {
347
+ if (!str.startsWith(prefix))
348
+ return prefix + str;
349
+ return str;
350
+ }
351
+ /**
352
+ * Ensures that a string ends with a specified suffix.
353
+ *
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.
357
+ */
358
+ function ensureSuffix(str, suffix) {
359
+ if (!str.endsWith(suffix))
360
+ return str + suffix;
361
+ return str;
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
+ }
383
+
384
+ /**
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.
392
+ *
393
+ * @return {number} - The interpolated value.
394
+ */
395
+ function lerp(n, inMin, inMax, outMin, outMax) {
396
+ return outMin + (outMax - outMin) * ((n - inMin) / (inMax - inMin));
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
+ */
406
+ function clamp(n, min, max) {
407
+ if (n <= min)
408
+ return min;
409
+ if (n >= max)
410
+ return max;
411
+ return n;
412
+ }
413
+
213
414
  class CacheDictionary {
214
415
  constructor(fallback = null, defaultDuration = null) {
215
416
  this.fallback = fallback;
@@ -717,6 +918,9 @@ class Grouping extends From {
717
918
  }
718
919
  }
719
920
 
921
+ /**
922
+ * A class representing a timer that executes a callback function at a specified interval.
923
+ */
720
924
  class Timer {
721
925
  /**
722
926
  * @param callback Callback
@@ -768,6 +972,9 @@ class Timer {
768
972
  }
769
973
  }
770
974
 
975
+ /**
976
+ * Represents a duration of time in milliseconds.
977
+ */
771
978
  class TimeSpan {
772
979
  constructor(milliseconds) {
773
980
  this.milliseconds = milliseconds;
@@ -936,6 +1143,10 @@ class TimeSpan {
936
1143
  TimeSpan.INFINITE = new TimeSpan(Number.POSITIVE_INFINITY);
937
1144
  TimeSpan.NEGATIVE_INFINITE = new TimeSpan(Number.NEGATIVE_INFINITY);
938
1145
 
1146
+ /**
1147
+ * Represents a lazy value that is created only when it is accessed for the first time.
1148
+ * @template T The type of the lazy value.
1149
+ */
939
1150
  class Lazy {
940
1151
  constructor(getter) {
941
1152
  this._valueCreated = false;
@@ -945,9 +1156,9 @@ class Lazy {
945
1156
  get valueCreated() {
946
1157
  return this._valueCreated;
947
1158
  }
948
- async getValue() {
1159
+ get value() {
949
1160
  if (!this._valueCreated) {
950
- this._value = await promisify(this._factoryMethod);
1161
+ this._value = this._factoryMethod();
951
1162
  this._valueCreated = true;
952
1163
  }
953
1164
  return this._value;
@@ -958,6 +1169,138 @@ class Lazy {
958
1169
  }
959
1170
  }
960
1171
 
1172
+ /**
1173
+ * Represents a class for manipulating an array of items.
1174
+ *
1175
+ * @template T - The type of items in the array.
1176
+ */
1177
+ class ArrayManipulator {
1178
+ constructor(array) {
1179
+ this._array = array;
1180
+ }
1181
+ [Symbol.iterator]() {
1182
+ return this._array[Symbol.iterator]();
1183
+ }
1184
+ /**
1185
+ * Retrieves or sets the value at the specified index in the array.
1186
+ * If `item` is not provided, returns the value at index `n`.
1187
+ * If `item` is provided, sets the value at index `n` to the provided value.
1188
+ *
1189
+ * @param {number} n - The index at which to retrieve or set the value.
1190
+ * @param {T | undefined} [item] - The value to set at index `n`, if provided.
1191
+ *
1192
+ * @return {T | void} - If `item` is not provided, returns the value at index `n`.
1193
+ * - If `item` is provided, does not return anything.
1194
+ */
1195
+ at(n, item = undefined) {
1196
+ if (item === undefined)
1197
+ return this._array[n];
1198
+ else
1199
+ this._array[n] = item;
1200
+ }
1201
+ /**
1202
+ * Removes the specified item from the array.
1203
+ *
1204
+ * @param {T} item - The item to be removed.
1205
+ * @returns {ArrayManipulator<T>} - The updated ArrayManipulator instance.
1206
+ */
1207
+ remove(item) {
1208
+ const index = this._array.indexOf(item);
1209
+ if (index !== -1) {
1210
+ this._array.splice(index, 1);
1211
+ }
1212
+ return this;
1213
+ }
1214
+ /**
1215
+ * Applies a mapping function to each element in the array, transforming it into a new array.
1216
+ * @template R
1217
+ * @param {UnaryFunction<T, R>} mapFn - The function to be applied to each element.
1218
+ * @return {ArrayManipulator<R>} - The new ArrayManipulator instance with the mapped elements.
1219
+ */
1220
+ map(mapFn) {
1221
+ for (let i = 0; i < this._array.length; i++) {
1222
+ this._array[i] = mapFn(this._array[i]);
1223
+ }
1224
+ return this;
1225
+ }
1226
+ /**
1227
+ * Filters the elements of the array based on a given filter function.
1228
+ * The filter function should return a boolean value, indicating whether the element should be included in the filtered array or not.
1229
+ *
1230
+ * @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.
1231
+ * @returns {ArrayManipulator<T>} - The filtered ArrayManipulator instance with the elements that passed the filter.
1232
+ */
1233
+ filter(filterFn) {
1234
+ for (let i = 0; i < this._array.length; i++) {
1235
+ if (!filterFn(this._array[i])) {
1236
+ this._array.splice(i, 1);
1237
+ i--;
1238
+ }
1239
+ }
1240
+ return this;
1241
+ }
1242
+ head(n) {
1243
+ this._array.splice(n);
1244
+ return this;
1245
+ }
1246
+ slice(start, count = undefined) {
1247
+ this._array.splice(0, start); // Eliminar los primeros elementos hasta `start`
1248
+ if (count !== undefined) {
1249
+ this._array.splice(count); // Mantener solo `count` elementos restantes
1250
+ }
1251
+ return this;
1252
+ }
1253
+ mapMany(mapper) {
1254
+ let i = 0;
1255
+ while (i < this._array.length) {
1256
+ let mappedItems = mapper(this._array[i]);
1257
+ if (!Array.isArray(mappedItems))
1258
+ mappedItems = Array.from(mappedItems);
1259
+ this._array.splice(i, 1, ...mappedItems);
1260
+ i += mappedItems.length;
1261
+ }
1262
+ return this;
1263
+ }
1264
+ tail(n) {
1265
+ const start = this._array.length - n;
1266
+ this._array.splice(0, start);
1267
+ return this;
1268
+ }
1269
+ /**
1270
+ * Appends one or more items to the array.
1271
+ *
1272
+ * @param {...T} items - The items to be appended to the array.
1273
+ * @return {ArrayManipulator<T>} - The ArrayManipulator instance.
1274
+ */
1275
+ append(...items) {
1276
+ this._array.push(...items);
1277
+ return this;
1278
+ }
1279
+ /**
1280
+ * Prepend items to the beginning of the array.
1281
+ *
1282
+ * @param {...T} items - The items to be prepended.
1283
+ * @return {ArrayManipulator<T>} The ArrayManipulator instance.
1284
+ */
1285
+ prepend(...items) {
1286
+ this._array.unshift(...items);
1287
+ return this;
1288
+ }
1289
+ get array() {
1290
+ return this._array;
1291
+ }
1292
+ }
1293
+ /**
1294
+ * Creates an instance of ArrayManipulator with the given array.
1295
+ * @template T
1296
+ * @param {T[]} array - The input array.
1297
+ * @return {ArrayManipulator<T>} - An instance of the ArrayManipulator class.
1298
+ */
1299
+ function array(array) {
1300
+ return new ArrayManipulator(array);
1301
+ }
1302
+
1303
+ exports.ArrayManipulator = ArrayManipulator;
961
1304
  exports.CacheDictionary = CacheDictionary;
962
1305
  exports.From = From;
963
1306
  exports.Lazy = Lazy;
@@ -966,14 +1309,18 @@ exports.TimeSpan = TimeSpan;
966
1309
  exports.Timer = Timer;
967
1310
  exports.ajaxSubmission = ajaxSubmission;
968
1311
  exports.ajaxSubmit = ajaxSubmit;
1312
+ exports.array = array;
969
1313
  exports.clamp = clamp;
970
1314
  exports.deepClone = deepClone;
971
1315
  exports.ensurePrefix = ensurePrefix;
972
1316
  exports.ensureSuffix = ensureSuffix;
1317
+ exports.format = format;
973
1318
  exports.from = from;
1319
+ exports.getType = getType;
974
1320
  exports.isArray = isArray;
975
1321
  exports.isBigInt = isBigInt;
976
1322
  exports.isBoolean = isBoolean;
1323
+ exports.isDefined = isDefined;
977
1324
  exports.isEmpty = isEmpty;
978
1325
  exports.isEmptyOrWhitespace = isEmptyOrWhitespace;
979
1326
  exports.isFunction = isFunction;