Neo Zero

ansi

Pool

Modern, zero-dependency ANSI escape code parser and stripper - beats strip-ansi by 10%+

$ lpm install @lpm.dev/neo.ansi
28 exportsTypeScript

dist

Functions

strip
function strip(input: string, options?: StripOptions): string

Strip all ANSI escape codes from a string This is the primary function for removing ANSI codes. It handles all sequence types: CSI, OSC, DCS, and simple escapes.

ParameterTypeDescription
input
string- Input string with ANSI codes
optionsoptional
StripOptions- Optional configuration for stripping behavior
stripLines
function stripLines(lines: string[], options?: StripOptions): string[]

Strip ANSI codes from multiple lines Convenience function for processing multiple lines at once. More efficient than calling strip() on each line separately.

ParameterTypeDescription
lines
string[]- Array of strings with ANSI codes
optionsoptional
StripOptions- Optional configuration for stripping behavior
parse
function parse(input: string): ParseResult

Parse ANSI escape sequences from input string This is the core parsing function that uses a state machine to identify and extract ANSI escape sequences. It returns both the stripped text and the sequences found.

ParameterTypeDescription
input
string- Input string potentially containing ANSI codes
hasAnsi
function hasAnsi(input: string): boolean

Check if string contains ANSI escape codes This is a fast check that looks for ESC characters without doing full parsing. Useful for conditionally processing strings.

ParameterTypeDescription
input
string- Input string to check
hasAnsiAny
function hasAnsiAny(inputs: string[]): boolean

Check if any string in array contains ANSI codes Convenience function for checking multiple strings. Short-circuits on first match for efficiency.

ParameterTypeDescription
inputs
string[]- Array of strings to check
hasAnsiAll
function hasAnsiAll(inputs: string[]): boolean

Check if all strings in array contain ANSI codes

ParameterTypeDescription
inputs
string[]- Array of strings to check
isDigit
function isDigit(code: number): boolean

Helper function to check if character code is a digit (0-9)

ParameterTypeDescription
code
number- Character code to check
isCsiFinalByte
function isCsiFinalByte(code: number): boolean

Helper function to check if character code is a CSI final byte CSI final bytes are in the range @ to ~ (0x40 to 0x7e)

ParameterTypeDescription
code
number- Character code to check
isCsiParamByte
function isCsiParamByte(code: number): boolean

Helper function to check if character code is a CSI parameter byte Parameter bytes include digits (0-9), semicolon (;), and colon (:)

ParameterTypeDescription
code
number- Character code to check

Enums

AnsiType

ANSI escape sequence types based on VT100 specification

MemberValue
CSI"csi"
OSC"osc"
DCS"dcs"
Simple"simple"
Unknown"unknown"
ParserState

Parser state machine states

MemberValue
Normal0
Escape1
CSI2
OSC3
DCS4

Variables

CHAR_CODE{ /** ESC - Escape character (0x1B) */ readonly ESC: 27; /** [ - Left bracket for CSI */ readonly LEFT_BRACKET: 91; /** ] - Right bracket for OSC */ readonly RIGHT_BRACKET: 93; /** P - Device Control String */ readonly CAPITAL_P: 80; /** X - SOS (Start of String) */ readonly CAPITAL_X: 88; /** ^ - PM (Privacy Message) */ readonly CARET: 94; /** _ - APC (Application Program Command) */ readonly UNDERSCORE: 95; /** BEL - Bell character (OSC terminator) */ readonly BEL: 7; /** ST - String Terminator (ESC \) */ readonly BACKSLASH: 92; /** : - Colon (parameter separator in some sequences) */ readonly COLON: 58; /** ; - Semicolon (parameter separator) */ readonly SEMICOLON: 59; /** 0 - Zero digit */ readonly ZERO: 48; /** 9 - Nine digit */ readonly NINE: 57; /** ? - Question mark (private CSI sequences) */ readonly QUESTION: 63; /** @ - At symbol (CSI final byte range start) */ readonly AT: 64; /** ~ - Tilde (CSI final byte range end) */ readonly TILDE: 126; }

Character codes for ANSI escape sequences

CSI_FINAL_BYTE{ /** Minimum final byte (@ = 0x40) */ readonly MIN: 64; /** Maximum final byte (~ = 0x7e) */ readonly MAX: 126; }

CSI (Control Sequence Introducer) final byte ranges CSI sequences have the format: ESC [ <params> <final> The final byte determines the command type (m for SGR, H for cursor position, etc.)

CSI_COMMANDS{ /** SGR - Select Graphic Rendition (colors, bold, etc.) */ readonly SGR: 109; /** CUP - Cursor Position */ readonly CUP: 72; /** ED - Erase Display */ readonly ED: 74; /** EL - Erase Line */ readonly EL: 75; /** CUU - Cursor Up */ readonly CUU: 65; /** CUD - Cursor Down */ readonly CUD: 66; /** CUF - Cursor Forward */ readonly CUF: 67; /** CUB - Cursor Back */ readonly CUB: 68; }

Common CSI final bytes for reference (not used in state machine)

dist

Functions

strip
function strip(input: string, options?: StripOptions): string

Strip all ANSI escape codes from a string This is the primary function for removing ANSI codes. It handles all sequence types: CSI, OSC, DCS, and simple escapes.

ParameterTypeDescription
input
string- Input string with ANSI codes
optionsoptional
StripOptions- Optional configuration for stripping behavior
stripLines
function stripLines(lines: string[], options?: StripOptions): string[]

Strip ANSI codes from multiple lines Convenience function for processing multiple lines at once. More efficient than calling strip() on each line separately.

ParameterTypeDescription
lines
string[]- Array of strings with ANSI codes
optionsoptional
StripOptions- Optional configuration for stripping behavior
parse
function parse(input: string): ParseResult

Parse ANSI escape sequences from input string This is the core parsing function that uses a state machine to identify and extract ANSI escape sequences. It returns both the stripped text and the sequences found.

ParameterTypeDescription
input
string- Input string potentially containing ANSI codes
hasAnsi
function hasAnsi(input: string): boolean

Check if string contains ANSI escape codes This is a fast check that looks for ESC characters without doing full parsing. Useful for conditionally processing strings.

ParameterTypeDescription
input
string- Input string to check
hasAnsiAny
function hasAnsiAny(inputs: string[]): boolean

Check if any string in array contains ANSI codes Convenience function for checking multiple strings. Short-circuits on first match for efficiency.

ParameterTypeDescription
inputs
string[]- Array of strings to check
hasAnsiAll
function hasAnsiAll(inputs: string[]): boolean

Check if all strings in array contain ANSI codes

ParameterTypeDescription
inputs
string[]- Array of strings to check
isDigit
function isDigit(code: number): boolean

Helper function to check if character code is a digit (0-9)

ParameterTypeDescription
code
number- Character code to check
isCsiFinalByte
function isCsiFinalByte(code: number): boolean

Helper function to check if character code is a CSI final byte CSI final bytes are in the range @ to ~ (0x40 to 0x7e)

ParameterTypeDescription
code
number- Character code to check
isCsiParamByte
function isCsiParamByte(code: number): boolean

Helper function to check if character code is a CSI parameter byte Parameter bytes include digits (0-9), semicolon (;), and colon (:)

ParameterTypeDescription
code
number- Character code to check

Enums

AnsiType

ANSI escape sequence types based on VT100 specification

MemberValue
CSI"csi"
OSC"osc"
DCS"dcs"
Simple"simple"
Unknown"unknown"
ParserState

Parser state machine states

MemberValue
Normal0
Escape1
CSI2
OSC3
DCS4

Variables

CHAR_CODE{ /** ESC - Escape character (0x1B) */ readonly ESC: 27; /** [ - Left bracket for CSI */ readonly LEFT_BRACKET: 91; /** ] - Right bracket for OSC */ readonly RIGHT_BRACKET: 93; /** P - Device Control String */ readonly CAPITAL_P: 80; /** X - SOS (Start of String) */ readonly CAPITAL_X: 88; /** ^ - PM (Privacy Message) */ readonly CARET: 94; /** _ - APC (Application Program Command) */ readonly UNDERSCORE: 95; /** BEL - Bell character (OSC terminator) */ readonly BEL: 7; /** ST - String Terminator (ESC \) */ readonly BACKSLASH: 92; /** : - Colon (parameter separator in some sequences) */ readonly COLON: 58; /** ; - Semicolon (parameter separator) */ readonly SEMICOLON: 59; /** 0 - Zero digit */ readonly ZERO: 48; /** 9 - Nine digit */ readonly NINE: 57; /** ? - Question mark (private CSI sequences) */ readonly QUESTION: 63; /** @ - At symbol (CSI final byte range start) */ readonly AT: 64; /** ~ - Tilde (CSI final byte range end) */ readonly TILDE: 126; }

Character codes for ANSI escape sequences

CSI_FINAL_BYTE{ /** Minimum final byte (@ = 0x40) */ readonly MIN: 64; /** Maximum final byte (~ = 0x7e) */ readonly MAX: 126; }

CSI (Control Sequence Introducer) final byte ranges CSI sequences have the format: ESC [ <params> <final> The final byte determines the command type (m for SGR, H for cursor position, etc.)

CSI_COMMANDS{ /** SGR - Select Graphic Rendition (colors, bold, etc.) */ readonly SGR: 109; /** CUP - Cursor Position */ readonly CUP: 72; /** ED - Erase Display */ readonly ED: 74; /** EL - Erase Line */ readonly EL: 75; /** CUU - Cursor Up */ readonly CUU: 65; /** CUD - Cursor Down */ readonly CUD: 66; /** CUF - Cursor Forward */ readonly CUF: 67; /** CUB - Cursor Back */ readonly CUB: 68; }

Common CSI final bytes for reference (not used in state machine)

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.

LicenseMIT
Size105.88 KB
Files12
Node version>= 18
TypeScriptYes