"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.iso7064Check = iso7064Check; exports.luhnCheck = luhnCheck; exports.reverseMultiplyAndSum = reverseMultiplyAndSum; exports.verhoeffCheck = verhoeffCheck; /** * Algorithmic validation functions * May be used as is or implemented in the workflow of other validators. */ /* * ISO 7064 validation function * Called with a string of numbers (incl. check digit) * to validate according to ISO 7064 (MOD 11, 10). */ function iso7064Check(str) { var checkvalue = 10; for (var i = 0; i < str.length - 1; i++) { checkvalue = (parseInt(str[i], 10) + checkvalue) % 10 === 0 ? 10 * 2 % 11 : (parseInt(str[i], 10) + checkvalue) % 10 * 2 % 11; } checkvalue = checkvalue === 1 ? 0 : 11 - checkvalue; return checkvalue === parseInt(str[10], 10); } /* * Luhn (mod 10) validation function * Called with a string of numbers (incl. check digit) * to validate according to the Luhn algorithm. */ function luhnCheck(str) { var checksum = 0; var second = false; for (var i = str.length - 1; i >= 0; i--) { if (second) { var product = parseInt(str[i], 10) * 2; if (product > 9) { // sum digits of product and add to checksum checksum += product.toString().split('').map(function (a) { return parseInt(a, 10); }).reduce(function (a, b) { return a + b; }, 0); } else { checksum += product; } } else { checksum += parseInt(str[i], 10); } second = !second; } return checksum % 10 === 0; } /* * Reverse TIN multiplication and summation helper function * Called with an array of single-digit integers and a base multiplier * to calculate the sum of the digits multiplied in reverse. * Normally used in variations of MOD 11 algorithmic checks. */ function reverseMultiplyAndSum(digits, base) { var total = 0; for (var i = 0; i < digits.length; i++) { total += digits[i] * (base - i); } return total; } /* * Verhoeff validation helper function * Called with a string of numbers * to validate according to the Verhoeff algorithm. */ function verhoeffCheck(str) { 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]]; 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 var str_copy = str.split('').reverse().join(''); var checksum = 0; for (var i = 0; i < str_copy.length; i++) { checksum = d_table[checksum][p_table[i % 8][parseInt(str_copy[i], 10)]]; } return checksum === 0; }