@@ -2,20 +2,36 @@ var Filter = (function() {
2
2
function Filter ( options ) {
3
3
options = options || { } ;
4
4
this . list = options . emptyList && [ ] || Array . prototype . concat . apply ( require ( './lang.json' ) . words , [ require ( 'badwords-list' ) . array , options . list || [ ] ] ) ;
5
+ this . exclude = options . exclude || [ ] ;
5
6
this . placeHolder = options . placeHolder || '*' ;
6
7
this . regex = options . regex || / [ ^ a - z A - z 0 - 9 | \$ | \@ ] | \^ / g;
7
8
this . replaceRegex = options . replaceRegex || / \w / g;
8
9
}
9
10
10
11
Filter . prototype . isProfane = function isProfane ( string ) {
11
- var words = string . split ( ' ' ) ;
12
- for ( var j = 0 ; j < words . length ; j ++ ) {
13
- var word = words [ j ] . toLowerCase ( ) . replace ( this . regex , '' ) ;
14
- if ( ~ this . list . indexOf ( word ) ) {
15
- return true ;
16
- }
17
- }
18
- return false ;
12
+ return string
13
+ . split ( ' ' )
14
+ . map ( function ( w ) {
15
+ return w . toLowerCase ( ) . replace ( this . regex , '' ) ;
16
+ } , this )
17
+ . map ( this . isProfaneLike , this )
18
+ . filter ( function ( profane ) {
19
+ return profane ;
20
+ } )
21
+ . shift ( ) || false ;
22
+ } ;
23
+
24
+ Filter . prototype . isProfaneLike = function profaneLike ( word ) {
25
+ return ! ! ~ this . exclude . indexOf ( word ) ? false : ! ! ~ this . list . indexOf ( word ) || this . list
26
+ . map ( function ( w ) {
27
+ return new RegExp ( w . replace ( / ( \W ) / g, '\\$1' ) ) ;
28
+ } , this )
29
+ . reduce ( function ( outcome , wordExp ) {
30
+ if ( wordExp . test ( word ) ) {
31
+ return true ;
32
+ }
33
+ return outcome ;
34
+ } , false ) ;
19
35
} ;
20
36
21
37
Filter . prototype . replaceWord = function replaceWord ( string ) {
@@ -31,17 +47,17 @@ var Filter = (function() {
31
47
Filter . prototype . addWords = function addWords ( words ) {
32
48
words = ( words instanceof Array ) ? words : [ words ] ;
33
49
this . list = this . list . concat ( words ) ;
50
+
51
+ words . forEach ( function ( word ) {
52
+ if ( ! ! ~ this . exclude . indexOf ( word ) ) {
53
+ this . exclude . splice ( this . exclude . indexOf ( word ) , 1 ) ;
54
+ }
55
+ } , this ) ;
34
56
} ;
35
57
36
58
Filter . prototype . removeWords = function removeWords ( ) {
37
59
var words = Array . prototype . slice . call ( arguments ) ;
38
- words . map ( function ( word ) {
39
- return this . list . indexOf ( word ) ;
40
- } , this ) . filter ( function ( index ) {
41
- return ! ! ~ index ;
42
- } ) . forEach ( function ( index ) {
43
- this . list . splice ( index , 1 ) ;
44
- } , this ) ;
60
+ this . exclude . push . apply ( this . exclude , words ) ;
45
61
} ;
46
62
47
63
return Filter ;
0 commit comments