or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-strip-ansi

Strip ANSI escape codes from a string

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/strip-ansi@7.1.x

To install, run

npx @tessl/cli install tessl/npm-strip-ansi@7.1.0

index.mddocs/

Strip ANSI

Strip ANSI provides a lightweight utility for removing ANSI escape codes from strings. It offers a simple, single-function API that accepts any string and returns a clean version with all ANSI formatting codes (colors, styles, cursor movements, etc.) stripped out, making it essential for processing terminal output and colored text in Node.js applications.

Package Information

  • Package Name: strip-ansi
  • Package Type: npm
  • Language: JavaScript (ES modules) with TypeScript definitions
  • Installation: npm install strip-ansi

Core Imports

import stripAnsi from 'strip-ansi';

Basic Usage

import stripAnsi from 'strip-ansi';

// Remove ANSI codes from colored text
stripAnsi('\u001B[4mUnicorn\u001B[0m');
//=> 'Unicorn'

// Remove ANSI codes from terminal links
stripAnsi('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
//=> 'Click'

// Process complex terminal output
const terminalOutput = '\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m';
const cleanText = stripAnsi(terminalOutput);
//=> 'foofoo'

Capabilities

Strip ANSI Function

Removes all ANSI escape codes from a string while preserving the original text content.

/**
 * Strip ANSI escape codes from a string
 * @param string - The input string containing ANSI escape codes to be stripped
 * @returns The input string with all ANSI escape codes removed
 * @throws TypeError when input is not a string type
 */
export default function stripAnsi(string: string): string;

Parameters:

  • string (string, required): The input string containing ANSI escape codes to be stripped

Returns:

  • string: The input string with all ANSI escape codes removed

Error Handling:

  • TypeError: Thrown when input is not a string type
  • Error Message Format: Expected a \string`, got `${typeof input}``

Supported ANSI Codes:

  • Color codes (foreground and background)
  • Text styling (bold, italic, underline, etc.)
  • Cursor movement and positioning codes
  • Terminal link codes (\u001B]8;;\u0007)
  • Reset and clearing codes
  • Extended color codes (256-color and RGB)

Usage Examples:

import stripAnsi from 'strip-ansi';

// Basic color removal
stripAnsi('\u001B[31mRed text\u001B[0m');
//=> 'Red text'

// Complex styling removal
stripAnsi('\u001B[0;33;49;3;9;4mbar\u001B[0m');
//=> 'bar'

// Terminal link removal
stripAnsi('\u001B]8;;https://example.com\u0007Link text\u001B]8;;\u0007');
//=> 'Link text'

// Input validation
try {
  stripAnsi(123);
} catch (error) {
  console.log(error.message);
  //=> 'Expected a `string`, got `number`'
}

// Common use cases
const logOutput = '\u001B[32m[INFO]\u001B[0m \u001B[1mOperation completed\u001B[0m';
const cleanLog = stripAnsi(logOutput);
//=> '[INFO] Operation completed'

// Processing command line output
const cliOutput = '\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfilename.js\u001B[0m';
const filename = stripAnsi(cliOutput);
//=> 'filename.js'

Implementation Details:

  • Uses the ansi-regex library for comprehensive ANSI code pattern matching
  • Globally replaces all matched ANSI codes in a single pass
  • Optimized for performance with automatic regex lastIndex management
  • Handles all standard ANSI escape sequence formats
  • Type-safe with built-in input validation