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
68 lines • 3.69 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());
});
};
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) => {
var _a;
const [result, setResult] = (0, react_1.useState)(null);
const filterConfig = (0, react_1.useMemo)(() => {
var _a;
const effective = Object.assign(Object.assign({}, (config !== null && config !== void 0 ? 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 });
if (effective.allowObfuscatedMatch && effective.wordBoundaries) {
console.warn('[Glin-Profanity] Obfuscated match enabled → wordBoundaries will be ignored internally.');
}
return effective;
}, [config]);
const filter = (0, react_1.useMemo)(() => new Filter_1.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,
};
};
exports.useProfanityChecker = useProfanityChecker;
//# sourceMappingURL=useProfanityChecker.js.map