DEV Community

limaodev
limaodev

Posted on

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, 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
};
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)