aboutsummaryrefslogtreecommitdiff
path: root/src/modules/helpers/luhn-check.ts
blob: fd7d54cc6d36e003e112cceb0e5aec95baecd095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * Checks that the given string passes the luhn algorithm.
 *
 * @param str The string to validate.
 */
export function luhnCheck(str: string): boolean {
  return luhnChecksum(str) === 0;
}

/**
 * Calculates the luhn check value for the given string.
 *
 * @param str The string to calculate the check value for.
 * May contain the `L` placeholder at the end.
 */
export function luhnCheckValue(str: string): number {
  const checksum = luhnChecksum(str.replace(/L?$/, '0'));
  return checksum === 0 ? 0 : 10 - checksum;
}

/**
 * Calculates the luhn checksum value for the given value.
 *
 * @param str The string to generate the checksum for.
 */
function luhnChecksum(str: string): number {
  str = str.replaceAll(/[\s-]/g, '');
  let sum = 0;
  let alternate = false;
  for (let i = str.length - 1; i >= 0; i--) {
    let n = Number.parseInt(str[i]);
    if (alternate) {
      n *= 2;
      if (n > 9) {
        n = (n % 10) + 1;
      }
    }

    sum += n;
    alternate = !alternate;
  }

  return sum % 10;
}