-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
368 lines (310 loc) · 8.9 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
;
(function () {
/**
* The Censoring object constructor.
*
* @constructor
*/
function Censoring() {
/**
* The string to replaces found matches with. Defaults to ***
*
* @type {String|function(match: string): string}
*/
this.replacementString = '***';
/**
* The color used for highlighting
*
* @type {string}
*/
this.highlightColor = 'F2B8B8';
/**
* Holds the currently matched text.
*
* @type {{replace: string, hasMatches: boolean}}
*/
this.currentMatch = {
replace: '',
hasMatches: false,
matches: []
};
/**
* The available patterns. These are as follows:
* [name] [description]
* - long_number ; Matches long, consecutive numbers
* - phone_number ; Matches phone numbers.
* - email_address ; Matches email addresses in many formats.
* - url ; Matches URL patterns/
* - words ; Finds words, even when in disguise.
*
* @type {{long_number: {pattern: RegExp, enabled: boolean}, phone_number: {pattern: RegExp, enabled: boolean}, email_address: {pattern: RegExp, enabled: boolean}, url: {pattern: RegExp, enabled: boolean}, words: {enabled: boolean, pattern: Array}}}
*/
this.patterns = {
long_number: {
pattern: /\d{8,}/,
enabled: false
},
phone_number: {
pattern: /([+-]?[\d]{1,}[\d\s-]+|\([\d]+\))[-\d.\s]{8,}/gi,
enabled: false
},
email_address: {
pattern: /[\w._%+-]+(@|\[at\]|\(at\))[\w.-]+(\.|\[dot\]|\(dot\)|\(punt\)|\[punt\])[a-zA-Z]{2,4}/gi,
enabled: false
},
url: {
pattern: /((https?:\/{1,2})?([-\w]\.{0,1}){2,}(\.|\[dot\]|\(dot\)|\(punt\)|\[punt\])([a-zA-Z]{2}\.[a-zA-Z]{2,3}|[a-zA-Z]{2,4}).*?(?=$|[^\w\/-]))/gi,
enabled: false
},
words: {
pattern: [],
enabled: false
}
};
/**
* A mapping that maps regular characters to 1337 characters.
*
* @type {{o: string, g: string, b: Array, t: string, s: string, a: string, e: string, z: string, i: string, l: string}}
*/
this.map1337 = {
o: '0',
g: '9',
b: ['8', '6'],
t: '7',
s: '5',
a: '4',
e: '3',
z: '2',
i: '1',
l: '1'
};
}
Censoring.prototype = {
/**
* Set the color of the highlighted occurrences in HEX.
*
* @param {string} color
*/
setHighlightColor: function (color) {
this.highlightColor = color.replace(/^#/, '');
},
/**
* Is the given object a array.
* @param {Array} subject
* @return {Boolean}
*/
isArray: function (subject) {
return subject instanceof Array;
},
/**
* Add a pattern to the list of filters. This will allow you to enable / disable them.
*
* @param {string} name
* @param {{pattern: RegExp, enabled: boolean}} pattern
*/
addFilter: function (name, pattern) {
this.patterns[name] = pattern;
},
/**
* Enable a filter by name.
*
* @param {String} filter
* @returns {Censoring}
*/
enableFilter: function (filter) {
if (typeof this.patterns[filter] === 'undefined') {
throw 'Invalid filter supplied.';
}
this.patterns[filter].enabled = true;
return this;
},
/**
* Enable multiple filters at once.
*
* @param {Array} filters
* @returns {Censoring}
* @see Censoring.enableFilter
*/
enableFilters: function (filters) {
if (!this.isArray(filters)) {
throw 'Invalid filters type supplied. Expected Array.';
}
for (var i = 0; i < filters.length; i++) {
this.enableFilter(filters[i]);
}
return this;
},
/**
* Disable a filter by name.
*
* @param {String} filter
* @returns {Censoring}
*/
disableFilter: function (filter) {
if (typeof this.patterns[filter] === 'undefined') {
throw 'Invalid filter supplied.';
}
this.patterns[filter].enabled = false;
return this;
},
/**
* Add multiple filterWords.
*
* @param {[]} words
* @returns {Censoring}
*/
addFilterWords: function (words) {
if (!this.isArray(words)) {
throw 'Invalid type supplied for addFilterWords. Expected array.';
}
for (var i = 0; i < words.length; i++) {
this.addFilterWord(words[i]);
}
return this;
},
/**
* Add a word to filter out.
*
* @param {String} word
* @returns {Censoring}
*/
addFilterWord: function (word) {
var pattern = '',
any = '[^a-z0-9]?',
last = false,
character;
for (var i = 0; i < word.length; i++) {
last = i === (word.length - 1);
character = word.charAt(i);
if (typeof this.map1337[character] === 'undefined') {
pattern += (character + (!last ? any : ''));
continue;
}
if (typeof this.map1337[character] === 'string') {
pattern += ('((' + character + '|' + this.map1337[character] + ')' + (!last ? any : '') + ')');
continue;
}
pattern += '((' + character;
for (var m = 0; m < this.map1337[character].length; m++) {
pattern += '|' + this.map1337[character][m];
}
pattern += ')' + (!last ? any : '') + ')';
}
this.patterns.words.pattern.push(new RegExp(pattern, 'ig'));
return this;
},
/**
* Set the string to replace matches in the filterString() method.
*
* @param {String|function(match: string): string} str
* @returns {Censoring}
*/
setReplacementString: function (str) {
if (typeof str !== 'string' && typeof str !== 'function') {
throw 'Invalid replacementString type supplied. Expected string.';
}
this.replacementString = str;
return this;
},
/**
* @returns {String|function(match: string): string}
*/
getReplacementString: function () {
return this.replacementString;
},
/**
* Returns matches array or FALSE
*
* @returns {Boolean}
*/
test: function () {
return this.currentMatch.hasMatches ? this.currentMatch.matches : false;
},
/**
* Prepare some text to be matched against.
*
* @param {String} str
* @param {Boolean} [highlight]
* @returns {Censoring}
*/
prepare: function (str, highlight) {
this.currentMatch.replace = this.filterString(str, highlight);
this.currentMatch.hasMatches = str !== this.currentMatch.replace;
return this;
},
/**
* Get the filtered text.
*
* @returns {Censoring}
*/
replace: function () {
return this.currentMatch.replace;
},
/**
* Filter the string.
*
* @param {String} str
* @param {Boolean} [highlight]
* @returns {String}}
*/
filterString: function (str, highlight) {
highlight = highlight || false;
var self = this;
var highlightColor = this.highlightColor;
var replace = function (str, pattern) {
if (!highlight) {
return str.replace(pattern, function (match) {
self.currentMatch.matches.push(match);
if (typeof self.replacementString === 'function') {
return self.replacementString(match);
}
return self.replacementString;
});
}
return str.replace(pattern, function (match) {
self.currentMatch.matches.push(match);
return '<span style="background: #' + highlightColor + ';">' + match + '</span>';
});
}.bind(this);
if (typeof str !== 'string') {
throw 'Invalid "str" type supplied in filterString. Expected string.';
}
for (var p in this.patterns) {
if (!this.patterns[p].enabled) {
continue;
}
if (this.patterns[p].pattern instanceof RegExp) {
str = replace(str, this.patterns[p].pattern);
continue;
}
if (!this.isArray(this.patterns[p].pattern)) {
throw 'Invalid pattern type supplied. Expected Array.';
}
for (var i = 0; i < this.patterns[p].pattern.length; i++) {
if (!this.patterns[p].pattern[i] instanceof RegExp) {
throw 'Expected valid RegExp.';
}
str = replace(str, this.patterns[p].pattern[i]);
}
}
return str;
}
};
/*
* Make sure Censoring is loadable through amd.
*/
if (typeof define === 'function' && define.amd) {
define([], function () {
return Censoring;
});
return;
}
if (typeof module === 'object') {
module.exports = Censoring;
return;
}
/*
* Make sure Censoring is accessible beyond our scope.
*/
window.Censoring = Censoring;
})();