Skip to content

Instantly share code, notes, and snippets.

@eengineergz
Created May 4, 2021 20:30
Show Gist options
  • Save eengineergz/6edf91d6fb2487badae1fe3ed6f69bac to your computer and use it in GitHub Desktop.
Save eengineergz/6edf91d6fb2487badae1fe3ed6f69bac to your computer and use it in GitHub Desktop.
const wordCondenser = /(?:^|\s)\S(?:(\s+)\S)(?:\1\S)*(?:$|\s)/g;
const spaceCondenser = /\s{2,}/g;
const condenseSpaces = msg => {
return msg
.replace(wordCondenser, m => m.replace(/\s+/g, ''))
.replace(spaceCondenser, ' ');
};
const makeRegex = str => {
const content = str.split('').map(char => {
const mangledChars = mangleMap[char];
if (!mangledChars || mangledChars.length === 1) {
return char;
} else {
return `[${mangledChars.join('')}]`;
}
});
return new RegExp(`(?:^|\\s)${content.join('')}(?:$|\\s)`, 'i');
};
const blacklistRegexps = spamPhrases.map(phrase => makeRegex(phrase));
const isSpam = msg => {
const spacesCondensed = condenseSpaces(msg);
return blacklistRegexps.some(re => re.test(spacesCondensed));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment