File size: 1,069 Bytes
bda3cf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Fast String Width

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

See [`fast-string-truncated-width`](https://github.com/fabiospampinato/fast-string-truncated-width) for a lower-level version of this.

## Install

```sh
npm install fast-string-width
```

## Usage

```ts
import fastStringWidth from 'fast-string-width';

// The width of various classes of characters is configurable

const options = {
  ansiWidth: 0,
  controlWidth: 0,
  tabWidth: 8,
  ambiguousWidth: 1,
  emojiWidth: 2,
  fullWidthWidth: 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