Created
November 16, 2017 15:34
-
-
Save plugn/514a63143a7bdf77276bf47352ebc593 to your computer and use it in GitHub Desktop.
leet.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var leet = { | |
/** | |
* Map of conversions. | |
* | |
* @var object | |
*/ | |
characterMap: { | |
'a': '4', | |
'b': '8', | |
'e': '3', | |
'g': '6', | |
'l': '1', | |
'o': '0', | |
's': '5', | |
't': '7', | |
'æ': '43', | |
'ø': '03', | |
'å': '44' | |
}, | |
/** | |
* Convert a string to 1337 based on the character map. | |
* | |
* @param string string Regular ol' text to convert | |
* @return string | |
*/ | |
convert: function (string) { | |
var letter; | |
string = string || ''; | |
string = string.replace(/cks/g, 'x'); | |
for (letter in leet.characterMap) { | |
if (leet.characterMap.hasOwnProperty(letter)) { | |
string = string.replace(new RegExp(letter, 'g'), leet.characterMap[letter]); | |
} | |
} | |
return string.toUpperCase(); | |
}, | |
/** | |
* Test character to see if it's a vovel or special (or neither). | |
* | |
* @param string character Character to test | |
* @return mixed | |
*/ | |
test: function (character) { | |
var vowel = /^[4I30U]$/i, | |
special = /^[!?.,\-]$/i, | |
type = false; | |
if (vowel.test(character)) { | |
type = 'vowel'; | |
} else if (special.test(character)) { | |
type = 'special'; | |
} | |
return type; | |
}, | |
/** | |
* Converts the string to 1337 along with special rules. | |
* | |
* @param string string Regular ol' text to convert | |
* @return string | |
*/ | |
output: function (string) { | |
string = leet.convert(string); | |
if ('' === string) { | |
return string; | |
} | |
var last = string[string.length - 1], | |
type = leet.test(last), | |
result; | |
if (type === 'special') { | |
result = string.substr(0, string.length - 1) + 'ZORZ' + last; | |
} else if (type === 'vowel') { | |
result = string + 'XOR'; | |
} else { | |
result = string + 'ZORZ'; | |
} | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment