diff --git a/.changeset/early-cats-exercise.md b/.changeset/early-cats-exercise.md new file mode 100644 index 0000000..47de1b7 --- /dev/null +++ b/.changeset/early-cats-exercise.md @@ -0,0 +1,5 @@ +--- +"unicode-segmenter": patch +--- + +grapheme: rename `countGrapheme()` to `countGraphemes()`. existing name is deprecated alias. diff --git a/README.md b/README.md index a3f6ea8..7760b17 100644 --- a/README.md +++ b/README.md @@ -74,21 +74,21 @@ import { splitGraphemes } from 'unicode-segmenter/grapheme'; #### Example: Count graphemes ```js -import { countGrapheme } from 'unicode-segmenter/grapheme'; +import { countGraphemes } from 'unicode-segmenter/grapheme'; '👋 안녕!'.length; // => 6 -countGrapheme('👋 안녕!'); +countGraphemes('👋 안녕!'); // => 5 'a̐éö̲'.length; // => 7 -countGrapheme('a̐éö̲'); +countGraphemes('a̐éö̲'); // => 3 ``` > [!NOTE] -> `countGrapheme()` is a small wrapper around `graphemeSegments()`. +> `countGraphemes()` is a small wrapper around `graphemeSegments()`. > > If you need it more than once at a time, consider memoization or use `graphemeSegments()` or `splitSegments()` once instead. @@ -251,7 +251,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb | Name | Unicode® | ESM? | Size | Size (min) | Size (min+gzip) | Size (min+br) | |------------------------------|----------|------|----------:|-----------:|----------------:|--------------:| -| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 17,347 | 12,822 | 5,307 | 4,093 | +| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 17,348 | 12,822 | 5,307 | 4,089 | | `graphemer` | 15.0.0 | ✖️ ️| 410,435 | 95,104 | 15,752 | 10,660 | | `grapheme-splitter` | 10.0.0 | ✖️ | 122,252 | 23,680 | 7,852 | 4,841 | | `@formatjs/intl-segmenter`* | 15.0.0 | ✖️ | 491,043 | 318,721 | 54,248 | 34,380 | @@ -267,7 +267,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb | Name | Bytecode size | Bytecode size (gzip)* | |------------------------------|--------------:|----------------------:| -| `unicode-segmenter/grapheme` | 24,521 | 12,773 | +| `unicode-segmenter/grapheme` | 24,538 | 12,788 | | `graphemer` | 133,949 | 31,710 | | `grapheme-splitter` | 63,810 | 19,125 | | `@formatjs/intl-segmenter`* | 315,865 | 99,063 | diff --git a/benchmark/grapheme/bundle-entries/unicode-segmenter.js b/benchmark/grapheme/bundle-entries/unicode-segmenter.js index 6340e15..ecb7310 100644 --- a/benchmark/grapheme/bundle-entries/unicode-segmenter.js +++ b/benchmark/grapheme/bundle-entries/unicode-segmenter.js @@ -1,5 +1,5 @@ export { graphemeSegments, - countGrapheme, splitGraphemes, + countGraphemes, } from '../../../src/grapheme.js'; diff --git a/src/grapheme.js b/src/grapheme.js index 9050348..d3478ff 100644 --- a/src/grapheme.js +++ b/src/grapheme.js @@ -189,12 +189,19 @@ export function* graphemeSegments(input) { * @param {string} str * @return number count of graphemes */ -export function countGrapheme(str) { +export function countGraphemes(str) { let count = 0; for (let _ of graphemeSegments(str)) count += 1; return count; } +export { + /** + * @deprecated use {@link countGraphemes} + */ + countGraphemes as countGrapheme, +}; + /** * @param {string} str * @return {IterableIterator} diff --git a/test/grapheme.js b/test/grapheme.js index 860185b..92366b8 100644 --- a/test/grapheme.js +++ b/test/grapheme.js @@ -7,8 +7,8 @@ import fc from 'fast-check'; import { GraphemeCategory, graphemeSegments, - countGrapheme, splitGraphemes, + countGraphemes, } from 'unicode-segmenter/grapheme'; import { assertObjectContaining } from './_helper.js'; @@ -74,34 +74,34 @@ test('graphemeSegments', async t => { }); }); -test('countGrapheme', async t => { +test('countGraphemes', async t => { await t.test('latin', () => { - assert.equal(countGrapheme('abcd'), 4); + assert.equal(countGraphemes('abcd'), 4); }); await t.test('flags', () => { - assert.equal(countGrapheme('🇷🇸🇮🇴'), 2); + assert.equal(countGraphemes('🇷🇸🇮🇴'), 2); }); await t.test('emoji', () => { - assert.equal(countGrapheme('👻👩‍👩‍👦‍👦'), 2); - assert.equal(countGrapheme('🌷🎁💩😜👍🏳️‍🌈'), 6); + assert.equal(countGraphemes('👻👩‍👩‍👦‍👦'), 2); + assert.equal(countGraphemes('🌷🎁💩😜👍🏳️‍🌈'), 6); }); await t.test('diacritics as combining marks', () => { - assert.equal(countGrapheme('Ĺo͂řȩm̅'), 5); + assert.equal(countGraphemes('Ĺo͂řȩm̅'), 5); }); await t.test('Jamo', () => { - assert.equal(countGrapheme('뎌쉐'), 2); + assert.equal(countGraphemes('뎌쉐'), 2); }); await t.test('Hindi', () => { - assert.equal(countGrapheme('अनुच्छेद'), 4); + assert.equal(countGraphemes('अनुच्छेद'), 4); }); await t.test('demonic', () => { - assert.equal(countGrapheme('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞'), 6); + assert.equal(countGraphemes('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞'), 6); }); });