Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to get list of found curse words #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions __tests__/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ describe('ProfanityEngine Functions tests', () => {
let terms = await profanity.all();
expect(terms.length).toEqual(959);
});

it('Should return list of found profanity words', async () => {
const sentence = 'This is a test sentence with bad words like hell and damn';
const badWords = await profanity.getCurseWords(sentence);
expect(badWords).toEqual(['hell', 'damn']);
});

it('Should return empty array if no curse words found', async () => {
const sentence = 'This is a test sentence with no bad words';
const result = await profanity.getCurseWords(sentence);
expect(result).toEqual([]);
});
});
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ export class ProfanityEngine {
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();
Expand Down