Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed the ansi-regex code into the project to fix an import dependency issue #123

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
27 changes: 27 additions & 0 deletions lib/utils/stripAnsi.js
Original file line number Diff line number Diff line change
@@ -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;
48 changes: 48 additions & 0 deletions lib/utils/tests/stripAnsi.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"spec",
"examples",
"dist",
"tests",
"**/tests",
"coverage"
]
}