Last active
October 17, 2018 19:36
-
-
Save VenkataRaju/06996f2ba307c6d92e9a286a31946d75 to your computer and use it in GitHub Desktop.
JavaScript Utility Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findCssRule(selectorText) | |
{ | |
for (let styleSheet of document.styleSheets) | |
for (let cssRule of styleSheet.cssRules) | |
if (cssRule.selectorText === selectorText) | |
return cssRule; | |
throw Error(`Rule with selectorText '${selectorText}' is not found`); | |
} | |
/** | |
Chainable function `on` | |
Usage: | |
let els = document.querySelectorAll('div'); | |
let listener = e => console.log(e.target.textContent, e.type); | |
let rels = on('click', listener, null, els) | |
.on('mouseover', listener) | |
.on('mouseout', listener) | |
.els; | |
console.log(els === rels); // true | |
*/ | |
function on(type, listener, options, els) | |
{ | |
let ob = | |
{ | |
on(type, listener, options) | |
{ | |
for (let el of els) | |
el.addEventListener(type, listener, options); | |
return this; | |
}, | |
els | |
}; | |
return ob.on(type, listener, options); | |
} | |
function split(str, sepChar, escChar) | |
{ | |
if (sepChar.length !== 1) | |
throw new Error(`sepChar[${sepChar}] length should be exactly 1`); | |
if (escChar.length !== 1) | |
throw new Error(`escChar[${escChar}] length should be exactly 1`); | |
if (sepChar === escChar) | |
throw new Error(`sepChar[${sepChar}] and escChar should not be same`); | |
let part = '', parts = []; | |
for (let i = 0, len = str.length, nc; i < len; i++) | |
{ | |
let c = str[i]; | |
if (c === sepChar) | |
{ | |
parts.push(part); | |
part = ''; | |
continue; | |
} | |
if (c === escChar) | |
{ | |
if ((i + 1) < len && ((nc = str[i + 1]) === sepChar || nc === escChar)) | |
{ | |
part += nc; | |
i++; | |
continue; | |
} | |
// Strict implementation should throw exception here as escChar is not escaped properly | |
} | |
part += c; | |
} | |
parts.push(part); | |
return parts; | |
} | |
/** | |
Usage: | |
for (let childElPathsAry of childElPaths([document.body])) | |
{ | |
console.log(childElPathsAry); | |
for (let childElPath of childElPathsAry) | |
console.log(childElPath.map(el => el.nodeName).join(' > ')); | |
} | |
*/ | |
function childElPaths(parentEls) // returns [[[child1, child2, ...], ], ] | |
{ | |
let childElPaths = []; | |
for (let parentEl of parentEls) | |
{ | |
let childElPathsResult = childElPaths0(parentEl, [parentEl]); | |
childElPaths.push(childElPathsResult); | |
} | |
return childElPaths; | |
function childElPaths0(parentEl, parentElPathAry) // returns [[child1, child2, ...], ] | |
{ | |
let children = parentEl.children; | |
if (!children.length) | |
return [parentElPathAry]; | |
let childElPaths = []; | |
let lastIndex = parentElPathAry.length; | |
let childElPathAry = [...parentElPathAry, null]; | |
for (let childEl of children) | |
{ | |
childElPathAry[lastIndex] = childEl; | |
let subChildElPaths = childElPaths0(childEl, childElPathAry); | |
childElPaths.push(...subChildElPaths); | |
} | |
return childElPaths; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment