Skip to content

Commit

Permalink
grapheme: rename countGrapheme() to countGraphemes() (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim authored Nov 24, 2024
1 parent be49399 commit 9d688d8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-cats-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"unicode-segmenter": patch
---

grapheme: rename `countGrapheme()` to `countGraphemes()`. existing name is deprecated alias.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 |
Expand All @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion benchmark/grapheme/bundle-entries/unicode-segmenter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
graphemeSegments,
countGrapheme,
splitGraphemes,
countGraphemes,
} from '../../../src/grapheme.js';
9 changes: 8 additions & 1 deletion src/grapheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>}
Expand Down
20 changes: 10 additions & 10 deletions test/grapheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import fc from 'fast-check';
import {
GraphemeCategory,
graphemeSegments,
countGrapheme,
splitGraphemes,
countGraphemes,
} from 'unicode-segmenter/grapheme';
import { assertObjectContaining } from './_helper.js';

Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 9d688d8

Please sign in to comment.