aprog 1.0.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/src/aprog.js ADDED
@@ -0,0 +1,1306 @@
1
+ export const pause = (variable) => {
2
+ if (isNumber(variable)) {
3
+ return new Promise(resolve => setTimeout(resolve, variable));
4
+ } else {
5
+ return new Promise(resolve => {
6
+ const checkVariable = () => {
7
+ if (variable()) {
8
+ resolve();
9
+ } else {
10
+ setTimeout(checkVariable, 100);
11
+ }
12
+ };
13
+ checkVariable();
14
+ });
15
+ }
16
+ }
17
+
18
+
19
+ export const date = (format, timestamp = Date.now()) => {
20
+ const date = new Date(timestamp);
21
+
22
+ const map = {
23
+ Y: date.getFullYear(),
24
+ m: String(date.getMonth() + 1).padStart(2, '0'),
25
+ d: String(date.getDate()).padStart(2, '0'),
26
+ H: String(date.getHours()).padStart(2, '0'),
27
+ i: String(date.getMinutes()).padStart(2, '0'),
28
+ s: String(date.getSeconds()).padStart(2, '0')
29
+ };
30
+
31
+ return format.replace(/[YmdHis]/g, match => map[match]);
32
+ }
33
+
34
+
35
+ export const chars = (word) => {
36
+ const cyrillicToLatin = {
37
+ 'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'H', 'Д': 'D', 'Е': 'E', 'Є': 'Ye', 'Ж': 'Zh',
38
+ 'З': 'Z', 'И': 'Y', 'І': 'I', 'Ї': 'Yi', 'Й': 'Y', 'К': 'K', 'Л': 'L', 'М': 'M',
39
+ 'Н': 'N', 'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T', 'У': 'U', 'Ф': 'F',
40
+ 'Х': 'Kh', 'Ц': 'Ts', 'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Shch', 'Ю': 'Yu', 'Я': 'Ya',
41
+ 'а': 'a', 'б': 'b', 'в': 'v', 'г': 'h', 'д': 'd', 'е': 'e', 'є': 'ye', 'ж': 'zh',
42
+ 'з': 'z', 'и': 'y', 'і': 'i', 'ї': 'yi', 'й': 'y', 'к': 'k', 'л': 'l', 'м': 'm',
43
+ 'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u', 'ф': 'f',
44
+ 'х': 'kh', 'ц': 'ts', 'ч': 'ch', 'ш': 'sh', 'щ': 'shch', 'ю': 'yu', 'я': 'ya',
45
+ 'Ь': '', 'ь': '', 'Ъ': '', 'ъ': '', 'Э': 'E', 'э': 'e', 'Ы': 'Y', 'ы': 'y'
46
+ };
47
+ return word.split('').map(char => cyrillicToLatin[char] || char).join('');
48
+ }
49
+
50
+
51
+ // Validates
52
+ export const isArray = (value) => {
53
+ return Object.prototype.toString.call(value) === '[object Array]';
54
+ }
55
+
56
+
57
+ export const isObject = (value) => {
58
+ return Object.prototype.toString.call(value) === '[object Object]';
59
+ }
60
+
61
+
62
+ export const isPromise = (value) => {
63
+ return Object.prototype.toString.call(value) === '[object Promise]' ||
64
+ (typeof value === 'object' && value !== null && typeof value.then === 'function' && typeof value.catch === 'function');
65
+ }
66
+
67
+
68
+ export const isNumber = (value) => {
69
+ return typeof value === 'number' && !Number.isNaN(value);
70
+ }
71
+
72
+
73
+ export const isNumeric = (value) => {
74
+ return /^-?\d+(\.\d+)?$/.test(value);
75
+ }
76
+
77
+
78
+ export const isFloat = (value) => {
79
+ return !isNaN(value) && Number(value) === value && !Number.isInteger(value);
80
+ }
81
+
82
+
83
+ export const isString = (value) => {
84
+ return typeof value === 'string';
85
+ }
86
+
87
+
88
+ export const isNull = (value) => {
89
+ return value === null || value === undefined;
90
+ }
91
+
92
+
93
+ const isHtmlElement = (value) => {
94
+ const htmlElements = [
95
+ 'HTMLDivElement', "HTMLAnchorElement", 'HTMLSpanElement',
96
+ 'HTMLButtonElement', 'HTMLSelectElement',
97
+ 'HTMLInputElement', 'HTMLUlElement', "Promise",
98
+ 'HTMLListElement', 'HTMLLabelElement',
99
+ 'HTMLHRElement', 'SVGSVGElement', 'HTMLPreElement',
100
+ 'Promise', "HTMLTextAreaElement", "HTMLTableElement",
101
+ "HTMLTHeaderElement", "HTMLTBodyElement", "HTMLTRElement",
102
+ "HTMLTHElement", "HTMLTDElement", "HTMLTableSectionElement",
103
+ "HTMLTableRowElement", "HTMLTableCellElement", "HTMLImageElement"
104
+ ]
105
+
106
+ return htmlElements.some((element) => {
107
+ return Object.prototype.toString.call(value) === `[object ${element}]`;
108
+ });
109
+ }
110
+
111
+
112
+ export const isEmpty = (variable) => {
113
+ if (variable === undefined || variable === null) {
114
+ return true;
115
+ }
116
+ if (typeof variable === 'string' && variable.trim() === '') {
117
+ return true;
118
+ }
119
+ if (Array.isArray(variable) && variable.length === 0) {
120
+ return true;
121
+ }
122
+ if (typeof variable === 'object' && !Array.isArray(variable) && Object.keys(variable).length === 0) {
123
+ return true;
124
+ }
125
+ if (typeof variable === 'number' && variable === 0) {
126
+ return false;
127
+ }
128
+ return !variable;
129
+ }
130
+
131
+
132
+ export const elementToObject = (element) => {
133
+ const obj = {};
134
+ for (let attr of element.attributes) {
135
+ obj[attr.name] = attr.value;
136
+ }
137
+ if (element.tagName.toLowerCase() === 'input') {
138
+ obj['inputValue'] = element.value;
139
+ } else if (element.tagName.toLowerCase() === 'select') {
140
+ obj['selectValue'] = element.value;
141
+ }
142
+ return obj;
143
+ }
144
+
145
+
146
+ export const nodeListToObject = (nodeList) => {
147
+ return Array.from(nodeList).map((el, index) => ({
148
+ ...(el.id && {id: el.id}),
149
+ ...(el.classList && {classList: [...el.classList]}),
150
+ ...(el.name && {name: el.name}),
151
+ ...(el.tagName && {element: el.tagName.toLowerCase()}),
152
+ ...(el.children && {children: [...nodeListToObject(el.children)]}),
153
+ ...(el.attributes && {attributes: Array.from(el.attributes).reduce((acc, attr) => {
154
+ acc[attr.name] = attr.value;
155
+ return acc;
156
+ }, {})}),
157
+ ...(el.value && {value: el.value}),
158
+ ...(el.innerText && {value: el.innerText}),
159
+ index: index
160
+ }));
161
+ }
162
+
163
+
164
+ export const deepEqual = (obj1, obj2) => {
165
+ if (obj1 === obj2) return true;
166
+ if (obj1 == null || obj2 == null) return false;
167
+ if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return false;
168
+ const keys1 = Object.keys(obj1);
169
+ const keys2 = Object.keys(obj2);
170
+ if (keys1.length !== keys2.length) return false;
171
+ for (const key of keys1) {
172
+ if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {
173
+ return false;
174
+ }
175
+ }
176
+ return true;
177
+ }
178
+
179
+
180
+ export const ucfirst = (str) => {
181
+ if (!str) return str;
182
+ return str.charAt(0).toUpperCase() + str.slice(1);
183
+ }
184
+
185
+
186
+ export const firstKey = (object) => {
187
+ return Object.keys(object)[0] || null;
188
+ }
189
+
190
+
191
+ export const copyText = async (element) => {
192
+ try {
193
+ const textToCopy = isHtmlElement(element) ? JSON.stringify(JSON.parse(element.innerText)) : element;
194
+ await navigator.clipboard.writeText(textToCopy);
195
+ console.log("Скопійовано");
196
+ } catch (err) {
197
+ console.error('Помилка копіювання! Error:', err);
198
+ }
199
+ }
200
+
201
+
202
+ // HTML Elements
203
+ export const options = (object, selected = null, disabled = null) => {
204
+ if (isObject(object)) {
205
+ return Object.entries(object).map(([key, value]) => {
206
+ key = isNumeric(key) ? parseFloat(key) : key;
207
+ return {
208
+ value: key,
209
+ name: value,
210
+ selected: key === selected,
211
+ disabled: key === disabled,
212
+ };
213
+ });
214
+ } else {
215
+ return object.map((value, index) => {
216
+ if (isObject(value)) {
217
+ return Object.entries(value).map(([key, value]) => {
218
+ key = isNumeric(key) ? parseFloat(key) : key;
219
+ return {
220
+ value: key,
221
+ name: value,
222
+ selected: key === selected,
223
+ disabled: key === disabled,
224
+ };
225
+ });
226
+ } else {
227
+ return {
228
+ value: value,
229
+ name: value,
230
+ selected: value === selected,
231
+ disabled: value === disabled,
232
+ };
233
+ }
234
+ });
235
+ }
236
+ }
237
+
238
+
239
+ export const input = (attributes = {}) => {
240
+ let input = document.createElement("input");
241
+ if (attributes) {
242
+ Object.entries(attributes).forEach(([name, attrValue]) => {
243
+ if (name.startsWith('on') && typeof attrValue === 'function') {
244
+ const eventType = name.slice(2).toLowerCase();
245
+ input.addEventListener(eventType, attrValue);
246
+ } else {
247
+ switch (name) {
248
+ case "type":
249
+ input.type = String(attrValue);
250
+ break;
251
+ case "class":
252
+ input.className = String(attrValue);
253
+ break;
254
+ case "style":
255
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
256
+ switch (styleName) {
257
+ case "zIndex":
258
+ input.style[styleName] = `99999${styleValue}`;
259
+ break;
260
+ default:
261
+ input.style[styleName] = styleValue;
262
+ break;
263
+ }
264
+ });
265
+ break;
266
+ case "disabled":
267
+ input.disabled = attrValue;
268
+ break;
269
+ default:
270
+ if (name.startsWith("data")) {
271
+ const kebabName = name
272
+ .replace(/^data/, "data")
273
+ .replace(/([A-Z])/g, "-$1")
274
+ .toLowerCase();
275
+ input.setAttribute(kebabName, String(attrValue));
276
+ } else {
277
+ input.setAttribute(name, String(attrValue));
278
+ }
279
+ }
280
+ }
281
+ });
282
+ }
283
+ return input;
284
+ }
285
+
286
+
287
+ export const label = (attributes = {}) => {
288
+ let label = document.createElement("label");
289
+ label.setAttribute("for", "...");
290
+ if (attributes) {
291
+ Object.entries(attributes).forEach(([name, attrValue]) => {
292
+ if (name.startsWith('on') && typeof attrValue === 'function') {
293
+ const eventType = name.slice(2).toLowerCase();
294
+ label.addEventListener(eventType, attrValue);
295
+ } else {
296
+ switch (name) {
297
+ case "class":
298
+ label.className = String(attrValue);
299
+ break;
300
+ case "value":
301
+ if (isArray(attrValue)) {
302
+ attrValue.forEach((element) => {
303
+ isHtmlElement(element) ? label.appendChild(element) : label.innerHTML = String(element)
304
+ })
305
+ } else {
306
+ isHtmlElement(attrValue) ? label.appendChild(attrValue) : label.innerHTML = String(attrValue)
307
+ }
308
+ break;
309
+ case "style":
310
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
311
+ switch (styleName) {
312
+ case "zIndex":
313
+ label.style[styleName] = `99999${styleValue}`;
314
+ break;
315
+ default:
316
+ label.style[styleName] = styleValue;
317
+ break;
318
+ }
319
+ });
320
+ break;
321
+ default:
322
+ if (name.startsWith("data")) {
323
+ const kebabName = name
324
+ .replace(/^data/, "data")
325
+ .replace(/([A-Z])/g, "-$1")
326
+ .toLowerCase();
327
+ label.setAttribute(kebabName, String(attrValue));
328
+ } else {
329
+ label.setAttribute(name, String(attrValue));
330
+ }
331
+ }
332
+ }
333
+ });
334
+ }
335
+ return label;
336
+ }
337
+
338
+
339
+ export const form = (attributes = {}) => {
340
+ let form = document.createElement("form");
341
+ if (attributes) {
342
+ Object.entries(attributes).forEach(([name, attrValue]) => {
343
+ if (name.startsWith('on') && typeof attrValue === 'function') {
344
+ const eventType = name.slice(2).toLowerCase();
345
+ form.addEventListener(eventType, attrValue);
346
+ } else {
347
+ switch (name) {
348
+ case "class":
349
+ form.className = String(attrValue);
350
+ break;
351
+ case "action":
352
+ form.action = String(attrValue);
353
+ break;
354
+ case "method":
355
+ form.method = String(attrValue);
356
+ break;
357
+ case "enctype":
358
+ form.enctype = String(attrValue);
359
+ break;
360
+ case "value":
361
+ if (isArray(attrValue)) {
362
+ attrValue.forEach((element) => {
363
+ isHtmlElement(element) ? form.append(element) : form.innerHTML = String(element)
364
+ })
365
+ } else {
366
+ isHtmlElement(attrValue) ? form.append(attrValue) : form.innerHTML = String(attrValue)
367
+ }
368
+ break;
369
+ case "style":
370
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
371
+ switch (styleName) {
372
+ case "zIndex":
373
+ form.style[styleName] = `99999${styleValue}`;
374
+ break;
375
+ default:
376
+ form.style[styleName] = styleValue;
377
+ break;
378
+ }
379
+ });
380
+ break;
381
+ default:
382
+ if (name.startsWith("data")) {
383
+ const kebabName = name
384
+ .replace(/^data/, "data")
385
+ .replace(/([A-Z])/g, "-$1")
386
+ .toLowerCase();
387
+ form.setAttribute(kebabName, String(attrValue));
388
+ } else {
389
+ form.setAttribute(name, String(attrValue));
390
+ }
391
+ }
392
+ }
393
+ });
394
+ }
395
+ return form;
396
+ }
397
+
398
+
399
+ export const div = (attributes = {}) => {
400
+ let div = document.createElement("div");
401
+ if (attributes) {
402
+ Object.entries(attributes).forEach(([name, attrValue]) => {
403
+ if (name.startsWith('on') && typeof attrValue === 'function') {
404
+ const eventType = name.slice(2).toLowerCase();
405
+ div.addEventListener(eventType, attrValue);
406
+ } else {
407
+ switch (name) {
408
+ case "class":
409
+ div.className = String(attrValue);
410
+ break;
411
+ case "value":
412
+ if (isArray(attrValue)) {
413
+ attrValue.forEach((element) => {
414
+ isHtmlElement(element) ? div.append(element) : div.innerHTML = String(element)
415
+ })
416
+ } else {
417
+ isHtmlElement(attrValue) ? div.append(attrValue) : div.innerHTML = String(attrValue)
418
+ }
419
+ break;
420
+ case "style":
421
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
422
+ switch (styleName) {
423
+ case "zIndex":
424
+ div.style[styleName] = `99999${styleValue}`;
425
+ break;
426
+ default:
427
+ div.style[styleName] = styleValue;
428
+ break;
429
+ }
430
+ });
431
+ break;
432
+ default:
433
+ if (name.startsWith("data")) {
434
+ const kebabName = name
435
+ .replace(/^data/, "data")
436
+ .replace(/([A-Z])/g, "-$1")
437
+ .toLowerCase();
438
+ div.setAttribute(kebabName, String(attrValue));
439
+ } else {
440
+ div.setAttribute(name, String(attrValue));
441
+ }
442
+ }
443
+ }
444
+ });
445
+ }
446
+ return div;
447
+ }
448
+
449
+
450
+ export const a = (attributes = {}) => {
451
+ let a = document.createElement("a");
452
+ if (attributes) {
453
+ Object.entries(attributes).forEach(([name, attrValue]) => {
454
+ if (name.startsWith('on') && typeof attrValue === 'function') {
455
+ const eventType = name.slice(2).toLowerCase();
456
+ a.addEventListener(eventType, attrValue);
457
+ } else {
458
+ switch (name) {
459
+ case "class":
460
+ a.className = String(attrValue);
461
+ break;
462
+ case "value":
463
+ if (isArray(attrValue)) {
464
+ attrValue.forEach((element) => {
465
+ isHtmlElement(element) ? a.append(element) : a.innerHTML = String(element)
466
+ })
467
+ } else {
468
+ isHtmlElement(attrValue) ? a.append(attrValue) : a.innerHTML = String(attrValue)
469
+ }
470
+ break;
471
+ case "style":
472
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
473
+ switch (styleName) {
474
+ case "zIndex":
475
+ a.style[styleName] = `99999${styleValue}`;
476
+ break;
477
+ default:
478
+ a.style[styleName] = styleValue;
479
+ break;
480
+ }
481
+ });
482
+ break;
483
+ default:
484
+ if (name.startsWith("data")) {
485
+ const kebabName = name
486
+ .replace(/^data/, "data")
487
+ .replace(/([A-Z])/g, "-$1")
488
+ .toLowerCase();
489
+ a.setAttribute(kebabName, String(attrValue));
490
+ } else {
491
+ a.setAttribute(name, String(attrValue));
492
+ }
493
+ }
494
+ }
495
+ });
496
+ }
497
+ return a;
498
+ }
499
+
500
+
501
+ export const span = (attributes = {}) => {
502
+ let span = document.createElement("span");
503
+ if (attributes) {
504
+ Object.entries(attributes).forEach(([name, attrValue]) => {
505
+ if (name.startsWith('on') && typeof attrValue === 'function') {
506
+ const eventType = name.slice(2).toLowerCase();
507
+ span.addEventListener(eventType, attrValue);
508
+ } else {
509
+ switch (name) {
510
+ case "class":
511
+ span.className = String(attrValue);
512
+ break;
513
+ case "value":
514
+ if (isArray(attrValue)) {
515
+ attrValue.forEach((element) => {
516
+ isHtmlElement(element) ? span.append(element) : span.innerHTML = String(element)
517
+ })
518
+ } else {
519
+ isHtmlElement(attrValue) ? span.append(attrValue) : span.innerHTML = String(attrValue)
520
+ }
521
+ break;
522
+ case "style":
523
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
524
+ switch (styleName) {
525
+ case "zIndex":
526
+ span.style[styleName] = `99999${styleValue}`;
527
+ break;
528
+ default:
529
+ span.style[styleName] = styleValue;
530
+ break;
531
+ }
532
+ });
533
+ break;
534
+ default:
535
+ if (name.startsWith("data")) {
536
+ const kebabName = name
537
+ .replace(/^data/, "data")
538
+ .replace(/([A-Z])/g, "-$1")
539
+ .toLowerCase();
540
+ span.setAttribute(kebabName, String(attrValue));
541
+ } else {
542
+ span.setAttribute(name, String(attrValue));
543
+ }
544
+ }
545
+ }
546
+ });
547
+ }
548
+ return span;
549
+ }
550
+
551
+
552
+ export const textarea = (attributes = {}) => {
553
+ let textarea = document.createElement("textarea");
554
+ if (attributes) {
555
+ Object.entries(attributes).forEach(([name, attrValue]) => {
556
+ if (name.startsWith('on') && typeof attrValue === 'function') {
557
+ const eventType = name.slice(2).toLowerCase();
558
+ textarea.addEventListener(eventType, attrValue);
559
+ } else {
560
+ switch (name) {
561
+ case "class":
562
+ textarea.className = String(attrValue);
563
+ break;
564
+ case "value":
565
+ if (isArray(attrValue)) {
566
+ attrValue.forEach((element) => {
567
+ isHtmlElement(element) ? textarea.appendChild(element) : textarea.innerHTML = String(element)
568
+ })
569
+ } else {
570
+ isHtmlElement(attrValue) ? textarea.appendChild(attrValue) : textarea.innerHTML = String(attrValue)
571
+ }
572
+ break;
573
+ case "style":
574
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
575
+ switch (styleName) {
576
+ case "zIndex":
577
+ textarea.style[styleName] = `99999${styleValue}`;
578
+ break;
579
+ default:
580
+ textarea.style[styleName] = styleValue;
581
+ break;
582
+ }
583
+ });
584
+ break;
585
+ default:
586
+ if (name.startsWith("data")) {
587
+ const kebabName = name
588
+ .replace(/^data/, "data")
589
+ .replace(/([A-Z])/g, "-$1")
590
+ .toLowerCase();
591
+ textarea.setAttribute(kebabName, String(attrValue));
592
+ } else {
593
+ textarea.setAttribute(name, String(attrValue));
594
+ }
595
+ }
596
+ }
597
+ });
598
+ }
599
+ return textarea;
600
+ }
601
+
602
+
603
+ export const pre = (attributes = {}) => {
604
+ let pre = document.createElement("pre");
605
+ if (attributes) {
606
+ Object.entries(attributes).forEach(([name, attrValue]) => {
607
+ if (name.startsWith('on') && typeof attrValue === 'function') {
608
+ const eventType = name.slice(2).toLowerCase();
609
+ pre.addEventListener(eventType, attrValue);
610
+ } else {
611
+ switch (name) {
612
+ case "class":
613
+ pre.className = String(attrValue);
614
+ break;
615
+ case "value":
616
+ if (isArray(attrValue)) {
617
+ attrValue.forEach((element) => {
618
+ isHtmlElement(element) ? pre.appendChild(element) : pre.innerHTML = String(element)
619
+ })
620
+ } else {
621
+ isHtmlElement(attrValue) ? pre.appendChild(attrValue) : pre.innerHTML = String(attrValue)
622
+ }
623
+ break;
624
+ case "style":
625
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
626
+ switch (styleName) {
627
+ case "zIndex":
628
+ pre.style[styleName] = `99999${styleValue}`;
629
+ break;
630
+ default:
631
+ pre.style[styleName] = styleValue;
632
+ break;
633
+ }
634
+ });
635
+ break;
636
+ default:
637
+ if (name.startsWith("data")) {
638
+ const kebabName = name
639
+ .replace(/^data/, "data")
640
+ .replace(/([A-Z])/g, "-$1")
641
+ .toLowerCase();
642
+ pre.setAttribute(kebabName, String(attrValue));
643
+ } else {
644
+ pre.setAttribute(name, String(attrValue));
645
+ }
646
+ }
647
+ }
648
+ });
649
+ }
650
+ return pre;
651
+ }
652
+
653
+
654
+ export const button = (attributes = {}) => {
655
+ let button = document.createElement("button");
656
+ if (attributes) {
657
+ Object.entries(attributes).forEach(([name, attrValue]) => {
658
+ if (name.startsWith('on') && typeof attrValue === 'function') {
659
+ const eventType = name.slice(2).toLowerCase();
660
+ button.addEventListener(eventType, attrValue);
661
+ } else {
662
+ switch (name) {
663
+ case "class":
664
+ button.className = String(attrValue);
665
+ break;
666
+ case "type":
667
+ button.type = String(attrValue);
668
+ break;
669
+ case "value":
670
+ button.innerHTML = String(attrValue);
671
+ break;
672
+ case "disabled":
673
+ button.disabled = attrValue;
674
+ break;
675
+ case "style":
676
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
677
+ switch (styleName) {
678
+ case "zIndex":
679
+ button.style[styleName] = `99999${styleValue}`;
680
+ break;
681
+ default:
682
+ button.style[styleName] = styleValue;
683
+ break;
684
+ }
685
+ });
686
+ break;
687
+ default:
688
+ if (name.startsWith("data")) {
689
+ const kebabName = name
690
+ .replace(/^data/, "data")
691
+ .replace(/([A-Z])/g, "-$1")
692
+ .toLowerCase();
693
+ button.setAttribute(kebabName, String(attrValue));
694
+ } else {
695
+ button.setAttribute(name, String(attrValue));
696
+ }
697
+ }
698
+ }
699
+ });
700
+ }
701
+ return button;
702
+ }
703
+
704
+
705
+ export const select = (attributes = {}) => {
706
+ let select = document.createElement("select");
707
+ let option = document.createElement("option");
708
+ if (attributes) {
709
+ Object.entries(attributes).forEach(([name, attrValue]) => {
710
+ if (name.startsWith('on') && typeof attrValue === 'function') {
711
+ const eventType = name.slice(2).toLowerCase();
712
+ select.addEventListener(eventType, attrValue);
713
+ } else {
714
+ switch (name) {
715
+ case "class":
716
+ select.className = String(attrValue);
717
+ break;
718
+ case "style":
719
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
720
+ switch (styleName) {
721
+ case "zIndex":
722
+ select.style[styleName] = `99999${styleValue}`;
723
+ break;
724
+ default:
725
+ select.style[styleName] = styleValue;
726
+ break;
727
+ }
728
+ });
729
+ break;
730
+ case "option":
731
+ Object.entries(attrValue).forEach(([optionName, optionValue]) => {
732
+ switch (optionName) {
733
+ case "value":
734
+ option.setAttribute('value', String(optionValue));
735
+ break;
736
+ case "name":
737
+ option.innerHTML = String(optionValue);
738
+ break;
739
+ case "selected":
740
+ option.selected = true;
741
+ break;
742
+ case "disabled":
743
+ option.disabled = true;
744
+ break;
745
+ }
746
+ });
747
+ select.appendChild(option);
748
+ break;
749
+ case "options":
750
+ attrValue.forEach((opt) => {
751
+ let customOption = document.createElement("option");
752
+ Object.entries(opt).forEach(([optionName, optionValue]) => {
753
+ switch (optionName) {
754
+ case "value":
755
+ customOption.value = String(optionValue);
756
+ break;
757
+ case "name":
758
+ customOption.innerHTML = String(optionValue);
759
+ break;
760
+ case "selected":
761
+ customOption.selected = Boolean(optionValue);
762
+ break;
763
+ case "disabled":
764
+ customOption.disabled = Boolean(optionValue);
765
+ break;
766
+ }
767
+ });
768
+ select.appendChild(customOption);
769
+ });
770
+ break;
771
+ default:
772
+ if (name.startsWith("data")) {
773
+ const kebabName = name
774
+ .replace(/^data/, "data")
775
+ .replace(/([A-Z])/g, "-$1")
776
+ .toLowerCase();
777
+ select.setAttribute(kebabName, String(attrValue));
778
+ } else {
779
+ select.setAttribute(name, String(attrValue));
780
+ }
781
+ }
782
+ }
783
+ });
784
+ }
785
+ return select;
786
+ };
787
+
788
+
789
+
790
+ export const option = (attributes = {}) => {
791
+ let option = document.createElement("option");
792
+ if (attributes) {
793
+ Object.entries(attributes).forEach(([optionName, optionValue]) => {
794
+ switch (optionName) {
795
+ case "value": option.value = optionValue; break;
796
+ case "name": option.textContent = optionValue; break;
797
+ case "selected": option.selected = !!optionValue; break;
798
+ case "disabled": option.disabled = !!optionValue; break;
799
+ }
800
+ });
801
+ }
802
+ return option;
803
+ }
804
+
805
+
806
+ export const hr = (attributes = {}) => {
807
+ let hr = document.createElement("hr");
808
+ if (attributes) {
809
+ Object.entries(attributes).forEach(([name, attrValue]) => {
810
+ switch (name) {
811
+ case "class": hr.className = String(attrValue); break;
812
+ case "style": Object.entries(attrValue).forEach(([styleName, styleValue]) => {
813
+ switch (styleName) {
814
+ case "zIndex": hr.style[styleName] = `99999${styleValue}`; break;
815
+ default: hr.style[styleName] = styleValue; break;
816
+ }
817
+ }); break;
818
+ default: hr.setAttribute(name, String(attrValue));
819
+ }
820
+ });
821
+ }
822
+ return hr;
823
+ }
824
+
825
+
826
+ export const meta = (attributes = {}) => {
827
+ let meta = document.createElement("meta");
828
+ if (attributes) {
829
+ Object.entries(attributes).forEach(([name, attrValue]) => {
830
+ switch (name) {
831
+ case "class": meta.className = String(attrValue); break;
832
+ case "style": Object.entries(attrValue).forEach(([styleName, styleValue]) => {
833
+ switch (styleName) {
834
+ case "zIndex": meta.style[styleName] = `99999${styleValue}`; break;
835
+ default: meta.style[styleName] = styleValue; break;
836
+ }
837
+ }); break;
838
+ default: meta.setAttribute(name, String(attrValue));
839
+ }
840
+ });
841
+ }
842
+ return meta;
843
+ }
844
+
845
+
846
+ export const table = (attributes = {}) => {
847
+ let table = document.createElement("table");
848
+ if (attributes) {
849
+ Object.entries(attributes).forEach(([name, attrValue]) => {
850
+ if (name.startsWith('on') && typeof attrValue === 'function') {
851
+ const eventType = name.slice(2).toLowerCase();
852
+ table.addEventListener(eventType, attrValue);
853
+ } else {
854
+ switch (name) {
855
+ case "class":
856
+ table.className = String(attrValue);
857
+ break;
858
+ case "value":
859
+ if (isArray(attrValue)) {
860
+ attrValue.forEach((element) => {
861
+ isHtmlElement(element) ? table.append(element) : table.innerHTML = String(element)
862
+ })
863
+ } else {
864
+ isHtmlElement(attrValue) ? table.append(attrValue) : table.innerHTML = String(attrValue)
865
+ }
866
+ break;
867
+ case "style":
868
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
869
+ switch (styleName) {
870
+ case "zIndex":
871
+ table.style[styleName] = `99999${styleValue}`;
872
+ break;
873
+ default:
874
+ table.style[styleName] = styleValue;
875
+ break;
876
+ }
877
+ });
878
+ break;
879
+ default:
880
+ if (name.startsWith("data")) {
881
+ const kebabName = name
882
+ .replace(/^data/, "data")
883
+ .replace(/([A-Z])/g, "-$1")
884
+ .toLowerCase();
885
+ table.setAttribute(kebabName, String(attrValue));
886
+ } else {
887
+ table.setAttribute(name, String(attrValue));
888
+ }
889
+ }
890
+ }
891
+ });
892
+ }
893
+ return table;
894
+ }
895
+
896
+
897
+ export const thead = (attributes = {}) => {
898
+ let thead = document.createElement("thead");
899
+ if (attributes) {
900
+ Object.entries(attributes).forEach(([name, attrValue]) => {
901
+ if (name.startsWith('on') && typeof attrValue === 'function') {
902
+ const eventType = name.slice(2).toLowerCase();
903
+ thead.addEventListener(eventType, attrValue);
904
+ } else {
905
+ switch (name) {
906
+ case "class":
907
+ thead.className = String(attrValue);
908
+ break;
909
+ case "value":
910
+ if (isArray(attrValue)) {
911
+ attrValue.forEach((element) => {
912
+ isHtmlElement(element) ? thead.append(element) : thead.innerHTML = String(element)
913
+ })
914
+ } else {
915
+ isHtmlElement(attrValue) ? thead.append(attrValue) : thead.innerHTML = String(attrValue)
916
+ }
917
+ break;
918
+ case "style":
919
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
920
+ switch (styleName) {
921
+ case "zIndex":
922
+ thead.style[styleName] = `99999${styleValue}`;
923
+ break;
924
+ default:
925
+ thead.style[styleName] = styleValue;
926
+ break;
927
+ }
928
+ });
929
+ break;
930
+ default:
931
+ if (name.startsWith("data")) {
932
+ const kebabName = name
933
+ .replace(/^data/, "data")
934
+ .replace(/([A-Z])/g, "-$1")
935
+ .toLowerCase();
936
+ thead.setAttribute(kebabName, String(attrValue));
937
+ } else {
938
+ thead.setAttribute(name, String(attrValue));
939
+ }
940
+ }
941
+ }
942
+ });
943
+ }
944
+ return thead;
945
+ }
946
+
947
+
948
+ export const tbody = (attributes = {}) => {
949
+ let tbody = document.createElement("tbody");
950
+ if (attributes) {
951
+ Object.entries(attributes).forEach(([name, attrValue]) => {
952
+ if (name.startsWith('on') && typeof attrValue === 'function') {
953
+ const eventType = name.slice(2).toLowerCase();
954
+ tbody.addEventListener(eventType, attrValue);
955
+ } else {
956
+ switch (name) {
957
+ case "class":
958
+ tbody.className = String(attrValue);
959
+ break;
960
+ case "value":
961
+ if (isArray(attrValue)) {
962
+ attrValue.forEach((element) => {
963
+ isHtmlElement(element) ? tbody.append(element) : tbody.innerHTML = String(element)
964
+ })
965
+ } else {
966
+ isHtmlElement(attrValue) ? tbody.append(attrValue) : tbody.innerHTML = String(attrValue)
967
+ }
968
+ break;
969
+ case "style":
970
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
971
+ switch (styleName) {
972
+ case "zIndex":
973
+ tbody.style[styleName] = `99999${styleValue}`;
974
+ break;
975
+ default:
976
+ tbody.style[styleName] = styleValue;
977
+ break;
978
+ }
979
+ });
980
+ break;
981
+ default:
982
+ if (name.startsWith("data")) {
983
+ const kebabName = name
984
+ .replace(/^data/, "data")
985
+ .replace(/([A-Z])/g, "-$1")
986
+ .toLowerCase();
987
+ tbody.setAttribute(kebabName, String(attrValue));
988
+ } else {
989
+ tbody.setAttribute(name, String(attrValue));
990
+ }
991
+ }
992
+ }
993
+ });
994
+ }
995
+ return tbody;
996
+ }
997
+
998
+
999
+ export const tr = (attributes = {}) => {
1000
+ let tr = document.createElement("tr");
1001
+ if (attributes) {
1002
+ Object.entries(attributes).forEach(([name, attrValue]) => {
1003
+ if (name.startsWith('on') && typeof attrValue === 'function') {
1004
+ const eventType = name.slice(2).toLowerCase();
1005
+ tr.addEventListener(eventType, attrValue);
1006
+ } else {
1007
+ switch (name) {
1008
+ case "class":
1009
+ tr.className = String(attrValue);
1010
+ break;
1011
+ case "value":
1012
+ if (isArray(attrValue)) {
1013
+ attrValue.forEach((element) => {
1014
+ isHtmlElement(element) ? tr.append(element) : tr.innerHTML = String(element)
1015
+ })
1016
+ } else {
1017
+ isHtmlElement(attrValue) ? tr.append(attrValue) : tr.innerHTML = String(attrValue)
1018
+ }
1019
+ break;
1020
+ case "style":
1021
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
1022
+ switch (styleName) {
1023
+ case "zIndex":
1024
+ tr.style[styleName] = `99999${styleValue}`;
1025
+ break;
1026
+ default:
1027
+ tr.style[styleName] = styleValue;
1028
+ break;
1029
+ }
1030
+ });
1031
+ break;
1032
+ default:
1033
+ if (name.startsWith("data")) {
1034
+ const kebabName = name
1035
+ .replace(/^data/, "data")
1036
+ .replace(/([A-Z])/g, "-$1")
1037
+ .toLowerCase();
1038
+ tr.setAttribute(kebabName, String(attrValue));
1039
+ } else {
1040
+ tr.setAttribute(name, String(attrValue));
1041
+ }
1042
+ }
1043
+ }
1044
+ });
1045
+ }
1046
+ return tr;
1047
+ }
1048
+
1049
+
1050
+ export const th = (attributes = {}) => {
1051
+ let th = document.createElement("th");
1052
+ if (attributes) {
1053
+ Object.entries(attributes).forEach(([name, attrValue]) => {
1054
+ if (name.startsWith('on') && typeof attrValue === 'function') {
1055
+ const eventType = name.slice(2).toLowerCase();
1056
+ th.addEventListener(eventType, attrValue);
1057
+ } else {
1058
+ switch (name) {
1059
+ case "class":
1060
+ th.className = String(attrValue);
1061
+ break;
1062
+ case "value":
1063
+ if (isArray(attrValue)) {
1064
+ attrValue.forEach((element) => {
1065
+ isHtmlElement(element) ? th.append(element) : th.innerHTML = String(element)
1066
+ })
1067
+ } else {
1068
+ isHtmlElement(attrValue) ? th.append(attrValue) : th.innerHTML = String(attrValue)
1069
+ }
1070
+ break;
1071
+ case "style":
1072
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
1073
+ switch (styleName) {
1074
+ case "zIndex":
1075
+ th.style[styleName] = `99999${styleValue}`;
1076
+ break;
1077
+ default:
1078
+ th.style[styleName] = styleValue;
1079
+ break;
1080
+ }
1081
+ });
1082
+ break;
1083
+ default:
1084
+ if (name.startsWith("data")) {
1085
+ const kebabName = name
1086
+ .replace(/^data/, "data")
1087
+ .replace(/([A-Z])/g, "-$1")
1088
+ .toLowerCase();
1089
+ th.setAttribute(kebabName, String(attrValue));
1090
+ } else {
1091
+ th.setAttribute(name, String(attrValue));
1092
+ }
1093
+ }
1094
+ }
1095
+ });
1096
+ }
1097
+ return th;
1098
+ }
1099
+
1100
+
1101
+ export const td = (attributes = {}) => {
1102
+ let td = document.createElement("td");
1103
+ if (attributes) {
1104
+ Object.entries(attributes).forEach(([name, attrValue]) => {
1105
+ if (name.startsWith('on') && typeof attrValue === 'function') {
1106
+ const eventType = name.slice(2).toLowerCase();
1107
+ td.addEventListener(eventType, attrValue);
1108
+ } else {
1109
+ switch (name) {
1110
+ case "class":
1111
+ td.className = String(attrValue);
1112
+ break;
1113
+ case "value":
1114
+ if (isArray(attrValue)) {
1115
+ attrValue.forEach((element) => {
1116
+ isHtmlElement(element) ? td.append(element) : td.innerHTML = String(element)
1117
+ })
1118
+ } else {
1119
+ isHtmlElement(attrValue) ? td.append(attrValue) : td.innerHTML = String(attrValue)
1120
+ }
1121
+ break;
1122
+ case "style":
1123
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
1124
+ switch (styleName) {
1125
+ case "zIndex":
1126
+ td.style[styleName] = `99999${styleValue}`;
1127
+ break;
1128
+ default:
1129
+ td.style[styleName] = styleValue;
1130
+ break;
1131
+ }
1132
+ });
1133
+ break;
1134
+ default:
1135
+ if (name.startsWith("data")) {
1136
+ const kebabName = name
1137
+ .replace(/^data/, "data")
1138
+ .replace(/([A-Z])/g, "-$1")
1139
+ .toLowerCase();
1140
+ td.setAttribute(kebabName, String(attrValue));
1141
+ } else {
1142
+ td.setAttribute(name, String(attrValue));
1143
+ }
1144
+ }
1145
+ }
1146
+ });
1147
+ }
1148
+ return td;
1149
+ }
1150
+
1151
+ export const img = (attributes = {}) => {
1152
+ let img = document.createElement("img");
1153
+ img.alt = "img...";
1154
+ if (attributes) {
1155
+ Object.entries(attributes).forEach(([name, attrValue]) => {
1156
+ if (name.startsWith('on') && typeof attrValue === 'function') {
1157
+ const eventType = name.slice(2).toLowerCase();
1158
+ img.addEventListener(eventType, attrValue);
1159
+ } else {
1160
+ switch (name) {
1161
+ case "class":
1162
+ img.className = String(attrValue);
1163
+ break;
1164
+ case "src":
1165
+ img.src = String(attrValue);
1166
+ break;
1167
+ case "alt":
1168
+ img.alt = String(attrValue);
1169
+ break;
1170
+ case "style":
1171
+ Object.entries(attrValue).forEach(([styleName, styleValue]) => {
1172
+ switch (styleName) {
1173
+ case "zIndex":
1174
+ img.style[styleName] = `99999${styleValue}`;
1175
+ break;
1176
+ default:
1177
+ img.style[styleName] = styleValue;
1178
+ break;
1179
+ }
1180
+ });
1181
+ break;
1182
+ default:
1183
+ if (name.startsWith("data")) {
1184
+ const kebabName = name
1185
+ .replace(/^data/, "data")
1186
+ .replace(/([A-Z])/g, "-$1")
1187
+ .toLowerCase();
1188
+ img.setAttribute(kebabName, String(attrValue));
1189
+ } else {
1190
+ img.setAttribute(name, String(attrValue));
1191
+ }
1192
+ }
1193
+ }
1194
+ });
1195
+ }
1196
+ return img;
1197
+ }
1198
+
1199
+ export const apState = (initialValue) => {
1200
+ let state = initialValue;
1201
+ const getState = () => state;
1202
+ const setState = (newValue) => {
1203
+ if (typeof newValue === "function") {
1204
+ state = newValue(state);
1205
+ } else {
1206
+ state = newValue;
1207
+ }
1208
+ }
1209
+ return [getState, setState];
1210
+ }
1211
+
1212
+ export const apFetch = async (url, params = {}) => {
1213
+ const [data, setData] = apState(null);
1214
+ const [error, setError] = apState(null);
1215
+ const [isLoading, setIsLoading] = apState(false);
1216
+
1217
+ const fetchData = async () => {
1218
+ setIsLoading(true);
1219
+ setError(null);
1220
+ try {
1221
+ const response = await fetch(url, params);
1222
+ if (!response.ok) {
1223
+ throw new Error(`HTTP error! Status: ${response.status}`);
1224
+ }
1225
+ const result = await response.json();
1226
+ setData(result);
1227
+ } catch (err) {
1228
+ console.error(err.message)
1229
+ setError(err.message);
1230
+ } finally {
1231
+ setIsLoading(false);
1232
+ }
1233
+ };
1234
+ await fetchData();
1235
+
1236
+ return [data, error, isLoading];
1237
+ }
1238
+
1239
+ export const scrollToTop = () => {
1240
+ window.scrollTo({
1241
+ top: 0,
1242
+ behavior: "smooth",
1243
+ });
1244
+ }
1245
+
1246
+ export const scrollToBottom = () => {
1247
+ window.scrollTo({
1248
+ top: document.documentElement.scrollHeight,
1249
+ behavior: "smooth",
1250
+ });
1251
+ }
1252
+
1253
+ export const _get = (name, defaulting = null) => {
1254
+ let url = new URL(window.location.href);
1255
+ let searchParams = new URLSearchParams(url.search);
1256
+ if (searchParams.has(name)) {
1257
+ return isFloat(searchParams.get(name)) || isNumeric(searchParams.get(name))
1258
+ ? parseFloat(searchParams.get(name))
1259
+ : searchParams.get(name);
1260
+ }
1261
+ return defaulting;
1262
+ }
1263
+
1264
+ export const GUID = () => {
1265
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
1266
+ const random = Math.random() * 16 | 0;
1267
+ const value = char === 'x' ? random : (random & 0x3 | 0x8);
1268
+ return value.toString(16);
1269
+ });
1270
+ }
1271
+
1272
+ export const cookie = {
1273
+ get(name) {
1274
+ const cookies = document.cookie.split('; ');
1275
+ for (let cookie of cookies) {
1276
+ let [key, value] = cookie.split('=');
1277
+ if (key === name) {
1278
+ return decodeURIComponent(value);
1279
+ }
1280
+ }
1281
+ return null;
1282
+ },
1283
+
1284
+ set(name, value, days = 7) {
1285
+ let expires = "";
1286
+ if (days) {
1287
+ let date = new Date();
1288
+ date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
1289
+ expires = "; expires=" + date.toUTCString();
1290
+ }
1291
+ document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
1292
+ },
1293
+
1294
+ delete(name) {
1295
+ document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
1296
+ }
1297
+ };
1298
+
1299
+ export const checkArrays = (arr1, arr2, checkAll = false) => {
1300
+ const set1 = new Set(arr1);
1301
+ if (checkAll) {
1302
+ return arr2.every(item => set1.has(item));
1303
+ } else {
1304
+ return arr2.some(item => set1.has(item));
1305
+ }
1306
+ }