From 0ed2be36c64c7373f35eba9b0f2784672658ee90 Mon Sep 17 00:00:00 2001 From: Evan C <96417438+imevanc@users.noreply.github.com> Date: Sat, 12 Oct 2024 00:16:03 +0100 Subject: [PATCH] feat: add toLeetSpeak (#25) --- README.md | 7 +++++- __test__/index.spec.js | 48 ++++++++++++++++++++++++++++++++++++++++++ dist/index.js | 26 ++++++++++++++++++++++- src/index.js | 32 ++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ea3091..6004278 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ - 🐬 **SnakeCase** - Format string in `snake_case`. - 🦁 **PathCase** - Get your string into `/pathCase`. - 🦧 **CobolCase** - Format string in `COBOL-CASE`. +- 🐫 **LeetSpeak** - Make everything `1337 5p34k` --- @@ -55,7 +56,8 @@ import { toNoCase, toSnakeCase, toPathCase, - toCobolCase + toCobolCase, + toLeetSpeak } from 'casenator'; // Camel Case @@ -96,6 +98,9 @@ console.log(toPathCase('hello world')); // '/helloWorld' // Cobol Case console.log(toCobolCase('hello world')); // 'HELLO-WORLD' + +// Leet Speak +console.log(toLeetSpeak('hello world')) // h3110 w021d ``` --- diff --git a/__test__/index.spec.js b/__test__/index.spec.js index 54e9412..b1c35f0 100644 --- a/__test__/index.spec.js +++ b/__test__/index.spec.js @@ -7,6 +7,7 @@ import { toConstantCase, toDotCase, toKebabCase, + toLeetSpeak, toLowerCase, toNoCase, toPascalCase, @@ -530,3 +531,50 @@ describe("COBOL-CASE", () => { expect(() => toCobolCase({})).toThrow("Input must be a string"); }); }); + +describe("toLeetSpeak", () => { + // Test for basic leet conversions + test("should convert lowercase letters to leet speak", () => { + expect(toLeetSpeak("leet")).toBe("1337"); + expect(toLeetSpeak("gamer")).toBe("64m32"); + expect(toLeetSpeak("robot")).toBe("20807"); + }); + + // Test for handling uppercase letters + test("should convert uppercase letters to leet speak and preserve their case", () => { + expect(toLeetSpeak("LEET")).toBe("1337"); + expect(toLeetSpeak("GAMER")).toBe("64M32"); + expect(toLeetSpeak("RoBoT")).toBe("20807"); + }); + + // Test for non-alphabetic characters (should be preserved) + test("should preserve non-alphabetic characters like spaces, punctuation, and numbers", () => { + expect(toLeetSpeak("hello world!")).toBe("h3110 w021d!"); // Corrected expected value + expect(toLeetSpeak("123!")).toBe("123!"); + expect(toLeetSpeak("leet speak 2.0")).toBe("1337 5p34k 2.0"); + }); + + // Test for mixed alphanumeric strings + test("should handle mixed alphanumeric and leet mappings properly", () => { + expect(toLeetSpeak("a1b2c3")).toBe("4182c3"); // Corrected expected value + }); + + // Test for empty string input + test("should return an empty string if input is an empty string", () => { + expect(toLeetSpeak("")).toBe(""); + }); + + // Test for non-string input (should throw a TypeError) + test("should throw a TypeError when input is not a string", () => { + expect(() => toLeetSpeak(12345)).toThrow(TypeError); + expect(() => toLeetSpeak(null)).toThrow(TypeError); + expect(() => toLeetSpeak(undefined)).toThrow(TypeError); + expect(() => toLeetSpeak({})).toThrow(TypeError); + }); + + // Test for strings with no leet mappings (should return the same string) + test("should return the original string if no leet mappings are present", () => { + expect(toLeetSpeak("xyz")).toBe("xyz"); + expect(toLeetSpeak("!@#$")).toBe("!@#$"); + }); +}); diff --git a/dist/index.js b/dist/index.js index d1d25f8..8cbebff 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.toUpperCase = exports.toSnakeCase = exports.toPathCase = exports.toPascalCase = exports.toNoCase = exports.toLowerCase = exports.toKebabCase = exports.toDotCase = exports.toConstantCase = exports.toCobolCase = exports.toCapitalCase = exports.toCamelCase = exports.substring = exports.reverseString = void 0; +exports.toUpperCase = exports.toSnakeCase = exports.toPathCase = exports.toPascalCase = exports.toNoCase = exports.toLowerCase = exports.toLeetSpeak = exports.toKebabCase = exports.toDotCase = exports.toConstantCase = exports.toCobolCase = exports.toCapitalCase = exports.toCamelCase = exports.substring = exports.reverseString = void 0; /** * Converts a string to camelCase. */ @@ -177,4 +177,28 @@ var toCobolCase = exports.toCobolCase = function toCobolCase(input) { } // Convert to uppercase ).filter(Boolean) // Remove any empty segments from the split .join("-"); // Join all segments with hyphens +}; +var toLeetSpeak = exports.toLeetSpeak = function toLeetSpeak(input) { + if (typeof input !== "string") throw new TypeError("Input must be a string"); + var leetMap = { + a: "4", + e: "3", + i: "1", + o: "0", + s: "5", + t: "7", + l: "1", + g: "6", + b: "8", + r: "2" + }; + return input.split("").map(function (_char) { + var lowerChar = _char.toLowerCase(); // Convert char to lowercase for consistent mapping + var mappedChar = leetMap[lowerChar]; // Map to leetspeak if available + if (mappedChar) { + return _char === _char.toUpperCase() ? mappedChar.toUpperCase() : mappedChar; + } else { + return _char; // Preserve original non-leet chars (spaces, punctuation, etc.) + } + }).join(""); }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 98feb5b..5828542 100644 --- a/src/index.js +++ b/src/index.js @@ -177,3 +177,35 @@ export const toCobolCase = (input) => { .filter(Boolean) // Remove any empty segments from the split .join("-"); // Join all segments with hyphens }; + +export const toLeetSpeak = (input) => { + if (typeof input !== "string") throw new TypeError("Input must be a string"); + + const leetMap = { + a: "4", + e: "3", + i: "1", + o: "0", + s: "5", + t: "7", + l: "1", + g: "6", + b: "8", + r: "2", + }; + + return input + .split("") + .map((char) => { + const lowerChar = char.toLowerCase(); // Convert char to lowercase for consistent mapping + const mappedChar = leetMap[lowerChar]; // Map to leetspeak if available + if (mappedChar) { + return char === char.toUpperCase() + ? mappedChar.toUpperCase() + : mappedChar; + } else { + return char; // Preserve original non-leet chars (spaces, punctuation, etc.) + } + }) + .join(""); +};