string-length

Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes

MIT 16 个版本
安装
npm install string-length
yarn add string-length
pnpm add string-length
bun add string-length
README

string-length

Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes

String#length erroneously counts astral symbols as two characters.

Install

npm install string-length

Usage

import stringLength from 'string-length';

'🐴'.length;
//=> 2

stringLength('🐴');
//=> 1

stringLength('\u001B[1municorn\u001B[22m');
//=> 7

API

stringLength(string, options?)

options

Type: object

countAnsiEscapeCodes

Type: boolean
Default: false

Whether ANSI escape codes should be counted. They are ignored by default.

版本列表
7.0.1 2026-01-21
7.0.0 2026-01-21
6.0.0 2023-06-04
5.0.1 2021-09-13
5.0.0 2021-06-05
4.0.2 2021-03-17
4.0.1 2020-03-19
4.0.0 2020-02-21
3.1.0 2019-04-02
3.0.0 2019-03-18
2.0.0 2017-07-27
1.0.1 2015-07-16
1.0.0 2014-08-13
0.1.2 2014-05-01
0.1.1 2014-04-30
0.1.0 2014-04-16