Represents a phone number with international format support.

PhoneNumber is immutable and automatically normalizes the format. Supports E.164 format validation (international standard).

const result = PhoneNumber.create('+1234567890');
if (result.isSuccess) {
const phone = result.value;
console.log(phone.value); // "+1234567890"
console.log(phone.countryCode); // "+1"
}

Hierarchy (View Summary)

Accessors

  • get countryCode(): string

    Gets the country code from the phone number. Uses the CountryCallingCodeRegistry for accurate code detection.

    Returns string

    const phone = PhoneNumber.create('+1234567890').unwrap();
    console.log(phone.countryCode); // "+1"
  • get nationalNumber(): string

    Gets the national number (without country code).

    Returns string

    const phone = PhoneNumber.create('+1234567890').unwrap();
    console.log(phone.nationalNumber); // "234567890"

Methods

  • Compares this Value Object with another for equality. Value Objects are equal if they are of the same type and all equality components match.

    Parameters

    • other: ValueObject

      The other Value Object to compare with

    Returns boolean

    true if the Value Objects are equal, false otherwise

  • Creates a PhoneNumber instance with validation. Accepts various formats and normalizes to E.164 format.

    Parameters

    • value: string

      The phone number string (with or without formatting)

    Returns Result<PhoneNumber, DomainError>

    Success with PhoneNumber or Failure with DomainError

    const result = PhoneNumber.create('+1 (555) 123-4567');
    if (result.isSuccess) {
    console.log(result.value.value); // "+15551234567"
    }

    const invalid = PhoneNumber.create('invalid');
    if (invalid.isFailure) {
    console.log(invalid.error.code); // "INVALID_PHONE_FORMAT"
    }
  • Gets the country name for this phone number.

    Returns undefined | string

    The country name or undefined if not found

    const phone = PhoneNumber.create('+52123456789').unwrap();
    console.log(phone.getCountryName()); // "Mexico"
  • Gets the ISO 2-letter country code.

    Returns undefined | string

    The ISO2 code or undefined if not found

    const phone = PhoneNumber.create('+52123456789').unwrap();
    console.log(phone.getISO2()); // "MX"
  • Gets the ISO 3-letter country code.

    Returns undefined | string

    The ISO3 code or undefined if not found

    const phone = PhoneNumber.create('+52123456789').unwrap();
    console.log(phone.getISO3()); // "MEX"
  • Formats the phone number in a human-readable format.

    Returns string

    A formatted phone number string

    const phone = PhoneNumber.create('+15551234567').unwrap();
    console.log(phone.format()); // "+1 (555) 123-4567"

Properties

value: string