Skip to content

Instantly share code, notes, and snippets.

@lionel-rowe
Last active Mar 22, 2019
Embed
What would you like to do?
const alphabetMap = {
a: 'a@4', b: 'b8', c: 'c{\\[(', d: 'd', e: 'e3', f: 'f', g: 'g69', h: 'h',
i: 'i|', j: 'j', k: 'k', l: 'l', m: 'm', n: 'n', o: 'o0', p: 'p', q: 'q',
r: 'r', s: 's5', t: 't7', u: 'u', v: 'v', w: 'w', x: 'x', y: 'y', z: 'z2'
};
const makeRegex = str => {
const inner = str.split('').map(char => {
const chars = alphabetMap[char];
return (!chars || chars.length === 1)
? char
: `[${chars}]`;
});
return new RegExp(`\\b${inner.join('\\s*')}\\b`, 'i');
};
const spamPhrases = [
'viagra', 'free money', 'work from home', 'stock alert', 'dear friend'
];
const regexps = spamPhrases.map(phrase => makeRegex(phrase));
const isSpam = msg => {
return regexps.some(re => re.test(msg));
};
const tests = [
[ 'fr33 m0ney' , true ],
[ 'F R E E M O N E Y', true ],
[ 'w0rk from h0m3' , true ],
[ 'great d3als 0n VI4GR4!', true ],
[ 'Hello John,', false ],
[ 'The PPT from the meeting is attached', false ],
[ '', false ]
];
tests.forEach(test => {
console.assert(isSpam(test[0]) === test[1]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment