All files postcodeGenerator.ts

91.07% Statements 51/56
100% Branches 0/0
94.73% Functions 18/19
91.07% Lines 51/56

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145          1x                         1x   2x                                           1x   2x 2x 2x 2x 2x       1x 1x       1x 1x       1x 1x       1x 1x 1x 1x 1x 1x 1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x 1x       1x 1x 1x 1x 1x       1x 1x       1x 1x       1x      
// src/postcodeGenerator.ts
 
/**
 * Genereert een geldige Nederlandse postcode (formaat: 1234 AB)
 */
export function generateTestDutchPostcode(): string {
  // 1000 t/m 9999
  const numbers = Math.floor(1000 + Math.random() * 9000);
  // Twee hoofdletters, geen F, I, O, Q, U, Y, X, Z (niet gebruikt in NL postcodes)
  const allowed = 'ABCEGHJKLMNPRSTVW';
  const letter1 = allowed[Math.floor(Math.random() * allowed.length)];
  const letter2 = allowed[Math.floor(Math.random() * allowed.length)];
  return `${numbers} ${letter1}${letter2}`;
}
 
/**
 * Valideert of een string een geldige Nederlandse postcode is.
 */
export function isValidDutchPostcode(postcode: string): boolean {
  // Formaat: 1234 AB
  return /^[1-9][0-9]{3} [A-CEGHJKLMNPRSTVW]{2}$/.test(postcode);
}
 
export type SupportedPostcodeCountry =
  | 'Netherlands'
  | 'Germany'
  | 'Belgium'
  | 'France'
  | 'UnitedKingdom'
  | 'Spain'
  | 'Italy'
  | 'Austria'
  | 'Switzerland'
  | 'Sweden'
  | 'Norway'
  | 'Denmark'
  | 'Finland'
  | 'Portugal'
  | 'Ireland'
  | 'Turkey'
  | 'Morocco';
 
export const generateTestPostcode: Record<SupportedPostcodeCountry, () => string> = {
  Netherlands: () => {
    const numbers = Math.floor(1000 + Math.random() * 9000);
    const allowed = 'ABCEGHJKLMNPRSTVW';
    const letter1 = allowed[Math.floor(Math.random() * allowed.length)];
    const letter2 = allowed[Math.floor(Math.random() * allowed.length)];
    return `${numbers} ${letter1}${letter2}`;
  },
  Germany: () => {
    // 01000 - 99999
    const numbers = Math.floor(10000 + Math.random() * 90000);
    return `${numbers}`;
  },
  Belgium: () => {
    // 1000 - 9999
    const numbers = Math.floor(1000 + Math.random() * 9000);
    return `${numbers}`;
  },
  France: () => {
    // 01000 - 99999
    const numbers = Math.floor(10000 + Math.random() * 90000);
    return `${numbers}`;
  },
  UnitedKingdom: () => {
    // Eenvoudig testformaat: AA1 1AA
    const letters = 'ABCDEFGHJKLMNOPRSTUWYZ';
    const letter1 = letters[Math.floor(Math.random() * letters.length)];
    const letter2 = letters[Math.floor(Math.random() * letters.length)];
    const digit = Math.floor(1 + Math.random() * 9);
    const digit2 = Math.floor(1 + Math.random() * 9);
    const letter3 = letters[Math.floor(Math.random() * letters.length)];
    const letter4 = letters[Math.floor(Math.random() * letters.length)];
    return `${letter1}${letter2}${digit} ${digit2}${letter3}${letter4}`;
  },
  Spain: () => {
    // 01000 - 52999
    const numbers = Math.floor(10000 + Math.random() * 43000);
    return `${numbers}`;
  },
  Italy: () => {
    // 00010 - 98168
    const numbers = Math.floor(10 + Math.random() * 98158);
    return `${numbers.toString().padStart(5, '0')}`;
  },
  Austria: () => {
    // 1000 - 9999
    const numbers = Math.floor(1000 + Math.random() * 9000);
    return `${numbers}`;
  },
  Switzerland: () => {
    // 1000 - 9658
    const numbers = Math.floor(1000 + Math.random() * 8659);
    return `${numbers}`;
  },
  Sweden: () => {
    // 100 00 - 984 99
    const numbers = Math.floor(10000 + Math.random() * 88499);
    return `${numbers.toString().slice(0, 3)} ${numbers.toString().slice(3)}`;
  },
  Norway: () => {
    // 0001 - 9991
    const numbers = Math.floor(1 + Math.random() * 9991);
    return `${numbers.toString().padStart(4, '0')}`;
  },
  Denmark: () => {
    // 1000 - 9990
    const numbers = Math.floor(1000 + Math.random() * 8991);
    return `${numbers}`;
  },
  Finland: () => {
    // 00001 - 99999
    const numbers = Math.floor(1 + Math.random() * 99999);
    return `${numbers.toString().padStart(5, '0')}`;
  },
  Portugal: () => {
    // 1000-9999
    const numbers = Math.floor(1000 + Math.random() * 9000);
    return `${numbers}`;
  },
  Ireland: () => {
    // Eircode: A65 F4E2 (voor test: 1 letter+2 digits, spatie, 4 chars)
    const letters = 'ABCDEFGHJKLMNOPRSTUWYZ';
    const letter = letters[Math.floor(Math.random() * letters.length)];
    const digits = Math.floor(10 + Math.random() * 90);
    const suffix = Math.random().toString(36).substring(2, 6).toUpperCase();
    return `${letter}${digits} ${suffix}`;
  },
  Turkey: () => {
    // 01000 - 81999
    const numbers = Math.floor(10000 + Math.random() * 72000);
    return `${numbers}`;
  },
  Morocco: () => {
    // 10000 - 99999
    const numbers = Math.floor(10000 + Math.random() * 90000);
    return `${numbers}`;
  },
};
 
export const postcodeGenerator = {
  generateTestPostcode,
};