Skip to content

Instantly share code, notes, and snippets.

@kingcc

kingcc/leetString.js

Last active Jan 5, 2021
Embed
What would you like to do?
Replace a string with a `leet` style string
/**
* Replace a string with a `leet` style string
*/
function stringToNumber(str) {
const dir = {
o: 0,
O: 0,
i: 1,
I: 1,
l: 1,
z: 2,
Z: 2,
E: 3,
A: 4,
s: 5,
S: 5,
b: 6,
G: 6,
T: 7,
B: 8,
g: 9,
}
const keySet = new Set(Object.keys(dir))
const mapLetterToNumber = letter => keySet.has(letter) ? dir[letter] : letter
return str.split('').map(mapLetterToNumber).join('')
}
// stringToNumber('Youth never say would')
// > "Y0uth never 5ay w0u1d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment