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

58 lines 2.9 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useProfanityChecker = void 0; const react_1 = require("react"); const Filter_1 = require("../filters/Filter"); const globalWhitelist_json_1 = __importDefault(require("../data/globalWhitelist.json")); const useProfanityChecker = (config) => { const [result, setResult] = (0, react_1.useState)(null); const filterConfig = (0, react_1.useMemo)(() => { var _a; const effectiveConfig = Object.assign(Object.assign({}, config), { ignoreWords: globalWhitelist_json_1.default.whitelist, fuzzyToleranceLevel: (_a = config === null || config === void 0 ? void 0 : config.fuzzyToleranceLevel) !== null && _a !== void 0 ? _a : 0.8 }); // Optional - warn developer if (effectiveConfig.allowObfuscatedMatch && effectiveConfig.wordBoundaries) { console.warn('[Glin-Profanity] Obfuscated match enabled → wordBoundaries will be ignored internally.'); } return effectiveConfig; }, [config]); const filter = (0, react_1.useMemo)(() => new Filter_1.Filter(filterConfig), [filterConfig]); const checkText = (text) => { const checkResult = filter.checkProfanity(text); setResult(checkResult); if (config === null || config === void 0 ? void 0 : config.customActions) { config.customActions(checkResult); } }; const checkTextAsync = (text) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve) => { const checkResult = filter.checkProfanity(text); setResult(checkResult); if (config === null || config === void 0 ? void 0 : config.customActions) { config.customActions(checkResult); } resolve(checkResult); }); }); const reset = () => setResult(null); return { result, checkText, checkTextAsync, reset, }; }; exports.useProfanityChecker = useProfanityChecker; //# sourceMappingURL=useProfanityChecker.js.map