Skip to content

Commit 913c34e

Browse files
Merge pull request #22 from ElviraBurchik/get-profound-words
Add function to get list of found curse words
2 parents 16465bd + 06b6791 commit 913c34e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

__tests__/engine.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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();

0 commit comments

Comments
 (0)