glin-profanity
Version:
Glin-Profanity is a lightweight and efficient npm package designed to detect and filter profane language in text inputs across multiple languages. Whether you’re building a chat application, a comment section, or any platform where user-generated content
193 lines (190 loc) • 6 kB
TypeScript
import { F as FilterConfig, C as CheckProfanityResult, S as SeverityLevel } from './types-B9c_ik4k.js';
/**
* Core profanity filter class.
* Provides comprehensive profanity detection with support for multiple languages,
* leetspeak detection, Unicode normalization, and context-aware filtering.
*
* @example
* ```typescript
* const filter = new Filter({
* languages: ['english'],
* detectLeetspeak: true,
* normalizeUnicode: true,
* });
*
* filter.isProfane('f4ck'); // Returns: true
* filter.isProfane('fυck'); // Returns: true (Greek upsilon)
* ```
*/
declare class Filter {
private words;
private caseSensitive;
private wordBoundaries;
private replaceWith?;
private severityLevels;
private ignoreWords;
private logProfanity;
private allowObfuscatedMatch;
private fuzzyToleranceLevel;
private enableContextAware;
private contextWindow;
private confidenceThreshold;
private contextAnalyzer?;
private primaryLanguage;
private detectLeetspeak;
private leetspeakLevel;
private normalizeUnicodeEnabled;
private cacheResults;
private maxCacheSize;
private cache;
private regexCache;
/**
* Creates a new Filter instance with the specified configuration.
*
* @param config - Filter configuration options
*
* @example
* ```typescript
* // Basic usage
* const filter = new Filter({ languages: ['english'] });
*
* // With leetspeak detection
* const filter = new Filter({
* languages: ['english'],
* detectLeetspeak: true,
* leetspeakLevel: 'moderate',
* });
*
* // With all advanced features
* const filter = new Filter({
* languages: ['english', 'spanish'],
* detectLeetspeak: true,
* normalizeUnicode: true,
* cacheResults: true,
* enableContextAware: true,
* });
* ```
*/
constructor(config?: FilterConfig);
private debugLog;
/**
* Normalizes text for profanity detection using all enabled normalization methods.
* Applies Unicode normalization, leetspeak detection, and obfuscation handling.
*
* @param text - The input text to normalize
* @param aggressive - If true, collapses to single chars (for repeated char detection)
* @returns The normalized text
*/
private normalizeText;
/**
* Legacy obfuscation normalization method (for backward compatibility).
* @deprecated Use normalizeText() with detectLeetspeak option instead.
*/
private normalizeObfuscated;
/**
* Clears the result cache.
* Useful when dictionary or configuration changes.
*/
clearCache(): void;
/**
* Gets the current cache size.
* @returns Number of cached results
*/
getCacheSize(): number;
/**
* Exports the current filter configuration as a JSON-serializable object.
* Useful for saving configuration to files or sharing between environments.
*
* @returns The current filter configuration
*
* @example
* ```typescript
* const filter = new Filter({
* languages: ['english', 'spanish'],
* detectLeetspeak: true,
* leetspeakLevel: 'aggressive',
* });
*
* const config = filter.getConfig();
* // Save to file: fs.writeFileSync('filter.config.json', JSON.stringify(config));
*
* // Later, restore:
* // const saved = JSON.parse(fs.readFileSync('filter.config.json'));
* // const restored = new Filter(saved);
* ```
*/
getConfig(): FilterConfig;
/**
* Returns the current word dictionary size.
* Useful for monitoring and debugging.
*
* @returns Number of words in the dictionary
*/
getWordCount(): number;
/**
* Adds a result to the cache, evicting oldest entries if necessary.
*/
private addToCache;
/**
* Gets a cached result if available.
*/
private getFromCache;
private getRegex;
private isFuzzyToleranceMatch;
private evaluateSeverity;
/**
* Checks if the given text contains profanity.
*
* @param value - The text to check
* @returns True if the text contains profanity
*
* @example
* ```typescript
* const filter = new Filter({ detectLeetspeak: true });
*
* filter.isProfane('hello'); // false
* filter.isProfane('fuck'); // true
* filter.isProfane('f4ck'); // true (leetspeak)
* filter.isProfane('fυck'); // true (Unicode homoglyph)
* ```
*/
isProfane(value: string): boolean;
matches(word: string): boolean;
/**
* Performs a comprehensive profanity check on the given text.
*
* @param text - The text to check for profanity
* @returns Result object containing detected profanity information
*
* @example
* ```typescript
* const filter = new Filter({
* languages: ['english'],
* detectLeetspeak: true,
* normalizeUnicode: true,
* });
*
* const result = filter.checkProfanity('This is f4ck!ng bad');
* console.log(result.containsProfanity); // true
* console.log(result.profaneWords); // ['fuck']
*
* // With caching for repeated checks
* const filter2 = new Filter({ cacheResults: true });
* filter2.checkProfanity('same text'); // Computed
* filter2.checkProfanity('same text'); // Retrieved from cache
* ```
*/
checkProfanity(text: string): CheckProfanityResult;
/**
* Checks profanity with minimum severity filtering.
*
* @param text - The text to check
* @param minSeverity - Minimum severity level to include in results
* @returns Object with filtered words and full result
*/
checkProfanityWithMinSeverity(text: string, minSeverity?: SeverityLevel): {
filteredWords: string[];
result: CheckProfanityResult;
};
}
export { Filter as F };