UNPKG

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

61 lines 3.31 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { useMemo, useState } from 'react'; import { Filter } from '../filters/Filter'; import globalWhitelistData from '../data/globalWhitelist.json'; export const useProfanityChecker = (config) => { var _a; const [result, setResult] = useState(null); const filterConfig = useMemo(() => { var _a; const effective = Object.assign(Object.assign({}, (config !== null && config !== void 0 ? config : {})), { ignoreWords: globalWhitelistData.whitelist, fuzzyToleranceLevel: (_a = config === null || config === void 0 ? void 0 : config.fuzzyToleranceLevel) !== null && _a !== void 0 ? _a : 0.8 }); if (effective.allowObfuscatedMatch && effective.wordBoundaries) { console.warn('[Glin-Profanity] Obfuscated match enabled → wordBoundaries will be ignored internally.'); } return effective; }, [config]); const filter = useMemo(() => new Filter(filterConfig), [filterConfig]); const checkText = (text) => { var _a; const checkResult = filter.checkProfanity(text); // Filter based on minSeverity (if provided) const filteredWords = (config === null || config === void 0 ? void 0 : config.minSeverity) && checkResult.severityMap ? checkResult.profaneWords.filter((word) => checkResult.severityMap && checkResult.severityMap[word] >= config.minSeverity) : checkResult.profaneWords; // Optional auto-replace const autoReplaced = (config === null || config === void 0 ? void 0 : config.autoReplace) && checkResult.processedText ? checkResult.processedText : text; setResult(checkResult); (_a = config === null || config === void 0 ? void 0 : config.customActions) === null || _a === void 0 ? void 0 : _a.call(config, checkResult); return Object.assign(Object.assign({}, checkResult), { filteredWords, autoReplaced }); }; const checkTextAsync = (text) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve) => { const checkResult = checkText(text); // sync call resolve(checkResult); }); }); const isWordProfane = (word) => { return filter.checkProfanity(word).containsProfanity; }; const reset = () => setResult(null); return { result, checkText, checkTextAsync, reset, isDirty: (_a = result === null || result === void 0 ? void 0 : result.containsProfanity) !== null && _a !== void 0 ? _a : false, isWordProfane, }; }; //# sourceMappingURL=useProfanityChecker.js.map