Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
string-width
Advanced tools
Get the visual width of a string - the number of columns required to display it
The string-width npm package is used to calculate and return the visual width of a string - the number of columns required to display it. This is particularly useful when dealing with strings containing characters that may take up more than one column in terminals or monospaced fonts, such as emojis or certain international characters.
Calculate string width
This feature calculates the visual width of a string. For example, an ASCII character has a width of 1, while many CJK characters have a width of 2. Emojis can also have a width of 2 or more.
const stringWidth = require('string-width');
console.log(stringWidth('古')); // 2
console.log(stringWidth('a')); // 1
console.log(stringWidth('👍')); // 2
The wcwidth package is similar to string-width in that it provides functionality to determine the number of terminal column cells a wide-character string will occupy. It is based on the wcwidth() function defined in POSIX.1-2001 and POSIX.1-2008 which is used in Unix-based systems.
This package is designed to provide the functionality to determine the East Asian Width property as defined by Unicode. It can be used to figure out if a character is full-width, half-width, wide, narrow, ambiguous, or neutral. This is similar to string-width but focuses specifically on East Asian characters.
While string-width calculates the number of columns a string will take up in a monospaced font, string-pixel-width calculates the width of a string in pixels. This is useful for determining the actual rendered width of a string in a web page or an application with proportional fonts.
Get the visual width of a string - the number of columns required to display it
Some Unicode characters are fullwidth and use double the normal width. ANSI escape codes are stripped and doesn't affect the width.
Useful to be able to measure the actual width of command-line output.
npm install string-width
import stringWidth from 'string-width';
stringWidth('a');
//=> 1
stringWidth('古');
//=> 2
stringWidth('\u001B[1m古\u001B[22m');
//=> 2
Type: string
The string to be counted.
Type: object
Type: boolean
Default: true
Count ambiguous width characters as having narrow width (count of 1) instead of wide width (count of 2).
Ambiguous characters behave like wide or narrow characters depending on the context (language tag, script identification, associated font, source of data, or explicit markup; all can provide the context). If the context cannot be established reliably, they should be treated as narrow characters by default.
Type: boolean
Default: false
Whether ANSI escape codes should be counted.
FAQs
Get the visual width of a string - the number of columns required to display it
The npm package string-width receives a total of 132,470,174 weekly downloads. As such, string-width popularity was classified as popular.
We found that string-width demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.