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.

27 lines
726 B

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