-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
censor.js
36 lines (31 loc) · 987 Bytes
/
censor.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
const fs = require( 'fs' );
const naughtylist = fs.readFileSync( "facebook-bad-words-list_comma-separated-text-file_2018_07_29.txt", "utf8" )
.split( ", " ).filter( Boolean );
const naughtyRegexList = naughtylist
.map( word => new RegExp( `\\b${ word }\\b`, "gi" ) )
const globalblacklist = fs.readFileSync( "blacklist.txt", "utf8" ).split( "\n" )
.filter( Boolean )
.map( word => new RegExp( `\\b${ word }\\b`, "gi" ) );
const CENSORED = "[censored]"
module.exports = {
naughtyToNice,
containsNaughtyWord,
hasBlacklistedWord
}
function naughtyToNice( text ) {
return naughtyRegexList.reduce(
( string, regex ) => string.replace( regex, CENSORED ),
text
)
}
function containsNaughtyWord( text ) {
for( let i = 0, len = naughtylist.length; i < len; i++ ) {
if( text.includes( naughtylist[ i ] ) ) {
return true;
}
}
return false;
}
function hasBlacklistedWord( string ) {
return globalblacklist.some( regex => regex.test( string ) )
}