How I built a Region-Aware Phone Number Generator in TypeScript
Source: Dev.to
The Challenge
Different Formats: US numbers look like (###) ###-####, while others might look like #### ####.
Uniqueness: When generating 10,000 numbers for a database seed, we can’t have duplicates.
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 the 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(); // 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.
🚀 Random Phone Number Generator (Online Tool)
It runs entirely in your browser, so it’s privacy‑friendly and super fast.
Happy coding!