Skip to content

Commit

Permalink
feat: add toLeetSpeak (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
imevanc authored Oct 11, 2024
1 parent f3d86ba commit 0ed2be3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

---

Expand Down Expand Up @@ -55,7 +56,8 @@ import {
toNoCase,
toSnakeCase,
toPathCase,
toCobolCase
toCobolCase,
toLeetSpeak
} from 'casenator';

// Camel Case
Expand Down Expand Up @@ -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
```

---
Expand Down
48 changes: 48 additions & 0 deletions __test__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
toConstantCase,
toDotCase,
toKebabCase,
toLeetSpeak,
toLowerCase,
toNoCase,
toPascalCase,
Expand Down Expand Up @@ -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("!@#$");
});
});
26 changes: 25 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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("");
};
32 changes: 32 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
};

0 comments on commit 0ed2be3

Please sign in to comment.