Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. # Processing map
  2. ```javascript
  3. const normalize = (value, start1, stop1, start2, stop2, stopAtStop2) => {
  4. const retVal = (start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1)))
  5. return (stopAtStop2 ? (retVal < stop2 ? stop2 : retVal) : retVal);
  6. }
  7. ```
  8.  
  9. # CPF Validation
  10. ```javascript
  11. const cpfValidation = (cpf = '') => {
  12. cpf = cpf.replace(/[^\d]+/g, '');
  13. if (cpf == '') return false;
  14. // Elimina CPFs invalidos conhecidos
  15. if (cpf.length != 11 ||
  16. cpf == "00000000000" ||
  17. cpf == "11111111111" ||
  18. cpf == "22222222222" ||
  19. cpf == "33333333333" ||
  20. cpf == "44444444444" ||
  21. cpf == "55555555555" ||
  22. cpf == "66666666666" ||
  23. cpf == "77777777777" ||
  24. cpf == "88888888888" ||
  25. cpf == "99999999999")
  26. return false;
  27. // Valida 1º digito
  28. let add = 0;
  29. for (let i = 0; i < 9; i++)
  30. add += parseInt(cpf.charAt(i)) * (10 - i);
  31. rev = 11 - (add % 11);
  32. if (rev == 10 || rev == 11)
  33. rev = 0;
  34. if (rev != parseInt(cpf.charAt(9)))
  35. return false;
  36. // Valida 2º digito
  37. add = 0;
  38. for (let i = 0; i < 10; i++)
  39. add += parseInt(cpf.charAt(i)) * (11 - i);
  40. let rev = 11 - (add % 11);
  41. if (rev == 10 || rev == 11)
  42. rev = 0;
  43. if (rev != parseInt(cpf.charAt(10)))
  44. return false;
  45. return true;
  46. }
  47. ```
  48.  
  49. # CNPJ Validation
  50. ```javascript
  51. const validationCNPJ = (c) => {
  52. var b = [6,5,4,3,2,9,8,7,6,5,4,3,2];
  53.  
  54. if((c = c.replace(/[^\d]/g,"")).length != 14)
  55. return false;
  56.  
  57. if(/0{14}/.test(c))
  58. return false;
  59.  
  60. for (var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
  61. if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n))
  62. return false;
  63.  
  64. for (var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
  65. if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n))
  66. return false;
  67.  
  68. return true;
  69. }
  70. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement