Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

%IntlSegmentsPrototype%.values() #945

Open
rauschma opened this issue Dec 3, 2024 · 3 comments
Open

%IntlSegmentsPrototype%.values() #945

rauschma opened this issue Dec 3, 2024 · 3 comments

Comments

@rauschma
Copy link

rauschma commented Dec 3, 2024

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.

@gibson042
Copy link
Contributor

gibson042 commented Dec 3, 2024

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.

@bakkot
Copy link
Contributor

bakkot commented Dec 3, 2024

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.

@rauschma
Copy link
Author

rauschma commented Dec 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants