Skip to content

Commit

Permalink
Merge pull request #18 from coffee-and-fun/search-support
Browse files Browse the repository at this point in the history
Search support
  • Loading branch information
RobertJGabriel authored Jul 27, 2023
2 parents 1456ee0 + 3fd9c86 commit 9e55efc
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 53 deletions.
1 change: 1 addition & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
with:
node-version: 16
- run: npm ci
- run: npm run format
- run: npm test

publish-npm:
Expand Down
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"printWidth": 80,
"arrowParens": "always",
"endOfLine": "auto"
}
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
![alt text](.github/readme.png "Logo Title Text 1")


![alt text](.github/readme.png 'Logo Title Text 1')

## Description

Expand All @@ -26,12 +24,10 @@ import { ProfanityEngine } from '@coffeeandfun/google-profanity-words';
// Pass the 'language' parameter to specify the language (optional).
// Defaults to 'en' if no valid language code is provided.
const profanity = new ProfanityEngine({ language: 'es' });

```

The language parameter is optional and can be used to specify the language for the profanity list. It defaults to 'en' if no valid language code is provided. If the specified language file is not found, it will fall back to the 'en' language file and display a console warning.


## API Functions

### 1. `all()`
Expand All @@ -51,12 +47,23 @@ const searchWord = profanity.search('shit');
// Returns true if the word is profane, otherwise false.
```

### 3. Handling Empty Strings
### 3. hasCurseWords(sentence)

Checks if a given sentence contains any profanity words.

```javascript
const sentence = 'Do not use bad words like mierda or idiota.';
const hasCurseWords = profanity.hasCurseWords(sentence);
// Returns true if the sentence contains profanity words, otherwise false.
```

### 4. Handling Empty Strings

The `search` function will return `false` for any empty string.
The `search` and `hasCurseWords` functions will return false for any empty string.

```javascript
const searchWord = profanity.search('');
const hasCurseWords = profanity.hasCurseWords('');
// Returns false for an empty string.
```

Expand Down
15 changes: 7 additions & 8 deletions __tests__/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import { ProfanityEngine } from '../index.js';
const language = process.env.LANGUAGE || 'en'; // Default to 'en' if the LANGUAGE environment variable is not set
let profanity;


describe('ProfanityEngine Functions tests', () => {
beforeAll(async () => {
profanity = new ProfanityEngine({
language: 'en',
testMode:true
});
await profanity.initialize(); // Initialize the profanity instance with the English language
});
profanity = new ProfanityEngine({
language: 'en',
testMode: true,
});
await profanity.initialize(); // Initialize the profanity instance with the English language
});

it('Should get the correct language file path', async () => {
const filePath = await profanity.getLanguageFilePath('es');
expect(filePath).toContain('es.txt');
});

it('Should return the default language file path for unknown language',async () => {
it('Should return the default language file path for unknown language', async () => {
const filePath = await profanity.getLanguageFilePath('fr');
expect(filePath).toContain('en.txt');
});
Expand Down
32 changes: 22 additions & 10 deletions __tests__/english.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ let profanity;

describe('English Profanity tests', () => {
beforeAll(async () => {
profanity = new ProfanityEngine({
language: 'en',
testMode:true
});
await profanity.initialize(); // Initialize the profanity instance with the English language
});

it('Should get all the profanity words in an array',async () => {
profanity = new ProfanityEngine({
language: 'en',
testMode: true,
});
await profanity.initialize(); // Initialize the profanity instance with the English language
});

it('Should get all the profanity words in an array', async () => {
const allWords = await profanity.all();
expect(allWords.length).toEqual(959);
});
Expand All @@ -22,13 +22,25 @@ describe('English Profanity tests', () => {
expect(searchWord).toEqual(true);
});

it('Should return false for normal words', async() => {
it('Should return false for normal words', async () => {
const searchWord = await profanity.search('ka');
expect(searchWord).toEqual(false);
});

it('Should return false for any empty string',async () => {
it('Should return false for any empty string', async () => {
const searchWord = await profanity.search('');
expect(searchWord).toEqual(true);
});

it('Should return true for a sentence containing a profanity word', async () => {
const sentence = 'Do not use bad words like shit or asshole.';
const hasCurseWords = await profanity.hasCurseWords(sentence);
expect(hasCurseWords).toEqual(true);
});

it('Should return false for a sentence with no profanity word', async () => {
const sentence = 'This is a clean and polite sentence.';
const hasCurseWords = await profanity.hasCurseWords(sentence);
expect(hasCurseWords).toEqual(false);
});
});
30 changes: 21 additions & 9 deletions __tests__/spanish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@ let profanity;

describe('Spanish Profanity tests', () => {
beforeAll(async () => {
profanity = new ProfanityEngine({
language: 'es',
testMode:true
});
await profanity.initialize(); // Initialize the profanity instance with the English language
});
profanity = new ProfanityEngine({
language: 'es',
testMode: true,
});
await profanity.initialize(); // Initialize the profanity instance with the English language
});
it('Should get all the profanity words in an array', async () => {
const allWords = await profanity.all();
expect(allWords.length).toEqual(565);
});

it('Should return true for profanity words',async () => {
it('Should return true for profanity words', async () => {
const searchWord = await profanity.search('labios');
expect(searchWord).toEqual(true);
});

it('Should return false for normal words', async() => {
it('Should return false for normal words', async () => {
const searchWord = await profanity.search('ka');
expect(searchWord).toEqual(false);
});

it('Should return false for any empty string',async () => {
it('Should return false for any empty string', async () => {
const searchWord = await profanity.search('');
expect(searchWord).toEqual(true);
});

it('Should return true for a sentence containing a profanity word', async () => {
const sentence = 'No deberías decir malas culo palabras como mierda.';
const hasCurseWords = await profanity.hasCurseWords(sentence);
expect(hasCurseWords).toEqual(true);
});

it('Should return false for a sentence with no profanity word', async () => {
const sentence = 'Esta es una oración limpia y educada.';
const hasCurseWords = await profanity.hasCurseWords(sentence);
expect(hasCurseWords).toEqual(false);
});
});
42 changes: 30 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { readFile } from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
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.language = config && config.language ? config.language : 'en';
this.terms = [];
this.filePath = "";
this.filePath = '';
}

async initialize() {
Expand All @@ -18,24 +18,24 @@ export class ProfanityEngine {
} catch (err) {
if (this.isTestMode === false) {
let message = `Error reading file: ${err.message}`;
console.warn("Profanity words issue:", 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 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);
console.warn('Profanity words issue:', message);
}
return path.join(dataFolderPath, "en.txt");
return path.join(dataFolderPath, 'en.txt');
}

return languageFilePath;
Expand All @@ -52,16 +52,34 @@ export class ProfanityEngine {

async readFileAndSplit(filePath) {
try {
const fileContent = await readFile(filePath, "utf8");
return fileContent.split("\n");
const fileContent = await readFile(filePath, 'utf8');
return fileContent.split('\n');
} catch (err) {
if (this.isTestMode === false) {
console.warn("Profanity words issue:", err);
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 all() {
if (this.terms.length === 0) {
await this.initialize();
Expand Down
28 changes: 25 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@coffeeandfun/google-profanity-words",
"version": "2.0.0",
"version": "2.1.0",
"description": "Full list of bad words and top swear words banned by Google.",
"main": "index.js",
"type": "module",
"scripts": {
"format": "npx prettier . --write",
"test": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest",
"en": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest english.test.js",
"es": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest spanish.test.js",
Expand All @@ -28,7 +29,7 @@
},
"homepage": "https://github.com/coffee-and-fun/google-profanity-words#readme",
"devDependencies": {
"jest": "^27.4.5"
},
"dependencies": {}
"jest": "^27.4.5",
"prettier": "3.0.0"
}
}

0 comments on commit 9e55efc

Please sign in to comment.