Skip to content

Commit 3c753c1

Browse files
authored
cleanup code to avoid unnecessary type assertions (#95)
1 parent d5dab70 commit 3c753c1

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb
220220

221221
| Name | Unicode® | ESM? | Size | Size (min) | Size (min+gzip) | Size (min+br) | Size (min+zstd) |
222222
|------------------------------|----------|------|----------:|-----------:|----------------:|--------------:|----------------:|
223-
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 15,919 | 12,187 | 5,057 | 3,738 | 4,745 |
223+
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 15,625 | 12,166 | 5,044 | 3,752 | 4,724 |
224224
| `graphemer` | 15.0.0 | ✖️ ️| 410,435 | 95,104 | 15,752 | 10,660 | 15,911 |
225225
| `grapheme-splitter` | 10.0.0 | ✖️ | 122,252 | 23,680 | 7,852 | 4,841 | 6,750 |
226226
| `@formatjs/intl-segmenter`* | 15.0.0 | ✖️ | 603,285 | 369,560 | 72,218 | 49,416 | 67,975 |
@@ -236,7 +236,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb
236236

237237
| Name | Bytecode size | Bytecode size (gzip)* |
238238
|------------------------------|--------------:|----------------------:|
239-
| `unicode-segmenter/grapheme` | 20,932 | 10,977 |
239+
| `unicode-segmenter/grapheme` | 20,932 | 10,994 |
240240
| `graphemer` | 133,978 | 31,713 |
241241
| `grapheme-splitter` | 63,835 | 19,137 |
242242

src/grapheme.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,30 @@ export { GraphemeCategory };
4343
* @return {GraphemeSegmenter} iterator for grapheme cluster segments
4444
*/
4545
export function* graphemeSegments(input) {
46+
let cp = input.codePointAt(0);
47+
4648
// do nothing on empty string
47-
if (input === '') {
48-
return;
49-
}
49+
if (cp == null) return;
5050

51-
/** @type {number} Current cursor position. */
51+
/** Current cursor position. */
5252
let cursor = 0;
5353

54-
/** @type {number} Total length of the input string. */
54+
/** Total length of the input string. */
5555
let len = input.length;
5656

57-
/** @type {GraphemeCategoryNum | null} Category of codepoint immediately preceding cursor, if known. */
58-
let catBefore = null;
57+
/** @type {import('./_grapheme_data.js').GraphemeCategoryRange} */
58+
let cache = [0, 0, 2 /* GC_Control */];
5959

60-
/** @type {GraphemeCategoryNum | null} Category of codepoint immediately preceding cursor, if known. */
61-
let catAfter = null;
60+
/** Category of codepoint immediately preceding cursor */
61+
let catBefore = cat(cp, cache);
6262

63-
/** @type {GraphemeCategoryNum | null} Beginning category of a segment */
64-
let catBegin = null;
63+
/** Beginning category of a segment */
64+
let catBegin = catBefore;
6565

66-
/** @type {import('./_grapheme_data.js').GraphemeCategoryRange} */
67-
let cache = [0, 0, 2 /* GC_Control */];
66+
/** @type {GraphemeCategoryNum | null} Category of codepoint immediately preceding cursor. */
67+
let catAfter = null;
6868

69-
/** @type {number} The number of RIS codepoints preceding `cursor`. */
69+
/** The number of RIS codepoints preceding `cursor`. */
7070
let risCount = 0;
7171

7272
/** Emoji state */
@@ -81,8 +81,6 @@ export function* graphemeSegments(input) {
8181
/** InCB=Consonant InCB=Linker x InCB=Consonant */
8282
let incb = false;
8383

84-
let cp = /** @type {number} */ (input.codePointAt(cursor));
85-
8684
/** Memoize the beginnig code point a the segment. */
8785
let _hd = cp;
8886

@@ -91,21 +89,13 @@ export function* graphemeSegments(input) {
9189
while (true) {
9290
cursor += cp < 0xFFFF ? 1 : 2;
9391

94-
// Note: Of course the nullish coalescing is useful here,
95-
// but avoid it for aggressive compatibility and perf claim
96-
catBefore = catAfter;
97-
if (catBefore === null) {
98-
catBefore = cat(cp, cache);
99-
catBegin = catBefore;
100-
}
101-
10292
if (cursor >= len) {
10393
yield {
10494
segment: input.slice(index, cursor),
10595
index,
10696
input,
10797
_hd,
108-
_catBegin: /** @type {typeof catBefore} */ (catBegin),
98+
_catBegin: catBegin,
10999
_catEnd: catBefore,
110100
};
111101
return;
@@ -149,7 +139,7 @@ export function* graphemeSegments(input) {
149139
index,
150140
input,
151141
_hd,
152-
_catBegin: /** @type {typeof catBefore} */ (catBegin),
142+
_catBegin: catBegin,
153143
_catEnd: catBefore,
154144
};
155145

@@ -160,6 +150,8 @@ export function* graphemeSegments(input) {
160150
catBegin = catAfter;
161151
_hd = cp;
162152
}
153+
154+
catBefore = catAfter;
163155
}
164156
}
165157

0 commit comments

Comments
 (0)