Vol. 1.1.0 A field guide to Unicode safe strings in JavaScript npm i @kwy404/graphemely

Nothing matches. Try truncate, length or ESLint.

Guide No. 02 Length

Counting characters

Because String.prototype.length counts UTF-16 code units, not characters. Most emoji need two code units, and many are sequences of several code points. To count what a person sees, count grapheme clusters with length(str) from graphemely.

One family emoji measured three ways: one grapheme, five code points and eight UTF-16 code units GRAPHEMESCODE POINTSUTF-16 UNITS 158 WHAT PEOPLE SEE[...STR].LENGTHSTR.LENGTH
Plate 2. One family emoji, measured three ways: one grapheme, five code points and eight UTF-16 code units.

The fix, copy and paste

example.js
import { length } from '@kwy404/graphemely';

length('hello');                                          // 5
length('\u{1F600}');                                      // 1 (grinning face)
length('\u{1F44D}\u{1F3FD}');                             // 1 (thumbs up, medium skin tone)
length('\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}');    // 1 (family of three)
length('e\u{301}');                                       // 1 (e with a combining accent)

Emoji in the code samples are written as JavaScript escapes, for example \u{1F468} for a man. In your own code you can type the emoji directly.

What .length actually measures

Text (described).length[...str].lengthlength(str)
Letter a111
e with a combining acute accent221
Grinning face emoji211
Thumbs up with a skin tone421
Flag of Brazil (two regional indicators)421
Keycap digit one331
Family: man, woman, girl with zero width joiners851
Flag of England (tag sequence)1471

The spread operator and Array.from iterate by code point, which fixes surrogate pairs but still counts every part of a sequence. Only grapheme segmentation, as defined by Unicode UAX #29, matches what users see.

Where this bites in real code

  • Character counters on text fields that disagree with what the user typed.
  • Validation like "username must be at most 20 characters" that rejects short names with emoji or accents.
  • Fixed width layouts and tables where padding is computed from .length. See padStart and padEnd in graphemely.

Note that some limits are defined in bytes or code units on purpose, for example database column sizes. Use .length or new TextEncoder().encode(str).length for those, and length(str) for anything a human reads.

A family of three is eight code units, five code points and one character.

Install

Terminal
npm install @kwy404/graphemely

Works in Node 16 and newer, all modern browsers, Deno and Bun. ESM and CommonJS, TypeScript types included, zero dependencies, about 2 kB gzipped. Try your own text in the Text Lab.

  1. 01Truncating textShorten text to a length with an ellipsis, never half a character.
  2. 03Splitting text into charactersVisible characters instead of code units or code points.
  3. 04Lone surrogatesFind and repair the broken halves of emoji.