Skip to content

Commit af382bf

Browse files
committed
grapheme: rename countGrapheme() to countGraphemes()
1 parent be49399 commit af382bf

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
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: 4 additions & 4 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

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,7 +7,7 @@ import fc from 'fast-check';
77
import {
88
GraphemeCategory,
99
graphemeSegments,
10-
countGrapheme,
10+
countGraphemes,
1111
splitGraphemes,
1212
} from 'unicode-segmenter/grapheme';
1313
import { assertObjectContaining } from './_helper.js';
@@ -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)