Skip to content

Instantly share code, notes, and snippets.

@zettamax
Created October 22, 2017 23:16
Show Gist options
  • Save zettamax/6fa6233ff2ce8df435703ff6ee6e522b to your computer and use it in GitHub Desktop.
Save zettamax/6fa6233ff2ce8df435703ff6ee6e522b to your computer and use it in GitHub Desktop.
Javrscirpt drusnk tetx transloaetr
function drunk(text) {
function modifyWord(word, i, a) {
var wordArray = word.split('');
var isPunct = punct.test(word);
if (!isPunct) {
if (chance(50)) {
var letter = letters[Math.floor(letters.length * rand())];
var position = Math.floor(wordArray.length * rand());
wordArray.splice(position, +chance(10), letter);
}
if (wordArray.length > 2 && chance(55 + wordArray.length * 2)) {
var start = Math.floor((wordArray.length - 4) * rand()) + 2;
var length = Math.floor(4 * rand()) + 2;
var end = start + length
end = end > wordArray.length ? wordArray.length : end;
var wordPart = wordArray.slice(start, end);
wordPart = wordPart.sort(function () {
return 0.5 - rand()
});
var upperIndex = Math.floor(wordPart.length * rand());
if (chance(20)) {
wordPart[upperIndex] = wordPart[upperIndex].toUpperCase();
}
wordArray.splice(start, end - start, wordPart.join(''));
}
}
if (!isPunct) {
wordArray.push(chance(70) ? ' ' : (chance(50) ? ' ' : ','));
} else if (isPunct || punct.test(a[i + 1])) {
wordArray.push(chance(50) ? ' ' : '-');
}
words[i] = wordArray.join('');
}
function chance(number) {
window.crypto.getRandomValues(rArray);
return (rArray[0] / 255 * 100) < number;
}
function rand() {
window.crypto.getRandomValues(rArray);
return (rArray / 256);
}
var words = [];
var rArray = new Uint8Array(1);
var punct = /^[!@#$%^&*()_+=|\\\/?.,<>:;"'-]+$/;
var letters = text.toLowerCase()
.replace(/[\s]/g, '').split('')
.filter(function (l, x, a) {
return a.indexOf(l) === x && !punct.test(l);
});
text.split(/\s+/)
.forEach(modifyWord);
return words.join('');
}
console.log(drunk('Javascript drunk text translator'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment