export function valid(digitString: string): boolean { digitString = digitString.replace(/ /g, ''); if (digitString.length < 2) { return false; } if (digitString.match(/\D/)) { return false; } const len = digitString.length; const zero = 0x30; // ASCII '0' let sum = 0; for (let i = len - 1; i >= 0; i--) { // We know digit is in [0,9] because we filtered against \D above. let digit = (digitString.codePointAt(i) ?? zero) - zero if ((len - i) % 2 == 0) { digit *= 2; if (digit > 9) { digit -= 9; } } sum += digit; sum %= 10; } return sum % 10 === 0; }