@@ -64,8 +64,13 @@ export function searchGraphemeCategory(cp) {
64
64
}
65
65
66
66
/**
67
+ * Unicode segmentation by extended grapheme rules.
68
+ *
69
+ * This is fully compatible with the {@link Intl.Segmenter.segment} API
70
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/segment
71
+ *
67
72
* @param {string } input
68
- * @return {GraphemeSegmenter }
73
+ * @return {GraphemeSegmenter } iterator for grapheme cluster segments
69
74
*/
70
75
export function * graphemeSegments ( input ) {
71
76
// do nothing on empty string
@@ -186,12 +191,21 @@ export function* graphemeSegments(input) {
186
191
}
187
192
188
193
/**
189
- * @param {string } str
190
- * @return number count of graphemes
194
+ * Count number of extended grapheme clusters in given text.
195
+ *
196
+ * NOTE:
197
+ *
198
+ * This function is a small wrapper around {@link graphemeSegments}.
199
+ *
200
+ * If you call it more than once at a time, consider memoization
201
+ * or use {@link graphemeSegments} or {@link splitGraphemes} once instead
202
+ *
203
+ * @param {string } text
204
+ * @return {number } count of grapheme clusters
191
205
*/
192
- export function countGraphemes ( str ) {
206
+ export function countGraphemes ( text ) {
193
207
let count = 0 ;
194
- for ( let _ of graphemeSegments ( str ) ) count += 1 ;
208
+ for ( let _ of graphemeSegments ( text ) ) count += 1 ;
195
209
return count ;
196
210
}
197
211
@@ -203,11 +217,18 @@ export {
203
217
} ;
204
218
205
219
/**
206
- * @param {string } str
207
- * @return {IterableIterator<string> }
220
+ * Split given text into extended grapheme clusters.
221
+ *
222
+ * @param {string } text
223
+ * @return {IterableIterator<string> } iterator for grapheme clusters
224
+ *
225
+ * @see {@link graphemeSegments } if you need extra information.
226
+ *
227
+ * @example
228
+ * [...splitGraphemes('abc')] // => ['a', 'b', 'c']
208
229
*/
209
- export function * splitGraphemes ( str ) {
210
- for ( let s of graphemeSegments ( str ) ) yield s . segment ;
230
+ export function * splitGraphemes ( text ) {
231
+ for ( let s of graphemeSegments ( text ) ) yield s . segment ;
211
232
}
212
233
213
234
/**
@@ -358,4 +379,3 @@ function isBoundary(catBefore, catAfter, risCount, emoji, incb) {
358
379
// GB999
359
380
return true ;
360
381
}
361
-
0 commit comments