Socket
Socket
Sign inDemoInstall

string-length

Package Overview
Dependencies
2
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-length

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


Version published
Maintainers
1
Weekly downloads
25,218,188
increased by3.95%
Bundle size
492 bytes
Minified + gzipped

Weekly downloads

Package description

What is string-length?

The string-length npm package is used to get the real length of a string, taking into account Unicode characters and ansi escape codes that might be present in the string. It provides a more accurate count of visual symbols in a string, which can be different from the string's `.length` property due to such characters.

What are string-length's main functionalities?

Counting characters in a basic string

This feature allows you to count the number of characters in a basic string, which is straightforward and similar to the native JavaScript `.length` property, but it also correctly handles Unicode characters that are represented by surrogate pairs in JavaScript.

"string-length": "^4.0.1"

Counting characters in a string with Unicode

This feature is particularly useful for strings containing Unicode characters, such as emojis or characters from non-Latin scripts, which may be represented by multiple code units in JavaScript. The `string-length` package accurately counts these as single characters.

"string-length": "^4.0.1"

Counting characters in a string with ansi escape codes

When dealing with strings that include ansi escape codes, such as those used for coloring terminal output, `string-length` can correctly count the visible characters, ignoring the escape codes that do not contribute to the visible length of the string.

"string-length": "^4.0.1"

Other packages similar to string-length

Readme

Source

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.

Keywords

FAQs

Last updated on 04 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc