npm.io
3.0.2 β€’ Published 9 months ago

fast-string-width

Licence
MIT
Version
3.0.2
Deps
1
Size
3 kB
Vulns
0
Weekly
0
Stars
42

Fast String Width

A fast function for calculating the visual width of a string once printed to the terminal.

See fast-string-truncated-width for a lower-level version of this.

Install

npm install fast-string-width

Usage

import fastStringWidth from 'fast-string-width';

// The width of various classes of characters is configurable

const options = {
  controlWidth: 0,
  tabWidth: 8,
  emojiWidth: 2,
  regularWidth: 1,
  wideWidth: 2
};

// Calculating the visual width of some strings

fastStringWidth ( 'hello', options ); // => 5
fastStringWidth ( '\x1b[31mhello', options ); // => 5
fastStringWidth ( 'πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦', options ); // => 2
fastStringWidth ( 'helloπŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦', options ); // => 7

// Calculating the visual width while tweaking the width of emojis

fastStringWidth ( 'πŸ‘ΆπŸ‘ΆπŸ½', { ...options, emojiWidth: 1.5 } ); // => 3

License

MIT Fabio Spampinato

Keywords