Add remove ANSI escape codes operation (#2143)

Co-authored-by: GCHQDeveloper581 <63102987+GCHQDeveloper581@users.noreply.github.com> (additional test case)
This commit is contained in:
A Normal Ladd 2026-06-03 03:57:13 -06:00 committed by GitHub
parent 89b5c7dee3
commit 72ca1aeec7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 105 additions and 0 deletions

View File

@ -304,6 +304,7 @@
"Diff",
"Remove whitespace",
"Remove null bytes",
"Remove ANSI Escape Codes",
"To Upper case",
"To Lower case",
"Swap case",

View File

@ -0,0 +1,41 @@
/**
* @author Louis-Ladd [lewisharshman1@gmail.com]
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
/**
* Remove ANSI Escape Codes operation
*/
class RemoveANSIEscapeCodes extends Operation {
/**
* RemoveANSIEscapeCodes constructor
*/
constructor() {
super();
this.name = "Remove ANSI Escape Codes";
this.module = "Default";
this.description = "Removes ANSI Escape Codes.";
this.infoURL = "https://wikipedia.org/wiki/ANSI_escape_code";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const ansiRegex = /\x1B\[[0-?]*[ -/]*[@-~]/g;
return input.replace(ansiRegex, "");
}
}
export default RemoveANSIEscapeCodes;

View File

@ -148,6 +148,7 @@ import "./tests/Rabbit.mjs";
import "./tests/RAKE.mjs";
import "./tests/Regex.mjs";
import "./tests/Register.mjs";
import "./tests/RemoveANSIEscapeCodes.mjs";
import "./tests/RegularExpression.mjs";
import "./tests/RenderMarkdown.mjs";
import "./tests/RisonEncodeDecode.mjs";

View File

@ -0,0 +1,62 @@
/**
* @author Louis-Ladd [lewisharshman1@gmail.com]
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
"name": "Remove ANSI Escape Codes: text using x1b escape code",
"input": "\x1b[31mHello, \x1b[31mWorld!",
"expectedOutput": "Hello, World!",
"recipeConfig": [
{
"op": "Remove ANSI Escape Codes",
"args": [],
},
],
},
{
"name": "Remove ANSI Escape Codes: text with incomplete codes",
"input": "\x1b[31 Hello, World!",
"expectedOutput": "ello, World!",
"recipeConfig": [
{
"op": "Remove ANSI Escape Codes",
"args": [],
},
],
},
{
"name": "Remove ANSI Escape Codes: cursor commands and clear screen",
"input": "\x1b[2J\x1b[H\x1b[3BHello, World!",
"expectedOutput": "Hello, World!",
"recipeConfig": [
{
"op": "Remove ANSI Escape Codes",
"args": [],
},
],
},
{
"name": "Remove ANSI Escape Codes: text containing javascript escape representation of ansi escape codes",
// input/output expressed in hex to avoid accidental interpretation of Javascript escapes and to make the test case explicit
"input": "5c 30 33 33 5b 33 32 3b 31 3b 33 3b 34 3b 39 6d 48 65 6c 6c 6f 2c 20 5c 30 33 33 5b 33 32 3b 31 3b 33 3b 34 3b 39 6d 57 6f 72 6c 64 21",
"expectedOutput": "5c 30 33 33 5b 33 32 3b 31 3b 33 3b 34 3b 39 6d 48 65 6c 6c 6f 2c 20 5c 30 33 33 5b 33 32 3b 31 3b 33 3b 34 3b 39 6d 57 6f 72 6c 64 21",
"recipeConfig": [
{
"op": "From Hex",
"args": ["Auto"]
},
{
"op": "Remove ANSI Escape Codes",
"args": []
},
{
"op": "To Hex",
"args": ["Space", 0]
}
],
},
]);