Skip to content

Commit 5e86659

Browse files
committed
grapheme: add more detail to JSDoc
1 parent 9d688d8 commit 5e86659

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

.changeset/unlucky-deers-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"unicode-segmenter": patch
3+
---
4+
5+
grapheme: add more detail to API JSDoc

src/grapheme.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ export function searchGraphemeCategory(cp) {
6464
}
6565

6666
/**
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+
*
6772
* @param {string} input
68-
* @return {GraphemeSegmenter}
73+
* @return {GraphemeSegmenter} iterator for grapheme cluster segments
6974
*/
7075
export function* graphemeSegments(input) {
7176
// do nothing on empty string
@@ -186,12 +191,21 @@ export function* graphemeSegments(input) {
186191
}
187192

188193
/**
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
191205
*/
192-
export function countGraphemes(str) {
206+
export function countGraphemes(text) {
193207
let count = 0;
194-
for (let _ of graphemeSegments(str)) count += 1;
208+
for (let _ of graphemeSegments(text)) count += 1;
195209
return count;
196210
}
197211

@@ -203,11 +217,18 @@ export {
203217
};
204218

205219
/**
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']
208229
*/
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;
211232
}
212233

213234
/**
@@ -358,4 +379,3 @@ function isBoundary(catBefore, catAfter, risCount, emoji, incb) {
358379
// GB999
359380
return true;
360381
}
361-

0 commit comments

Comments
 (0)