-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Grapheme cluster splitting | ||
|
||
[`grapheme-splitter`](https://github.com/orling/grapheme-splitter) and [`graphemer`](https://github.com/flmnt/graphemer) are no longer maintained, and can often be replaced with platform-provided APIs. | ||
|
||
Or you can find much lighter, actively maintained alternative. | ||
|
||
## Alternatives | ||
|
||
### Intl API | ||
|
||
[`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter) is provided by following modern JavaScript runtimes | ||
|
||
- Node.js v16 / Bun / Deno | ||
- Chrome v87 | ||
- Safari v14.1 | ||
- Firefox v132 | ||
|
||
```js | ||
const segmenter = new Intl.Segmenter(); | ||
|
||
const segments = [...segmenter.segment(text)]; | ||
``` | ||
|
||
### unicode-segmenter | ||
|
||
[`unicode-segmenter`](https://github.com/cometkim/unicode-segmenter) is a lighter and faster alternative that implement Unicode text segmentation standard with zero dependencies. | ||
|
||
```js | ||
import { graphemeSegments } from 'unicode-segmenter/grapheme'; | ||
|
||
const segments = [...graphemeSegments(text)]; | ||
``` | ||
|
||
The API is almost same with the `Intl.Segmenter`, so you can also use it as polyfill. | ||
|
||
```js | ||
import { Segmenter } from 'unicode-segmenter/intl-adapter'; | ||
// or | ||
import 'unicode-segmenter/intl-polyfill'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters