diff --git a/lib/index.js b/lib/index.js index 147bd9e..7694509 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,7 +20,7 @@ const get = require('lodash.get'); const each = require('lodash.foreach'); const fromPairs = require('lodash.frompairs'); const toPairs = require('lodash.topairs'); -const stripAnsi = require('strip-ansi'); +const stripAnsi = require('./utils/stripAnsi'); function getAssetPath(compilation, name) { return path.join(compilation.getPath(compilation.compiler.outputPath), name.split('?')[0]); diff --git a/lib/utils/stripAnsi.js b/lib/utils/stripAnsi.js new file mode 100644 index 0000000..a301b8c --- /dev/null +++ b/lib/utils/stripAnsi.js @@ -0,0 +1,27 @@ +/* + * This code is based on the strip-ansi library by Chalk. + * Source: https://github.com/chalk/strip-ansi + */ + +function ansiRegex({ onlyFirst = false } = {}) { + const pattern = [ + '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', + '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))', + ].join('|'); + + return new RegExp(pattern, onlyFirst ? undefined : 'g'); +} + +function stripAnsi(string) { + if (typeof string !== 'string') { + throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``); + } + + // Even though the regex is global, we don't need to reset the `.lastIndex` + // because unlike `.exec()` and `.test()`, `.replace()` does it automatically + // and doing it manually has a performance penalty. + const regex = ansiRegex(); + return string.replace(regex, ''); +} + +module.exports = stripAnsi; diff --git a/lib/utils/tests/stripAnsi.test.js b/lib/utils/tests/stripAnsi.test.js new file mode 100644 index 0000000..288aa3a --- /dev/null +++ b/lib/utils/tests/stripAnsi.test.js @@ -0,0 +1,48 @@ +/* eslint-env jest */ +'use strict'; + +const stripAnsi = require('../stripAnsi'); + +describe('stripAnsi tests', () => { + it('It should handle an empty string', () => { + const output = stripAnsi(''); + expect(output).toBe(''); + }); + + it('It should return the same string if there are no ANSI codes', () => { + const input = 'Hello'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should remove ANSI codes from a string', () => { + const input = '\u001B[4mHello\u001B[0m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should strip color from string', () => { + const input = '\u001B[0m\u001B[4m\u001B[42m\u001B[31mHe\u001B[39m\u001B[49m\u001B[24mllo\u001B[0m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should strip color from ls command', () => { + const input = + '\u001B[00m\u001B[01;34mHello\u001B[00m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should reset;setfg;setbg;italics;strike;underline sequence from string', () => { + const input = '\u001B[0;33;49;3;9;4mHello\u001B[0m'; + const output = stripAnsi(input); + expect(output).toBe('Hello'); + }); + + it('It should strip link from terminal link', () => { + const input = '\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'; + const output = stripAnsi(input); + expect(output).toBe('click'); + }); +}); diff --git a/package.json b/package.json index 2def6c1..1f82a1f 100644 --- a/package.json +++ b/package.json @@ -43,8 +43,7 @@ "lodash.foreach": "^4.5.0", "lodash.frompairs": "^4.0.1", "lodash.get": "^4.4.2", - "lodash.topairs": "^4.3.0", - "strip-ansi": "^6.0.1" + "lodash.topairs": "^4.3.0" }, "devDependencies": { "@types/babel__traverse": "7.0.6", diff --git a/tsconfig.json b/tsconfig.json index ca34c20..e5d289c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -33,7 +33,7 @@ "spec", "examples", "dist", - "tests", + "**/tests", "coverage" ] }