ansi
PoolModern, zero-dependency ANSI escape code parser and stripper - beats strip-ansi by 10%+
dist
Functions
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.
| Parameter | Type | Description |
|---|---|---|
input | string | - Input string with ANSI codes |
optionsoptional | StripOptions | - Optional configuration for stripping behavior |
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.
| Parameter | Type | Description |
|---|---|---|
lines | string[] | - Array of strings with ANSI codes |
optionsoptional | StripOptions | - Optional configuration for stripping behavior |
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.
| Parameter | Type | Description |
|---|---|---|
input | string | - Input string potentially containing ANSI codes |
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.
| Parameter | Type | Description |
|---|---|---|
input | string | - Input string to check |
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.
| Parameter | Type | Description |
|---|---|---|
inputs | string[] | - Array of strings to check |
function hasAnsiAll(inputs: string[]): boolean
Check if all strings in array contain ANSI codes
| Parameter | Type | Description |
|---|---|---|
inputs | string[] | - Array of strings to check |
function isDigit(code: number): boolean
Helper function to check if character code is a digit (0-9)
| Parameter | Type | Description |
|---|---|---|
code | number | - Character code to check |
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)
| Parameter | Type | Description |
|---|---|---|
code | number | - Character code to check |
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 (:)
| Parameter | Type | Description |
|---|---|---|
code | number | - Character code to check |
Enums
ANSI escape sequence types based on VT100 specification
| Member | Value |
|---|---|
CSI | "csi" |
OSC | "osc" |
DCS | "dcs" |
Simple | "simple" |
Unknown | "unknown" |
Parser state machine states
| Member | Value |
|---|---|
Normal | 0 |
Escape | 1 |
CSI | 2 |
OSC | 3 |
DCS | 4 |
Variables
{
/** 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
{
/** 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.)
{
/** 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
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.
| Parameter | Type | Description |
|---|---|---|
input | string | - Input string with ANSI codes |
optionsoptional | StripOptions | - Optional configuration for stripping behavior |
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.
| Parameter | Type | Description |
|---|---|---|
lines | string[] | - Array of strings with ANSI codes |
optionsoptional | StripOptions | - Optional configuration for stripping behavior |
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.
| Parameter | Type | Description |
|---|---|---|
input | string | - Input string potentially containing ANSI codes |
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.
| Parameter | Type | Description |
|---|---|---|
input | string | - Input string to check |
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.
| Parameter | Type | Description |
|---|---|---|
inputs | string[] | - Array of strings to check |
function hasAnsiAll(inputs: string[]): boolean
Check if all strings in array contain ANSI codes
| Parameter | Type | Description |
|---|---|---|
inputs | string[] | - Array of strings to check |
function isDigit(code: number): boolean
Helper function to check if character code is a digit (0-9)
| Parameter | Type | Description |
|---|---|---|
code | number | - Character code to check |
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)
| Parameter | Type | Description |
|---|---|---|
code | number | - Character code to check |
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 (:)
| Parameter | Type | Description |
|---|---|---|
code | number | - Character code to check |
Enums
ANSI escape sequence types based on VT100 specification
| Member | Value |
|---|---|
CSI | "csi" |
OSC | "osc" |
DCS | "dcs" |
Simple | "simple" |
Unknown | "unknown" |
Parser state machine states
| Member | Value |
|---|---|
Normal | 0 |
Escape | 1 |
CSI | 2 |
OSC | 3 |
DCS | 4 |
Variables
{
/** 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
{
/** 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.)
{
/** 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)
Taxes calculated at checkout based on your location.