UNPKG

test-numbers-generator

Version:

Generate and validate European test phone numbers (mobile and landline) in safe, non-existent ranges.

145 lines (139 loc) 4.47 kB
// 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, };