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

Convenience: method to get current token from iterator #4

Closed
inexorabletash opened this issue Sep 29, 2016 · 3 comments
Closed

Convenience: method to get current token from iterator #4

inexorabletash opened this issue Sep 29, 2016 · 3 comments

Comments

@inexorabletash
Copy link

In every example I've written using this (like this one) I end up with this pattern:

const words = [];
const iterator = Intl.Segmenter(locale, {type: 'word'}).segment(text);
let pos = iterator.index();
for (let {index, breakType} of iterator) {
  if (breakType !== 'none')
    words.push(text.slice(pos, index));
  pos = index;
}

i.e. I need to maintain pos tracking the previous index and slice the string myself. How about:

%SegmentIterator%.prototype.segment

Return a substring of the input string from last index to the current index.

...so you can just write:

const words = [];
const iterator = Intl.Segmenter(locale, {type: 'word'}).segment(text);
for (let {index, breakType} of iterator) {
  if (breakType !== 'none')
    words.push(iterator.segment());
}
@littledan
Copy link
Member

Maybe the iterator should always be of the string slices, with the understanding that the iterator is an extra convenience thing, on top of the lower-level imperative API based on advance which gives you the index. Thoughts?

@inexorabletash
Copy link
Author

The strings aren't useful without the type so you'd need a (substring, type) tuple of some sort.

On Oct 18, 2016, at 1:13 PM, littledan [email protected] wrote:

Maybe the iterator should always be of the string slices, with the understanding that the iterator is an extra convenience thing, on top of the lower-level imperative API based on advance which gives you the index. Thoughts?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

littledan added a commit that referenced this issue Nov 16, 2016
Implements changes suggested in #5 , #6 and #4 , but omits further convenience methods.
@littledan
Copy link
Member

The next method does this.

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

2 participants