File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -46,4 +46,16 @@ describe('ProfanityEngine Functions tests', () => {
4646 let terms = await profanity . all ( ) ;
4747 expect ( terms . length ) . toEqual ( 959 ) ;
4848 } ) ;
49+
50+ it ( 'Should return list of found profanity words' , async ( ) => {
51+ const sentence = 'This is a test sentence with bad words like hell and damn' ;
52+ const badWords = await profanity . getCurseWords ( sentence ) ;
53+ expect ( badWords ) . toEqual ( [ 'hell' , 'damn' ] ) ;
54+ } ) ;
55+
56+ it ( 'Should return empty array if no curse words found' , async ( ) => {
57+ const sentence = 'This is a test sentence with no bad words' ;
58+ const result = await profanity . getCurseWords ( sentence ) ;
59+ expect ( result ) . toEqual ( [ ] ) ;
60+ } ) ;
4961} ) ;
Original file line number Diff line number Diff line change @@ -80,6 +80,26 @@ export class ProfanityEngine {
8080 return false ;
8181 }
8282
83+ async getCurseWords ( sentence ) {
84+ if ( this . terms . length === 0 ) {
85+ await this . initialize ( ) ;
86+ }
87+
88+ let foundCurseWords = [ ] ;
89+
90+ const wordsInSentence = sentence . split ( / \s + / ) ;
91+ const lowerCasedTerms = this . terms . map ( ( term ) => term . toLowerCase ( ) ) ;
92+
93+ for ( const word of wordsInSentence ) {
94+ const lowerCasedWord = word . toLowerCase ( ) ;
95+ if ( lowerCasedTerms . includes ( lowerCasedWord ) ) {
96+ foundCurseWords . push ( word ) ;
97+ }
98+ }
99+
100+ return foundCurseWords ;
101+ }
102+
83103 async all ( ) {
84104 if ( this . terms . length === 0 ) {
85105 await this . initialize ( ) ;
You can’t perform that action at this time.
0 commit comments