You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
695 B

4 years ago
  1. /**
  2. * Converts any DOM node/s to a loopable array
  3. * @param { HTMLElement|NodeList } els - single html element or a node list
  4. * @returns { Array } always a loopable object
  5. */
  6. export default function domToArray(els) {
  7. // can this object be already looped?
  8. if (!Array.isArray(els)) {
  9. // is it a node list?
  10. if (
  11. /^\[object (HTMLCollection|NodeList|Object)\]$/
  12. .test(Object.prototype.toString.call(els))
  13. && typeof els.length === 'number'
  14. )
  15. return Array.from(els)
  16. else
  17. // if it's a single node
  18. // it will be returned as "array" with one single entry
  19. return [els]
  20. }
  21. // this object could be looped out of the box
  22. return els
  23. }