Skip to content

Commit 9d688d8

Browse files
authored
grapheme: rename countGrapheme() to countGraphemes() (#65)
1 parent be49399 commit 9d688d8

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

.changeset/early-cats-exercise.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: rename `countGrapheme()` to `countGraphemes()`. existing name is deprecated alias.

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ import { splitGraphemes } from 'unicode-segmenter/grapheme';
7474
#### Example: Count graphemes
7575

7676
```js
77-
import { countGrapheme } from 'unicode-segmenter/grapheme';
77+
import { countGraphemes } from 'unicode-segmenter/grapheme';
7878

7979
'👋 안녕!'.length;
8080
// => 6
81-
countGrapheme('👋 안녕!');
81+
countGraphemes('👋 안녕!');
8282
// => 5
8383

8484
'a̐éö̲'.length;
8585
// => 7
86-
countGrapheme('a̐éö̲');
86+
countGraphemes('a̐éö̲');
8787
// => 3
8888
```
8989

9090
> [!NOTE]
91-
> `countGrapheme()` is a small wrapper around `graphemeSegments()`.
91+
> `countGraphemes()` is a small wrapper around `graphemeSegments()`.
9292
>
9393
> If you need it more than once at a time, consider memoization or use `graphemeSegments()` or `splitSegments()` once instead.
9494
@@ -251,7 +251,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb
251251

252252
| Name | Unicode® | ESM? | Size | Size (min) | Size (min+gzip) | Size (min+br) |
253253
|------------------------------|----------|------|----------:|-----------:|----------------:|--------------:|
254-
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 17,347 | 12,822 | 5,307 | 4,093 |
254+
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 17,348 | 12,822 | 5,307 | 4,089 |
255255
| `graphemer` | 15.0.0 | ✖️ ️| 410,435 | 95,104 | 15,752 | 10,660 |
256256
| `grapheme-splitter` | 10.0.0 | ✖️ | 122,252 | 23,680 | 7,852 | 4,841 |
257257
| `@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
267267

268268
| Name | Bytecode size | Bytecode size (gzip)* |
269269
|------------------------------|--------------:|----------------------:|
270-
| `unicode-segmenter/grapheme` | 24,521 | 12,773 |
270+
| `unicode-segmenter/grapheme` | 24,538 | 12,788 |
271271
| `graphemer` | 133,949 | 31,710 |
272272
| `grapheme-splitter` | 63,810 | 19,125 |
273273
| `@formatjs/intl-segmenter`* | 315,865 | 99,063 |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export {
22
graphemeSegments,
3-
countGrapheme,
43
splitGraphemes,
4+
countGraphemes,
55
} from '../../../src/grapheme.js';

src/grapheme.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,19 @@ export function* graphemeSegments(input) {
189189
* @param {string} str
190190
* @return number count of graphemes
191191
*/
192-
export function countGrapheme(str) {
192+
export function countGraphemes(str) {
193193
let count = 0;
194194
for (let _ of graphemeSegments(str)) count += 1;
195195
return count;
196196
}
197197

198+
export {
199+
/**
200+
* @deprecated use {@link countGraphemes}
201+
*/
202+
countGraphemes as countGrapheme,
203+
};
204+
198205
/**
199206
* @param {string} str
200207
* @return {IterableIterator<string>}

test/grapheme.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import fc from 'fast-check';
77
import {
88
GraphemeCategory,
99
graphemeSegments,
10-
countGrapheme,
1110
splitGraphemes,
11+
countGraphemes,
1212
} from 'unicode-segmenter/grapheme';
1313
import { assertObjectContaining } from './_helper.js';
1414

@@ -74,34 +74,34 @@ test('graphemeSegments', async t => {
7474
});
7575
});
7676

77-
test('countGrapheme', async t => {
77+
test('countGraphemes', async t => {
7878
await t.test('latin', () => {
79-
assert.equal(countGrapheme('abcd'), 4);
79+
assert.equal(countGraphemes('abcd'), 4);
8080
});
8181

8282
await t.test('flags', () => {
83-
assert.equal(countGrapheme('🇷🇸🇮🇴'), 2);
83+
assert.equal(countGraphemes('🇷🇸🇮🇴'), 2);
8484
});
8585

8686
await t.test('emoji', () => {
87-
assert.equal(countGrapheme('👻👩‍👩‍👦‍👦'), 2);
88-
assert.equal(countGrapheme('🌷🎁💩😜👍🏳️‍🌈'), 6);
87+
assert.equal(countGraphemes('👻👩‍👩‍👦‍👦'), 2);
88+
assert.equal(countGraphemes('🌷🎁💩😜👍🏳️‍🌈'), 6);
8989
});
9090

9191
await t.test('diacritics as combining marks', () => {
92-
assert.equal(countGrapheme('Ĺo͂řȩm̅'), 5);
92+
assert.equal(countGraphemes('Ĺo͂řȩm̅'), 5);
9393
});
9494

9595
await t.test('Jamo', () => {
96-
assert.equal(countGrapheme('뎌쉐'), 2);
96+
assert.equal(countGraphemes('뎌쉐'), 2);
9797
});
9898

9999
await t.test('Hindi', () => {
100-
assert.equal(countGrapheme('अनुच्छेद'), 4);
100+
assert.equal(countGraphemes('अनुच्छेद'), 4);
101101
});
102102

103103
await t.test('demonic', () => {
104-
assert.equal(countGrapheme('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞'), 6);
104+
assert.equal(countGraphemes('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞'), 6);
105105
});
106106
});
107107

0 commit comments

Comments
 (0)