-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.ts
77 lines (75 loc) · 2.15 KB
/
app.ts
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
interface ValidationOptions {
language: string;
swear?: boolean;
negative?: boolean;
political?: boolean;
religion?: boolean;
}
/**
* @description Check given string to validate
* @param {data}: string : text to validate
* @param {options}: ValidationOptions : ValidationOptions option to fetch related word lists
*/
export const checkIsValid = (data: string, options: ValidationOptions) => {
let string: any[] = data.split(' ');
let badWordList: string[] = [];
let isValid: boolean = true;
try {
for (const [key, value] of Object.entries(options)) {
if (value && key != 'language') {
const item: string = key;
const data: any = require(`./data/${
options.language
}/${item.toLowerCase()}.json`);
badWordList.push(...data);
}
}
} catch (error) {
console.error(error);
}
for (let i = 0; i < badWordList.length; i++) {
for (let j = 0; j < string.length; j++) {
if (badWordList[i].toLowerCase() === string[j].toLowerCase()) {
isValid = false;
break;
}
}
}
return isValid;
};
/**
* @description Replace bad word with given mask string
* @param {data}: string : text to validate
* @param {replacer}: string : mask string to replace it
* @param {options}: ValidationOptions : ValidationOptions option to fetch related word lists
*/
export const replaceWordWith = (
data: string,
replacer: string,
options: ValidationOptions,
) => {
let string: any[] = data.split(' ');
let badWordList: string[] = [];
try {
for (const [key, value] of Object.entries(options)) {
if (value && key != 'language') {
const item: string = key;
const data: any = require(`./data/${
options.language
}/${item.toLowerCase()}.json`);
badWordList.push(...data);
}
}
} catch (error) {
console.error(error);
}
for (let i = 0; i < badWordList.length; i++) {
for (let j = 0; j < string.length; j++) {
if (badWordList[i].toLowerCase() === string[j].toLowerCase()) {
string[string.indexOf(string[j])] = replacer.repeat(string[j].length);
break;
}
}
}
return string.join(' ');
};