-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
67 lines (57 loc) · 1.49 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
/**
* no-swears - Filter out or detect swear words in your strings
*
* @author Gabriel Simmer <https://github.com/gmemstr>
* @version 1.0
* @license MIT
**/
"use strict";
const swearList = require("./swearList");
module.exports = {
/**
* Replaces bad words with asterisks
**/
filterSwearWords: (words, callback) => {
let finalString = words;
swearList.getList((lines) => {
callback(getFilteredString(finalString, lines));
});
},
filterSync: (words) => {
var lines = swearListSync();
var finalString = words;
for (var i = 0; i < lines.length; i++) {
var bw = new RegExp(lines[i], "gi");
finalString = finalString.replace(bw, "*".repeat(lines[i].length));
}
return finalString;
},
/**
* True if swearword is found, false if not
**/
hasSwears: (words, callback) => {
swearList.getList((lines) => {
callback(hasSwearWords(words, lines));
});
},
hasSwearsSync: (words) => {
let lines = swearList.getListSync();
return hasSwearWords(words, lines);
},
};
let hasSwearWords = (words, lines) => {
let itHasASwear = false;
for (let i = 0; i < lines.length; i++) {
if (words.indexOf(lines[i]) > -1) {
itHasASwear = true;
}
}
return itHasASwear;
};
let getFilteredString = (finalString, lines) => {
for (let i = 0; i < lines.length; i++) {
let badWord = new RegExp(lines[i], "gi");
finalString = finalString.replace(badWord, "*".repeat(lines[i].length));
}
return finalString;
};