Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/string-length
Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes
https://github.com/sindresorhus/string-length
Last synced: 2 days ago
JSON representation
Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes
- Host: GitHub
- URL: https://github.com/sindresorhus/string-length
- Owner: sindresorhus
- License: mit
- Created: 2014-04-16T21:48:16.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2024-06-22T04:25:25.000Z (7 months ago)
- Last Synced: 2025-01-16T15:06:55.913Z (9 days ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 164
- Watchers: 9
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-cn - string-length - 获取字符串的真实长度-通过正确计算星号并忽略ansi转义码. (包 / 文本)
- awesome-nodejs - string-length - Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes. (Repository / Text/String)
- awesome-nodejs - string-length - Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes. (Packages / Text)
- awesome-nodejs - string-length - Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes - ★ 58 (Text)
- awesome-node - string-length - Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes. (Packages / Text)
- awesome-nodejs-cn - string-length - 获取字符串的真实长度-通过正确计算星号并忽略ansi转义码. (目录 / 文本处理)
- awesome-nodejs-cn - string-length - **star:164** 通过正确计算星体符号和忽略ansi转义码来获得字符串的实际长度 (包 / 文本)
README
# string-length
> Get the real length of a string - by correctly counting astral symbols and ignoring [ansi escape codes](https://github.com/sindresorhus/strip-ansi)
`String#length` erroneously counts [astral symbols](https://web.archive.org/web/20150721114550/http://www.tlg.uci.edu/~opoudjis/unicode/unicode_astral.html) as two characters.
## Install
```sh
npm install string-length
```## Usage
```js
import stringLength from 'string-length';'🐴'.length;
//=> 2stringLength('🐴');
//=> 1stringLength('\u001B[1municorn\u001B[22m');
//=> 7
```## API
### stringLength(string, options?)
#### options
Type: `object`
##### countAnsiEscapeCodes
Type: `boolean`\
Default: `false`Whether [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) should be counted. They are ignored by default.
## Related
- [string-length-cli](https://github.com/LitoMore/string-length-cli) - CLI for this module
- [string-width](https://github.com/sindresorhus/string-width) - Get visual width of a string