arabic-profanity-filter
v0.1.3
Published
Configurable Arabic word and phrase profanity checking for browsers and Node.js
Maintainers
Readme
Arabic profanity filter
A zero-dependency Arabic word and phrase checker for React, other browser apps, and Node.js. Includes ESM, CommonJS, and TypeScript declarations. No network requests or user-text collection.
Installation
npm install arabic-profanity-filterSupports browsers (through a bundler), React, and Node.js, with ESM, CommonJS, and TypeScript declarations. No runtime dependencies.
Usage
import { createArabicProfanityFilter } from 'arabic-profanity-filter';
const filter = createArabicProfanityFilter({
additionalWords: ['عبارة محظورة'],
allowlist: [],
});
filter.isProfane('اشتراك شهري'); // false
filter.check('عبارة محظورة');
// { isProfane: true, matches: ['عبارة محظورة'], normalizedText: 'عبارة محظورة' }Provide words: [...] to replace the starter dictionary entirely. additionalWords
extends it. allowlist removes exact normalized dictionary entries; it does not
exempt a whole sentence containing other blocked words. Options are copied when
the filter is created. Non-string text and invalid word lists throw TypeError.
React example
Create the filter once outside your component. Show feedback after the field has been touched, and recheck on submit:
import { useState } from 'react';
import { createArabicProfanityFilter } from 'arabic-profanity-filter';
const filter = createArabicProfanityFilter();
export function ArabicDescription({ onSave }) {
const [text, setText] = useState('');
const [touched, setTouched] = useState(false);
const invalid = filter.isProfane(text);
function submit(event) {
event.preventDefault();
setTouched(true);
if (filter.isProfane(text)) return;
onSave(text);
}
return (
<form onSubmit={submit}>
<label htmlFor="arabic-description">الوصف بالعربية</label>
<textarea
id="arabic-description"
dir="rtl"
value={text}
onChange={event => setText(event.target.value)}
onBlur={() => setTouched(true)}
aria-invalid={touched && invalid}
aria-describedby={touched && invalid ? 'description-error' : undefined}
/>
{touched && invalid && (
<p id="description-error" role="alert">
يحتوي النص على ألفاظ غير مسموح بها. يرجى تعديله.
</p>
)}
<button type="submit">حفظ</button>
</form>
);
}Matching policy and limits
- Normalizes a copy with Unicode NFKC, strips combining marks, invisible format characters and Arabic elongation, folds alef variants and final alef maqsura, and lowercases text. Original input is never modified.
- Matches complete letter/number tokens and adjacent phrases. Punctuation is a separator, so a phrase can match across punctuation or line breaks.
- Does not use JavaScript
\bboundaries, which do not represent Arabic words. - The starter list covers explicit sexual profanity, common insults, and curse phrases across major Arabic dialects, including ta marbuta and ha spellings. It is not a complete Arabic dictionary, toxicity model, or dialect benchmark. Review it with native speakers against your product policy before use.
- Does not automatically expand Arabic prefixes/suffixes, Arabizi, repeated letters, or words split with punctuation. Add reviewed variants explicitly.
- Context can change meaning; exact matching can still produce false positives. Test real product names and descriptions, and adjust the list accordingly.
- Frontend validation is bypassable. Enforce equivalent rules in your backend. This package can also run on Node.js for server-side validation.
Development
Run the tests and preview the package contents:
npm test
npm pack --dry-runLicense
MIT — see LICENSE.
