Advertisement
Guest User

Untitled

a guest
Sep 10th, 2016
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. function checkForCloseMatch(longString, shortString) {
  2. // too many false positives with very short strings
  3. if (shortString.length < 3) return '';
  4.  
  5. // test if the shortString is in the string (so everything is fine)
  6. if (longString.includes(shortString)) return '';
  7.  
  8. // split the shortString string into two at each postion e.g. g|mail gm|ail gma|il gmai|l
  9. // and test that each half exists with one gap
  10. for (let i = 1; i < shortString.length; i++) {
  11. const firstPart = shortString.substring(0, i);
  12. const secondPart = shortString.substring(i);
  13.  
  14. // test for wrong letter
  15. const wrongLetterRegEx = new RegExp(`${firstPart}.${secondPart.substring(1)}`);
  16. if (wrongLetterRegEx.test(longString)) {
  17. return longString.replace(wrongLetterRegEx, shortString);
  18. }
  19.  
  20. // test for extra letter
  21. const extraLetterRegEx = new RegExp(`${firstPart}.${secondPart}`);
  22. if (extraLetterRegEx.test(longString)) {
  23. return longString.replace(extraLetterRegEx, shortString);
  24. }
  25.  
  26. // test for missing letter
  27. if (secondPart !== 'mail') {
  28. const missingLetterRegEx = new RegExp(`${firstPart}{0}${secondPart}`);
  29. if (missingLetterRegEx.test(longString)) {
  30. return longString.replace(missingLetterRegEx, shortString);
  31. }
  32. }
  33.  
  34. // test for switched letters
  35. const switchedLetters = [
  36. shortString.substring(0, i - 1),
  37. shortString.charAt(i),
  38. shortString.charAt(i - 1),
  39. shortString.substring(i + 1),
  40. ].join('');
  41.  
  42. if (longString.includes(switchedLetters)) {
  43. return longString.replace(switchedLetters, shortString);
  44. }
  45. }
  46.  
  47. // if nothing was close, then there wasn't a typo
  48. return '';
  49. }
  50.  
  51. function checkForDomainTypo(userEmail) {
  52. const domains = ['gmail', 'hotmail', 'outlook', 'yahoo', 'icloud', 'mail', 'zoho'];
  53. const [leftPart, rightPart] = userEmail.split('@');
  54.  
  55. for (let i = 0; i < domains.length; i++) {
  56. const domain = domains[i];
  57.  
  58. const result = checkForCloseMatch(rightPart, domain);
  59.  
  60. if (result) return `${leftPart}@${result}`;
  61. }
  62.  
  63. return '';
  64. }
  65.  
  66. function checkForNameTypo(userEmail, name) {
  67. const [leftPart, rightPart] = userEmail.split('@');
  68. const result = checkForCloseMatch(leftPart, name);
  69.  
  70. if (result) return `${result}@${rightPart}`;
  71.  
  72. return '';
  73. }
  74.  
  75. function checkForCommonTypos(userInput) {
  76. const commonTypos = [
  77. {
  78. pattern: /,com$/,
  79. fix: str => str.replace(/,com$/, '.com'),
  80. },
  81. {
  82. pattern: /,co\.\w{2}$/,
  83. fix: str => str.replace(/,(co\.\w{2}$)/, '.$1'),
  84. },
  85. {
  86. pattern: /@\w*$/,
  87. fix: str => str + '.com',
  88. },
  89. ];
  90.  
  91. typo = commonTypos.find(typo => typo.pattern.test(userInput));
  92.  
  93. if (typo) return typo.fix(userInput);
  94.  
  95. return '';
  96. }
  97.  
  98. function checkForTypo(userInput) {
  99. const email = userInput.email.trim().toLowerCase();
  100.  
  101. return checkForCommonTypos(email)
  102. || checkForDomainTypo(email)
  103. || checkForNameTypo(email, userInput.firstName.trim().toLowerCase())
  104. || checkForNameTypo(email, userInput.lastName.trim().toLowerCase());
  105. }
  106.  
  107. function runTests() {
  108. const testInputs = [
  109. // correct
  110. {firstName: '', lastName: '', email: 'first.last@example.com', expectedResult: ''},
  111. {firstName: 'yo', lastName: 'shortname', email: 'david.gilbertson@yahoo.com', expectedResult: ''},
  112. {firstName: 'david', lastName: 'gilbertson', email: 'david.GILBERTSON@gmail.com', expectedResult: ''},
  113. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@SOME+THING-ODD!!.com', expectedResult: ''},
  114. {firstName: 'david', lastName: 'gilbertson', email: 'DAVId.gilbertson@outlook.com', expectedResult: ''},
  115. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@yahoo.com', expectedResult: ''},
  116. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@hotmail.com', expectedResult: ''},
  117. {firstName: 'david', lastName: 'gilbertson', email: 'blah@unknown.com', expectedResult: ''},
  118. {firstName: 'david', lastName: 'gilbertson', email: 'blah@hotmail.co.uk', expectedResult: ''},
  119.  
  120. // common mistakes
  121. {firstName: 'a', lastName: 'b', email: 'a.b@example,com', expectedResult: 'a.b@example.com'},
  122. {firstName: 'a', lastName: 'b', email: 'a.b@example,co.de', expectedResult: 'a.b@example.co.de'},
  123. {firstName: 'a', lastName: 'b', email: 'a.b@noDotCom', expectedResult: 'a.b@nodotcom.com'},
  124.  
  125. // domain typo
  126. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@gmial.com', expectedResult: 'david.gilbertson@gmail.com'},
  127. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@gmal.com', expectedResult: 'david.gilbertson@gmail.com'},
  128. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@outloik.com', expectedResult: 'david.gilbertson@outlook.com'},
  129. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@outloook.com', expectedResult: 'david.gilbertson@outlook.com'},
  130. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertson@yohoo.com', expectedResult: 'david.gilbertson@yahoo.com'},
  131.  
  132. // name typo
  133. {firstName: 'david', lastName: 'gilbertson', email: 'daivd.gilbertson@xxxxx.com', expectedResult: 'david.gilbertson@xxxxx.com'},
  134. {firstName: 'david', lastName: 'gilbertson', email: 'daavid.gilbertson@xxxxx.com', expectedResult: 'david.gilbertson@xxxxx.com'},
  135. {firstName: 'david', lastName: 'gilbertson', email: 'davd.gilbertson@xxxxx.com', expectedResult: 'david.gilbertson@xxxxx.com'},
  136. {firstName: 'david', lastName: 'gilbertson', email: 'daivd.gilbertson@xxxxx.com', expectedResult: 'david.gilbertson@xxxxx.com'},
  137. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertsin@xxxxx.com', expectedResult: 'david.gilbertson@xxxxx.com'},
  138. {firstName: 'david', lastName: 'gilbertson', email: 'david.gilbertsson@xxxxx.com', expectedResult: 'david.gilbertson@xxxxx.com'},
  139. ];
  140.  
  141. testInputs.forEach(testInput => {
  142. console.assert(
  143. testInput.expectedResult === checkForTypo(testInput),
  144. `${testInput.email} was not given the suggestion: ${testInput.expectedResult}`
  145. );
  146. });
  147. }
  148.  
  149. runTests();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement