23 lines
695 B
JavaScript
23 lines
695 B
JavaScript
/**
|
|
* Converts any DOM node/s to a loopable array
|
|
* @param { HTMLElement|NodeList } els - single html element or a node list
|
|
* @returns { Array } always a loopable object
|
|
*/
|
|
export default function domToArray(els) {
|
|
// can this object be already looped?
|
|
if (!Array.isArray(els)) {
|
|
// is it a node list?
|
|
if (
|
|
/^\[object (HTMLCollection|NodeList|Object)\]$/
|
|
.test(Object.prototype.toString.call(els))
|
|
&& typeof els.length === 'number'
|
|
)
|
|
return Array.from(els)
|
|
else
|
|
// if it's a single node
|
|
// it will be returned as "array" with one single entry
|
|
return [els]
|
|
}
|
|
// this object could be looped out of the box
|
|
return els
|
|
} |