Class: Phone
The Phone class represents a phone number and provides methods to retrieve information about it. It implements the PhoneModel interface.
Implements
Implements
Constructors
Constructor
new Phone(phone: RawPhoneType, country?: Country): Phone;The Phone class constructor takes a phone argument of type RawPhoneType and an optional country argument of type RawCountryType. It initializes Phone instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
phone | RawPhoneType | The phone number to be parsed and validated. |
country? | Country | The country code of the phone number. If not provided, the country will be guessed based on the phone number. |
Returns
Phone
Properties
| Property | Modifier | Type | Description |
|---|---|---|---|
country | readonly | CountryOrUnknown | Returns the country code of the phone number. |
e164 | readonly | string | Returns the E.164 format of the phone number. Remarks If the phone number is not a valid phone number, the initial phone number which was passed to the constructor is returned. |
international | readonly | string | Returns the international format of the phone number. Remarks If the phone number is not a valid phone number, the initial phone number which was passed to the constructor is returned. |
mobile | readonly | boolean | Returns whether the phone number is a mobile number or not. |
national | readonly | string | Returns the national format of the phone number. Remarks If the phone number is not a valid phone number, the initial phone number which was passed to the constructor is returned. |
phone | readonly | string | Returns the phone number as a string stripped of all non-numeric characters. |
raw | readonly | string | Returns the raw phone number. Remarks If the phone number is not a valid phone number, the initial phone number which was passed to the constructor is returned. |
timezone | readonly | PossiblePhoneTimezone | Returns the timezone of the phone number. |
type | readonly | | "FIXED_LINE" | "MOBILE" | "FIXED_LINE_OR_MOBILE" | "TOLL_FREE" | "PREMIUM_RATE" | "SHARED_COST" | "VOIP" | "PERSONAL_NUMBER" | "PAGER" | "UAN" | "VOICEMAIL" | "UNKNOWN" | "INVALID" | Returns the type of the phone number. |
valid | readonly | boolean | Returns whether the phone number uses a valid format for the parsed country or not. |
Methods
inspect()
inspect(): string;Returns a stringified representation of the phone number.
Returns
string
- A string representation of the phone number.
serialize()
serialize(): string;Serializes the phone object to an obfuscated string which can be used to recreate the phone object from the Phone.deserialize method.
Returns
string
- The serialized phone object.
toJSON()
toJSON(): {
country: CountryOrUnknown;
e164: string;
international: string;
mobile: boolean;
national: string;
phone: string;
raw: string;
timezone: PossiblePhoneTimezone;
type: | "FIXED_LINE"
| "MOBILE"
| "FIXED_LINE_OR_MOBILE"
| "TOLL_FREE"
| "PREMIUM_RATE"
| "SHARED_COST"
| "VOIP"
| "PERSONAL_NUMBER"
| "PAGER"
| "UAN"
| "VOICEMAIL"
| "UNKNOWN"
| "INVALID";
valid: boolean;
};Returns a JSON representation of the phone number.
Returns
- A JSON-safe representation of the phone number.
| Name | Type | Description |
|---|---|---|
country | CountryOrUnknown | The country code of the phone number. Remarks In cases where the country was not recognized and could not be guessed from the phone number, 'XX' is used. |
e164 | string | The phone number as a string in the E.164 format. |
international | string | The phone number as a string in the international format for the parsed country. |
mobile | boolean | Whether the phone number is possibly a mobile number or not. |
national | string | The phone number as a string in the national format for the parsed country. |
phone | string | The phone number as a string stripped of all non-numeric characters. |
raw | string | The phone number as a string stripped of all non-numeric characters. |
timezone | PossiblePhoneTimezone | The estimated timezone of the phone number based on the phone number's country. It can be either a CountryTimezone or 'UTC'. |
type | | "FIXED_LINE" | "MOBILE" | "FIXED_LINE_OR_MOBILE" | "TOLL_FREE" | "PREMIUM_RATE" | "SHARED_COST" | "VOIP" | "PERSONAL_NUMBER" | "PAGER" | "UAN" | "VOICEMAIL" | "UNKNOWN" | "INVALID" | The type of the phone number. |
valid | boolean | Whether the phone number uses a valid format for the parsed country or not. |
Implementation of
toObject()
toObject(): {
country: CountryOrUnknown;
e164: string;
international: string;
mobile: boolean;
national: string;
phone: string;
raw: string;
timezone: PossiblePhoneTimezone;
type: | "FIXED_LINE"
| "MOBILE"
| "FIXED_LINE_OR_MOBILE"
| "TOLL_FREE"
| "PREMIUM_RATE"
| "SHARED_COST"
| "VOIP"
| "PERSONAL_NUMBER"
| "PAGER"
| "UAN"
| "VOICEMAIL"
| "UNKNOWN"
| "INVALID";
valid: boolean;
};Returns an object representation of the phone number.
Returns
- An object representation of the phone number.
| Name | Type | Description |
|---|---|---|
country | CountryOrUnknown | The country code of the phone number. Remarks In cases where the country was not recognized and could not be guessed from the phone number, 'XX' is used. |
e164 | string | The phone number as a string in the E.164 format. |
international | string | The phone number as a string in the international format for the parsed country. |
mobile | boolean | Whether the phone number is possibly a mobile number or not. |
national | string | The phone number as a string in the national format for the parsed country. |
phone | string | The phone number as a string stripped of all non-numeric characters. |
raw | string | The phone number as a string stripped of all non-numeric characters. |
timezone | PossiblePhoneTimezone | The estimated timezone of the phone number based on the phone number's country. It can be either a CountryTimezone or 'UTC'. |
type | | "FIXED_LINE" | "MOBILE" | "FIXED_LINE_OR_MOBILE" | "TOLL_FREE" | "PREMIUM_RATE" | "SHARED_COST" | "VOIP" | "PERSONAL_NUMBER" | "PAGER" | "UAN" | "VOICEMAIL" | "UNKNOWN" | "INVALID" | The type of the phone number. |
valid | boolean | Whether the phone number uses a valid format for the parsed country or not. |
Implementation of
toString()
toString(): string;Returns the E.164 format of the phone number as a string.
Returns
string
- The E.164 format of the phone number.
Implementation of
deserialize()
static deserialize(serialized: string): Phone;Creates a new phone object from a serialized phone object.
Parameters
| Parameter | Type | Description |
|---|---|---|
serialized | string | The serialized phone object returned from the Phone.serialize method. |
Returns
Phone
A Phone instance with the same properties as the original phone object.
Throws
An error if the serialized phone object is not valid.