-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypoglycemia.js
More file actions
50 lines (38 loc) · 1.2 KB
/
typoglycemia.js
File metadata and controls
50 lines (38 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
jQuery = typeof jQuery === 'undefined' ? $ : jQuery;
jQuery.typoglycemia = function(text) {
var output = '';
text.split(' ').forEach(
function(word) {
var length = word.length;
if (length > 3) {
var first = word.substr(0, 1);
var last = word.substr(-1, 1);
var middleLength = length - 2;
var middle = word.substr(1, middleLength);
var middleArray = middle.split('');
for(var i = middleLength - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = middleArray[i];
middleArray[i] = middleArray[j];
middleArray[j] = tmp;
}
middle = middleArray.join('');
word = first + middle + last;
}
output += ' ' + word;
}
);
return output.substr(1);
};
jQuery.fn.typoglycemia = function() {
jQuery(this).get().forEach(
function(element) {
if (typeof element.innerText !== 'undefined' && element.innerText !== '') {
element.innerText = jQuery.typoglycemia(element.innerText);
}
if (typeof element.value !== 'undefined' && element.value !== '') {
element.value = jQuery.typoglycemia(element.value);
}
}
);
};