Skip to content

Commit

Permalink
Add iterateGraphemes method
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Jun 25, 2018
1 parent b4500fe commit 1827bc1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ var splitter = new GraphemeSplitter();
// split the string to an array of grapheme clusters (one string each)
var graphemes = splitter.splitGraphemes(string);

// iterate the string to an iterable iterator of grapheme clusters (one string each)
var graphemes = splitter.iterateGraphemes(string);

// or do this if you just need their number
var graphemeCount = splitter.countGraphemes(string);
```
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ declare class GraphemeSplitter {
countGraphemes(s: string): number
/** split the string to an array of grapheme clusters */
splitGraphemes(s: string): string[]
/** iterate the string to an iterable iterator of grapheme clusters */
iterateGraphemes(s: string): IterableIterator<string>
}

export = GraphemeSplitter
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,34 @@ function GraphemeSplitter(){
}
return res;
};


// Returns the iterator of grapheme clusters there are in the given string
this.iterateGraphemes = function(str) {
var index = 0;
var res = {
next: (function() {
var value;
var brk;
if ((brk = this.nextBreak(str, index)) < str.length) {
value = str.slice(index, brk);
index = brk;
return { value: value, done: false };
}
if (index < str.length) {
value = str.slice(index);
index = str.length;
return { value: value, done: false };
}
return { value: undefined, done: true };
}).bind(this)
};
// ES2015 @@iterator method (iterable) for spread syntax and for...of statement
if (typeof Symbol !== 'undefined' && Symbol.iterator) {
res[Symbol.iterator] = function() {return res};
}
return res;
};

// Returns the number of grapheme clusters there are in the given string
this.countGraphemes = function(str){
var count = 0;
Expand Down
14 changes: 14 additions & 0 deletions tests/grapheme_splitter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ test('splitGraphemes returns properly split list from string', t => {
t.end();
});

test('iterateGraphemes returns properly split iterator from string', t => {
const splitter = new GraphemeSplitter();

t.plan(testData.length);

testData.forEach( ({ input, expected }) => {
const result = splitter.iterateGraphemes(input);

t.deepLooseEqual([...result], expected);
});

t.end();
});

test('countGraphemes returns the correct number of graphemes in string', t => {
const splitter = new GraphemeSplitter();

Expand Down

0 comments on commit 1827bc1

Please sign in to comment.