@coffeeandfun/google-profanity-words
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -6,11 +6,10 @@ import { ProfanityEngine } from '../index.js'; | ||
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 | ||
}); | ||
@@ -22,3 +21,3 @@ it('Should get the correct language file path', async () => { | ||
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'); | ||
@@ -25,0 +24,0 @@ expect(filePath).toContain('en.txt'); |
@@ -8,10 +8,10 @@ import { ProfanityEngine } from '../index.js'; | ||
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 all the profanity words in an array',async () => { | ||
it('Should get all the profanity words in an array', async () => { | ||
const allWords = await profanity.all(); | ||
@@ -26,3 +26,3 @@ expect(allWords.length).toEqual(959); | ||
it('Should return false for normal words', async() => { | ||
it('Should return false for normal words', async () => { | ||
const searchWord = await profanity.search('ka'); | ||
@@ -32,6 +32,18 @@ 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); | ||
}); | ||
}); |
@@ -8,8 +8,8 @@ import { ProfanityEngine } from '../index.js'; | ||
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 () => { | ||
@@ -20,3 +20,3 @@ const allWords = await profanity.all(); | ||
it('Should return true for profanity words',async () => { | ||
it('Should return true for profanity words', async () => { | ||
const searchWord = await profanity.search('labios'); | ||
@@ -26,3 +26,3 @@ 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'); | ||
@@ -32,6 +32,18 @@ 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
index.js
@@ -1,4 +0,4 @@ | ||
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'; | ||
@@ -8,5 +8,5 @@ export class ProfanityEngine { | ||
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 = ''; | ||
} | ||
@@ -22,3 +22,3 @@ | ||
let message = `Error reading file: ${err.message}`; | ||
console.warn("Profanity words issue:", message); | ||
console.warn('Profanity words issue:', message); | ||
} | ||
@@ -31,3 +31,3 @@ this.terms = []; | ||
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`); | ||
@@ -39,5 +39,5 @@ const fileExists = await this.fileExists(languageFilePath); | ||
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'); | ||
} | ||
@@ -59,7 +59,7 @@ | ||
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); | ||
} | ||
@@ -70,2 +70,20 @@ 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() { | ||
@@ -72,0 +90,0 @@ if (this.terms.length === 0) { |
{ | ||
"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.", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
"scripts": { | ||
"format": "npx prettier . --write", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest", | ||
@@ -32,5 +33,5 @@ "en": "NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest english.test.js", | ||
"devDependencies": { | ||
"jest": "^27.4.5" | ||
}, | ||
"dependencies": {} | ||
"jest": "^27.4.5", | ||
"prettier": "3.0.0" | ||
} | ||
} |
@@ -1,5 +0,3 @@ | ||
![alt text](.github/readme.png "Logo Title Text 1") | ||
![alt text](.github/readme.png 'Logo Title Text 1') | ||
## Description | ||
@@ -29,3 +27,2 @@ | ||
const profanity = new ProfanityEngine({ language: 'es' }); | ||
``` | ||
@@ -35,3 +32,2 @@ | ||
## API Functions | ||
@@ -56,8 +52,19 @@ | ||
### 3. Handling Empty Strings | ||
### 3. hasCurseWords(sentence) | ||
The `search` function will return `false` for any empty string. | ||
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` 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. | ||
@@ -64,0 +71,0 @@ ``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
78382
12
209
91
0
2