ecma402 icon indicating copy to clipboard operation
ecma402 copied to clipboard

`%IntlSegmentsPrototype%.values()`

Open rauschma opened this issue 11 months ago • 3 comments

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.

rauschma avatar Dec 03 '24 18:12 rauschma

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.

gibson042 avatar Dec 03 '24 19:12 gibson042

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.

bakkot avatar Dec 03 '24 19:12 bakkot

  • 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.

rauschma avatar Dec 03 '24 19:12 rauschma