test-numbers-generator
Version:
Generate and validate European test phone numbers (mobile and landline) in safe, non-existent ranges.
280 lines (279 loc) • 12.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchPostcodesEnNummerRangeByPlaatsnaam = fetchPostcodesEnNummerRangeByPlaatsnaam;
exports.fetchPostcodesMetHuisnummerToevoeging = fetchPostcodesMetHuisnummerToevoeging;
exports.checkAdresByPostcodeHuisnummer = checkAdresByPostcodeHuisnummer;
exports.getRandomAdresInPlaatsnaam = getRandomAdresInPlaatsnaam;
exports.getRandomPlaatsnaam = getRandomPlaatsnaam;
const node_fetch_1 = __importDefault(require("node-fetch"));
/**
* Haalt maximaal 10 unieke postcodes en het bereik van huisnummers op voor een opgegeven plaatsnaam via de PDOK Locatieserver.
* @param {string} plaatsnaam - De naam van de plaats (bijv. 'Amsterdam')
* @returns {Promise<Array<{ postcode: string, min: number, max: number }>>} - Array met postcode en huisnummerbereik
*/
async function fetchPostcodesEnNummerRangeByPlaatsnaam(plaatsnaam, maxPostcodes = 20) {
const result = new Map();
let start = 0;
const rows = 100; // PDOK limiet
let hasMore = true;
const seenPostcodes = [];
while (hasMore) {
const url = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=woonplaatsnaam:${encodeURIComponent(plaatsnaam)}&fq=type:adres&rows=${rows}&start=${start}`;
const response = await (0, node_fetch_1.default)(url);
if (!response.ok)
throw new Error(`PDOK API error: ${response.status} ${response.statusText}`);
const data = await response.json();
const docs = data.response?.docs || [];
for (const doc of docs) {
if (doc.postcode && doc.huisnummer) {
const postcode = doc.postcode.slice(0, 4) + ' ' + doc.postcode.slice(4, 6);
const huisnummer = parseInt(doc.huisnummer, 10);
if (!result.has(postcode)) {
if (seenPostcodes.length < maxPostcodes) {
seenPostcodes.push(postcode);
result.set(postcode, new Set());
}
}
if (seenPostcodes.includes(postcode)) {
result.get(postcode).add(huisnummer);
}
}
}
if (docs.length < rows) {
hasMore = false;
}
else {
start += rows;
}
if (seenPostcodes.length >= maxPostcodes && Array.from(result.values()).every(set => set.size > 0)) {
hasMore = false;
}
}
return seenPostcodes.map(postcode => {
const nummersSet = result.get(postcode);
const nummers = Array.from(nummersSet);
const min = Math.min(...nummers);
const max = Math.max(...nummers);
return { postcode, min, max };
});
}
/**
* Zoekt alle unieke postcodes in een plaats waar adressen voorkomen met een huisnummertoevoeging (bijv. 12A, 12-B).
* @param {string} plaatsnaam - De naam van de plaats
* @param {number} maxPostcodes - Maximaal aantal postcodes om op te halen (default 50)
* @returns {Promise<Set<string>>} - Set met unieke postcodes (formaat: '1234 AB')
*/
async function fetchPostcodesMetHuisnummerToevoeging(plaatsnaam, maxPostcodes = 20) {
const result = new Set();
let start = 0;
const rows = 100;
let hasMore = true;
while (hasMore && result.size < maxPostcodes) {
// Zoek alleen adressen met een niet-lege huisnummertoevoeging
const url = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=woonplaatsnaam:${encodeURIComponent(plaatsnaam)}+huisnummertoevoeging:*&fq=type:adres&rows=${rows}&start=${start}`;
const response = await (0, node_fetch_1.default)(url);
if (!response.ok)
throw new Error(`PDOK API error: ${response.status} ${response.statusText}`);
const data = await response.json();
const docs = data.response?.docs || [];
for (const doc of docs) {
if (doc.postcode && doc.huisnummertoevoeging) {
const postcode = doc.postcode.slice(0, 4) + ' ' + doc.postcode.slice(4, 6);
result.add(postcode);
if (result.size >= maxPostcodes)
break;
}
}
if (docs.length < rows || result.size >= maxPostcodes) {
hasMore = false;
}
else {
start += rows;
}
}
return result;
}
/**
* Haalt adresgegevens (straatnaam, woonplaats) op via de PDOK Locatieserver op basis van postcode en huisnummer.
* @param {string} postcode - Postcode in formaat '1234 AB' of '1234AB'
* @param {string|number} huisnummer - Huisnummer (zonder toevoeging)
* @returns {Promise<{ straatnaam: string, woonplaats: string } | null>} - Adresgegevens of null als niet gevonden
*/
async function checkAdresByPostcodeHuisnummer(postcode, huisnummer) {
// Verwijder spatie uit postcode voor query
const cleanPostcode = postcode.replace(/\s+/g, '');
const url = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=postcode:${cleanPostcode}+huisnummer:${huisnummer}&fq=type:adres&rows=1`;
const response = await (0, node_fetch_1.default)(url);
if (!response.ok)
throw new Error(`PDOK API error: ${response.status} ${response.statusText}`);
const data = await response.json();
const doc = data.response?.docs?.[0];
if (doc && doc.straatnaam && doc.woonplaatsnaam) {
return {
straatnaam: doc.straatnaam,
woonplaats: doc.woonplaatsnaam
};
}
return null;
}
/**
* Haalt een willekeurig bestaand adres op in een opgegeven plaatsnaam.
* @param {string} plaatsnaam - De naam van de plaats
* @returns {Promise<{ postcode: string, huisnummer: number, straatnaam: string, woonplaats: string } | null>}
*/
async function getRandomAdresInPlaatsnaam(plaatsnaam) {
try {
// Haal eerst unieke postcodes op voor de plaatsnaam
const postcodesSet = new Set();
let start = 0;
const rows = 100;
let hasMore = true;
while (hasMore && postcodesSet.size < 20) {
const url = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=woonplaatsnaam:${encodeURIComponent(plaatsnaam)}&fq=type:adres&rows=${rows}&start=${start}`;
const response = await (0, node_fetch_1.default)(url);
if (!response.ok)
throw new Error(`PDOK API error: ${response.status} ${response.statusText}`);
const data = await response.json();
const docs = data.response?.docs || [];
for (const doc of docs) {
if (doc.postcode) {
const postcode = doc.postcode.slice(0, 4) + ' ' + doc.postcode.slice(4, 6);
postcodesSet.add(postcode);
if (postcodesSet.size >= 20)
break;
}
}
if (docs.length < rows || postcodesSet.size >= 20) {
hasMore = false;
}
else {
start += rows;
}
}
const postcodes = Array.from(postcodesSet);
if (!postcodes.length)
return null;
// Kies een willekeurige postcode
const randomPostcode = postcodes[Math.floor(Math.random() * postcodes.length)];
// Haal ALLE huisnummers op voor deze postcode (met paginering)
let huisnummersSet = new Set();
start = 0;
hasMore = true;
while (hasMore) {
const url = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=postcode:${randomPostcode.replace(' ', '')}&fq=type:adres&rows=${rows}&start=${start}`;
const response = await (0, node_fetch_1.default)(url);
if (!response.ok)
throw new Error(`PDOK API error: ${response.status} ${response.statusText}`);
const data = await response.json();
const docs = data.response?.docs || [];
for (const doc of docs) {
if (doc.huisnummer) {
huisnummersSet.add(parseInt(doc.huisnummer, 10));
}
}
if (docs.length < rows) {
hasMore = false;
}
else {
start += rows;
}
}
const uniekeHuisnummers = Array.from(huisnummersSet).filter(hn => hn > 0);
// Filter huisnummer 1 uit als er meer dan 1 huisnummer is
const huisnummersZonder1 = uniekeHuisnummers.length > 1 ? uniekeHuisnummers.filter(hn => hn !== 1) : uniekeHuisnummers;
if (!huisnummersZonder1.length)
return null;
// Kies een willekeurig bestaand huisnummer uit de lijst
const huisnummer = huisnummersZonder1[Math.floor(Math.random() * huisnummersZonder1.length)];
const adres = await checkAdresByPostcodeHuisnummer(randomPostcode, huisnummer);
if (!adres)
return null;
return {
postcode: randomPostcode,
huisnummer,
straatnaam: adres.straatnaam,
woonplaats: adres.woonplaats
};
}
catch (e) {
return null;
}
}
/**
* Haalt maximaal 20 unieke postcodes en ALLE unieke huisnummers per postcode op voor een opgegeven plaatsnaam via de PDOK Locatieserver.
* @param {string} plaatsnaam - De naam van de plaats (bijv. 'Amsterdam')
* @param {number} maxPostcodes - Maximaal aantal postcodes om op te halen (default 20)
* @returns {Promise<Array<{ postcode: string, huisnummers: number[] }>>} - Array met postcode en alle unieke huisnummers
*/
async function fetchPostcodesEnUniekeHuisnummersByPlaatsnaam(plaatsnaam, maxPostcodes = 20) {
const result = new Map();
let start = 0;
const rows = 100; // PDOK limiet
let hasMore = true;
const seenPostcodes = [];
while (hasMore) {
const url = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=woonplaatsnaam:${encodeURIComponent(plaatsnaam)}&fq=type:adres&rows=${rows}&start=${start}`;
const response = await (0, node_fetch_1.default)(url);
if (!response.ok)
throw new Error(`PDOK API error: ${response.status} ${response.statusText}`);
const data = await response.json();
const docs = data.response?.docs || [];
for (const doc of docs) {
if (doc.postcode && doc.huisnummer) {
const postcode = doc.postcode.slice(0, 4) + ' ' + doc.postcode.slice(4, 6);
const huisnummer = parseInt(doc.huisnummer, 10);
if (!result.has(postcode)) {
if (seenPostcodes.length < maxPostcodes) {
seenPostcodes.push(postcode);
result.set(postcode, new Set());
}
}
if (seenPostcodes.includes(postcode)) {
result.get(postcode).add(huisnummer);
}
}
}
if (docs.length < rows) {
hasMore = false;
}
else {
start += rows;
}
if (seenPostcodes.length >= maxPostcodes && Array.from(result.values()).every(set => set.size > 0)) {
hasMore = false;
}
}
return seenPostcodes.map(postcode => {
const nummersSet = result.get(postcode);
const nummers = Array.from(nummersSet);
return { postcode, huisnummers: nummers };
});
}
/**
* Haalt een lijst van plaatsnamen op uit de PDOK Locatieserver en kiest er willekeurig één uit.
* Gebruikt een random letter/cijfer als prefix voor meer variatie.
*/
async function getRandomPlaatsnaam() {
try {
// Kies een random letter (A-Z) of cijfer (0-9) als prefix
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const randomChar = chars[Math.floor(Math.random() * chars.length)];
// Zoek plaatsnamen die beginnen met deze letter/cijfer
const url = `https://api.pdok.nl/bzk/locatieserver/search/v3_1/free?q=woonplaatsnaam:${randomChar}*&fq=type:woonplaats&rows=100`;
const response = await (0, node_fetch_1.default)(url);
if (!response.ok)
return null;
const data = await response.json();
const plaatsnamen = (data.response?.docs || []).map((doc) => doc.woonplaatsnaam).filter(Boolean);
if (plaatsnamen.length === 0)
return null;
const randomIndex = Math.floor(Math.random() * plaatsnamen.length);
return plaatsnamen[randomIndex];
}
catch (e) {
return null;
}
}