1- import { readFile } from " fs/promises" ;
2- import path from " path" ;
3- import { fileURLToPath } from " url" ;
1+ import { readFile } from ' fs/promises' ;
2+ import path from ' path' ;
3+ import { fileURLToPath } from ' url' ;
44
55export class ProfanityEngine {
66 constructor ( config ) {
77 this . isTestMode = config && config . testMode ? config . testMode : false ;
8- this . language = config && config . language ? config . language : "en" ;
8+ this . language = config && config . language ? config . language : 'en' ;
99 this . terms = [ ] ;
10- this . filePath = "" ;
10+ this . filePath = '' ;
1111 }
1212
1313 async initialize ( ) {
@@ -18,24 +18,24 @@ export class ProfanityEngine {
1818 } catch ( err ) {
1919 if ( this . isTestMode === false ) {
2020 let message = `Error reading file: ${ err . message } ` ;
21- console . warn ( " Profanity words issue:" , message ) ;
21+ console . warn ( ' Profanity words issue:' , message ) ;
2222 }
2323 this . terms = [ ] ;
2424 }
2525 }
2626
2727 async getLanguageFilePath ( language ) {
2828 const currentFilePath = fileURLToPath ( import . meta. url ) ;
29- const dataFolderPath = path . join ( path . dirname ( currentFilePath ) , " data" ) ;
29+ const dataFolderPath = path . join ( path . dirname ( currentFilePath ) , ' data' ) ;
3030 const languageFilePath = path . join ( dataFolderPath , `${ language } .txt` ) ;
3131 const fileExists = await this . fileExists ( languageFilePath ) ;
3232
3333 if ( ! fileExists ) {
3434 if ( this . isTestMode === false ) {
3535 let message = `Warning: The ${ language } language file could not be found. Defaulting to 'en' language.` ;
36- console . warn ( " Profanity words issue:" , message ) ;
36+ console . warn ( ' Profanity words issue:' , message ) ;
3737 }
38- return path . join ( dataFolderPath , " en.txt" ) ;
38+ return path . join ( dataFolderPath , ' en.txt' ) ;
3939 }
4040
4141 return languageFilePath ;
@@ -52,16 +52,34 @@ export class ProfanityEngine {
5252
5353 async readFileAndSplit ( filePath ) {
5454 try {
55- const fileContent = await readFile ( filePath , " utf8" ) ;
56- return fileContent . split ( "\n" ) ;
55+ const fileContent = await readFile ( filePath , ' utf8' ) ;
56+ return fileContent . split ( '\n' ) ;
5757 } catch ( err ) {
5858 if ( this . isTestMode === false ) {
59- console . warn ( " Profanity words issue:" , err ) ;
59+ console . warn ( ' Profanity words issue:' , err ) ;
6060 }
6161 return [ ] ;
6262 }
6363 }
6464
65+ async hasCurseWords ( sentence ) {
66+ if ( this . terms . length === 0 ) {
67+ await this . initialize ( ) ;
68+ }
69+
70+ const wordsInSentence = sentence . split ( / \s + / ) ;
71+ const lowerCasedTerms = this . terms . map ( ( term ) => term . toLowerCase ( ) ) ;
72+
73+ for ( const word of wordsInSentence ) {
74+ const lowerCasedWord = word . toLowerCase ( ) ;
75+ if ( lowerCasedTerms . includes ( lowerCasedWord ) ) {
76+ return true ;
77+ }
78+ }
79+
80+ return false ;
81+ }
82+
6583 async all ( ) {
6684 if ( this . terms . length === 0 ) {
6785 await this . initialize ( ) ;
0 commit comments