@fevo-tech/obscenity
v0.0.8
Published
A robust profanity and obscenity filtering library for JavaScript and TypeScript applications, designed to detect and censor offensive language effectively.
Downloads
715
Readme
@fevo/obscenity
A robust profanity and obscenity filtering library for Node.js, built on top of obscenity with extended dictionaries and HTML support.
Features
- 🔍 Detect profanity in text strings
- 🔒 Censor bad words with customizable strategies
- 🌐 HTML-aware censoring that preserves HTML tags
- 📚 Extended dictionary including racial slurs, LGBTQ+ slurs, and other offensive terms
- ⚡ Fast and efficient using RegExp-based matching
- 🎯 TypeScript support with full type definitions
Installation
npm install @fevo/obscenityUsage
Basic Text Censoring
import { censorBadWords, hasContainsBadWords } from "@fevo/obscenity";
// Censor bad words in a string
const text = "This is a test with some bad words";
const censored = censorBadWords(text);
console.log(censored); // "This is a test with some b** words"
// Check if text contains bad words
const hasProfanity = hasContainsBadWords(text);
console.log(hasProfanity); // true or falseHTML Censoring
The library can censor bad words in HTML content while preserving all HTML tags and structure:
import { censorHtmlPreservingTags } from "@fevo/obscenity";
const html = "<p>This has some <strong>bad words</strong> in it</p>";
const censoredHtml = censorHtmlPreservingTags(html);
console.log(censoredHtml);
// Output: '<p>This has some <strong>b** words</strong> in it</p>'API Reference
censorBadWords(input: string): string
Censors profanity in the input string by replacing offensive words with asterisks, keeping the first character.
Parameters:
input- The text string to censor
Returns:
- A string with profanity censored
Example:
censorBadWords("bad word here"); // "b** word here"hasContainsBadWords(input: string): boolean
Checks whether the input string contains any profanity.
Parameters:
input- The text string to check
Returns:
trueif profanity is found,falseotherwise
Example:
hasContainsBadWords("clean text"); // false
hasContainsBadWords("profane text"); // truecensorHtmlPreservingTags(htmlStr: string): string
Censors profanity in HTML content while preserving all HTML tags, attributes, and structure.
Parameters:
htmlStr- The HTML string to censor
Returns:
- A string with profanity censored but HTML structure intact
Example:
censorHtmlPreservingTags("<div>bad word</div>");
// '<div>b** word</div>'How It Works
The library uses:
- obscenity library for core matching and censoring functionality
- Custom extended dictionary with various categories of offensive terms
- htmlparser2 for HTML parsing and tag preservation
- RegExp-based matching with transformers for variant detection
- Keep-start censoring strategy that preserves the first character and replaces the rest with asterisks
Dictionary Coverage
The extended dictionary includes:
- Racial slurs (various ethnicities)
- LGBTQ+ slurs
- Gender-based slurs
- Ableist terms
- Sexual content
- Religious slurs
- And many other offensive terms with common variations
Development
Build
npm run buildClean
npm run cleanDependencies
- obscenity - Core profanity detection
- htmlparser2 - HTML parsing
License
ISC
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Disclaimer
This library is designed to help filter offensive content, but no automated system is perfect. The dictionary includes explicit terms for detection purposes only. Use responsibly and consider your specific use case and audience.
