Filter Class
Object-oriented JavaScript/TypeScript API with persistent configuration
The Filter class provides an object-oriented interface for profanity detection with persistent configuration and advanced state management capabilities for JavaScript and TypeScript applications.
Constructor
constructor(config?: FilterConfig)Initialize profanity filter with comprehensive configuration options including context-aware filtering, multi-language support, and custom word lists.
Configuration Options
| Prop | Type | Default |
|---|---|---|
languages? | Language[] | ['english'] |
allLanguages? | boolean | false |
caseSensitive? | boolean | false |
wordBoundaries? | boolean | !allowObfuscatedMatch |
allowObfuscatedMatch? | boolean | false |
fuzzyToleranceLevel? | number | 0.8 |
customWords? | string[] | [] |
ignoreWords? | string[] | globalWhitelist |
replaceWith? | string | undefined |
severityLevels? | boolean | false |
enableContextAware? | boolean | false |
contextWindow? | number | 3 |
confidenceThreshold? | number | 0.7 |
domainWhitelists? | Record<string, string[]> | {} |
logProfanity? | boolean | false |
Basic Usage Example
import { Filter } from 'glin-profanity';
// Create filter with default configuration
const filter = new Filter();
// Check text for profanity
const result = filter.checkProfanity("This is damn good!");
console.log(result.containsProfanity); // true
console.log(result.profaneWords); // ["damn"]Public Methods
Private Methods
Advanced Usage Examples
import { Filter } from 'glin-profanity';
// Multi-language filter
const globalFilter = new Filter({
languages: ['english', 'spanish', 'french'],
caseSensitive: false
});
// Check different languages
console.log(globalFilter.isProfane('shit')); // true (English)
console.log(globalFilter.isProfane('mierda')); // true (Spanish)
console.log(globalFilter.isProfane('merde')); // true (French)
// Or check all 23 languages
const universalFilter = new Filter({ allLanguages: true });
const result = universalFilter.checkProfanity("Text with profanity");import { Filter } from 'glin-profanity';
// Context-aware filtering
const contextFilter = new Filter({
enableContextAware: true,
contextWindow: 5,
confidenceThreshold: 0.8,
domainWhitelists: {
english: ['boss', 'enemy', 'game', 'character']
}
});
// Gaming context - not flagged
const gaming = contextFilter.checkProfanity("That boss fight is badass!");
console.log(gaming.containsProfanity); // false
console.log(gaming.reason); // "Gaming context detected"
// Negative context - flagged
const negative = contextFilter.checkProfanity("You fucking idiot!");
console.log(negative.containsProfanity); // true
console.log(negative.reason); // "Negative emotional context"import { Filter } from 'glin-profanity';
// Efficient batch processing
const filter = new Filter({
enableContextAware: true,
severityLevels: true
});
const comments = [
"Great product, love it!",
"This shit is broken as hell",
"Fucking amazing movie, best ever!",
"You damn moron, fix this crap"
];
// Process multiple texts efficiently
const results = comments.map(text => filter.checkProfanity(text));
results.forEach((result, i) => {
console.log(`Comment ${i + 1}:`);
console.log(`- Contains profanity: ${result.containsProfanity}`);
console.log(`- Context score: ${result.contextScore || 'N/A'}`);
console.log(`- Reason: ${result.reason || 'No context analysis'}`);
});Performance Features
The Filter class is optimized for performance with efficient algorithms, lazy loading, and optional caching for repeated text analysis.
Built-in Optimizations
- Early termination: Stops processing on first match when possible
- Regex compilation: Pre-compiled patterns for faster matching
- Memory management: Efficient dictionary loading and cleanup
- Algorithm efficiency: Optimized fuzzy matching with configurable tolerance
Usage Tips
- Reuse Filter instances for better performance
- Enable context-aware filtering only when needed
- Use appropriate
fuzzyToleranceLevelfor your use case - Consider
caseSensitive: falsefor better coverage
Cross-References
- Core Functions - Functional programming interface
- React Hook - React integration with useProfanityChecker
- Python API - Python Filter class equivalent
- Quick Start - Basic usage examples
- Context-Aware Filtering - Advanced sentiment analysis