WordSensor v2.0.0 🚀

WordSensor is a powerful and flexible word filtering library for JavaScript/TypeScript. It helps you detect, replace, or remove forbidden words from text with advanced features like regex patterns, statistics, batch processing, and more.
✨ Features
- 🔍 Advanced Detection: Detect prohibited words with precise positioning
- 🚫 Multiple Filtering Modes: Replace, remove, or highlight forbidden words
- 🎭 Smart Masking: Full, partial, or smart masking options
- 📊 Statistics & Analytics: Track detections and get detailed insights
- 🔧 Regex Support: Use custom regex patterns for complex filtering
- 📦 Batch Processing: Process multiple texts efficiently
- 🎯 Preset Filters: Ready-to-use profanity, spam, and phishing filters
- 🔄 Custom Replacers: Create custom replacement functions
- 📈 Real-time Monitoring: Log and track all detections
- 🌐 API Integration: Load forbidden words from external APIs
- 📁 File Support: Import word lists from files
- ⚡ High Performance: Optimized for speed and memory efficiency
- 🎨 Emoji Replacers: Replace words with emojis
- 🔒 Word Boundaries: Configurable word boundary detection
- 📝 TypeScript Support: Full TypeScript definitions included
📦 Installation
npm install word-sensor
or
yarn add word-sensor
🚀 Quick Start
Basic Usage
import { WordSensor } from 'word-sensor';
const sensor = new WordSensor({
words: ['badword', 'offensive', 'rude'],
maskChar: '*',
caseInsensitive: true,
logDetections: true
});
const result = sensor.filter('This is a badword test.');
console.log(result);
Using Preset Filters
import { createProfanityFilter, createSpamFilter, createPhishingFilter } from 'word-sensor';
const profanityFilter = createProfanityFilter();
const spamFilter = createSpamFilter();
const phishingFilter = createPhishingFilter();
console.log(profanityFilter.filter('This is badword content.'));
console.log(spamFilter.filter('Buy now! Free money!'));
📚 API Reference
WordSensor Class
Constructor
new WordSensor(config?: WordSensorConfig)
Configuration Options:
words?: string[] - Initial list of forbidden words
maskChar?: string - Character used for masking (default: "*")
caseInsensitive?: boolean - Case-insensitive matching (default: true)
logDetections?: boolean - Enable detection logging (default: false)
enableRegex?: boolean - Enable regex pattern support (default: false)
wordBoundary?: boolean - Use word boundaries (default: true)
customReplacer?: (word: string, context: string) => string - Custom replacement function
Core Methods
filter(text: string, mode?: "replace" | "remove" | "highlight", maskType?: "full" | "partial" | "smart"): string
Filter text with specified mode and masking type.
sensor.filter('This is badword.');
sensor.filter('This is badword.', 'remove');
sensor.filter('This is badword.', 'highlight');
sensor.filter('This is badword.', 'replace', 'smart');
detect(text: string): string[]
Detect all forbidden words in text.
const detected = sensor.detect('This contains badword and offensive content.');
console.log(detected);
detectWithPositions(text: string): Array<{word: string, start: number, end: number}>
Detect forbidden words with their positions.
const positions = sensor.detectWithPositions('This badword is offensive.');
console.log(positions);
Word Management
sensor.addWord('newbadword', '###');
sensor.addWords(['word1', 'word2']);
sensor.removeWord('badword');
sensor.removeWords(['word1', 'word2']);
sensor.hasWord('badword');
sensor.getWords();
sensor.clearWords();
Regex Patterns
const regexSensor = new WordSensor({ enableRegex: true });
regexSensor.addRegexPattern('\\b\\w+@\\w+\\.\\w+\\b', '[EMAIL]');
regexSensor.addRegexPattern('\\b\\d{4}-\\d{4}-\\d{4}-\\d{4}\\b', '[CARD]');
const result = regexSensor.filter('Contact me at test@example.com');
console.log(result);
Statistics & Monitoring
const stats = sensor.getStats();
console.log(stats);
const logs = sensor.getDetectionLogs();
console.log(logs);
sensor.resetStats();
Configuration Methods
sensor.setMaskChar('#');
sensor.setCaseInsensitive(false);
sensor.setLogDetections(true);
sensor.setCustomReplacer((word) => `[${word.toUpperCase()}]`);
Utility Methods
sensor.isClean('This is clean text.');
sensor.isClean('This has badword.');
sensor.getCleanPercentage('This badword is offensive.');
sensor.sanitizeText('This is badword.');
Utility Functions
Preset Filters
import {
createProfanityFilter,
createSpamFilter,
createPhishingFilter,
PRESET_WORDS
} from 'word-sensor';
const profanityFilter = createProfanityFilter('*');
const spamFilter = createSpamFilter('#');
const phishingFilter = createPhishingFilter('!');
console.log(PRESET_WORDS.profanity);
console.log(PRESET_WORDS.spam);
console.log(PRESET_WORDS.phishing);
Batch Processing
import { batchFilter, batchDetect, getBatchStats } from 'word-sensor';
const texts = [
'This is bad.',
'This is offensive.',
'This is clean.'
];
const filtered = batchFilter(texts, sensor);
console.log(filtered);
const detected = batchDetect(texts, sensor);
console.log(detected);
const stats = getBatchStats(texts, sensor);
console.log(stats);
Custom Replacers
import { createCustomReplacer, createEmojiReplacer } from 'word-sensor';
const customReplacer = createCustomReplacer({
'bad': 'good',
'offensive': 'appropriate',
'rude': 'polite'
});
const emojiReplacer = createEmojiReplacer();
sensor.setCustomReplacer(customReplacer);
sensor.setCustomReplacer(emojiReplacer);
Regex Utilities
import { validateRegexPattern, escapeRegexSpecialChars } from 'word-sensor';
validateRegexPattern('\\b\\w+\\b');
validateRegexPattern('invalid[');
escapeRegexSpecialChars('test.com');
escapeRegexSpecialChars('test*test');
API Integration
import { loadForbiddenWordsFromAPI, loadWordsFromFile } from 'word-sensor';
await loadForbiddenWordsFromAPI(
'https://api.example.com/forbidden-words',
'data.words',
sensor
);
const fileInput = document.getElementById('file') as HTMLInputElement;
const file = fileInput.files[0];
if (file) {
const words = await loadWordsFromFile(file);
sensor.addWords(words);
}
🎯 Advanced Examples
Content Moderation System
import { WordSensor, createProfanityFilter, createSpamFilter } from 'word-sensor';
class ContentModerator {
private profanityFilter: WordSensor;
private spamFilter: WordSensor;
private customFilter: WordSensor;
constructor() {
this.profanityFilter = createProfanityFilter();
this.spamFilter = createSpamFilter();
this.customFilter = new WordSensor({
enableRegex: true,
wordBoundary: false
});
this.customFilter.addRegexPattern('\\b\\w+@\\w+\\.\\w+\\b', '[EMAIL]');
this.customFilter.addRegexPattern('\\b\\d{10,}\\b', '[PHONE]');
}
moderateContent(content: string): {
isClean: boolean;
filteredContent: string;
violations: string[];
stats: any;
} {
let filteredContent = content;
const violations: string[] = [];
const profanityDetected = this.profanityFilter.detect(content);
if (profanityDetected.length > 0) {
violations.push('profanity');
filteredContent = this.profanityFilter.filter(filteredContent);
}
const spamDetected = this.spamFilter.detect(content);
if (spamDetected.length > 0) {
violations.push('spam');
filteredContent = this.spamFilter.filter(filteredContent);
}
filteredContent = this.customFilter.filter(filteredContent);
return {
isClean: violations.length === 0,
filteredContent,
violations,
stats: {
profanity: this.profanityFilter.getStats(),
spam: this.spamFilter.getStats(),
custom: this.customFilter.getStats()
}
};
}
}
const moderator = new ContentModerator();
const result = moderator.moderateContent('This is badword spam content with test@example.com');
console.log(result);
Real-time Chat Filter
import { WordSensor, createEmojiReplacer } from 'word-sensor';
class ChatFilter {
private sensor: WordSensor;
private messageHistory: string[] = [];
constructor() {
this.sensor = new WordSensor({
words: ['badword', 'offensive'],
logDetections: true,
customReplacer: createEmojiReplacer()
});
}
processMessage(message: string, userId: string): {
filteredMessage: string;
isClean: boolean;
warning: string | null;
} {
const filteredMessage = this.sensor.filter(message);
const isClean = this.sensor.isClean(message);
const userViolations = this.messageHistory.filter(msg =>
msg.includes(userId) && !this.sensor.isClean(msg)
).length;
let warning = null;
if (!isClean) {
if (userViolations >= 3) {
warning = 'You have been warned multiple times. Further violations may result in a ban.';
} else {
warning = 'Please keep the chat appropriate.';
}
}
this.messageHistory.push(`${userId}: ${message}`);
return { filteredMessage, isClean, warning };
}
getModerationStats() {
return this.sensor.getStats();
}
}
Batch Content Analysis
import { WordSensor, batchDetect, getBatchStats } from 'word-sensor';
class ContentAnalyzer {
private sensor: WordSensor;
constructor() {
this.sensor = new WordSensor({
words: ['inappropriate', 'spam', 'offensive'],
logDetections: true
});
}
analyzeBatch(contentList: string[]): {
summary: any;
details: Array<{
content: string;
isClean: boolean;
detectedWords: string[];
cleanPercentage: number;
}>;
} {
const batchResults = batchDetect(contentList, this.sensor);
const batchStats = getBatchStats(contentList, this.sensor);
const details = contentList.map((content, index) => ({
content,
isClean: batchResults[index].detected.length === 0,
detectedWords: batchResults[index].detected,
cleanPercentage: this.sensor.getCleanPercentage(content)
}));
return {
summary: {
...batchStats,
sensorStats: this.sensor.getStats()
},
details
};
}
}
🧪 Testing
npm test
npm run test:watch
npm run test:coverage
📦 Build
npm run build
npm run dev
npm run clean
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add some amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
👨💻 Author
Developed by Asrul Harahap.
🙏 Acknowledgments
- Thanks to all contributors who helped improve this library
- Inspired by the need for better content moderation tools
- Built with TypeScript for better developer experience
📈 Changelog
v2.0.0
- ✨ Major Release: Complete rewrite with advanced features
- 🔧 New Constructor: Config-based initialization
- 📊 Statistics: Comprehensive detection tracking
- 🔍 Regex Support: Custom regex pattern filtering
- 📦 Batch Processing: Efficient multi-text processing
- 🎯 Preset Filters: Ready-to-use specialized filters
- 🎨 Custom Replacers: Flexible replacement functions
- 📈 Position Detection: Get exact word positions
- 🔄 Smart Masking: Intelligent masking algorithms
- 🌐 API Integration: External word list loading
- 📁 File Support: Import word lists from files
- 🎨 Emoji Replacers: Fun emoji-based replacements
- 📝 Enhanced Types: Better TypeScript support
- 🧪 Comprehensive Tests: 36 test cases covering all features
v1.0.5
- 🐛 Bug fixes and improvements
- 📝 Better documentation
⭐ Star this repository if you find it useful!