@webalternatif/js-core 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/dom.js CHANGED
@@ -68,16 +68,16 @@ export default {
68
68
  return this.first(this.children(el, selector));
69
69
  },
70
70
  /**
71
- * @param {Element|Document} refEl
72
- * @param {string|Element|NodeList|Array<Element>} [selector]
73
- * @returns {Element|null}
71
+ * @param {Element|Document|string} refEl
72
+ * @param {string|Element|NodeList|Array<Element>} selector
73
+ * @returns {Element}
74
74
  */
75
75
  findOne: function findOne(refEl, selector) {
76
76
  var _this$find$;
77
77
  return (_this$find$ = this.find(refEl, selector)[0]) !== null && _this$find$ !== void 0 ? _this$find$ : null;
78
78
  },
79
79
  /**
80
- * @param {Element|Document} refEl
80
+ * @param {Element|Document|string} refEl
81
81
  * @param {string|Element|NodeList|Array<Element>} selector
82
82
  * @returns {Array<Element>}
83
83
  */
@@ -97,12 +97,16 @@ export default {
97
97
  return null;
98
98
  });
99
99
  }
100
- return Array.from(refEl.querySelectorAll(selector));
100
+ try {
101
+ return refEl.querySelectorAll(selector);
102
+ } catch (e) {
103
+ return [];
104
+ }
101
105
  },
102
106
  /**
103
- * @param {Element} el
107
+ * @param {Element|string} el
104
108
  * @param {string} data
105
- * @param {string} value
109
+ * @param {string} [value]
106
110
  * @returns {Element|null}
107
111
  */
108
112
  findOneByData: function findOneByData(el, data, value) {
@@ -110,9 +114,9 @@ export default {
110
114
  return (_this$findByData$ = this.findByData(el, data, value)[0]) !== null && _this$findByData$ !== void 0 ? _this$findByData$ : null;
111
115
  },
112
116
  /**
113
- * @param {Element} el
117
+ * @param {Element|string} el
114
118
  * @param {string} data
115
- * @param {string} value
119
+ * @param {string} [value]
116
120
  * @returns {Element[]}
117
121
  */
118
122
  findByData: function findByData(el, data, value) {
@@ -218,7 +222,7 @@ export default {
218
222
  for (var _len2 = arguments.length, children = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
219
223
  children[_key2 - 1] = arguments[_key2];
220
224
  }
221
- foreach(children, function (child) {
225
+ foreach([].concat(children).reverse(), function (child) {
222
226
  if (isString(child)) {
223
227
  child = _this2.create(child);
224
228
  }
@@ -249,7 +253,7 @@ export default {
249
253
  },
250
254
  /**
251
255
  * @param {Element} el
252
- * @param {string|Element} selector
256
+ * @param {string|Element} [selector]
253
257
  * @returns {Element|null}
254
258
  */
255
259
  closest: function closest(el, selector) {
@@ -266,6 +270,9 @@ export default {
266
270
  }
267
271
  return null;
268
272
  }
273
+ if (undefined === selector) {
274
+ return el;
275
+ }
269
276
  return el.closest(selector);
270
277
  },
271
278
  /**
@@ -565,7 +572,7 @@ export default {
565
572
  },
566
573
  /**
567
574
  * @param {Element|Document|Window} el
568
- * @param {string} events
575
+ * @param {string} [events]
569
576
  * @param {string|Element|function} selector
570
577
  * @param {function|AddEventListenerOptions|boolean} [handler]
571
578
  * @param {AddEventListenerOptions|boolean} [options]
@@ -579,9 +586,10 @@ export default {
579
586
  }
580
587
  var store = LISTENERS.get(el);
581
588
  if (!store) return el;
582
- foreach(events.split(' '), function (event) {
589
+ var evts = events ? events.split(' ') : [undefined];
590
+ foreach(evts.split(' '), function (event) {
583
591
  each(_toConsumableArray(store).reverse(), function (i, l) {
584
- if (l.event === event && l.handler === handler && l.selector === selector && (options === undefined || l.options === options)) {
592
+ if ((undefined === event || l.event === event) && (undefined === handler || l.handler === handler) && (undefined === selector || l.selector === selector) && (undefined === options || l.options === options)) {
585
593
  el.removeEventListener(event, l.listener, l.options);
586
594
  var index = store.indexOf(l);
587
595
  index !== -1 && store.splice(index, 1);
package/dist/cjs/is.js CHANGED
@@ -24,7 +24,9 @@ export var isUndefined = function isUndefined(v) {
24
24
  return typeof v === 'undefined';
25
25
  };
26
26
  export var isArrayLike = function isArrayLike(o) {
27
- return null !== o && !isString(o) && !isFunction(o) && isInt(o.length) && o.length >= 0 && Number.isFinite(o.length);
27
+ return !!o && !isString(o) && !isFunction(o) && isInt(o.length)
28
+ // && o.length >= 0
29
+ && Number.isFinite(o.length);
28
30
  };
29
31
  export var isArray = function isArray(a) {
30
32
  return Array.isArray(a);
@@ -68,8 +68,8 @@ export var each = function each(o, callback, context) {
68
68
  _iterator2.f();
69
69
  }
70
70
  } else if (isArrayLike(o)) {
71
- o = Array.from(o);
72
- for (var _i2 = 0; _i2 < o.length; _i2++) if (false === callback.call(context || o[_i2], _i2, o[_i2], o, _i2)) return;
71
+ var _arr = Array.from(o);
72
+ for (var _i2 = 0; _i2 < _arr.length; _i2++) if (false === callback.call(context || _arr[_i2], _i2, _arr[_i2], _arr, _i2)) return;
73
73
  }
74
74
  return o;
75
75
  };
package/dist/esm/dom.js CHANGED
@@ -68,16 +68,16 @@ export default {
68
68
  return this.first(this.children(el, selector));
69
69
  },
70
70
  /**
71
- * @param {Element|Document} refEl
72
- * @param {string|Element|NodeList|Array<Element>} [selector]
73
- * @returns {Element|null}
71
+ * @param {Element|Document|string} refEl
72
+ * @param {string|Element|NodeList|Array<Element>} selector
73
+ * @returns {Element}
74
74
  */
75
75
  findOne: function findOne(refEl, selector) {
76
76
  var _this$find$;
77
77
  return (_this$find$ = this.find(refEl, selector)[0]) !== null && _this$find$ !== void 0 ? _this$find$ : null;
78
78
  },
79
79
  /**
80
- * @param {Element|Document} refEl
80
+ * @param {Element|Document|string} refEl
81
81
  * @param {string|Element|NodeList|Array<Element>} selector
82
82
  * @returns {Array<Element>}
83
83
  */
@@ -97,12 +97,16 @@ export default {
97
97
  return null;
98
98
  });
99
99
  }
100
- return Array.from(refEl.querySelectorAll(selector));
100
+ try {
101
+ return refEl.querySelectorAll(selector);
102
+ } catch (e) {
103
+ return [];
104
+ }
101
105
  },
102
106
  /**
103
- * @param {Element} el
107
+ * @param {Element|string} el
104
108
  * @param {string} data
105
- * @param {string} value
109
+ * @param {string} [value]
106
110
  * @returns {Element|null}
107
111
  */
108
112
  findOneByData: function findOneByData(el, data, value) {
@@ -110,9 +114,9 @@ export default {
110
114
  return (_this$findByData$ = this.findByData(el, data, value)[0]) !== null && _this$findByData$ !== void 0 ? _this$findByData$ : null;
111
115
  },
112
116
  /**
113
- * @param {Element} el
117
+ * @param {Element|string} el
114
118
  * @param {string} data
115
- * @param {string} value
119
+ * @param {string} [value]
116
120
  * @returns {Element[]}
117
121
  */
118
122
  findByData: function findByData(el, data, value) {
@@ -218,7 +222,7 @@ export default {
218
222
  for (var _len2 = arguments.length, children = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
219
223
  children[_key2 - 1] = arguments[_key2];
220
224
  }
221
- foreach(children, function (child) {
225
+ foreach([].concat(children).reverse(), function (child) {
222
226
  if (isString(child)) {
223
227
  child = _this2.create(child);
224
228
  }
@@ -249,7 +253,7 @@ export default {
249
253
  },
250
254
  /**
251
255
  * @param {Element} el
252
- * @param {string|Element} selector
256
+ * @param {string|Element} [selector]
253
257
  * @returns {Element|null}
254
258
  */
255
259
  closest: function closest(el, selector) {
@@ -266,6 +270,9 @@ export default {
266
270
  }
267
271
  return null;
268
272
  }
273
+ if (undefined === selector) {
274
+ return el;
275
+ }
269
276
  return el.closest(selector);
270
277
  },
271
278
  /**
@@ -565,7 +572,7 @@ export default {
565
572
  },
566
573
  /**
567
574
  * @param {Element|Document|Window} el
568
- * @param {string} events
575
+ * @param {string} [events]
569
576
  * @param {string|Element|function} selector
570
577
  * @param {function|AddEventListenerOptions|boolean} [handler]
571
578
  * @param {AddEventListenerOptions|boolean} [options]
@@ -579,9 +586,10 @@ export default {
579
586
  }
580
587
  var store = LISTENERS.get(el);
581
588
  if (!store) return el;
582
- foreach(events.split(' '), function (event) {
589
+ var evts = events ? events.split(' ') : [undefined];
590
+ foreach(evts.split(' '), function (event) {
583
591
  each(_toConsumableArray(store).reverse(), function (i, l) {
584
- if (l.event === event && l.handler === handler && l.selector === selector && (options === undefined || l.options === options)) {
592
+ if ((undefined === event || l.event === event) && (undefined === handler || l.handler === handler) && (undefined === selector || l.selector === selector) && (undefined === options || l.options === options)) {
585
593
  el.removeEventListener(event, l.listener, l.options);
586
594
  var index = store.indexOf(l);
587
595
  index !== -1 && store.splice(index, 1);
package/dist/esm/is.js CHANGED
@@ -24,7 +24,9 @@ export var isUndefined = function isUndefined(v) {
24
24
  return typeof v === 'undefined';
25
25
  };
26
26
  export var isArrayLike = function isArrayLike(o) {
27
- return null !== o && !isString(o) && !isFunction(o) && isInt(o.length) && o.length >= 0 && Number.isFinite(o.length);
27
+ return !!o && !isString(o) && !isFunction(o) && isInt(o.length)
28
+ // && o.length >= 0
29
+ && Number.isFinite(o.length);
28
30
  };
29
31
  export var isArray = function isArray(a) {
30
32
  return Array.isArray(a);
@@ -68,8 +68,8 @@ export var each = function each(o, callback, context) {
68
68
  _iterator2.f();
69
69
  }
70
70
  } else if (isArrayLike(o)) {
71
- o = Array.from(o);
72
- for (var _i2 = 0; _i2 < o.length; _i2++) if (false === callback.call(context || o[_i2], _i2, o[_i2], o, _i2)) return;
71
+ var _arr = Array.from(o);
72
+ for (var _i2 = 0; _i2 < _arr.length; _i2++) if (false === callback.call(context || _arr[_i2], _i2, _arr[_i2], _arr, _i2)) return;
73
73
  }
74
74
  return o;
75
75
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webalternatif/js-core",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "type": "module",
5
5
  "types": "types/index.d.ts",
6
6
  "main": "dist/cjs/index.js",
@@ -28,6 +28,7 @@
28
28
  "@babel/plugin-transform-export-namespace-from": "^7.27.1",
29
29
  "@babel/preset-env": "^7.26.0",
30
30
  "@testing-library/dom": "^10.4.1",
31
+ "@testing-library/jest-dom": "^6.9.1",
31
32
  "@types/jest": "^29.5.14",
32
33
  "babel-jest": "^30.2.0",
33
34
  "babel-loader": "^9.2.1",
package/types/dom.d.ts CHANGED
@@ -16,31 +16,31 @@ declare namespace _default {
16
16
  */
17
17
  function child(el: Element, selector?: string): Element | null;
18
18
  /**
19
- * @param {Element|Document} refEl
20
- * @param {string|Element|NodeList|Array<Element>} [selector]
21
- * @returns {Element|null}
19
+ * @param {Element|Document|string} refEl
20
+ * @param {string|Element|NodeList|Array<Element>} selector
21
+ * @returns {Element}
22
22
  */
23
- function findOne(refEl: Element | Document, selector?: string | Element | NodeList | Array<Element>): Element | null;
23
+ function findOne(refEl: Element | Document | string, selector: string | Element | NodeList | Array<Element>): Element;
24
24
  /**
25
- * @param {Element|Document} refEl
25
+ * @param {Element|Document|string} refEl
26
26
  * @param {string|Element|NodeList|Array<Element>} selector
27
27
  * @returns {Array<Element>}
28
28
  */
29
- function find(refEl: Element | Document, selector: string | Element | NodeList | Array<Element>): Array<Element>;
29
+ function find(refEl: Element | Document | string, selector: string | Element | NodeList | Array<Element>): Array<Element>;
30
30
  /**
31
- * @param {Element} el
31
+ * @param {Element|string} el
32
32
  * @param {string} data
33
- * @param {string} value
33
+ * @param {string} [value]
34
34
  * @returns {Element|null}
35
35
  */
36
- function findOneByData(el: Element, data: string, value: string): Element | null;
36
+ function findOneByData(el: Element | string, data: string, value?: string): Element | null;
37
37
  /**
38
- * @param {Element} el
38
+ * @param {Element|string} el
39
39
  * @param {string} data
40
- * @param {string} value
40
+ * @param {string} [value]
41
41
  * @returns {Element[]}
42
42
  */
43
- function findByData(el: Element, data: string, value: string): Element[];
43
+ function findByData(el: Element | string, data: string, value?: string): Element[];
44
44
  /**
45
45
  * @param {Element|NodeList|Array<Element>} el
46
46
  * @param {string} className
@@ -85,10 +85,10 @@ declare namespace _default {
85
85
  function remove(...els: Element | NodeList | Array<Element> | string): void;
86
86
  /**
87
87
  * @param {Element} el
88
- * @param {string|Element} selector
88
+ * @param {string|Element} [selector]
89
89
  * @returns {Element|null}
90
90
  */
91
- function closest(el: Element, selector: string | Element): Element | null;
91
+ function closest(el: Element, selector?: string | Element): Element | null;
92
92
  /**
93
93
  * @param {Element} el
94
94
  * @param {string} [selector]
@@ -198,13 +198,13 @@ declare namespace _default {
198
198
  function on(el: Element | Document | Window, events: string, selector: string | Element | Function, handler?: Function | AddEventListenerOptions | boolean, options?: AddEventListenerOptions | boolean): Element;
199
199
  /**
200
200
  * @param {Element|Document|Window} el
201
- * @param {string} events
201
+ * @param {string} [events]
202
202
  * @param {string|Element|function} selector
203
203
  * @param {function|AddEventListenerOptions|boolean} [handler]
204
204
  * @param {AddEventListenerOptions|boolean} [options]
205
205
  * @returns {Element}
206
206
  */
207
- function off(el: Element | Document | Window, events: string, selector: string | Element | Function, handler?: Function | AddEventListenerOptions | boolean, options?: AddEventListenerOptions | boolean): Element;
207
+ function off(el: Element | Document | Window, events?: string, selector: string | Element | Function, handler?: Function | AddEventListenerOptions | boolean, options?: AddEventListenerOptions | boolean): Element;
208
208
  /**
209
209
  * @param {HTMLElement} el
210
210
  * @param {Object<string, string>|string} style
package/types/index.d.ts CHANGED
@@ -31,10 +31,10 @@ declare const webf: {
31
31
  dom: {
32
32
  children(el: Element, selector?: string): NodeList;
33
33
  child(el: Element, selector?: string): Element | null;
34
- findOne(refEl: Element | Document, selector?: string | Element | NodeList | Array<Element>): Element | null;
35
- find(refEl: Element | Document, selector: string | Element | NodeList | Array<Element>): Array<Element>;
36
- findOneByData(el: Element, data: string, value: string): Element | null;
37
- findByData(el: Element, data: string, value: string): Element[];
34
+ findOne(refEl: Element | Document | string, selector: string | Element | NodeList | Array<Element>): Element;
35
+ find(refEl: Element | Document | string, selector: string | Element | NodeList | Array<Element>): Array<Element>;
36
+ findOneByData(el: Element | string, data: string, value?: string): Element | null;
37
+ findByData(el: Element | string, data: string, value?: string): Element[];
38
38
  addClass(el: Element | NodeList | Array<Element>, className: string): Element | NodeList | Array<Element>;
39
39
  removeClass(el: Element | NodeList | Array<Element>, className: string): Element | NodeList | Array<Element>;
40
40
  toggleClass(el: Element, classNames: string, force?: boolean): Element;
@@ -42,7 +42,7 @@ declare const webf: {
42
42
  append(node: Node, ...children: (Node | string)[]): Node;
43
43
  prepend(node: Node, ...children: (Node | string)[]): Node;
44
44
  remove(...els: Element | NodeList | Array<Element> | string): void;
45
- closest(el: Element, selector: string | Element): Element | null;
45
+ closest(el: Element, selector?: string | Element): Element | null;
46
46
  next(el: Element, selector?: string): Element | null;
47
47
  prev(el: Element, selector?: string | null): Element | null;
48
48
  nextAll(el: Element, selector?: string): Element[];
@@ -62,7 +62,7 @@ declare const webf: {
62
62
  } | string, value?: string): Element | DOMStringMap;
63
63
  removeData(el: Element, name: string): Element | any;
64
64
  on(el: Element | Document | Window, events: string, selector: string | Element | Function, handler?: Function | AddEventListenerOptions | boolean, options?: AddEventListenerOptions | boolean): Element;
65
- off(el: Element | Document | Window, events: string, selector: string | Element | Function, handler?: Function | AddEventListenerOptions | boolean, options?: AddEventListenerOptions | boolean): Element;
65
+ off(el: Element | Document | Window, events?: string, selector: string | Element | Function, handler?: Function | AddEventListenerOptions | boolean, options?: AddEventListenerOptions | boolean): Element;
66
66
  css(el: HTMLElement, style: {
67
67
  [x: string]: string;
68
68
  } | string, value?: string): Element;