diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 2602c3c09e..865ade95a4 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -274,7 +274,8 @@ "Unicode Text Format", "Remove Diacritics", "Unescape Unicode Characters", - "Convert to NATO alphabet" + "Convert to NATO alphabet", + "Convert Leet Speak" ] }, { diff --git a/src/core/operations/ConvertLeetSpeak.mjs b/src/core/operations/ConvertLeetSpeak.mjs new file mode 100644 index 0000000000..54fa62f6e7 --- /dev/null +++ b/src/core/operations/ConvertLeetSpeak.mjs @@ -0,0 +1,113 @@ +/** + * @author bartblaze [] + * @copyright Crown Copyright 2025 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Convert Leet Speak operation + */ +class ConvertLeetSpeak extends Operation { + /** + * ConvertLeetSpeak constructor + */ + constructor() { + super(); + + this.name = "Convert Leet Speak"; + this.module = "Default"; + this.description = "Converts to and from Leet Speak"; + this.infoURL = "https://wikipedia.org/wiki/Leet"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + name: "Direction", + type: "option", + value: ["To Leet Speak", "From Leet Speak"], + defaultIndex: 0 + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const direction = args[0]; + if (direction === "To Leet Speak") { + return input.replace(/[abcdefghijklmnopqrstuvwxyz]/gi, char => { + return toLeetMap[char.toLowerCase()] || char; + }); + } else if (direction === "From Leet Speak") { + return input.replace(/[48cd3f6h1jklmn0pqr57uvwxyz]/g, char => { + return fromLeetMap[char] || char; + }); + } + } +} + +const toLeetMap = { + "a": "4", + "b": "b", + "c": "c", + "d": "d", + "e": "3", + "f": "f", + "g": "g", + "h": "h", + "i": "1", + "j": "j", + "k": "k", + "l": "l", + "m": "m", + "n": "n", + "o": "0", + "p": "p", + "q": "q", + "r": "r", + "s": "5", + "t": "7", + "u": "u", + "v": "v", + "w": "w", + "x": "x", + "y": "y", + "z": "z" +}; + +const fromLeetMap = { + "4": "a", + "b": "b", + "c": "c", + "d": "d", + "3": "e", + "f": "f", + "g": "g", + "h": "h", + "1": "i", + "j": "j", + "k": "k", + "l": "l", + "m": "m", + "n": "n", + "0": "o", + "p": "p", + "q": "q", + "r": "r", + "5": "s", + "7": "t", + "u": "u", + "v": "v", + "w": "w", + "x": "x", + "y": "y", + "z": "z" +}; + +export default ConvertLeetSpeak; + diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index ca7509fd6f..919ad9757c 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -54,6 +54,7 @@ import "./tests/Comment.mjs"; import "./tests/Compress.mjs"; import "./tests/ConditionalJump.mjs"; import "./tests/ConvertCoordinateFormat.mjs"; +import "./tests/ConvertLeetSpeak.mjs"; import "./tests/ConvertToNATOAlphabet.mjs"; import "./tests/Crypt.mjs"; import "./tests/CSV.mjs"; diff --git a/tests/operations/tests/ConvertLeetSpeak.mjs b/tests/operations/tests/ConvertLeetSpeak.mjs new file mode 100644 index 0000000000..198f7e57d2 --- /dev/null +++ b/tests/operations/tests/ConvertLeetSpeak.mjs @@ -0,0 +1,33 @@ +/** + * @author bartblaze [] + * @copyright Crown Copyright 2025 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Convert to Leet Speak: basic text", + input: "leet", + expectedOutput: "l337", + recipeConfig: [ + { + op: "Convert Leet Speak", + args: ["To Leet Speak"] + } + ] + }, + { + name: "Convert from Leet Speak: basic leet", + input: "l337", + expectedOutput: "leet", + recipeConfig: [ + { + op: "Convert Leet Speak", + args: ["From Leet Speak"] + } + ] + } +]); +