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
51 lines • 2.53 kB
JavaScript
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) => {
const [result, setResult] = useState(null);
const filterConfig = useMemo(() => {
var _a;
const effectiveConfig = Object.assign(Object.assign({}, config), { ignoreWords: globalWhitelistData.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 = useMemo(() => new 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,
};
};
//# sourceMappingURL=useProfanityChecker.js.map