Neo Zero

string-width

Pool

Modern, zero-dependency string width calculator - beats string-width by 10-20%, 2-3x faster on ASCII

$ lpm install @lpm.dev/neo.string-width
28 exportsTypeScript

dist

Functions

stringWidth
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.

ParameterTypeDescription
text
string- String to measure
optionsoptional
StringWidthOptions- Optional configuration
truncateToWidth
function truncateToWidth(text: string, width: number, options?: TruncateOptions): string

Truncate a string to fit within a specified width

ParameterTypeDescription
text
string- String to truncate
width
number- Maximum width in columns
optionsoptional
TruncateOptions- Truncation options
padToWidth
function padToWidth(text: string, width: number, options?: PadOptions): string

Pad a string to fit a specified width

ParameterTypeDescription
text
string- String to pad
width
number- Target width in columns
optionsoptional
PadOptions- Padding options
sliceByWidth
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)

ParameterTypeDescription
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
splitByWidth
function splitByWidth(text: string, maxWidth: number, options?: SplitOptions): string[]

Split a string into chunks that fit within a specified width

ParameterTypeDescription
text
string- String to split
maxWidth
number- Maximum width per chunk
optionsoptional
SplitOptions- Split options
getEastAsianWidth
function getEastAsianWidth(codePoint: number): EastAsianWidth

Get East Asian Width category for a code point Uses binary search for O(log n) performance.

ParameterTypeDescription
codePoint
number- Unicode code point
isZeroWidth
function isZeroWidth(codePoint: number): boolean

Check if a code point is zero-width

ParameterTypeDescription
codePoint
number- Unicode code point to check
isAsciiOnly
function isAsciiOnly(text: string): boolean

Check if entire string is ASCII-only This is the critical optimization for 2-3x speedup on ASCII strings.

ParameterTypeDescription
text
string- Text to check

Classes

GraphemeIteratorimplements Iterable<string>

Iterator for grapheme clusters Uses JavaScript's built-in Intl.Segmenter API which provides native, optimized grapheme segmentation per Unicode standards.

constructor(text: string)
MethodReturn TypeDescription
[Symbol.iterator]()Iterator<string>Iterator implementation Yields each grapheme cluster as a string.
toArray()string[]Get array of all grapheme clusters
count()numberGet count of grapheme clusters This is the "true" character count that matches user perception.
at(index)string | undefinedGet grapheme at specific index

Variables

ZERO_WIDTH_CHARSSet<number>

Zero-width characters These characters have no visual width in terminal/text display.

COMBINING_MARK_RANGESreadonly [number, number][]

Combining mark ranges (width = 0) These are diacritical marks that combine with the previous character. Per Unicode, they have zero width.

EMOJI_RANGESreadonly [number, number][]

Emoji presentation ranges These code points typically render as emoji (width = 2). Per Unicode Emoji specifications.

VARIATION_SELECTOR_TEXTunknown

Variation Selectors for emoji presentation These selectors control whether a character displays as text or emoji.

VARIATION_SELECTOR_EMOJIunknown

dist

Functions

stringWidth
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.

ParameterTypeDescription
text
string- String to measure
optionsoptional
StringWidthOptions- Optional configuration
truncateToWidth
function truncateToWidth(text: string, width: number, options?: TruncateOptions): string

Truncate a string to fit within a specified width

ParameterTypeDescription
text
string- String to truncate
width
number- Maximum width in columns
optionsoptional
TruncateOptions- Truncation options
padToWidth
function padToWidth(text: string, width: number, options?: PadOptions): string

Pad a string to fit a specified width

ParameterTypeDescription
text
string- String to pad
width
number- Target width in columns
optionsoptional
PadOptions- Padding options
sliceByWidth
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)

ParameterTypeDescription
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
splitByWidth
function splitByWidth(text: string, maxWidth: number, options?: SplitOptions): string[]

Split a string into chunks that fit within a specified width

ParameterTypeDescription
text
string- String to split
maxWidth
number- Maximum width per chunk
optionsoptional
SplitOptions- Split options
getEastAsianWidth
function getEastAsianWidth(codePoint: number): EastAsianWidth

Get East Asian Width category for a code point Uses binary search for O(log n) performance.

ParameterTypeDescription
codePoint
number- Unicode code point
isZeroWidth
function isZeroWidth(codePoint: number): boolean

Check if a code point is zero-width

ParameterTypeDescription
codePoint
number- Unicode code point to check
isAsciiOnly
function isAsciiOnly(text: string): boolean

Check if entire string is ASCII-only This is the critical optimization for 2-3x speedup on ASCII strings.

ParameterTypeDescription
text
string- Text to check

Classes

GraphemeIteratorimplements Iterable<string>

Iterator for grapheme clusters Uses JavaScript's built-in Intl.Segmenter API which provides native, optimized grapheme segmentation per Unicode standards.

constructor(text: string)
MethodReturn TypeDescription
[Symbol.iterator]()Iterator<string>Iterator implementation Yields each grapheme cluster as a string.
toArray()string[]Get array of all grapheme clusters
count()numberGet count of grapheme clusters This is the "true" character count that matches user perception.
at(index)string | undefinedGet grapheme at specific index

Variables

ZERO_WIDTH_CHARSSet<number>

Zero-width characters These characters have no visual width in terminal/text display.

COMBINING_MARK_RANGESreadonly [number, number][]

Combining mark ranges (width = 0) These are diacritical marks that combine with the previous character. Per Unicode, they have zero width.

EMOJI_RANGESreadonly [number, number][]

Emoji presentation ranges These code points typically render as emoji (width = 2). Per Unicode Emoji specifications.

VARIATION_SELECTOR_TEXTunknown

Variation Selectors for emoji presentation These selectors control whether a character displays as text or emoji.

VARIATION_SELECTOR_EMOJIunknown
Unlimited AccessInstall as many Pool packages as you need.
Fund Real WorkEvery install you run sends revenue directly to the developer who built it.

Taxes calculated at checkout based on your location.

Weekly Installs
3
Version
1.0.0
Published
LicenseMIT
Size200.15 KB
Files14
Node version>= 18
TypeScriptYes