algorithms.js 3.0 KB

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