string-width
PoolModern, zero-dependency string width calculator - beats string-width by 10-20%, 2-3x faster on ASCII
dist
Functions
function stringWidth(text: string, options?: StringWidthOptions): number
Calculate the visual width of a string This is the main function that determines how many columns a string will occupy when displayed in a terminal or fixed-width context.
| Parameter | Type | Description |
|---|---|---|
text | string | - String to measure |
optionsoptional | StringWidthOptions | - Optional configuration |
function truncateToWidth(text: string, width: number, options?: TruncateOptions): string
Truncate a string to fit within a specified width
| Parameter | Type | Description |
|---|---|---|
text | string | - String to truncate |
width | number | - Maximum width in columns |
optionsoptional | TruncateOptions | - Truncation options |
function padToWidth(text: string, width: number, options?: PadOptions): string
Pad a string to fit a specified width
| Parameter | Type | Description |
|---|---|---|
text | string | - String to pad |
width | number | - Target width in columns |
optionsoptional | PadOptions | - Padding options |
function sliceByWidth(text: string, startWidth: number, endWidth?: number, options?: SliceOptions): string
Slice a string by visual width (similar to String.prototype.slice but width-aware)
| Parameter | Type | Description |
|---|---|---|
text | string | - String to slice |
startWidth | number | - Starting width position (can be negative) |
endWidthoptional | number | - Ending width position (optional, can be negative) |
optionsoptional | SliceOptions | - Slice options |
function splitByWidth(text: string, maxWidth: number, options?: SplitOptions): string[]
Split a string into chunks that fit within a specified width
| Parameter | Type | Description |
|---|---|---|
text | string | - String to split |
maxWidth | number | - Maximum width per chunk |
optionsoptional | SplitOptions | - Split options |
function getEastAsianWidth(codePoint: number): EastAsianWidth
Get East Asian Width category for a code point Uses binary search for O(log n) performance.
| Parameter | Type | Description |
|---|---|---|
codePoint | number | - Unicode code point |
function isZeroWidth(codePoint: number): boolean
Check if a code point is zero-width
| Parameter | Type | Description |
|---|---|---|
codePoint | number | - Unicode code point to check |
function isAsciiOnly(text: string): boolean
Check if entire string is ASCII-only This is the critical optimization for 2-3x speedup on ASCII strings.
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to check |
Classes
Iterator for grapheme clusters Uses JavaScript's built-in Intl.Segmenter API which provides native, optimized grapheme segmentation per Unicode standards.
constructor(text: string)
| Method | Return Type | Description |
|---|---|---|
[Symbol.iterator]() | Iterator<string> | Iterator implementation Yields each grapheme cluster as a string. |
toArray() | string[] | Get array of all grapheme clusters |
count() | number | Get count of grapheme clusters This is the "true" character count that matches user perception. |
at(index) | string | undefined | Get grapheme at specific index |
Variables
Set<number>Zero-width characters These characters have no visual width in terminal/text display.
readonly [number, number][]Combining mark ranges (width = 0) These are diacritical marks that combine with the previous character. Per Unicode, they have zero width.
readonly [number, number][]Emoji presentation ranges These code points typically render as emoji (width = 2). Per Unicode Emoji specifications.
unknownVariation Selectors for emoji presentation These selectors control whether a character displays as text or emoji.
unknowndist
Functions
function stringWidth(text: string, options?: StringWidthOptions): number
Calculate the visual width of a string This is the main function that determines how many columns a string will occupy when displayed in a terminal or fixed-width context.
| Parameter | Type | Description |
|---|---|---|
text | string | - String to measure |
optionsoptional | StringWidthOptions | - Optional configuration |
function truncateToWidth(text: string, width: number, options?: TruncateOptions): string
Truncate a string to fit within a specified width
| Parameter | Type | Description |
|---|---|---|
text | string | - String to truncate |
width | number | - Maximum width in columns |
optionsoptional | TruncateOptions | - Truncation options |
function padToWidth(text: string, width: number, options?: PadOptions): string
Pad a string to fit a specified width
| Parameter | Type | Description |
|---|---|---|
text | string | - String to pad |
width | number | - Target width in columns |
optionsoptional | PadOptions | - Padding options |
function sliceByWidth(text: string, startWidth: number, endWidth?: number, options?: SliceOptions): string
Slice a string by visual width (similar to String.prototype.slice but width-aware)
| Parameter | Type | Description |
|---|---|---|
text | string | - String to slice |
startWidth | number | - Starting width position (can be negative) |
endWidthoptional | number | - Ending width position (optional, can be negative) |
optionsoptional | SliceOptions | - Slice options |
function splitByWidth(text: string, maxWidth: number, options?: SplitOptions): string[]
Split a string into chunks that fit within a specified width
| Parameter | Type | Description |
|---|---|---|
text | string | - String to split |
maxWidth | number | - Maximum width per chunk |
optionsoptional | SplitOptions | - Split options |
function getEastAsianWidth(codePoint: number): EastAsianWidth
Get East Asian Width category for a code point Uses binary search for O(log n) performance.
| Parameter | Type | Description |
|---|---|---|
codePoint | number | - Unicode code point |
function isZeroWidth(codePoint: number): boolean
Check if a code point is zero-width
| Parameter | Type | Description |
|---|---|---|
codePoint | number | - Unicode code point to check |
function isAsciiOnly(text: string): boolean
Check if entire string is ASCII-only This is the critical optimization for 2-3x speedup on ASCII strings.
| Parameter | Type | Description |
|---|---|---|
text | string | - Text to check |
Classes
Iterator for grapheme clusters Uses JavaScript's built-in Intl.Segmenter API which provides native, optimized grapheme segmentation per Unicode standards.
constructor(text: string)
| Method | Return Type | Description |
|---|---|---|
[Symbol.iterator]() | Iterator<string> | Iterator implementation Yields each grapheme cluster as a string. |
toArray() | string[] | Get array of all grapheme clusters |
count() | number | Get count of grapheme clusters This is the "true" character count that matches user perception. |
at(index) | string | undefined | Get grapheme at specific index |
Variables
Set<number>Zero-width characters These characters have no visual width in terminal/text display.
readonly [number, number][]Combining mark ranges (width = 0) These are diacritical marks that combine with the previous character. Per Unicode, they have zero width.
readonly [number, number][]Emoji presentation ranges These code points typically render as emoji (width = 2). Per Unicode Emoji specifications.
unknownVariation Selectors for emoji presentation These selectors control whether a character displays as text or emoji.
unknownTaxes calculated at checkout based on your location.