or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

examples-formatting.mdfinding.mdindex.mdnumber-from.mdparsing.mdregion-utilities.md
tile.json

number-from.mddocs/

Phone Number From

Utility for determining how a phone number would be dialed from a specific country. This functionality is essential for international calling applications, travel apps, and telecommunications systems that need to show users the correct dialing format based on their location.

Capabilities

Get Number From

Calculates how a valid phone number should be dialed when calling from a specific region or country.

/**
 * Get a phone number string as it would be called from another country.
 * 
 * @param parsedPhoneNumber A valid phone number object as returned from parsePhoneNumber()
 * @param regionCode Optional region code of the country to call from (defaults to international format)
 * @returns PhoneNumberFrom object with dialing information
 */
function getNumberFrom(
  parsedPhoneNumber: ParsedPhoneNumberValid,
  regionCode?: string
): PhoneNumberFrom;

type PhoneNumberFrom = PhoneNumberFromValid | PhoneNumberFromInvalid;

Usage Examples:

import { parsePhoneNumber, getNumberFrom } from "awesome-phonenumber";

// Parse a Swedish mobile number
const swedenNumber = parsePhoneNumber("+46707123456");

if (swedenNumber.valid) {
  // How to dial from Sweden (domestic)
  const fromSweden = getNumberFrom(swedenNumber, "SE");
  if (fromSweden.valid) {
    console.log(fromSweden.number); // "070-712 34 56" (national format)
  }
  
  // How to dial from United States
  const fromUS = getNumberFrom(swedenNumber, "US");
  if (fromUS.valid) {
    console.log(fromUS.number); // "011 46 70 712 34 56" (international with US prefix)
  }
  
  // How to dial from Germany
  const fromGermany = getNumberFrom(swedenNumber, "DE");
  if (fromGermany.valid) {
    console.log(fromGermany.number); // "00 46 70 712 34 56" (international with DE prefix)
  }
  
  // Default international format (no region specified)
  const international = getNumberFrom(swedenNumber);
  if (international.valid) {
    console.log(international.number); // "+46 70 712 34 56"
  }
}

Result Types

PhoneNumberFromValid

Result for successful "number from" calculations.

interface PhoneNumberFromValid {
  /** Always true for successful results */
  valid: true;
  
  /** The number as it should be dialed from the specified region */
  number: string;
}

PhoneNumberFromInvalid

Result for failed "number from" calculations.

interface PhoneNumberFromInvalid {
  /** Always false for failed results */
  valid: false;
  
  /** Optional number string (may be present even when invalid) */
  number?: string;
  
  /** Optional error information */
  error?: unknown;
}

Advanced Usage Patterns

International Calling Application

import { parsePhoneNumber, getNumberFrom } from "awesome-phonenumber";

class InternationalDialer {
  private userRegion: string;
  
  constructor(userRegion: string) {
    this.userRegion = userRegion;
  }
  
  getDialingInstructions(phoneNumberStr: string, targetRegion?: string) {
    // Parse the phone number
    const parsed = parsePhoneNumber(phoneNumberStr, { regionCode: targetRegion });
    
    if (!parsed.valid) {
      return {
        success: false,
        error: "Invalid phone number",
        possibility: parsed.possibility
      };
    }
    
    // Get dialing format from user's region
    const dialingResult = getNumberFrom(parsed, this.userRegion);
    
    if (!dialingResult.valid) {
      return {
        success: false,
        error: "Cannot determine dialing format"
      };
    }
    
    // Determine if it's domestic or international
    const isDomestic = parsed.regionCode === this.userRegion;
    
    return {
      success: true,
      dialingNumber: dialingResult.number,
      originalNumber: parsed.number.international,
      isDomestic,
      targetCountry: parsed.regionCode,
      targetType: parsed.type,
      instructions: isDomestic 
        ? `Dial ${dialingResult.number}` 
        : `Dial ${dialingResult.number} to reach ${parsed.regionCode}`
    };
  }
  
  setUserRegion(newRegion: string) {
    this.userRegion = newRegion;
  }
}

// Usage
const dialer = new InternationalDialer("US");

console.log(dialer.getDialingInstructions("+46707123456"));
// {
//   success: true,
//   dialingNumber: "011 46 70 712 34 56",
//   originalNumber: "+46 70 712 34 56",
//   isDomestic: false,
//   targetCountry: "SE",
//   targetType: "mobile",
//   instructions: "Dial 011 46 70 712 34 56 to reach SE"
// }

console.log(dialer.getDialingInstructions("2125551234", "US"));
// {
//   success: true,
//   dialingNumber: "(212) 555-1234",
//   originalNumber: "+1 212-555-1234",
//   isDomestic: true,
//   targetCountry: "US",
//   targetType: "fixed-line",
//   instructions: "Dial (212) 555-1234"
// }

Travel Application

import { parsePhoneNumber, getNumberFrom, getSupportedRegionCodes } from "awesome-phonenumber";

class TravelDialingGuide {
  generateDialingGuide(phoneNumber: string, fromRegions: string[]) {
    const parsed = parsePhoneNumber(phoneNumber);
    
    if (!parsed.valid) {
      throw new Error(`Invalid phone number: ${phoneNumber}`);
    }
    
    const guide = {
      targetNumber: parsed.number.international,
      targetCountry: parsed.regionCode,
      targetType: parsed.type,
      dialingFromRegions: new Map<string, any>()
    };
    
    fromRegions.forEach(region => {
      const fromResult = getNumberFrom(parsed, region);
      
      if (fromResult.valid) {
        const isDomestic = parsed.regionCode === region;
        
        guide.dialingFromRegions.set(region, {
          dialingNumber: fromResult.number,
          isDomestic,
          cost: this.estimateCallCost(isDomestic, parsed.type),
          instructions: this.generateInstructions(region, fromResult.number, isDomestic)
        });
      }
    });
    
    return guide;
  }
  
  private estimateCallCost(isDomestic: boolean, phoneType: string): string {
    if (isDomestic) {
      return phoneType === 'mobile' ? 'Standard mobile rate' : 'Local rate';
    }
    return 'International rate';
  }
  
  private generateInstructions(fromRegion: string, dialingNumber: string, isDomestic: boolean): string {
    const prefix = isDomestic ? 'Local call' : 'International call';
    return `${prefix}: Dial ${dialingNumber}`;
  }
}

// Usage for a traveler
const guide = new TravelDialingGuide();
const result = guide.generateDialingGuide("+46707123456", ["US", "GB", "DE", "SE"]);

console.log(result);

Contact Management System

import { parsePhoneNumber, getNumberFrom } from "awesome-phonenumber";

interface Contact {
  name: string;
  phoneNumbers: string[];
  region?: string;
}

class ContactManager {
  private userRegion: string;
  
  constructor(userRegion: string) {
    this.userRegion = userRegion;
  }
  
  formatContactForDisplay(contact: Contact) {
    const formattedNumbers = contact.phoneNumbers.map(phoneStr => {
      const parsed = parsePhoneNumber(phoneStr, { regionCode: contact.region });
      
      if (!parsed.valid) {
        return {
          original: phoneStr,
          display: phoneStr,
          dialable: phoneStr,
          valid: false
        };
      }
      
      const dialingResult = getNumberFrom(parsed, this.userRegion);
      
      return {
        original: phoneStr,
        display: parsed.number.international,
        dialable: dialingResult.valid ? dialingResult.number : parsed.number.international,
        valid: true,
        type: parsed.type,
        country: parsed.regionCode,
        isDomestic: parsed.regionCode === this.userRegion
      };
    });
    
    return {
      name: contact.name,
      numbers: formattedNumbers
    };
  }
  
  generateClickToCall(contact: Contact, phoneIndex: number = 0) {
    const phoneStr = contact.phoneNumbers[phoneIndex];
    if (!phoneStr) return null;
    
    const parsed = parsePhoneNumber(phoneStr, { regionCode: contact.region });
    if (!parsed.valid) return null;
    
    const dialingResult = getNumberFrom(parsed, this.userRegion);
    if (!dialingResult.valid) return null;
    
    // Generate tel: URI for click-to-call
    const telUri = `tel:${dialingResult.number.replace(/[^\d+]/g, '')}`;
    
    return {
      uri: telUri,
      displayNumber: dialingResult.number,
      label: `Call ${contact.name}`
    };
  }
}

// Usage
const contacts: Contact[] = [
  { name: "John Doe", phoneNumbers: ["+46707123456"], region: "SE" },
  { name: "Jane Smith", phoneNumbers: ["2125551234", "5551234567"], region: "US" }
];

const contactManager = new ContactManager("US");

contacts.forEach(contact => {
  const formatted = contactManager.formatContactForDisplay(contact);
  console.log(`${formatted.name}:`);
  
  formatted.numbers.forEach((num, index) => {
    console.log(`  ${num.display} (dial: ${num.dialable}) [${num.type}]`);
    
    const clickToCall = contactManager.generateClickToCall(contact, index);
    if (clickToCall) {
      console.log(`    Click-to-call: ${clickToCall.uri}`);
    }
  });
});

Multi-Region Call Center Application

import { parsePhoneNumber, getNumberFrom } from "awesome-phonenumber";

class CallCenterDialer {
  private agentRegions: Map<string, string> = new Map();
  
  registerAgent(agentId: string, regionCode: string) {
    this.agentRegions.set(agentId, regionCode);
  }
  
  getDialingInstructionsForAgent(agentId: string, customerPhoneNumber: string) {
    const agentRegion = this.agentRegions.get(agentId);
    if (!agentRegion) {
      throw new Error(`Unknown agent: ${agentId}`);
    }
    
    const parsed = parsePhoneNumber(customerPhoneNumber);
    if (!parsed.valid) {
      throw new Error(`Invalid customer phone number: ${customerPhoneNumber}`);
    }
    
    const dialingResult = getNumberFrom(parsed, agentRegion);
    if (!dialingResult.valid) {
      throw new Error("Cannot determine dialing format");
    }
    
    return {
      agentId,
      agentRegion,
      customerNumber: parsed.number.international,
      customerRegion: parsed.regionCode,
      customerType: parsed.type,
      dialingNumber: dialingResult.number,
      isDomestic: parsed.regionCode === agentRegion,
      estimatedCost: this.calculateCallCost(agentRegion, parsed.regionCode, parsed.type)
    };
  }
  
  private calculateCallCost(fromRegion: string, toRegion: string, phoneType: string): string {
    if (fromRegion === toRegion) {
      return phoneType === 'mobile' ? '$0.05/min' : '$0.02/min';
    }
    return '$0.15/min';
  }
}

// Usage
const callCenter = new CallCenterDialer();
callCenter.registerAgent("agent001", "US");
callCenter.registerAgent("agent002", "GB");

const instructions = callCenter.getDialingInstructionsForAgent("agent001", "+46707123456");
console.log(instructions);
// {
//   agentId: "agent001",
//   agentRegion: "US", 
//   customerNumber: "+46 70 712 34 56",
//   customerRegion: "SE",
//   customerType: "mobile",
//   dialingNumber: "011 46 70 712 34 56",
//   isDomestic: false,
//   estimatedCost: "$0.15/min"
// }