-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathindex.js
118 lines (98 loc) · 2.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { readFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
export class ProfanityEngine {
constructor(config) {
this.isTestMode = config && config.testMode ? config.testMode : false;
this.language = config && config.language ? config.language : 'en';
this.terms = [];
this.filePath = '';
}
async initialize() {
this.filePath = await this.getLanguageFilePath(this.language);
try {
const fileContent = await this.readFileAndSplit(this.filePath);
this.terms = fileContent;
} catch (err) {
if (this.isTestMode === false) {
let message = `Error reading file: ${err.message}`;
console.warn('Profanity words issue:', message);
}
this.terms = [];
}
}
async getLanguageFilePath(language) {
const currentFilePath = fileURLToPath(import.meta.url);
const dataFolderPath = path.join(path.dirname(currentFilePath), 'data');
const languageFilePath = path.join(dataFolderPath, `${language}.txt`);
const fileExists = await this.fileExists(languageFilePath);
if (!fileExists) {
if (this.isTestMode === false) {
let message = `Warning: The ${language} language file could not be found. Defaulting to 'en' language.`;
console.warn('Profanity words issue:', message);
}
return path.join(dataFolderPath, 'en.txt');
}
return languageFilePath;
}
async fileExists(filePath) {
try {
await readFile(filePath);
return true;
} catch (err) {
return false;
}
}
async readFileAndSplit(filePath) {
try {
const fileContent = await readFile(filePath, 'utf8');
return fileContent.split('\n');
} catch (err) {
if (this.isTestMode === false) {
console.warn('Profanity words issue:', err);
}
return [];
}
}
async hasCurseWords(sentence) {
if (this.terms.length === 0) {
await this.initialize();
}
const wordsInSentence = sentence.split(/\s+/);
const lowerCasedTerms = this.terms.map((term) => term.toLowerCase());
for (const word of wordsInSentence) {
const lowerCasedWord = word.toLowerCase();
if (lowerCasedTerms.includes(lowerCasedWord)) {
return true;
}
}
return false;
}
async getCurseWords(sentence) {
if (this.terms.length === 0) {
await this.initialize();
}
let foundCurseWords = [];
const wordsInSentence = sentence.split(/\s+/);
const lowerCasedTerms = this.terms.map((term) => term.toLowerCase());
for (const word of wordsInSentence) {
const lowerCasedWord = word.toLowerCase();
if (lowerCasedTerms.includes(lowerCasedWord)) {
foundCurseWords.push(word);
}
}
return foundCurseWords;
}
async all() {
if (this.terms.length === 0) {
await this.initialize();
}
return this.terms;
}
async search(term) {
if (this.terms.length === 0) {
await this.initialize();
}
return this.terms.includes(term);
}
}