-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
119 lines (106 loc) · 2.48 KB
/
index.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* typ3r.js
*/
// Substitutes
var chars = {
'o': ['0'],
'e': ['3'],
'i': ['1'],
'a': ['@']
}
// this is matched using regex (case-insensitive)
// the following match anywhere in the text
var words = {
'tha': 'da',
'wha': 'wa',
'this': 'dis'
}
// the following should match at word end (or they should be complete words)
var endWords = {
'ith': 'id',
'([erk])s': '$1zz'
}
// the following should match whole words
var completeWords = {
'is': 'izz',
'my': 'mah',
'you': 'u',
'are': 'r',
'the': 'da',
'love': 'luv',
'brother': 'broda',
'sister': 'sista',
'for' : '4'
}
// join to words
for (var key in endWords){
words[key + '(?=[^a-z])'] = endWords[key]
}
for (var key in completeWords){
words['([^a-z]|^)' + key + '(?=[^a-z])'] = '$1' + completeWords[key]
}
// chars substitution
function convertChars(text){
var text = String(text)
var probab = 0.6
for (var key in words) {
reg = new RegExp(key, "gi")
var bracketCount = (key.match(/\(/g) || []).length
if (reg.test(text)){
// replace
text = text.replace(reg, (match, p1, p2) => {
chance = Math.random()
if (chance <= probab){
if (bracketCount < 2){
return words[key]
} else if (bracketCount == 2){ // ([ae])s: $1zz, complete words
return words[key].replace("$1", p1)
}
} else {
return match
}
})
// end replace
}
}
return text
}
function loadSubstitutes(orig, newCh){
list = [newCh]
orig = orig.toLowerCase()
if (orig in chars) {
Array.prototype.push.apply(list, chars[orig])
}
return list
}
// single char replacement
function convertSingleChar(text){
var upProbab = 0.5
var downProbab = 0.7
len = text.length
var newText = ""
for (var i=0; i < len; i++){
charCode = text[i].charCodeAt()
chance = Math.random()
if (charCode >= 65 && charCode <= 90 && chance <= downProbab){
subs = loadSubstitutes(text[i], String.fromCharCode(charCode + 32))
newText += subs[Math.floor(Math.random() * subs.length)] // random from list
} else if (charCode >= 97 && charCode <= 122 && chance <= upProbab){
subs = loadSubstitutes(text[i], String.fromCharCode(charCode - 32))
newText += subs[Math.floor(Math.random() * subs.length)]
} else {
newText += text[i]
}
}
return newText
}
// main typ3r converter function
function typ3r_convert(val){
newVal = convertChars(val)
newVal = convertSingleChar(newVal)
return newVal
}
// Export
if (typeof module !== 'undefined' && module.exports && typeof exports !== 'undefined'){
module.exports = typ3r_convert
}