grapheme-splitter
and graphemer
are no longer maintained, and can often be replaced with platform-provided APIs.
Or you can find much lighter, actively maintained alternative.
Intl.Segmenter
is provided by following modern JavaScript runtimes
- Node.js v16 / Bun / Deno
- Chrome v87
- Safari v14.1
- Firefox v132
const segmenter = new Intl.Segmenter();
const segments = [...segmenter.segment(text)];
unicode-segmenter
is a lighter and faster alternative that implement Unicode text segmentation standard with zero dependencies.
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.
import { Segmenter } from 'unicode-segmenter/intl-adapter';
// or
import 'unicode-segmenter/intl-polyfill';