Skip to content

Commit

Permalink
grapheme: add more detail to JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Nov 24, 2024
1 parent 9d688d8 commit 5e86659
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-deers-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"unicode-segmenter": patch
---

grapheme: add more detail to API JSDoc
40 changes: 30 additions & 10 deletions src/grapheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ export function searchGraphemeCategory(cp) {
}

/**
* Unicode segmentation by extended grapheme rules.
*
* This is fully compatible with the {@link Intl.Segmenter.segment} API
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/segment
*
* @param {string} input
* @return {GraphemeSegmenter}
* @return {GraphemeSegmenter} iterator for grapheme cluster segments
*/
export function* graphemeSegments(input) {
// do nothing on empty string
Expand Down Expand Up @@ -186,12 +191,21 @@ export function* graphemeSegments(input) {
}

/**
* @param {string} str
* @return number count of graphemes
* Count number of extended grapheme clusters in given text.
*
* NOTE:
*
* This function is a small wrapper around {@link graphemeSegments}.
*
* If you call it more than once at a time, consider memoization
* or use {@link graphemeSegments} or {@link splitGraphemes} once instead
*
* @param {string} text
* @return {number} count of grapheme clusters
*/
export function countGraphemes(str) {
export function countGraphemes(text) {
let count = 0;
for (let _ of graphemeSegments(str)) count += 1;
for (let _ of graphemeSegments(text)) count += 1;
return count;
}

Expand All @@ -203,11 +217,18 @@ export {
};

/**
* @param {string} str
* @return {IterableIterator<string>}
* Split given text into extended grapheme clusters.
*
* @param {string} text
* @return {IterableIterator<string>} iterator for grapheme clusters
*
* @see {@link graphemeSegments} if you need extra information.
*
* @example
* [...splitGraphemes('abc')] // => ['a', 'b', 'c']
*/
export function* splitGraphemes(str) {
for (let s of graphemeSegments(str)) yield s.segment;
export function* splitGraphemes(text) {
for (let s of graphemeSegments(text)) yield s.segment;
}

/**
Expand Down Expand Up @@ -358,4 +379,3 @@ function isBoundary(catBefore, catAfter, risCount, emoji, incb) {
// GB999
return true;
}

0 comments on commit 5e86659

Please sign in to comment.