All files bsnGenerators.ts

96.55% Statements 28/29
87.5% Branches 7/8
100% Functions 2/2
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x 1x   19x 19x 19x 133x     19x 19x 152x     19x 19x 19x 17x 17x   17x             1x 19x 19x 19x 152x   19x 19x      
// src/bsnGenerators.ts
 
/**
 * Genereert een geldig Nederlands BSN (Burger Service Nummer) voor testdoeleinden.
 * Een BSN is 9 cijfers en voldoet aan de 11-proef.
 */
export function generateTestBSN(): string {
  while (true) {
    // Genereer 8 random cijfers, eerste cijfer mag niet 0 zijn
    const firstDigit = Math.floor(Math.random() * 9) + 1;
    const digits = [firstDigit];
    for (let i = 1; i < 8; i++) {
      digits.push(Math.floor(Math.random() * 10));
    }
    // Bereken het controlecijfer volgens de 11-proef
    let sum = 0;
    for (let i = 0; i < 8; i++) {
      sum += digits[i] * (9 - i);
    }
    // Controlecijfer kan negatief zijn
    let control = sum % 11;
    let lastDigit = 11 - control;
    if (lastDigit === 10) continue; // 10 is ongeldig
    if (lastDigit === 11) lastDigit = 0;
    const bsn = [...digits, lastDigit].join("");
    // Controleer of het voldoet aan de 11-proef
    if (isValidBSN(bsn)) return bsn;
  }
}
 
/**
 * Valideert of een string een geldig BSN is (11-proef).
 */
export function isValidBSN(bsn: string): boolean {
  Iif (!/^[0-9]{9}$/.test(bsn)) return false;
  let sum = 0;
  for (let i = 0; i < 8; i++) {
    sum += parseInt(bsn[i], 10) * (9 - i);
  }
  sum -= parseInt(bsn[8], 10);
  return sum % 11 === 0;
}