word-sensor
Version:
A powerful and flexible word filtering library for JavaScript/TypeScript with advanced features like regex patterns, statistics, and batch processing
90 lines (88 loc) • 3.72 kB
text/typescript
interface WordSensorConfig {
words?: string[];
maskChar?: string;
caseInsensitive?: boolean;
logDetections?: boolean;
enableRegex?: boolean;
wordBoundary?: boolean;
customReplacer?: (word: string, context: string) => string;
}
interface DetectionStats {
totalDetections: number;
uniqueWords: string[];
detectionCounts: Record<string, number>;
lastDetectionTime?: Date;
}
declare class WordSensor {
private forbiddenWords;
private regexPatterns;
private maskChar;
private caseInsensitive;
private logDetections;
private enableRegex;
private wordBoundary;
private customReplacer?;
private detectionLogs;
private detectionStats;
constructor(config?: WordSensorConfig);
addWord(word: string, mask?: string): void;
addWords(words: string[]): void;
addRegexPattern(pattern: string, mask?: string): void;
removeWord(word: string): void;
removeWords(words: string[]): void;
clearWords(): void;
getWords(): string[];
hasWord(word: string): boolean;
private applyMask;
private updateStats;
filter(text: string, mode?: "replace" | "remove" | "highlight", maskType?: "full" | "partial" | "smart"): string;
private filterWithWords;
private filterWithRegex;
detect(text: string): string[];
private detectWithWords;
private detectWithRegex;
detectWithPositions(text: string): Array<{
word: string;
start: number;
end: number;
}>;
getDetectionLogs(): string[];
getStats(): DetectionStats;
resetStats(): void;
setMaskChar(char: string): void;
setCaseInsensitive(value: boolean): void;
setLogDetections(value: boolean): void;
setCustomReplacer(replacer: (word: string, context: string) => string): void;
sanitizeText(text: string): string;
isClean(text: string): boolean;
getCleanPercentage(text: string): number;
}
declare const PRESET_WORDS: {
profanity: string[];
spam: string[];
phishing: string[];
};
declare function createWordSensor(config?: WordSensorConfig): WordSensor;
declare function createProfanityFilter(maskChar?: string): WordSensor;
declare function createSpamFilter(maskChar?: string): WordSensor;
declare function createPhishingFilter(maskChar?: string): WordSensor;
declare function getNestedValue(obj: any, path: string): any;
declare function loadForbiddenWordsFromAPI(url: string, path: string | null, sensor: WordSensor): Promise<boolean>;
declare function loadWordsFromFile(file: File): Promise<string[]>;
declare function validateRegexPattern(pattern: string): boolean;
declare function escapeRegexSpecialChars(str: string): string;
declare function createCustomReplacer(replacementMap: Record<string, string>): (word: string, context: string) => string;
declare function createEmojiReplacer(): (word: string, context: string) => string;
declare function batchFilter(texts: string[], sensor: WordSensor, mode?: "replace" | "remove" | "highlight", maskType?: "full" | "partial" | "smart"): string[];
declare function batchDetect(texts: string[], sensor: WordSensor): Array<{
text: string;
detected: string[];
}>;
declare function getBatchStats(texts: string[], sensor: WordSensor): {
totalTexts: number;
cleanTexts: number;
dirtyTexts: number;
totalDetections: number;
averageCleanPercentage: number;
};
export { DetectionStats, PRESET_WORDS, WordSensor, WordSensorConfig, batchDetect, batchFilter, createCustomReplacer, createEmojiReplacer, createPhishingFilter, createProfanityFilter, createSpamFilter, createWordSensor, escapeRegexSpecialChars, getBatchStats, getNestedValue, loadForbiddenWordsFromAPI, loadWordsFromFile, validateRegexPattern };