A proposal to add to JavaScript a means to concatenate the contents of an iterator into a string.
Authors: Kevin Gibbons
Champions: Kevin Gibbons
This proposal is currently at stage 4 of the TC39 process: it is complete and included in the standard. Any further changes will need to happen in a separate proposal through the usual process. This repo is archived.
Joining a list of strings into a single string for display or other reasons is a very common operation. If you have an array, it's trivial: just call .join(sep). If you have any other iterable, you can either do Iterator.from(it).reduce((a, b) => a + sep + b) (and pay the cost of allocating all the intermediate strings, plus this breaks on empty iterators), or Array.from(it).join(sep) (and pay the cost of converting the whole thing to an Array).
We should make that easier.
This came up during the original iterator helpers proposal and later on the Discourse.
While in principle there are various ways to solve this problem, the obvious one is to add Iterator.prototype.join which works exactly like Array.prototype.join except that it operates on its receiver as an iterator rather than as an Array. That is what is currently specified.