🔢 Encode ANSI Escape sequences to Unicode Literal strings, Eg: \u001b to \\u001b
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
index.js
index.test.js
package.json
unenki-logo.png

README.md

Beetle holding Unicode character

Unenki

🔢 Encode ANSI Escape sequences to Unicode Literal strings: \u001b > \\u001b

Build Status Coverage Status Npm Version XO code style

Why

Logging colored text to the console requires ANSI escape sequences. These ANSI escape sequences include unicode characters like \u001b.

When testing software it can become hard to grok the difference between the expected and the actual result when the ANSI escape sequences are used. To remedy this, Unenki encodes ANSI escape sequences to lower-case unicode literals, while keeping certain ASCII characters.

Encode

Encode unicode ANSI escape characters to a console friendly string:

const unenki = require('unenki')
const chalk = require('chalk')

// Blue text
const ansiEscapedStr = '\u001b[34m HELLO_BLUE_WORLD! \u001b[39m'

const encoded = unenki.encode(ansiEscapedStr)
// '\\u001b[34m HELLO_BLUE_WORLD! \\u001b[39m'

Strip

Strips ANSI escape sequences from unencoded ansi-escaped strings:

const unenki = require('unenki')
const chalk = require('chalk')

const ansiEscapedStr = chalk.green('Test')

const result = unenki.strip(ansiEscapedStr)
// Result = 'Test'

Strip Encoded

Strips Unicode literals from an encoded string:

const unenki = require('unenki')

const ansiEscapedStr = chalk.green('Test')
const encoded = unenki.encode(ansiEscapedStr)

const result = unenki.stripEncoded(encoded)
// Result = 'Test'

Keep

Do not encode certain characters:

const ansiEscapedStr = chalk.green('"Test"')

const opts = {
    keep: ['"']
}

const result = unenki.encode(ansiEscapedStr, opts)
// Result = '\\u001b[32m"Test"\\u001b[39m'

Force

Force certain characters to encode to different strings:

const ansiEscapedStr = chalk.green('a\nb')

const opts = {
    force: {
        '\n': '\\n'
    }
}

const result = unenki.encode(ansiEscapedStr, opts)
// Result = 'a\\n\b'

Installation

yarn add unenki

Credits

Thanks to Gustavo Ribeiro Vieira from the Noun Project for the beetle vector used in the Unenki logo.