Last active
March 22, 2019 10:17
-
-
Save lionel-rowe/0724546e4b5c1a71be29502aac4c825e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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