algorithms.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Algorithmic validation functions
  3. * May be used as is or implemented in the workflow of other validators.
  4. */
  5. /*
  6. * ISO 7064 validation function
  7. * Called with a string of numbers (incl. check digit)
  8. * to validate according to ISO 7064 (MOD 11, 10).
  9. */
  10. export function iso7064Check(str) {
  11. var checkvalue = 10;
  12. for (var i = 0; i < str.length - 1; i++) {
  13. checkvalue = (parseInt(str[i], 10) + checkvalue) % 10 === 0 ? 10 * 2 % 11 : (parseInt(str[i], 10) + checkvalue) % 10 * 2 % 11;
  14. }
  15. checkvalue = checkvalue === 1 ? 0 : 11 - checkvalue;
  16. return checkvalue === parseInt(str[10], 10);
  17. }
  18. /*
  19. * Luhn (mod 10) validation function
  20. * Called with a string of numbers (incl. check digit)
  21. * to validate according to the Luhn algorithm.
  22. */
  23. export function luhnCheck(str) {
  24. var checksum = 0;
  25. var second = false;
  26. for (var i = str.length - 1; i >= 0; i--) {
  27. if (second) {
  28. var product = parseInt(str[i], 10) * 2;
  29. if (product > 9) {
  30. // sum digits of product and add to checksum
  31. checksum += product.toString().split('').map(function (a) {
  32. return parseInt(a, 10);
  33. }).reduce(function (a, b) {
  34. return a + b;
  35. }, 0);
  36. } else {
  37. checksum += product;
  38. }
  39. } else {
  40. checksum += parseInt(str[i], 10);
  41. }
  42. second = !second;
  43. }
  44. return checksum % 10 === 0;
  45. }
  46. /*
  47. * Reverse TIN multiplication and summation helper function
  48. * Called with an array of single-digit integers and a base multiplier
  49. * to calculate the sum of the digits multiplied in reverse.
  50. * Normally used in variations of MOD 11 algorithmic checks.
  51. */
  52. export function reverseMultiplyAndSum(digits, base) {
  53. var total = 0;
  54. for (var i = 0; i < digits.length; i++) {
  55. total += digits[i] * (base - i);
  56. }
  57. return total;
  58. }
  59. /*
  60. * Verhoeff validation helper function
  61. * Called with a string of numbers
  62. * to validate according to the Verhoeff algorithm.
  63. */
  64. export function verhoeffCheck(str) {
  65. var d_table = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 0, 6, 7, 8, 9, 5], [2, 3, 4, 0, 1, 7, 8, 9, 5, 6], [3, 4, 0, 1, 2, 8, 9, 5, 6, 7], [4, 0, 1, 2, 3, 9, 5, 6, 7, 8], [5, 9, 8, 7, 6, 0, 4, 3, 2, 1], [6, 5, 9, 8, 7, 1, 0, 4, 3, 2], [7, 6, 5, 9, 8, 2, 1, 0, 4, 3], [8, 7, 6, 5, 9, 3, 2, 1, 0, 4], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]];
  66. var p_table = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], [5, 8, 0, 3, 7, 9, 6, 1, 4, 2], [8, 9, 1, 6, 0, 4, 3, 5, 2, 7], [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], [4, 2, 8, 6, 5, 7, 3, 9, 0, 1], [2, 7, 9, 3, 8, 0, 6, 4, 1, 5], [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]]; // Copy (to prevent replacement) and reverse
  67. var str_copy = str.split('').reverse().join('');
  68. var checksum = 0;
  69. for (var i = 0; i < str_copy.length; i++) {
  70. checksum = d_table[checksum][p_table[i % 8][parseInt(str_copy[i], 10)]];
  71. }
  72. return checksum === 0;
  73. }