2017-01-30 21:39:45 -05:00
|
|
|
function escapeRegexp(text) {
|
2018-06-15 11:03:24 -04:00
|
|
|
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&").replace(/\*/g, "S*");
|
2017-01-30 21:39:45 -05:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
export function censorFn(
|
|
|
|
censoredWords,
|
|
|
|
replacementLetter,
|
|
|
|
watchedWordsRegularExpressions
|
|
|
|
) {
|
2017-06-08 18:02:30 -04:00
|
|
|
let patterns = [];
|
|
|
|
|
|
|
|
replacementLetter = replacementLetter || "■";
|
2016-11-08 16:36:34 -05:00
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
if (censoredWords && censoredWords.length) {
|
2018-01-10 14:11:14 -05:00
|
|
|
patterns = censoredWords.split("|");
|
|
|
|
if (!watchedWordsRegularExpressions) {
|
|
|
|
patterns = patterns.map(t => `(${escapeRegexp(t)})`);
|
|
|
|
}
|
2016-11-08 16:36:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (patterns.length) {
|
2016-06-14 14:31:51 -04:00
|
|
|
let censorRegexp;
|
|
|
|
|
2016-11-08 16:36:34 -05:00
|
|
|
try {
|
2018-01-10 14:11:14 -05:00
|
|
|
if (watchedWordsRegularExpressions) {
|
2018-06-15 11:03:24 -04:00
|
|
|
censorRegexp = new RegExp(
|
|
|
|
"((?:" + patterns.join("|") + "))(?![^\\(]*\\))",
|
|
|
|
"ig"
|
|
|
|
);
|
2018-01-10 14:11:14 -05:00
|
|
|
} else {
|
2018-06-15 11:03:24 -04:00
|
|
|
censorRegexp = new RegExp(
|
|
|
|
"(\\b(?:" + patterns.join("|") + ")\\b)(?![^\\(]*\\))",
|
|
|
|
"ig"
|
|
|
|
);
|
2018-01-10 14:11:14 -05:00
|
|
|
}
|
2016-11-08 16:36:34 -05:00
|
|
|
|
|
|
|
if (censorRegexp) {
|
2017-06-08 18:02:30 -04:00
|
|
|
return function(text) {
|
|
|
|
let original = text;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let m = censorRegexp.exec(text);
|
2018-01-24 12:33:38 -05:00
|
|
|
const fourCharReplacement = new Array(5).join(replacementLetter);
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
while (m && m[0]) {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (m[0].length > original.length) {
|
|
|
|
return original;
|
|
|
|
} // regex is dangerous
|
2018-01-10 14:11:14 -05:00
|
|
|
if (watchedWordsRegularExpressions) {
|
2018-01-24 12:33:38 -05:00
|
|
|
text = text.replace(censorRegexp, fourCharReplacement);
|
2018-01-10 14:11:14 -05:00
|
|
|
} else {
|
2018-06-15 11:03:24 -04:00
|
|
|
const replacement = new Array(m[0].length + 1).join(
|
|
|
|
replacementLetter
|
|
|
|
);
|
|
|
|
text = text.replace(
|
|
|
|
new RegExp(
|
|
|
|
`(\\b${escapeRegexp(m[0])}\\b)(?![^\\(]*\\))`,
|
|
|
|
"ig"
|
|
|
|
),
|
|
|
|
replacement
|
|
|
|
);
|
2018-01-10 14:11:14 -05:00
|
|
|
}
|
2017-06-08 18:02:30 -04:00
|
|
|
m = censorRegexp.exec(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
} catch (e) {
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
};
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
2018-06-15 11:03:24 -04:00
|
|
|
} catch (e) {
|
2017-06-08 18:02:30 -04:00
|
|
|
// fall through
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 16:36:34 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
return function(t) {
|
|
|
|
return t;
|
|
|
|
};
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
2018-02-26 15:48:59 -05:00
|
|
|
export function censor(text, censoredWords, replacementLetter) {
|
|
|
|
return censorFn(censoredWords, replacementLetter)(text);
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|