-
Notifications
You must be signed in to change notification settings - Fork 2
/
BadWords.js
65 lines (51 loc) · 1.95 KB
/
BadWords.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
/*========================================
= Required Modules =
========================================*/
const fs = require('fs'),
_ = require('lodash')
/*=======================================
= Private Methods =
=======================================*/
const init = function init() {
// Set directory containing badword files
BadWords.corporaDir = (process.env.CORPORA_DIR) ? process.env.CORPORA_DIR : `${__dirname}/corpora`
// Setup split characters for input strings
if (process.env.SPLIT_CHARS) {
BadWords.splitCharsArr = process.env.SPLIT_CHARS.split(' ')
if (process.env.SPLIT_CHARS_INCLUDE_SPACE) BadWords.splitCharsArr.push(' ')
}
// Read corpora docs and build a master corpus of bad words
fs.readdirSync(BadWords.corporaDir).forEach(file => {
const fileTxt = fs.readFileSync(`${BadWords.corporaDir}/${file}`).toString(),
badWordsArr = fileTxt.split(',\n')
BadWords.masterCorpusArr = _.union(BadWords.masterCorpusArr, badWordsArr)
})
}
/*==============================
= Module =
==============================*/
const BadWords = {
corporaDir: '',
masterCorpusArr: [],
splitCharsArr: ['=', ',', ':', ' ', '.'],
splitMulti(str, delimiters) {
const tmpChar = delimiters[0]
for (let i = delimiters.length - 1; i >= 0; i--) {
str = str.split(delimiters[i]).join(tmpChar)
}
str = str.split(tmpChar)
return str
},
isProfane(inputStr) {
// Split into wordds based on custom delimiters
const words = this.splitMulti(inputStr, this.splitCharsArr)
// Loop through the words in the inputStr checking returning true if any are profane exiting the loop
for (let i = words.length - 1; i >= 0; i--) {
if (words[i] !== '' && words[i] !== ' ' && this.masterCorpusArr.includes(words[i].toLowerCase())) return true
}
return false
}
}
// Self initialize upon import/require
init()
module.exports = BadWords