Zum Hauptinhalt springen
YouTube Security Videosheise & c't: RAM teurer als sein Gewicht in Gold?!(03.10.2026 um 10:00 Uhr)
•
Videos & Konferenzenheise & c't: RAM teurer als sein Gewicht in Gold?!(03.10.2026 um 10:00 Uhr)
•••
Sichere ProgrammierungA Sanity Check for AI-Generated Cyber Attack Reconstructions(03.10.2026 um 10:31 Uhr)
•
Sichere ProgrammierungStudymat(03.10.2026 um 10:32 Uhr)
••
Sichere ProgrammierungDeadlocks (SQL Server)(03.10.2026 um 10:34 Uhr)
•••
YouTube Security Videosheise & c't: RAM teurer als sein Gewicht in Gold?!(03.10.2026 um 10:00 Uhr)
•
Videos & Konferenzenheise & c't: RAM teurer als sein Gewicht in Gold?!(03.10.2026 um 10:00 Uhr)
•••
Sichere ProgrammierungA Sanity Check for AI-Generated Cyber Attack Reconstructions(03.10.2026 um 10:31 Uhr)
•
Sichere ProgrammierungStudymat(03.10.2026 um 10:32 Uhr)
••
Sichere ProgrammierungDeadlocks (SQL Server)(03.10.2026 um 10:34 Uhr)
•••
Intelligence View
⚡ tsecurity.de Intelligence

How I built a Region-Aware Phone Number Generator in TypeScript

Generating "fake" but valid-looking test data is harder than it looks. Recently, I needed to build a generator that could handle multiple countries (US, China,…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

Generating "fake" but valid-looking test data is harder than it looks. Recently, I needed to build a generator that could handle multiple countries (US, China, UK, etc.) and different formats (E.164 vs National).



Here is the TypeScript logic I used to solve the "Pattern Matching" and "Uniqueness" problems.






The Challenge





  1. Different Formats: US numbers look like (###) ###-####, while others might look like #### ####.


  2. Uniqueness: When generating 10,000 numbers for a database seed, we can't have duplicates.


  3. Efficiency: We need to generate them fast.






The Solution: Pattern Replacement



Instead of hardcoding rules for every country, I used a Pattern Matching approach. Each country has a configuration plan like this:




const plan = {
countryCode: "+1",
nationalPattern: "(###) ###-####", // # represents a digit
digits: 10
};









The Core Loop (TypeScript)



Here is a simplified version of my generatePhoneNumbers function. It uses a Set to track uniqueness and fills the patterns dynamically:




export function generatePhoneNumbers(options: any) {
const { quantity, ensureUnique } = options;
const results = [];
const seen = new Set<string>(); // O(1) lookup for uniqueness

let attempts = 0;
const maxAttempts = quantity * 10; // Prevent infinite loops

while (results.length < quantity && attempts < maxAttempts) {
attempts++;

// 1. Generate raw random digits
const digits = createRandomDigits(10);

// 2. Uniqueness Check
if (ensureUnique && seen.has(digits)) {
continue; // Skip duplicate
}
seen.add(digits);

// 3. Format using Pattern Matching (The magic part)
// We iterate through the pattern and replace '#' with our digits
results.push({
formatted: insertDigits("(###) ###-####", digits),
raw: digits
});
}

return results;
}






The insertDigits helper simply walks through the string: if it sees a #, it pops a digit from our random string; otherwise, it keeps the character (like brackets or spaces).






Try it out



I wrapped this logic (plus support for 50+ countries and CSV export) into a free browser-based tool.



If you need test data for your QA team, you can use the live version here:



🚀 Random Phone Number Generator (Online Tool)



It runs entirely in your browser, so it's privacy-friendly and super fast.



Happy coding!

🔍 CTI & Forensik

Cyber Threat Intelligence & Forensik

ATT&CK-Navigator · IoC-Radar · Exploit-Belege
CTI Threat Relationship Graph
Akteure · Techniken · Beziehungen
2 Knoten · 1 Relationen
CVE / Incident Threat Actor Software MITRE ATT&CK CWE Weakness IoC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How I built a Region-Aware Phone Number Generator in TypeScript

Thematisch verwandte Begriffe: built, RegionAware, Phone, Number · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
Nächster Beitrag