# @nxmix/string-visual-width

> Get the visual width of the string

Latest version **1.0.1** (published 2019-10-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nxmix/string-visual-width
pnpm add @nxmix/string-visual-width
yarn add @nxmix/string-visual-width
bun add @nxmix/string-visual-width
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-10-12 |
| First published | 2018-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 3 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | jacobbubu |
| Maintainers | jacobbubu, nxdeploy, zhengle |
| Keywords | string, length, width, terminal, emoji, unicode, visual, wide-char, combination |

## Links

- npm: https://www.npmjs.com/package/@nxmix/string-visual-width
- Repository: https://github.com/NXMIX/string-visual-width
- Homepage: https://github.com/NXMIX/string-visual-width#readme
- Issues: https://github.com/NXMIX/string-visual-width/issues
- npm.io page: https://npm.io/package/@nxmix/string-visual-width

## Dependencies (3)

- [strip-ansi](https://npm.io/package/strip-ansi.md) ^5.2.0
- [@nxmix/is-full-width](https://npm.io/package/@nxmix/is-full-width.md) ^0.1.0
- [@nxmix/emoji-seq-match](https://npm.io/package/@nxmix/emoji-seq-match.md) ^1.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-10-12
- 1.0.0 — 2018-06-08
- 0.1.1 — 2018-06-04

## README

# string-visual-width

[![Build Status](https://travis-ci.org/NXMIX/string-visual-width.svg?branch=master)](https://travis-ci.org/NXMIX/string-visual-width)
[![Coverage Status](https://coveralls.io/repos/github/NXMIX/string-visual-width/badge.svg)](https://coveralls.io/github/NXMIX/string-visual-width)
[![npm](https://img.shields.io/npm/v/string-visual-width.svg?maxAge=1000)](https://www.npmjs.com/package/@nxmix/string-visual-width/) [![Greenkeeper badge](https://badges.greenkeeper.io/NXMIX/string-visual-width.svg)](https://greenkeeper.io/)

> Get the visual width of the string 

## Why

Because of the combination of Unicode and Emoji characters, it is difficult to calculate the actual display width of a string in Terminal applciation. Strictly speaking there is no universal method to do that.

I try to figure it out in accordance with the following steps:
* Strip out all ansi codes in string.
* Nomalize unocide character before calculation, `n\u0303` becomes `ñ`.
* Use [@nxmix/emoji-seq-match](https://github.com/NXMIX/emoji-seq-match.git) to match joined Emoji characters as long as possible and calculate their width as 2.
* Use [@nxmix/is-full-width](https://github.com/NXMIX/is-full-width.git) determine which characters should be treated as East Asian characters and count a width of 2.

## Usage

### Install

`npm i @nxmix/string-visual-width --save`

[Typescript](https://www.typescriptlang.org) definition file is already included.

## Examples

```js
const width = require('@nxmix/string-visual-width').default;

width('中');
//=> 2

width("👩‍👦");
//=> 2
```

```ts
import getMatchedLength from '@nxmix/string-visual-width';

width('中');
//=> 2
```

## Author
[Rong Shen](https://github/jacobbubu)

---
_Source: https://npm.io/package/@nxmix/string-visual-width · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
