Created
June 1, 2023 08:09
-
-
Save midnight-wonderer/ebdb0cc1d7390bba82363888225f547f to your computer and use it in GitHub Desktop.
Email domain suggestion for typo prone user segments
This file contains 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
import { default as curry } from 'ramda/src/curry'; | |
function deleteChar(deleteCount: number, word: string): string[] { | |
if (!(0 < deleteCount)) { | |
return [word]; | |
} | |
const accumulatedWordList = []; | |
for (let wordIterator = 0; wordIterator < word.length; wordIterator++) { | |
const deletedWord = `${ | |
word.substring(0, wordIterator) | |
}${ | |
word.substring(wordIterator + 1, word.length) | |
}`; | |
accumulatedWordList.push( | |
deleteChar( | |
deleteCount - 1, | |
deletedWord, | |
), | |
); | |
} | |
return [].concat(...accumulatedWordList); | |
} | |
const deleteCharN = curry(deleteChar); | |
const deleteChar1 = deleteCharN(1); | |
const extractName = /^[-\w\d]+/i; | |
const generateIndex = (domain: string) => { | |
const match = extractName.exec(domain); | |
if (!match) { | |
return []; | |
} | |
return [domain, deleteChar1(match[0])]; | |
}; | |
import { default as map } from 'ramda/src/map'; | |
const extractDomain = /^(.+)@([-.\w\d]+)$/i; | |
const deleteChar2 = deleteCharN(2); | |
const nullSuggesting = { | |
suggestOther: false, | |
suggestedEmail: null, | |
suggestedDomain: null, | |
}; | |
const suggest = ( | |
domainList: string[], | |
) => { | |
const index = map(generateIndex, domainList); | |
return ( | |
testEmail: string, | |
) => { | |
const domainMatching = extractDomain.exec(testEmail); | |
if (!domainMatching) { | |
return nullSuggesting; | |
} | |
const domainName = domainMatching[2].toLowerCase(); | |
for (const indexEntry of index) { | |
if (indexEntry[0] === domainName) { | |
return nullSuggesting; | |
} | |
} | |
const providerNameMatching = extractName.exec(domainName); | |
if (!providerNameMatching) { | |
return nullSuggesting; | |
} | |
const checkingName = providerNameMatching[0]; | |
const checkingList = [ | |
checkingName, | |
...deleteChar1(checkingName), | |
...deleteChar2(checkingName), | |
]; | |
for (const checkingWord of checkingList) { | |
for (const indexEntry of index) { | |
for (const indexMatch of indexEntry[1]) { | |
if (indexMatch === checkingWord) { | |
const suggestedDomain = indexEntry[0]; | |
return { | |
suggestOther: true, | |
suggestedEmail: `${domainMatching[1]}@${suggestedDomain}`, | |
suggestedDomain, | |
}; | |
} | |
} | |
} | |
} | |
return nullSuggesting; | |
}; | |
}; | |
// don't add 3-letter-or-less domain name here | |
const suggester = suggest([ | |
'hotmail.com', | |
'yahoo.com', | |
'yahoo.co.jp', | |
'outlook.com', | |
'gmail.com', | |
'ymail.com', | |
'live.com', | |
'mail.com', | |
'rocketmail.com', | |
'icloud.com', | |
]); | |
console.log('index', suggester('hahaha@yaahoo.com')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment