Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edgar0011/703491cf665dc01a1964a904cb8755f0 to your computer and use it in GitHub Desktop.
Save edgar0011/703491cf665dc01a1964a904cb8755f0 to your computer and use it in GitHub Desktop.
function dyslectize(text, wordTrehslod, letterTreshhold) {
wordTrehslod = wordTrehslod || 0.3;
letterTreshhold = letterTreshhold || 0.5;
var words = text.split(" ");
function isVowel(letter) {
var vowels = "aeiouy";
return vowels.indexOf(letter.toLowerCase()) > -1;
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
words.forEach(function(word, index){
if (Math.random() > wordTrehslod) {
var letters = word.split("");
var firstUpperCaseLetter = letters[0] === letters[0].toUpperCase();
var swappable = [];
letters.forEach(function(letter, index){
if (letter.match(/[a-z]/i) && !isVowel(letter) && Math.random() > letterTreshhold ) {
swappable.push(index);
}
});
var newSwappable = shuffleArray(swappable.concat());
var ll = letters.concat();
newSwappable.forEach(function(item, index){
letters.splice(swappable[index], 1, ll[item]);
});
words[index] = letters.join("").toLowerCase();
if (firstUpperCaseLetter) {
words[index] = words[index].substr(0, 1).toUpperCase() + words[index].substr(1);
}
}
});
return words.join(" ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment