@webalternatif/js-core 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.
Files changed (118) hide show
  1. package/.babelrc +12 -0
  2. package/.idea/inspectionProfiles/Project_Default.xml +12 -0
  3. package/.idea/js-core.iml +8 -0
  4. package/.idea/modules.xml +8 -0
  5. package/.idea/php.xml +20 -0
  6. package/.idea/vcs.xml +6 -0
  7. package/LICENSE +21 -0
  8. package/README.md +7 -0
  9. package/dist/cjs/array.js +116 -0
  10. package/dist/cjs/dom.js +466 -0
  11. package/dist/cjs/eventDispatcher.js +96 -0
  12. package/dist/cjs/i18n.js +50 -0
  13. package/dist/cjs/index.js +55 -0
  14. package/dist/cjs/is.js +85 -0
  15. package/dist/cjs/math.js +92 -0
  16. package/dist/cjs/random.js +38 -0
  17. package/dist/cjs/string.js +474 -0
  18. package/dist/cjs/stringPrototype.js +16 -0
  19. package/dist/cjs/traversal.js +110 -0
  20. package/dist/cjs/utils.js +118 -0
  21. package/dist/esm/array.js +116 -0
  22. package/dist/esm/dom.js +466 -0
  23. package/dist/esm/eventDispatcher.js +96 -0
  24. package/dist/esm/i18n.js +50 -0
  25. package/dist/esm/index.js +55 -0
  26. package/dist/esm/is.js +85 -0
  27. package/dist/esm/math.js +92 -0
  28. package/dist/esm/random.js +38 -0
  29. package/dist/esm/string.js +474 -0
  30. package/dist/esm/stringPrototype.js +16 -0
  31. package/dist/esm/traversal.js +110 -0
  32. package/dist/esm/utils.js +118 -0
  33. package/i18n/agenda/en.js +73 -0
  34. package/i18n/agenda/fr.js +73 -0
  35. package/i18n/agenda/index.js +2 -0
  36. package/i18n/ajaxform/en.js +5 -0
  37. package/i18n/ajaxform/fr.js +5 -0
  38. package/i18n/ajaxform/index.js +2 -0
  39. package/i18n/ajaxupload/en.js +12 -0
  40. package/i18n/ajaxupload/fr.js +12 -0
  41. package/i18n/ajaxupload/index.js +2 -0
  42. package/i18n/autocomplete/en.js +3 -0
  43. package/i18n/autocomplete/fr.js +3 -0
  44. package/i18n/autocomplete/index.js +2 -0
  45. package/i18n/confirm/en.js +5 -0
  46. package/i18n/confirm/fr.js +5 -0
  47. package/i18n/confirm/index.js +2 -0
  48. package/i18n/core/en.js +4 -0
  49. package/i18n/core/fr.js +4 -0
  50. package/i18n/core/index.js +2 -0
  51. package/i18n/datagrid/en.js +8 -0
  52. package/i18n/datagrid/fr.js +8 -0
  53. package/i18n/datagrid/index.js +2 -0
  54. package/i18n/date/en.js +51 -0
  55. package/i18n/date/fr.js +51 -0
  56. package/i18n/date/index.js +2 -0
  57. package/i18n/datetimepicker/en.js +30 -0
  58. package/i18n/datetimepicker/fr.js +30 -0
  59. package/i18n/datetimepicker/index.js +2 -0
  60. package/i18n/dialog/en.js +3 -0
  61. package/i18n/dialog/fr.js +3 -0
  62. package/i18n/dialog/index.js +2 -0
  63. package/i18n/fulldayeventagenda/en.js +73 -0
  64. package/i18n/fulldayeventagenda/fr.js +73 -0
  65. package/i18n/fulldayeventagenda/index.js +2 -0
  66. package/i18n/index.d.ts +4 -0
  67. package/i18n/index.js +15 -0
  68. package/i18n/richtexteditor/en.js +58 -0
  69. package/i18n/richtexteditor/fr.js +58 -0
  70. package/i18n/richtexteditor/index.js +2 -0
  71. package/i18n/select/en.js +3 -0
  72. package/i18n/select/fr.js +3 -0
  73. package/i18n/select/index.js +2 -0
  74. package/i18n/timepicker/en.js +3 -0
  75. package/i18n/timepicker/fr.js +3 -0
  76. package/i18n/timepicker/index.js +2 -0
  77. package/i18n/useragenda/en.js +74 -0
  78. package/i18n/useragenda/fr.js +73 -0
  79. package/i18n/useragenda/index.js +2 -0
  80. package/jest.config.js +14 -0
  81. package/package.json +33 -0
  82. package/src/array.js +124 -0
  83. package/src/dom.js +569 -0
  84. package/src/eventDispatcher.js +118 -0
  85. package/src/i18n.js +55 -0
  86. package/src/index.js +33 -0
  87. package/src/is.js +89 -0
  88. package/src/math.js +109 -0
  89. package/src/random.js +40 -0
  90. package/src/string.js +576 -0
  91. package/src/stringPrototype.js +15 -0
  92. package/src/traversal.js +134 -0
  93. package/src/utils.js +130 -0
  94. package/tests/array.test.js +326 -0
  95. package/tests/dom.test.js +239 -0
  96. package/tests/eventdispatcher.test.js +177 -0
  97. package/tests/i18n.test.js +132 -0
  98. package/tests/index.test.js +29 -0
  99. package/tests/is.test.js +354 -0
  100. package/tests/math.test.js +221 -0
  101. package/tests/random.test.js +72 -0
  102. package/tests/string.test.js +1106 -0
  103. package/tests/traversal.test.js +517 -0
  104. package/tests/utils.test.js +371 -0
  105. package/tsconfig.json +16 -0
  106. package/types/array.d.ts +8 -0
  107. package/types/dom.d.ts +241 -0
  108. package/types/eventDispatcher.d.ts +12 -0
  109. package/types/i18n.d.ts +4 -0
  110. package/types/index.d.ts +139 -0
  111. package/types/is.d.ts +16 -0
  112. package/types/math.d.ts +7 -0
  113. package/types/random.d.ts +7 -0
  114. package/types/string.d.ts +37 -0
  115. package/types/stringPrototype.d.ts +1 -0
  116. package/types/traversal.d.ts +7 -0
  117. package/types/utils.d.ts +7 -0
  118. package/webpack.config.cjs +31 -0
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.throttle = exports.strParseFloat = exports.sizeOf = exports.noop = exports.flatten = exports.equals = exports.debounce = void 0;
7
+ var _traversal = require("./traversal.js");
8
+ var _is = require("./is.js");
9
+ const equals = function (o1, o2, seen = new WeakMap()) {
10
+ if (o1 === o2) return true;
11
+ if (typeof o1 !== typeof o2 || o1 == null || o2 == null) {
12
+ return false;
13
+ }
14
+ if ((0, _is.isObject)(o1)) {
15
+ if (seen.has(o1)) {
16
+ return seen.get(o1) === o2;
17
+ }
18
+ seen.set(o1, o2);
19
+ const keys1 = Object.keys(o1),
20
+ keys2 = Object.keys(o2);
21
+ if (keys1.length !== keys2.length) {
22
+ return false;
23
+ }
24
+ return keys1.every(key => equals(o1[key], o2[key], seen));
25
+ }
26
+ return false;
27
+ };
28
+ exports.equals = equals;
29
+ const noop = function () {};
30
+ exports.noop = noop;
31
+ const sizeOf = function (o) {
32
+ return (0, _traversal.map)(o, noop).length;
33
+ };
34
+
35
+ /**
36
+ * Recursively flattens an object or array into a single-level array.
37
+ *
38
+ * @param {Object|Array} o - The object or array to flatten.
39
+ * @returns {Array} A flattened array containing all the values from the input object or array.
40
+ *
41
+ * @example <caption>Flatten an array of arrays</caption>
42
+ * const result = flatten([1, [2, [3, 4]], 5]);
43
+ * console.log(result);
44
+ * // Output: [1, 2, 3, 4, 5]
45
+ *
46
+ * @example <caption>Flatten an object</caption>
47
+ * const result = flatten({ a: 1, b: [2, { c: 3 }], d: 4 });
48
+ * console.log(result);
49
+ * // Output: [1, 2, 3, 4]
50
+ */
51
+ exports.sizeOf = sizeOf;
52
+ const flatten = function (o) {
53
+ if ((0, _is.isObject)(o) || (0, _is.isArray)(o)) {
54
+ return [].concat.apply([], (0, _traversal.map)(o, (i, val) => flatten(val)));
55
+ }
56
+ return o;
57
+ };
58
+ exports.flatten = flatten;
59
+ const strParseFloat = function (val) {
60
+ if (!val) return 0;
61
+ return parseFloat((val + '').replace(/\s/g, '').replace(',', '.'));
62
+ };
63
+ exports.strParseFloat = strParseFloat;
64
+ const throttle = function (func, wait, leading = true, trailing = true, context = null) {
65
+ let timeout = null;
66
+ let lastCall = 0;
67
+ return function (...args) {
68
+ const now = Date.now();
69
+ if (!lastCall && !leading) {
70
+ lastCall = now;
71
+ }
72
+ const remaining = wait - (now - lastCall);
73
+ if (remaining <= 0 || remaining > wait) {
74
+ lastCall = now;
75
+ func.apply(context || this, args);
76
+ } else if (!timeout && trailing) {
77
+ timeout = setTimeout(() => {
78
+ timeout = null;
79
+ lastCall = leading ? Date.now() : 0;
80
+ func.apply(context || this, args);
81
+ }, remaining);
82
+ }
83
+ };
84
+ };
85
+
86
+ /**
87
+ * Creates a debounced function that delays the invocation of `func` until after `wait` milliseconds
88
+ * have elapsed since the last time the debounced function was invoked.
89
+ *
90
+ * @param {Function} func - The function to debounce.
91
+ * @param {number} wait - The number of milliseconds to delay.
92
+ * @param {boolean} [immediate=false] - If true, execute `func` on the leading edge instead of the trailing.
93
+ * @param {Object} [context=null] - The context to bind to `func`.
94
+ * @returns {Function} The debounced function.
95
+ */
96
+ exports.throttle = throttle;
97
+ const debounce = function (func, wait, immediate = false, context = null) {
98
+ let timeout = null;
99
+ let lastCall = 0;
100
+ return function (...args) {
101
+ const now = Date.now();
102
+ if (immediate) {
103
+ if (!lastCall) {
104
+ lastCall = now;
105
+ func.apply(context || this, args);
106
+ }
107
+ }
108
+ clearTimeout(timeout);
109
+ timeout = null;
110
+ timeout = setTimeout(() => {
111
+ lastCall = now;
112
+ clearTimeout(timeout);
113
+ timeout = null;
114
+ func.apply(context || this, args);
115
+ }, wait);
116
+ };
117
+ };
118
+ exports.debounce = debounce;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.range = exports.indexOf = exports.inArray = exports.compareArray = exports.array_unique = exports.array_diff = exports.arrayUnique = exports.arrayDiff = void 0;
7
+ var _traversal = require("./traversal.js");
8
+ var _is = require("./is.js");
9
+ var _math = require("./math.js");
10
+ var _utils = require("./utils.js");
11
+ /**
12
+ * Checks if a value exists in an array or an object
13
+ *
14
+ * @param {*} value the searched value
15
+ * @param {Object|Array} arr the array
16
+ * @param {number} [index=0] if provided, search from this index
17
+ * @param {boolean} [strict=false] if true, search is done with strict equality
18
+ * @returns {boolean}
19
+ *
20
+ * @example
21
+ * inArray(2, [1, 2, 3])
22
+ * // → true
23
+ *
24
+ * @example
25
+ * inArray({a: 1}, {a: 1, b: 2})
26
+ * // → true
27
+ *
28
+ * @example
29
+ * inArray(5, [1, 2, 3])
30
+ * // → false
31
+ */
32
+ const inArray = function (value, arr, index = 0, strict = false) {
33
+ let ret = false;
34
+ (0, _traversal.each)(arr, (i, val) => {
35
+ if (i >= index) {
36
+ if (strict) {
37
+ if (val === value) {
38
+ ret = true;
39
+ return false;
40
+ }
41
+ } else {
42
+ if ((0, _is.isObject)(value) && (0, _is.isObject)(val)) {
43
+ ret = (0, _utils.equals)(val, value);
44
+ return false;
45
+ } else if ((0, _is.isArray)(value) && (0, _is.isObject)(val)) {
46
+ ret = compareArray(val, value);
47
+ return false;
48
+ } else if (val == value) {
49
+ ret = true;
50
+ return false;
51
+ }
52
+ }
53
+ }
54
+ });
55
+ return ret;
56
+ };
57
+ exports.inArray = inArray;
58
+ const indexOf = function (arr, elt, from = 0) {
59
+ from = from < 0 ? Math.ceil(from) + arr.length : Math.floor(from);
60
+ for (; from < arr.length; from++) {
61
+ if (from in arr && arr[from] === elt) {
62
+ return from;
63
+ }
64
+ }
65
+ return -1;
66
+ };
67
+ exports.indexOf = indexOf;
68
+ const compareArray = function (a1, a2) {
69
+ if (a1.length !== a2.length) {
70
+ return false;
71
+ } else {
72
+ for (let i = 0; i < a1.length; i++) {
73
+ if ((0, _is.isArray)(a1[i])) {
74
+ if (!(0, _is.isArray)(a2[i])) {
75
+ return false;
76
+ }
77
+ return compareArray(a1[i], a2[i]);
78
+ } else if (a1[i] !== a2[i]) {
79
+ return false;
80
+ }
81
+ }
82
+ }
83
+ return true;
84
+ };
85
+ exports.compareArray = compareArray;
86
+ const arrayUnique = function (arr) {
87
+ return arr.filter((el, index, arr) => index === indexOf(arr, el));
88
+ };
89
+ exports.arrayUnique = arrayUnique;
90
+ const array_unique = exports.array_unique = arrayUnique;
91
+ const arrayDiff = (array1, array2, strict = false) => {
92
+ return array1.filter(item => !inArray(item, array2, 0, strict));
93
+ };
94
+ exports.arrayDiff = arrayDiff;
95
+ const array_diff = exports.array_diff = arrayUnique;
96
+ const range = function (size, startAt = 0, step = 1) {
97
+ size = (0, _math.round)(size);
98
+ step = (0, _math.round)(step);
99
+ const rng = [];
100
+ if ((0, _is.isUndefined)(startAt) || size < 1 || step === 0 || size < Math.abs(step)) {
101
+ return rng;
102
+ }
103
+ const end = size * step;
104
+ if ((0, _is.isString)(startAt)) {
105
+ startAt = startAt.charCodeAt(0);
106
+ for (let i = 0; step > 0 ? i < end : i > end; i += step) {
107
+ rng.push(String.fromCharCode(startAt + i));
108
+ }
109
+ } else if ((0, _is.isInteger)(startAt)) {
110
+ for (let i = 0; step > 0 ? i < end : i > end; i += step) {
111
+ rng.push(startAt + i);
112
+ }
113
+ }
114
+ return rng;
115
+ };
116
+ exports.range = range;
@@ -0,0 +1,466 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isWindow = exports.isDomElement = exports.getStyle = exports.default = void 0;
7
+ var _is = require("./is.js");
8
+ var _string = require("./string.js");
9
+ var _traversal = require("./traversal.js");
10
+ var _array = require("./array.js");
11
+ const isWindow = function (o) {
12
+ return !!o && o === o.window;
13
+ };
14
+ exports.isWindow = isWindow;
15
+ const isDomElement = function (o) {
16
+ return (0, _is.isObject)(o) && o instanceof HTMLElement;
17
+ };
18
+ exports.isDomElement = isDomElement;
19
+ const getStyle = function (elem, cssRule) {
20
+ if (!isDomElement(elem)) {
21
+ return '';
22
+ }
23
+ if (window.getComputedStyle) {
24
+ const computedStyle = window.getComputedStyle(elem, null);
25
+ return computedStyle.getPropertyValue(cssRule) || computedStyle[(0, _string.camelCase)(cssRule)] || '';
26
+ }
27
+ return elem.style[(0, _string.camelCase)(cssRule)] || '';
28
+ };
29
+ exports.getStyle = getStyle;
30
+ var _default = exports.default = {
31
+ /**
32
+ * @param {Element} el
33
+ * @param {string} [selector]
34
+ * @returns {NodeList}
35
+ */
36
+ children(el, selector) {
37
+ return selector ? this.find(el, `:scope > ${selector}`) : el.children;
38
+ },
39
+ /**
40
+ * @param {Element} el
41
+ * @param {string} [selector]
42
+ * @returns {Element|null}
43
+ */
44
+ child(el, selector) {
45
+ return this.first(this.children(el, selector));
46
+ },
47
+ /**
48
+ * @param {Element|Document|string} refEl
49
+ * @param {string} [selector='*']
50
+ * @returns {NodeList}
51
+ */
52
+ find(refEl, selector = '*') {
53
+ if ((0, _is.isString)(refEl)) {
54
+ selector = refEl;
55
+ refEl = document;
56
+ }
57
+ try {
58
+ return refEl.querySelectorAll(selector);
59
+ } catch (e) {
60
+ return document.querySelectorAll(':not(*)');
61
+ }
62
+ },
63
+ /**
64
+ * @param {Element|Document|string} refEl
65
+ * @param {string} [selector='*']
66
+ * @returns {Element|null}
67
+ */
68
+ findOne(refEl, selector = '*') {
69
+ if ((0, _is.isString)(refEl)) {
70
+ selector = refEl;
71
+ refEl = document;
72
+ }
73
+ try {
74
+ return refEl.querySelector(selector);
75
+ } catch (e) {
76
+ return null;
77
+ }
78
+ },
79
+ /**
80
+ * @param {Element} el
81
+ * @param {string} className
82
+ * @returns {Element}
83
+ */
84
+ addClass(el, className) {
85
+ if (!className) return el;
86
+ const classNames = className.split(' ').map(c => c.trim()).filter(Boolean);
87
+ el.classList.add(...classNames);
88
+ return el;
89
+ },
90
+ /**
91
+ * @param {Element} el
92
+ * @param {string} classNames
93
+ * @returns {Element}
94
+ */
95
+ removeClass(el, classNames) {
96
+ if (!classNames) return el;
97
+ el.classList.remove(...classNames.split(' ').map(c => c.trim()).filter(Boolean));
98
+ return el;
99
+ },
100
+ /**
101
+ * @param {Element} el
102
+ * @param {string} classNames
103
+ * @param {boolean} [force]
104
+ * @returns {Element}
105
+ */
106
+ toggleClass(el, classNames, force) {
107
+ (0, _traversal.each)(classNames.split(' ').map(c => c.trim()).filter(Boolean), (i, c) => el.classList.toggle(c, force));
108
+ return el;
109
+ },
110
+ /**
111
+ * @param {Element} el
112
+ * @param {string} classNames
113
+ * @returns {boolean}
114
+ */
115
+ hasClass(el, classNames) {
116
+ return (0, _array.compareArray)([...el.classList], classNames.split(' ').map(c => c.trim()).filter(Boolean));
117
+ },
118
+ /**
119
+ * @param {Element} el
120
+ * @param {Element} child
121
+ * @returns {Element}
122
+ */
123
+ append(el, child) {
124
+ el.append(child);
125
+ return el;
126
+ },
127
+ /**
128
+ * @param {Element} el
129
+ * @param {Element} child
130
+ * @returns {Element}
131
+ */
132
+ prepend(el, child) {
133
+ el.prepend(child);
134
+ return el;
135
+ },
136
+ /**
137
+ * @param {Element} el
138
+ * @returns {void}
139
+ */
140
+ remove(el) {
141
+ el.remove();
142
+ },
143
+ /**
144
+ * @param {Element} el
145
+ * @param {string} [selector]
146
+ * @returns {Element|null}
147
+ */
148
+ closest(el, selector) {
149
+ return el.closest(selector);
150
+ },
151
+ /**
152
+ * @param {Element} el
153
+ * @param {string} [selector]
154
+ * @returns {Element|null}
155
+ */
156
+ next(el, selector = null) {
157
+ let sibling = el.nextElementSibling;
158
+ if (!selector) return sibling;
159
+ if (sibling && sibling.matches(selector)) {
160
+ return sibling;
161
+ }
162
+ return sibling;
163
+ },
164
+ /**
165
+ * @param {Element} el
166
+ * @param {string|null} [selector]
167
+ * @returns {Element|null}
168
+ */
169
+ prev(el, selector = null) {
170
+ let sibling = el.previousElementSibling;
171
+ if (!selector) return sibling;
172
+ if (sibling && sibling.matches(selector)) {
173
+ return sibling;
174
+ }
175
+ return null;
176
+ },
177
+ /**
178
+ * @param {Element} el
179
+ * @param {string} [selector]
180
+ * @returns {Element[]}
181
+ */
182
+ nextAll(el, selector) {
183
+ const siblings = [];
184
+ let sibling = el.nextElementSibling;
185
+ while (sibling) {
186
+ if (undefined === selector || sibling.matches(selector)) {
187
+ siblings.push(sibling);
188
+ }
189
+ sibling = sibling.nextElementSibling;
190
+ }
191
+ return siblings;
192
+ },
193
+ /**
194
+ * @param {Element} el
195
+ * @param {string} [selector]
196
+ * @returns {Element[]}
197
+ */
198
+ prevAll(el, selector) {
199
+ const siblings = [];
200
+ let sibling = el.previousElementSibling;
201
+ while (sibling) {
202
+ if (undefined === selector || sibling.matches(selector)) {
203
+ siblings.push(sibling);
204
+ }
205
+ sibling = sibling.previousElementSibling;
206
+ }
207
+ return siblings;
208
+ },
209
+ /**
210
+ * @param {Element} el
211
+ * @param {Element} wrappingElement
212
+ * @returns {Element}
213
+ */
214
+ wrap(el, wrappingElement) {
215
+ if (!wrappingElement.isConnected) {
216
+ el.parentNode.insertBefore(wrappingElement, el);
217
+ }
218
+ this.append(wrappingElement, el);
219
+ return el;
220
+ },
221
+ /**
222
+ * @param {Element} el
223
+ * @param {string} name
224
+ * @param {*} [value]
225
+ * @returns {Element|*}
226
+ */
227
+ attr(el, name, value) {
228
+ if (undefined === value) return el.getAttribute(name);
229
+ if (null === value) {
230
+ el.removeAttribute(name);
231
+ } else {
232
+ el.setAttribute(name, value);
233
+ }
234
+ return el;
235
+ },
236
+ /**
237
+ * @param {Element} el
238
+ * @param {string} name
239
+ * @param {*} [value]
240
+ * @returns {*|Element}
241
+ */
242
+ prop(el, name, value) {
243
+ if (undefined === value) {
244
+ return el[name];
245
+ }
246
+ el[name] = value;
247
+ return el;
248
+ },
249
+ /**
250
+ * @param {Element} el
251
+ * @param {string} [html]
252
+ * @returns {Element|*}
253
+ */
254
+ html(el, html) {
255
+ if (undefined === html) return el.innerHTML;
256
+ el.innerHTML = html;
257
+ return el;
258
+ },
259
+ /**
260
+ * @param {Element} el
261
+ * @param {string} [text]
262
+ * @returns {Element|*}
263
+ */
264
+ text(el, text) {
265
+ if (undefined === text) return el.innerText;
266
+ el.innerText = text;
267
+ return el;
268
+ },
269
+ /**
270
+ * @param {Element} el
271
+ * @returns {Element}
272
+ */
273
+ hide(el) {
274
+ if (undefined === this.data(el, '__display__')) {
275
+ const display = getComputedStyle(el).display;
276
+ this.data(el, '__display__', display);
277
+ }
278
+ el.style.display = 'none';
279
+ return el;
280
+ },
281
+ /**
282
+ * @param {Element} el
283
+ * @returns {Element}
284
+ */
285
+ show(el) {
286
+ const dataDisplay = this.data(el, '__display__');
287
+ if (undefined === dataDisplay) {
288
+ el.style.removeProperty('display');
289
+ } else {
290
+ el.style.display = dataDisplay;
291
+ this.removeData(el, '__display__');
292
+ }
293
+ return el;
294
+ },
295
+ /**
296
+ * @param {Element} el
297
+ * @returns {Element}
298
+ */
299
+ toggle(el) {
300
+ return 'none' === el.style.display ? this.show(el) : this.hide(el);
301
+ },
302
+ /**
303
+ * @param {Element} el
304
+ * @param {Object<string, string>|string} name
305
+ * @param {string} [value]
306
+ * @returns {Element}
307
+ */
308
+ data(el, name, value) {
309
+ if ((0, _is.isPlainObject)(name)) {
310
+ (0, _traversal.each)(name, (k, v) => this.data(el, k, v));
311
+ return el;
312
+ }
313
+ const isAttr = /^data-/.test(name + '');
314
+ const key = (0, _string.camelCase)(isAttr ? (name + '').replace(/^data-/, '') : name + '');
315
+ if (undefined === value) return el.dataset[key];
316
+ if (null === value) this.removeData(el, key);
317
+ el.dataset[key] = value;
318
+ return el;
319
+ },
320
+ /**
321
+ * @param {Element} el
322
+ * @param {string} name
323
+ * @returns {Element|*}
324
+ */
325
+ removeData(el, name) {
326
+ const key = (name + '').replace(/^data-/, '').camelCase();
327
+ delete el.dataset[key];
328
+ return el;
329
+ },
330
+ /**
331
+ * @param {Element|Document|Window} el
332
+ * @param {string} event
333
+ * @param {function} handler
334
+ * @param {AddEventListenerOptions|false} options
335
+ * @returns {Element}
336
+ */
337
+ on(el, event, handler, options = false) {
338
+ el.addEventListener(event, handler, options);
339
+ return el;
340
+ },
341
+ /**
342
+ * @param {Element|Document|Window} el
343
+ * @param {string} event
344
+ * @param {function} handler
345
+ * @param {Object} options
346
+ * @returns {Element}
347
+ */
348
+ off(el, event, handler, options) {
349
+ el.removeEventListener(event, handler, options);
350
+ return el;
351
+ },
352
+ /**
353
+ * @param {HTMLElement} el
354
+ * @param {Object<string, string>|string} styles
355
+ * @param {string} [value]
356
+ * @returns {Element}
357
+ */
358
+ css(el, styles, value) {
359
+ if ((0, _is.isString)(styles)) {
360
+ if (undefined === value) {
361
+ return styles.startsWith('--') ? el.style.getPropertyValue(styles) : el.style[styles];
362
+ }
363
+ if (styles.startsWith('--')) {
364
+ el.style.setProperty(styles, String(value));
365
+ } else {
366
+ el.style[styles] = value;
367
+ }
368
+ return el;
369
+ }
370
+ (0, _traversal.each)(styles, (name, v) => {
371
+ if (name.startsWith('--')) {
372
+ el.style.setProperty(name, String(v));
373
+ } else {
374
+ el.style[name] = v;
375
+ }
376
+ });
377
+ return el;
378
+ },
379
+ /**
380
+ * @param {Element} el
381
+ * @param {string} selectorClosest
382
+ * @param {string} selectorFind
383
+ * @returns {NodeList|null}
384
+ */
385
+ closestFind(el, selectorClosest, selectorFind) {
386
+ const closest = this.closest(el, selectorClosest);
387
+ if (closest) {
388
+ return this.find(closest, selectorFind);
389
+ }
390
+ return null;
391
+ },
392
+ /**
393
+ * @param {Element} el
394
+ * @param {string} selectorClosest
395
+ * @param {string} selectorFindOne
396
+ * @returns {Element|null}
397
+ */
398
+ closestFindOne(el, selectorClosest, selectorFindOne) {
399
+ const closest = this.closest(el, selectorClosest);
400
+ if (closest) {
401
+ return this.findOne(closest, selectorFindOne);
402
+ }
403
+ return null;
404
+ },
405
+ /**
406
+ * @param {NodeList} nodeList
407
+ * @returns {Element|null}
408
+ */
409
+ first(nodeList) {
410
+ return nodeList?.length ? nodeList.item(0) : null;
411
+ },
412
+ /**
413
+ * @param {NodeList} nodeList
414
+ * @returns {Element|null}
415
+ */
416
+ last(nodeList) {
417
+ return nodeList.length ? nodeList.item(nodeList.length - 1) : null;
418
+ },
419
+ /**
420
+ * @param {string} html
421
+ * @returns {Element}
422
+ */
423
+ create(html) {
424
+ const tpl = document.createElement('template');
425
+ tpl.innerHTML = html.trim();
426
+ return tpl.content.firstElementChild;
427
+ },
428
+ /**
429
+ * @param {NodeList} nodeList
430
+ * @param {number} [index=0]
431
+ * @returns {Element|null}
432
+ */
433
+ eq(nodeList, index = 0) {
434
+ if (Math.abs(index) >= nodeList.length) return null;
435
+ if (index < 0) {
436
+ index = nodeList.length + index;
437
+ }
438
+ return nodeList.item(index);
439
+ },
440
+ /**
441
+ * @param {Element} el
442
+ * @param {Element} newEl
443
+ * @returns {Element}
444
+ */
445
+ after(el, newEl) {
446
+ return el.parentElement.insertBefore(newEl, el.nextElementSibling);
447
+ },
448
+ /**
449
+ * @param {Element} el
450
+ * @param {Element} newEl
451
+ * @returns {Element}
452
+ */
453
+ before(el, newEl) {
454
+ return el.parentElement.insertBefore(newEl, el);
455
+ },
456
+ /**
457
+ * @param {Element} el
458
+ * @returns {Element}
459
+ */
460
+ empty(el) {
461
+ while (el.firstChild) {
462
+ el.removeChild(el.firstChild);
463
+ }
464
+ return el;
465
+ }
466
+ };