Last active
June 2, 2016 14:07
-
-
Save edgar0011/703491cf665dc01a1964a904cb8755f0 to your computer and use it in GitHub Desktop.
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
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