@develia/commons 0.1.0
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/package.json +15 -0
- package/src/develia-cache.js +64 -0
- package/src/develia-cache.js.map +1 -0
- package/src/develia-cache.ts +77 -0
- package/src/develia-commons.js +135 -0
- package/src/develia-commons.js.map +1 -0
- package/src/develia-commons.ts +150 -0
- package/tsconfig.json +11 -0
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@develia/commons",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Antonio Gil Espinosa",
|
|
6
|
+
"email": "antonio.gil.espinosa@gmail.com",
|
|
7
|
+
"url": "https://antoniogilespinosa.com"
|
|
8
|
+
},
|
|
9
|
+
"license": "proprietary",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"typescript": "^5.1.3"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export class CacheDictionary {
|
|
2
|
+
constructor(fallback = null, defaultDuration = null) {
|
|
3
|
+
this.fallback = fallback;
|
|
4
|
+
this.cache = {};
|
|
5
|
+
this.defaultDuration = defaultDuration;
|
|
6
|
+
this.expiration = {};
|
|
7
|
+
}
|
|
8
|
+
getExpiration(key) {
|
|
9
|
+
return this.expiration[key];
|
|
10
|
+
}
|
|
11
|
+
setExpiration(key, expiration) {
|
|
12
|
+
this.expiration[key] = expiration;
|
|
13
|
+
}
|
|
14
|
+
setDuration(key, duration) {
|
|
15
|
+
if (duration === null) {
|
|
16
|
+
delete this.expiration[key];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
const expiration = new Date();
|
|
20
|
+
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
21
|
+
this.expiration[key] = expiration;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
get(key) {
|
|
25
|
+
if (!this.cache.hasOwnProperty(key)) {
|
|
26
|
+
if (this.fallback) {
|
|
27
|
+
this.cache[key] = this.fallback(key);
|
|
28
|
+
this.expiration[key] = this.calculateExpiration(this.defaultDuration);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (this.isExpired(key)) {
|
|
35
|
+
this.remove(key);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return this.cache[key];
|
|
39
|
+
}
|
|
40
|
+
set(key, value, duration = null) {
|
|
41
|
+
this.cache[key] = value;
|
|
42
|
+
this.expiration[key] = this.calculateExpiration(duration);
|
|
43
|
+
}
|
|
44
|
+
remove(key) {
|
|
45
|
+
delete this.cache[key];
|
|
46
|
+
delete this.expiration[key];
|
|
47
|
+
}
|
|
48
|
+
isExpired(key) {
|
|
49
|
+
const expiration = this.expiration[key];
|
|
50
|
+
if (expiration instanceof Date) {
|
|
51
|
+
return expiration < new Date();
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
calculateExpiration(duration) {
|
|
56
|
+
if (duration === null) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const expiration = new Date();
|
|
60
|
+
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
61
|
+
return expiration;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=develia-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"develia-cache.js","sourceRoot":"","sources":["develia-cache.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,eAAe;IAMxB,YAAY,WAAwC,IAAI,EAAE,kBAAiC,IAAI;QAC3F,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,I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AC;IAC3B,CAAC;IAEM,GAAG,CAAC,GAAW,EAAE,KAAQ,EAAE,WAA0B,IAAI;QAC5D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAEM,MAAM,CAAC,GAAW;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,SAAS,CAAC,GAAW;QACzB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,UAAU,YAAY,IAAI,EAAE,CAAC;YAC7B,OAAO,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QACnC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAEO,mBAAmB,CAAC,QAAuB;QAC/C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACpB,OAAO,SAAS,CAAC;QACrB,CAAC;QAED,MAAM,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;QAC9B,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,GAAG,QAAQ,CAAC,CAAC;QAC1D,OAAO,UAAU,CAAC;IACtB,CAAC;CACJ"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export class CacheDictionary<T> {
|
|
2
|
+
private readonly fallback: ((key: string) => T) | null;
|
|
3
|
+
private readonly cache: { [key: string]: T };
|
|
4
|
+
private readonly defaultDuration: number | null;
|
|
5
|
+
private readonly expiration: { [key: string]: Date | undefined };
|
|
6
|
+
|
|
7
|
+
constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {
|
|
8
|
+
this.fallback = fallback;
|
|
9
|
+
this.cache = {};
|
|
10
|
+
this.defaultDuration = defaultDuration;
|
|
11
|
+
this.expiration = {};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public getExpiration(key: string): Date | undefined {
|
|
15
|
+
return this.expiration[key];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public setExpiration(key: string, expiration: Date | undefined): void {
|
|
19
|
+
this.expiration[key] = expiration;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public setDuration(key: string, duration: number | null): void {
|
|
23
|
+
if (duration === null) {
|
|
24
|
+
delete this.expiration[key];
|
|
25
|
+
} else {
|
|
26
|
+
const expiration = new Date();
|
|
27
|
+
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
28
|
+
this.expiration[key] = expiration;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public get(key: string): T | null {
|
|
33
|
+
if (!this.cache.hasOwnProperty(key)) {
|
|
34
|
+
if (this.fallback) {
|
|
35
|
+
this.cache[key] = this.fallback(key);
|
|
36
|
+
this.expiration[key] = this.calculateExpiration(this.defaultDuration);
|
|
37
|
+
} else {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (this.isExpired(key)) {
|
|
43
|
+
this.remove(key);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return this.cache[key];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public set(key: string, value: T, duration: number | null = null): void {
|
|
51
|
+
this.cache[key] = value;
|
|
52
|
+
this.expiration[key] = this.calculateExpiration(duration);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public remove(key: string): void {
|
|
56
|
+
delete this.cache[key];
|
|
57
|
+
delete this.expiration[key];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private isExpired(key: string): boolean {
|
|
61
|
+
const expiration = this.expiration[key];
|
|
62
|
+
if (expiration instanceof Date) {
|
|
63
|
+
return expiration < new Date();
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private calculateExpiration(duration: number | null): Date | undefined {
|
|
69
|
+
if (duration === null) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const expiration = new Date();
|
|
74
|
+
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
75
|
+
return expiration;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
export function isString(value) {
|
|
12
|
+
return typeof value === 'string';
|
|
13
|
+
}
|
|
14
|
+
export function isNumber(value) {
|
|
15
|
+
return typeof value === 'number';
|
|
16
|
+
}
|
|
17
|
+
export function isBoolean(value) {
|
|
18
|
+
return typeof value === 'boolean';
|
|
19
|
+
}
|
|
20
|
+
export function isObject(value) {
|
|
21
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
export function isArray(value) {
|
|
24
|
+
return Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
export function isFunction(value) {
|
|
27
|
+
return typeof value === 'function';
|
|
28
|
+
}
|
|
29
|
+
export function isUndefined(value) {
|
|
30
|
+
return typeof value === 'undefined';
|
|
31
|
+
}
|
|
32
|
+
export function isNull(value) {
|
|
33
|
+
return value === null;
|
|
34
|
+
}
|
|
35
|
+
export function isBigInt(value) {
|
|
36
|
+
return typeof value === 'bigint';
|
|
37
|
+
}
|
|
38
|
+
export function isSymbol(value) {
|
|
39
|
+
return typeof value === 'symbol';
|
|
40
|
+
}
|
|
41
|
+
export function isEmpty(value) {
|
|
42
|
+
return (isArray(value) && value.length === 0) ||
|
|
43
|
+
(isString(value) && value === '') ||
|
|
44
|
+
isNull(value) ||
|
|
45
|
+
isUndefined(value);
|
|
46
|
+
}
|
|
47
|
+
export function isEmptyOrWhitespace(value) {
|
|
48
|
+
return (isArray(value) && value.length === 0) ||
|
|
49
|
+
(isString(value) && value.trim() === '') ||
|
|
50
|
+
isNull(value) ||
|
|
51
|
+
isUndefined(value);
|
|
52
|
+
}
|
|
53
|
+
export function ajaxSubmit(selectorOrElement) {
|
|
54
|
+
const form = typeof selectorOrElement === 'string'
|
|
55
|
+
? document.querySelector(selectorOrElement)
|
|
56
|
+
: selectorOrElement;
|
|
57
|
+
if (!(form instanceof HTMLFormElement)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Crear un objeto FormData a partir del formulario
|
|
61
|
+
return fetch(form.action, {
|
|
62
|
+
method: form.method,
|
|
63
|
+
body: new FormData(form),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
export function ajaxSubmission(selectorOrElement, onSuccess = null, onFailure = null) {
|
|
67
|
+
const form = typeof selectorOrElement === 'string'
|
|
68
|
+
? document.querySelector(selectorOrElement)
|
|
69
|
+
: selectorOrElement;
|
|
70
|
+
if (!(form instanceof HTMLFormElement)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
form.addEventListener('submit', (event) => __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
var _a;
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
(_a = ajaxSubmit(form)) === null || _a === void 0 ? void 0 : _a.then(onSuccess).catch(onFailure);
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
export function deepClone(obj) {
|
|
80
|
+
if (obj === null || typeof obj !== 'object') {
|
|
81
|
+
return obj;
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(obj)) {
|
|
84
|
+
const copy = [];
|
|
85
|
+
for (const element of obj) {
|
|
86
|
+
copy.push(deepClone(element));
|
|
87
|
+
}
|
|
88
|
+
return copy;
|
|
89
|
+
}
|
|
90
|
+
const copy = {};
|
|
91
|
+
for (const key in obj) {
|
|
92
|
+
if (obj.hasOwnProperty(key)) {
|
|
93
|
+
copy[key] = deepClone(obj[key]);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return copy;
|
|
97
|
+
}
|
|
98
|
+
function objectToFormData(data, formData = null, parentKey = '') {
|
|
99
|
+
if (!formData) {
|
|
100
|
+
formData = new FormData();
|
|
101
|
+
}
|
|
102
|
+
for (const key in data) {
|
|
103
|
+
if (data.hasOwnProperty(key)) {
|
|
104
|
+
const value = data[key];
|
|
105
|
+
if (value instanceof Date) {
|
|
106
|
+
formData.append(parentKey ? `${parentKey}[${key}]` : key, value.toISOString());
|
|
107
|
+
}
|
|
108
|
+
else if (value instanceof File) {
|
|
109
|
+
formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
|
|
110
|
+
}
|
|
111
|
+
else if (typeof value === 'object' && !Array.isArray(value)) {
|
|
112
|
+
objectToFormData(value, formData, parentKey ? `${parentKey}[${key}]` : key);
|
|
113
|
+
}
|
|
114
|
+
else if (Array.isArray(value)) {
|
|
115
|
+
value.forEach((item, index) => {
|
|
116
|
+
const arrayKey = `${parentKey ? `${parentKey}[${key}]` : key}[${index}]`;
|
|
117
|
+
if (typeof item === 'object' && !Array.isArray(item)) {
|
|
118
|
+
objectToFormData(item, formData, arrayKey);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
formData.append(arrayKey, item);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return formData;
|
|
131
|
+
}
|
|
132
|
+
export function toFormData(data) {
|
|
133
|
+
return objectToFormData(data);
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=develia-commons.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"develia-commons.js","sourceRoot":"","sources":["develia-commons.ts"],"names":[],"mappings":"AAAA,qCAAqC;;;;;;;;;;AAErC,MAAM,UAAU,QAAQ,CAAC,KAAU;IAC/B,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,KAAU;IAC/B,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAU;IAChC,OAAO,OAAO,KAAK,KAAK,SAAS,CAAC;AACtC,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,KAAU;IAC/B,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChF,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,KAAU;IAC9B,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAU;IACjC,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAU;IAClC,OAAO,OAAO,KAAK,KAAK,WAAW,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,KAAU;IAC7B,OAAO,KAAK,KAAK,IAAI,CAAC;AAC1B,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,KAAU;IAC/B,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,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QAAQ,EAAE,QAAQ,CAAC,CAAC;oBAC/C,CAAC;yBAAM,CAAC;wBACJ,QAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;oBACpC,CAAC;gBACL,CAAC,CAAC,CAAC;YACP,CAAC;iBAAM,CAAC;gBACJ,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACrE,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,QAAQ,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAyB;IAChD,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols
|
|
2
|
+
|
|
3
|
+
export function isString(value: any): value is string {
|
|
4
|
+
return typeof value === 'string';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function isNumber(value: any): value is number {
|
|
8
|
+
return typeof value === 'number';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isBoolean(value: any): value is boolean {
|
|
12
|
+
return typeof value === 'boolean';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isObject(value: any): value is object {
|
|
16
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isArray(value: any): value is any[] {
|
|
20
|
+
return Array.isArray(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isFunction(value: any): value is Function {
|
|
24
|
+
return typeof value === 'function';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isUndefined(value: any): value is undefined {
|
|
28
|
+
return typeof value === 'undefined';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isNull(value: any): value is null {
|
|
32
|
+
return value === null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isBigInt(value: any): value is bigint {
|
|
36
|
+
return typeof value === 'bigint';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function isSymbol(value: any): value is symbol {
|
|
40
|
+
return typeof value === 'symbol';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function isEmpty(value: any): boolean {
|
|
44
|
+
return (isArray(value) && value.length === 0) ||
|
|
45
|
+
(isString(value) && value === '') ||
|
|
46
|
+
isNull(value) ||
|
|
47
|
+
isUndefined(value);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isEmptyOrWhitespace(value: any): boolean {
|
|
51
|
+
return (isArray(value) && value.length === 0) ||
|
|
52
|
+
(isString(value) && value.trim() === '') ||
|
|
53
|
+
isNull(value) ||
|
|
54
|
+
isUndefined(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function ajaxSubmit(selectorOrElement: HTMLFormElement | string) {
|
|
58
|
+
|
|
59
|
+
const form = typeof selectorOrElement === 'string'
|
|
60
|
+
? document.querySelector(selectorOrElement)
|
|
61
|
+
: selectorOrElement;
|
|
62
|
+
|
|
63
|
+
if (!(form instanceof HTMLFormElement)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Crear un objeto FormData a partir del formulario
|
|
68
|
+
return fetch(form.action, {
|
|
69
|
+
method: form.method,
|
|
70
|
+
body: new FormData(form),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function ajaxSubmission(selectorOrElement: HTMLFormElement | string, onSuccess = null, onFailure = null) {
|
|
75
|
+
|
|
76
|
+
const form = typeof selectorOrElement === 'string'
|
|
77
|
+
? document.querySelector(selectorOrElement)
|
|
78
|
+
: selectorOrElement;
|
|
79
|
+
|
|
80
|
+
if (!(form instanceof HTMLFormElement)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
form.addEventListener('submit', async (event) => {
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
ajaxSubmit(form)?.then(onSuccess).catch(onFailure);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
export function deepClone<T>(obj: T): T {
|
|
94
|
+
if (obj === null || typeof obj !== 'object') {
|
|
95
|
+
return obj;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (Array.isArray(obj)) {
|
|
99
|
+
const copy: any[] = [];
|
|
100
|
+
for (const element of obj) {
|
|
101
|
+
copy.push(deepClone(element));
|
|
102
|
+
}
|
|
103
|
+
return copy as T;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const copy: { [key: string]: any } = {};
|
|
107
|
+
for (const key in obj) {
|
|
108
|
+
if (obj.hasOwnProperty(key)) {
|
|
109
|
+
copy[key] = deepClone((obj as { [key: string]: any })[key]);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return copy as T;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function objectToFormData(data: Record<string, any>, formData: FormData | null = null, parentKey: string = ''): FormData {
|
|
116
|
+
if (!formData) {
|
|
117
|
+
formData = new FormData();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
for (const key in data) {
|
|
121
|
+
if (data.hasOwnProperty(key)) {
|
|
122
|
+
const value = data[key];
|
|
123
|
+
|
|
124
|
+
if (value instanceof Date) {
|
|
125
|
+
formData.append(parentKey ? `${parentKey}[${key}]` : key, value.toISOString());
|
|
126
|
+
} else if (value instanceof File) {
|
|
127
|
+
formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
|
|
128
|
+
} else if (typeof value === 'object' && !Array.isArray(value)) {
|
|
129
|
+
objectToFormData(value, formData, parentKey ? `${parentKey}[${key}]` : key);
|
|
130
|
+
} else if (Array.isArray(value)) {
|
|
131
|
+
value.forEach((item, index) => {
|
|
132
|
+
const arrayKey = `${parentKey ? `${parentKey}[${key}]` : key}[${index}]`;
|
|
133
|
+
if (typeof item === 'object' && !Array.isArray(item)) {
|
|
134
|
+
objectToFormData(item, formData, arrayKey);
|
|
135
|
+
} else {
|
|
136
|
+
formData.append(arrayKey, item);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
} else {
|
|
140
|
+
formData.append(parentKey ? `${parentKey}[${key}]` : key, value);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return formData;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function toFormData(data: Record<string, any>): FormData {
|
|
149
|
+
return objectToFormData(data);
|
|
150
|
+
}
|