1. string-length
Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes
string-length
Package: string-length
Created by: sindresorhus
Last modified: Fri, 09 Jun 2023 21:32:37 GMT
Version: 6.0.0
License: MIT
Downloads: 109,434,899
Repository: https://github.com/sindresorhus/string-length

Install

npm install string-length
yarn add string-length

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.

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code