Commit 75b3f41b authored by Avris's avatar Avris

Add _sel and _selAll

parent f781d1d8
......@@ -34,7 +34,7 @@ Or simply copy-paste whatever filters you need from the [source code](https://gi
### `_each(iterable, function (el, i))`
Iterate in a consistent way, whether it's over an array or an object,
Iterates in a consistent way, whether it's over an array or an object,
without having to check for `hasOwnProperty` in every loop.
_each(object, function (val, key) {
......@@ -43,10 +43,24 @@ without having to check for `hasOwnProperty` in every loop.
### `_el(html)`
Convert an HTML string into a DOM element with one simple helper.
Converts an HTML string into a DOM element with one simple helper.
const el = _el('<div class="lorem"><a href="/test">OK</a></div>');
### `_sel(selector, [startNode])`
Alias for `querySelector`:
const el = _sel('.card img.avatar')
### `_selAll(selector, [startNode])`
Alias for `querySelectorAll`:
_each(_selAll('.card img.avatar'), function (el) {
el.classList.add('active')
})
### `_findParent(el, selector)`
Finds the first parent of `el` that matches `selector`.
......
"use strict";window._each=function(n,e){for(var t in n)n.hasOwnProperty(t)&&e(n[t],t)},window._el=function(n){var e=document.createElement("div");return e.innerHTML=n.trim(),e.firstChild},window._findParent=function(n,e){do{if(n.matches(e))return n;n=n.parentNode}while(n&&n.matches)},window._on=function(n,e,t){document.addEventListener(e,function(e){var r=_findParent(e.target,n);r&&(!1===t.apply(r,[e])&&(e.preventDefault(),e.stopPropagation()))})},window._request=function(n,e,t,r){return new Promise(function(o,a){var i=new XMLHttpRequest;i.open(n,e,!0),_each(r||{},function(n,e){return i.setRequestHeader(e,n)}),i.onload=function(){i.status>=400?a(i):o(i.responseText,i)},i.onerror=function(){return a(i)};var u=void 0;t instanceof HTMLElement?u=new FormData(t):t instanceof FormData?u=t:(u=new FormData,_each(t,function(n,e){return u.append(e,n)})),i.send(u)})},window._extend=function(n){for(var e=arguments.length,t=Array(e>1?e-1:0),r=1;r<e;r++)t[r-1]=arguments[r];n=n||{};for(var o=1;o<t.length;o++){var a=t[o];a&&_each(a,function(e,t){n[t]=e})}return n};
\ No newline at end of file
"use strict";function _toConsumableArray(n){if(Array.isArray(n)){for(var e=0,r=Array(n.length);e<n.length;e++)r[e]=n[e];return r}return Array.from(n)}window._each=function(n,e){for(var r in n)n.hasOwnProperty(r)&&e(n[r],r)},window._el=function(n){var e=document.createElement("div");return e.innerHTML=n.trim(),e.firstChild},window._sel=function(n){return(arguments.length>1&&void 0!==arguments[1]?arguments[1]:document).querySelector(n)},window._selAll=function(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:document;return[].concat(_toConsumableArray(e.querySelectorAll(n)))},window._findParent=function(n,e){do{if(n.matches(e))return n;n=n.parentNode}while(n&&n.matches)},window._on=function(n,e,r){document.addEventListener(e,function(e){var t=_findParent(e.target,n);t&&(!1===r.apply(t,[e])&&(e.preventDefault(),e.stopPropagation()))})},window._request=function(n,e,r,t){return new Promise(function(o,a){var i=new XMLHttpRequest;i.open(n,e,!0),_each(t||{},function(n,e){return i.setRequestHeader(e,n)}),i.onload=function(){i.status>=400?a(i):o(i.responseText,i)},i.onerror=function(){return a(i)};var u=void 0;r instanceof HTMLElement?u=new FormData(r):r instanceof FormData?u=r:(u=new FormData,_each(r,function(n,e){return u.append(e,n)})),i.send(u)})},window._extend=function(n){for(var e=arguments.length,r=Array(e>1?e-1:0),t=1;t<e;t++)r[t-1]=arguments[t];n=n||{};for(var o=1;o<r.length;o++){var a=r[o];a&&_each(a,function(e,r){n[r]=e})}return n};
\ No newline at end of file
{
"name": "avris-vanillin",
"version": "0.0.1",
"version": "0.0.2",
"description": "Almost like vanilla JavaScript",
"keywords": [
"vanilla",
......
......@@ -13,6 +13,10 @@ window._el = (html) => {
return div.firstChild;
};
window._sel = (selector, startNode = document) => startNode.querySelector(selector);
window._selAll = (selector, startNode = document) => [...startNode.querySelectorAll(selector)];
window._findParent = (el, selector) => {
do {
if (el.matches(selector)) {
......
......@@ -92,6 +92,25 @@ test('el', (assert, ok, fail) => {
// -----------------
test('sel', (assert) => {
assert(_sel('.workspace') === document.querySelector('.workspace'));
});
test('sel-withStart', (assert) => {
const $ul = _sel('ul', _sel('.workspace'));
assert($ul.parentNode.classList.contains('bar-parent'));
});
test('selAll', (assert) => {
assert(_selAll('div').length === 10);
});
test('selAll-withStart', (assert) => {
assert(_selAll('div', _sel('.workspace')).length === 8);
});
// -----------------
test('findParent', (assert) => {
const $expectedParent = document.querySelectorAll('.bar-parent')[2];
const $el = document.querySelector('.bar');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment