graphemer

中文文档
1.4.0发布于 2022/09/19MITTypeScriptCommonJS零依赖近 30 天下载 1.5亿

A JavaScript library that breaks strings into their individual user-perceived characters (including emojis!)

原始使用文档
文档目录7 节
  1. Introduction
  2. Installation
  3. Usage
  4. Examples
  5. TypeScript
  6. Contributing
  7. Acknowledgements

Graphemer: Unicode Character Splitter 🪓

Introduction

This library continues the work of Grapheme Splitter and supports the following unicode versions:

  • Unicode 15 and below [v1.4.0]
  • Unicode 14 and below [v1.3.0]
  • Unicode 13 and below [v1.1.0]
  • Unicode 11 and below [v1.0.0] (Unicode 10 supported by grapheme-splitter)

In JavaScript there is not always a one-to-one relationship between string characters and what a user would call a separate visual "letter". Some symbols are represented by several characters. This can cause issues when splitting strings and inadvertently cutting a multi-char letter in half, or when you need the actual number of letters in a string.

For example, emoji characters like "🌷","🎁","💩","😜" and "👍" are represented by two JavaScript characters each (high surrogate and low surrogate). That is,

'🌷'.length == 2;

The combined emoji are even longer:

'🏳️‍🌈'.length == 6;

What's more, some languages often include combining marks - characters that are used to modify the letters before them. Common examples are the German letter ü and the Spanish letter ñ. Sometimes they can be represented alternatively both as a single character and as a letter + combining mark, with both forms equally valid:

var two = 'ñ'; // unnormalized two-char n+◌̃, i.e. "\u006E\u0303";
var one = 'ñ'; // normalized single-char, i.e. "\u00F1"

console.log(one != two); // prints 'true'

Unicode normalization, as performed by the popular punycode.js library or ECMAScript 6's String.normalize, can sometimes fix those differences and turn two-char sequences into single characters. But it is not enough in all cases. Some languages like Hindi make extensive use of combining marks on their letters, that have no dedicated single-codepoint Unicode sequences, due to the sheer number of possible combinations. For example, the Hindi word "अनुच्छेद" is comprised of 5 letters and 3 combining marks:

अ + न + ु + च + ् + छ + े + द

which is in fact just 5 user-perceived letters:

अ + नु + च् + छे + द

and which Unicode normalization would not combine properly. There are also the unusual letter+combining mark combinations which have no dedicated Unicode codepoint. The string Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘ obviously has 5 separate letters, but is in fact comprised of 58 JavaScript characters, most of which are combining marks.

Enter the graphemer library. It can be used to properly split JavaScript strings into what a human user would call separate letters (or "extended grapheme clusters" in Unicode terminology), no matter what their internal representation is. It is an implementation on the Default Grapheme Cluster Boundary of UAX #29.

Installation

Install graphemer using the NPM command below:

$ npm i graphemer

Usage

If you're using Typescript or a compiler like Babel (or something like Create React App) things are pretty simple; just import, initialize and use!

import Graphemer from 'graphemer';

const splitter = new Graphemer();

// split the string to an array of grapheme clusters (one string each)
const graphemes = splitter.splitGraphemes(string);

// iterate the string to an iterable iterator of grapheme clusters (one string each)
const graphemeIterator = splitter.iterateGraphemes(string);

// or do this if you just need their number
const graphemeCount = splitter.countGraphemes(string);

If you're using vanilla Node you can use the require() method.

const Graphemer = require('graphemer').default;

const splitter = new Graphemer();

const graphemes = splitter.splitGraphemes(string);

Examples

import Graphemer from 'graphemer';

const splitter = new Graphemer();

// plain latin alphabet - nothing spectacular
splitter.splitGraphemes('abcd'); // returns ["a", "b", "c", "d"]

// two-char emojis and six-char combined emoji
splitter.splitGraphemes('🌷🎁💩😜👍🏳️‍🌈'); // returns ["🌷","🎁","💩","😜","👍","🏳️‍🌈"]

// diacritics as combining marks, 10 JavaScript chars
splitter.splitGraphemes('Ĺo͂řȩm̅'); // returns ["Ĺ","o͂","ř","ȩ","m̅"]

// individual Korean characters (Jamo), 4 JavaScript chars
splitter.splitGraphemes('뎌쉐'); // returns ["뎌","쉐"]

// Hindi text with combining marks, 8 JavaScript chars
splitter.splitGraphemes('अनुच्छेद'); // returns ["अ","नु","च्","छे","द"]

// demonic multiple combining marks, 75 JavaScript chars
splitter.splitGraphemes('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞'); // returns ["Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍","A̴̵̜̰͔ͫ͗͢","L̠ͨͧͩ͘","G̴̻͈͍͔̹̑͗̎̅͛́","Ǫ̵̹̻̝̳͂̌̌͘","!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞"]

TypeScript

Graphemer is built with TypeScript and, of course, includes type declarations.

import Graphemer from 'graphemer';

const splitter = new Graphemer();

const split: string[] = splitter.splitGraphemes('Z͑ͫ̓ͪ̂ͫ̽͏̴̙̤̞͉͚̯̞̠͍A̴̵̜̰͔ͫ͗͢L̠ͨͧͩ͘G̴̻͈͍͔̹̑͗̎̅͛́Ǫ̵̹̻̝̳͂̌̌͘!͖̬̰̙̗̿̋ͥͥ̂ͣ̐́́͜͞');

Contributing

See Contribution Guide.

Acknowledgements

This library is a fork of the incredible work done by Orlin Georgiev and Huáng Jùnliàng at https://github.com/orling/grapheme-splitter.

The original library was heavily influenced by Devon Govett's excellent grapheme-breaker CoffeeScript library.

版本与发布记录

graphemer 共发布 6 个版本,首次发布于 2020/09/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.4.0latest2022/09/194 年前
1.3.02021/12/144 年前
1.2.02021/01/295 年前
1.1.12020/09/156 年前
1.1.02020/09/156 年前
1.0.02020/09/146 年前

关于 graphemer 的常见问题

graphemer 是什么?

A JavaScript library that breaks strings into their individual user-perceived characters (including emojis!)

如何安装 graphemer?

在项目目录运行 npm install graphemer,或使用 pnpm add / yarn add / bun add。

graphemer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

graphemer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

graphemer 可以商用吗?

graphemer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

graphemer 的下载量有多少?

根据 npm 官方下载统计,graphemer 在 2026/10/05 – 2026/11/03 这 30 天内被下载 148,248,300 次,日均约 4,941,610 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

graphemer 还在维护吗?

最新版本 1.4.0 发布于 2022/09/19(4 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/09/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。