1.0.1 • Published 6 years ago

ansi-encode v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

ANSI Encode

🔢 Convert ANSI escape sequences to regular strings.

Build Status Coverage Status Npm Version XO code style

Usage

const chalk = require('chalk')

const ansiEscapeStr = chalk.green('Hello, world!')
// '\u001b[32mHello, world!\u001b[39m'

const result = ansiEncode(ansiEscapeStr)
// '\\u001b[32mHello, world!\\u001b[39m'

Installation

yarn add ansi-encode

About

This simple Node module takes a JavaScript string containing ANSI escape sequences and converts them into regular JavaScript strings. It is a bit like using escape(string), where escape() turns individual characters into their respective codes; wheresansiEncode() converts ANSI escape sequences back into plain strings.

This ansi-encode exists because I ran into problems creating tests color terminal output in other Node modules. When a test failed in Mocha, I was able to see the ANSI escape codes and copy the correct string into the test. When I started using AVA - The Futuristic Test Runner for testing code, I discovered the escape sequences were not printed. This gave me no way to test color console output.