2024-06-10 08:44:31 -04:00
|
|
|
export function censorFn(regexpList, replacementLetter = "■") {
|
2022-08-02 06:09:51 -04:00
|
|
|
if (regexpList?.length) {
|
2024-06-10 08:44:31 -04:00
|
|
|
const censorRegexps = regexpList.map((entry) => {
|
|
|
|
const [regexp, options] = Object.entries(entry)[0];
|
|
|
|
return new RegExp(regexp, options.case_sensitive ? "gu" : "gui");
|
2022-08-02 04:06:03 -04:00
|
|
|
});
|
2019-07-31 13:33:49 -04:00
|
|
|
|
|
|
|
return function (text) {
|
2022-08-02 04:06:03 -04:00
|
|
|
censorRegexps.forEach((censorRegexp) => {
|
|
|
|
text = text.replace(censorRegexp, (fullMatch, ...groupMatches) => {
|
|
|
|
const stringMatch = groupMatches.find((g) => typeof g === "string");
|
|
|
|
return fullMatch.replace(
|
|
|
|
stringMatch,
|
|
|
|
new Array(stringMatch.length + 1).join(replacementLetter)
|
|
|
|
);
|
|
|
|
});
|
2019-07-31 13:33:49 -04:00
|
|
|
});
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2019-07-31 13:33:49 -04:00
|
|
|
return text;
|
|
|
|
};
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
2016-11-08 16:36:34 -05:00
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
return function (t) {
|
|
|
|
return t;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:33:49 -04:00
|
|
|
export function censor(text, censoredRegexp, replacementLetter) {
|
|
|
|
return censorFn(censoredRegexp, replacementLetter)(text);
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|