Skip to content

rish405/string-width-modern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

string-width-modern

Accurate displayed string width for modern Unicode. Grapheme‑aware, handles emojis, full‑width CJK, surrogate pairs, combining marks, and ZWJ sequences. Zero runtime dependencies.

Install

npm install string-width-modern

Usage

import stringWidth from 'string-width-modern';

stringWidth('hello'); // 5
stringWidth('こんにちは'); // 10
stringWidth('🙂'); // 2
stringWidth('e\u0301'); // 1
stringWidth('👨‍👩‍👧‍👦'); // 2
stringWidth('🇺🇸'); // 2

CommonJS

const stringWidth = require('string-width-modern');
console.log(stringWidth('✌️'));

Notes

  • Uses Intl.Segmenter to iterate grapheme clusters and Unicode property escapes for categorization.
  • Treats CJK scripts and Fullwidth Forms as width 2.
  • Treats emoji (including ZWJ, variation selectors, skin tones, flags) as width 2.
  • Combines marks are width 0 when standalone and do not increase cluster width.

Edge Cases

  • vs ✌️: without U+FE0F variation selector is width 1; with it is width 2.
  • Standalone combining mark U+0301 is width 0; e\u0301 is width 1.
  • Regional indicator pairs (e.g., 🇯🇵) are width 2.

API

function stringWidth(input: string): number

Build

  • ESM + CJS via tsup
  • Type definitions included

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published