whatwg/dom

Delegate to Iterator helper methods on NodeList

keithamus opened this issue · 2 comments

Now that the “iterator helpers” proposal is Stage 3 and browsers are intending to ship it, perhaps it’s a good time to investigate how we can make these ergonomic for DOM collection APIs like NodeList.

Currently NodeList can be coerced to an iterator, which means it will be possible to do document.querySelectorAll('.foo').entries().take(5) however this isn’t exactly in the top 10 list of elegant APIs.

I propose we expend some energy to see if it’s web compat to add iterator helper method aliases to NodeList proper, so that developers can use the much more pleasant document.querySelectorAll('.foo').take(5).

The following method names are part of iterator helpers:

  • .map
  • .filter
  • .take
  • .drop
  • .flatMap
  • .reduce
  • .toArray
  • .forEach (already on NodeList)
  • .some
  • .every
  • .find

I think we've found this is not web-compatible when it's been tried in the past. Too much code does things like conditional polyfilling, or assuming that if map does not exist then it must be a NodeList that needs to be wrapped in an Array or similar.

I doubt implementations will be excited to try this, to save people from typing .entries().

annevk commented

There's probably not enough benefit in typing less and fixing this to add parentNode.query[All](), but it does seem nice.