ecma402
ecma402 copied to clipboard
`%IntlSegmentsPrototype%.values()`
Use case
Example where Intl.Segmenter is a bit verbose:
const segmenter = new Intl.Segmenter('en-us', { granularity: 'word' });
const segments = segmenter.segment('How are you?');
console.log(
Iterator.from(segments).filter(x => x.isWordLike).map(x => x.segment).toArray()
// Alternative:
// segments[Symbol.iterator]().filter(x => x.isWordLike).map(x => x.segment).toArray()
);
Solution
Add a method (e.g.) called .values() to %IntlSegmentsPrototype% that returns this[Symbol.iterator]().
segments.values().filter(x => x.isWordLike).map(x => x.segment).toArray()
Almost all other built-in iterables have string-keyed methods that return iterators, so this addition would bring %IntlSegmentsPrototype% into the fold.
So the decrease in verbosity would be six characters, and only when using Iterator prototype methods rather than e.g. for..of?
- Iterator.from(segments).filter(x => x.isWordLike).map(x => x.segment).toArray()
+ segments.values().filter(x => x.isWordLike).map(x => x.segment).toArray()
I'm not opposed, but I'm also not particularly motivated to pursue this.
I think of this as less motivated by decrease in verbosity and more by consistency with the rest of the language/platform. Almost every other iterable already has a string-named alias for its Symbol.iterator method, and the ones which don't (e.g. String) I think should.
- I agree that it doesn’t have a high priority.
- For me it’s not purely about character counts (humans read in chunks) but also about how easy something is to decipher. And with
.values(), the data flow is easier to follow.