Class

PhoneNumber

PhoneNumber(number, optionsopt)

Create a new phone number instance that parses the phone number parameter for its constituent parts, and store them as separate fields in the returned object.

The options object may include any of these properties:

  • locale The locale with which to parse the number. This gives a clue as to which numbering plan to use.
  • mcc The mobile carrier code (MCC) associated with the carrier that the phone is currently connected to, if known. This also can give a clue as to which numbering plan to use
  • onLoad - a callback function to call when this instance is fully loaded. When the onLoad option is given, this class will attempt to load any missing locale data using the ilib loader callback. When the constructor is done (even if the data is already preassembled), the onLoad function is called with the current instance as a parameter, so this callback can be used with preassembled or dynamic loading or a mix of the two.
  • sync - tell whether to load any missing locale data synchronously or asynchronously. If this option is given as "false", then the "onLoad" callback must be given, as the instance returned from this constructor will not be usable for a while.
  • loadParams - an object containing parameters to pass to the loader callback function when locale data is missing. The parameters are not interpretted or modified in any way. They are simply passed along. The object may contain any property/value pairs as long as the calling code is in agreement with the loader callback function as to what those parameters mean.

This function is locale-sensitive, and will assume any number passed to it is appropriate for the given locale. If the MCC is given, this method will assume that numbers without an explicit country code have been dialled within the country given by the MCC. This affects how things like area codes are parsed. If the MCC is not given, this method will use the given locale to determine the country code. If the locale is not explicitly given either, then this function uses the region of current locale as the default.

The input number may contain any formatting characters for the given locale. Each field that is returned in the json object is a simple string of digits with all formatting and whitespace characters removed.

The number is decomposed into its parts, regardless if the number contains formatting characters. If a particular part cannot be extracted from given number, the field will not be returned as a field in the object. If no fields can be extracted from the number at all, then all digits found in the string will be returned in the subscriberNumber field. If the number parameter contains no digits, an empty object is returned.

This instance can contain any of the following fields after parsing is done:

  • vsc - if this number starts with a VSC (Vertical Service Code, or "star code"), this field will contain the star and the code together
  • iddPrefix - the prefix for international direct dialing. This can either be in the form of a plus character or the IDD access code for the given locale
  • countryCode - if this number is an international direct dial number, this is the country code
  • cic - for "dial-around" services (access to other carriers), this is the prefix used as the carrier identification code
  • emergency - an emergency services number
  • mobilePrefix - prefix that introduces a mobile phone number
  • trunkAccess - trunk access code (long-distance access)
  • serviceCode - like a geographic area code, but it is a required prefix for various services
  • areaCode - geographic area codes
  • subscriberNumber - the unique number of the person or company that pays for this phone line
  • extension - in some countries, extensions are dialed directly without going through an operator or a voice prompt system. If the number includes an extension, it is given in this field.
  • invalid - this property is added and set to true if the parser found that the number is invalid in the numbering plan for the country. This method will make its best effort at parsing, but any digits after the error will go into the subscriberNumber field

The following rules determine how the number is parsed:

  1. If the number starts with a character that is alphabetic instead of numeric, do not parse the number at all. There is a good chance that it is not really a phone number. In this case, an empty instance will be returned.
  2. If the phone number uses the plus notation or explicitly uses the international direct dialing prefix for the given locale, then the country code is identified in the number. The rules of given locale are used to parse the IDD prefix, and then the rules of the country in the prefix are used to parse the rest of the number.
  3. If a country code is provided as an argument to the function call, use that country's parsing rules for the number. This is intended for programs like a Contacts application that know what the country is of the person that owns the phone number and can pass that on as a hint.
  4. If the appropriate locale cannot be easily determined, default to using the rules for the current user's region.

Example: parsing the number "+49 02101345345-78" will give the following properties in the resulting phone number instance:

     {
       iddPrefix: "+",
       countryCode: "49",
       areaCode: "02101",
       subscriberNumber: "345345",
       extension: "78"
     }

Note that in this example, because international direct dialing is explicitly used in the number, the part of this number after the IDD prefix and country code will be parsed exactly the same way in all locales with German rules (country code 49).

Regions currently supported are:

  • NANP (North American Numbering Plan) countries - USA, Canada, Bermuda, various Caribbean nations
  • UK
  • Republic of Ireland
  • Germany
  • France
  • Spain
  • Italy
  • Mexico
  • India
  • People's Republic of China
  • Netherlands
  • Belgium
  • Luxembourg
  • Australia
  • New Zealand
  • Singapore
  • Korea
  • Japan
  • Russia
  • Brazil
Constructor

# new PhoneNumber(number, optionsopt)

Parameters:
Name Type Attributes Description
number string | PhoneNumber

A free-form phone number to be parsed, or another phone number instance to copy

options Object <optional>

options that guide the parser in parsing the number

View Source PhoneNumber.js, line 160

Members

string | undefined

# areaCode

The area code prefix of a land line number.

View Source PhoneNumber.js, line 258

string | undefined

# cic

The carrier identification code used to identify alternate long distance or international carriers.

View Source PhoneNumber.js, line 230

string | undefined

# countryCode

The unique IDD country code for the country where the phone number is serviced.

View Source PhoneNumber.js, line 217

PhoneLocale

# destinationLocale

View Source PhoneNumber.js, line 707

string | undefined

# emergency

Identifies an emergency number that is typically short, such as "911" in North America or "112" in many other places in the world.

View Source PhoneNumber.js, line 238

string | undefined

# extension

The direct dial extension number.

View Source PhoneNumber.js, line 271

string

# iddPrefix

The international direct dialing prefix. This is always followed by the country code.

View Source PhoneNumber.js, line 210

string | undefined

# mobilePrefix

The prefix of the subscriber number that indicates that this is the number of a mobile phone.

View Source PhoneNumber.js, line 245

string | undefined

# serviceCode

The prefix that identifies this number as commercial service number.

View Source PhoneNumber.js, line 252

string | undefined

# subscriberNumber

The unique number associated with the subscriber of this phone.

View Source PhoneNumber.js, line 265

string | undefined

# trunkAccess

The digits required to access the trunk.

View Source PhoneNumber.js, line 223

string | undefined

# vsc

The vertical service code. These are codes that typically start with a star or hash, like "*69" for "dial back the last number that called me".

View Source PhoneNumber.js, line 203

Methods

# compare(other) → {number}

This routine will compare the two phone numbers in an locale-sensitive manner to see if they possibly reference the same phone number.

In many places, there are multiple ways to reach the same phone number. In North America for example, you might have a number with the trunk access code of "1" and another without, and they reference the exact same phone number. This is considered a strong match. For a different pair of numbers, one may be a local number and the other a full phone number with area code, which may reference the same phone number if the local number happens to be located in that area code. However, you cannot say for sure if it is in that area code, so it will be considered a somewhat weaker match.

Similarly, in other countries, there are sometimes different ways of reaching the same destination, and the way that numbers match depends on the locale.

The various phone number fields are handled differently for matches. There are various fields that do not need to match at all. For example, you may type equally enter "00" or "+" into your phone to start international direct dialling, so the iddPrefix field does not need to match at all.

Typically, fields that require matches need to match exactly if both sides have a value for that field. If both sides specify a value and those values differ, that is a strong non-match. If one side does not have a value and the other does, that causes a partial match, because the number with the missing field may possibly have an implied value that matches the other number. For example, the numbers "650-555-1234" and "555-1234" have a partial match as the local number "555-1234" might possibly have the same 650 area code as the first number, and might possibly not. If both side do not specify a value for a particular field, that field is considered matching.

The values of following fields are ignored when performing matches:

  • vsc
  • iddPrefix
  • cic
  • trunkAccess

The values of the following fields matter if they do not match:

  • countryCode - A difference causes a moderately strong problem except for certain countries where there is a way to access the same subscriber via IDD and via intranetwork dialling
  • mobilePrefix - A difference causes a possible non-match
  • serviceCode - A difference causes a possible non-match
  • areaCode - A difference causes a possible non-match
  • subscriberNumber - A difference causes a very strong non-match
  • extension - A difference causes a minor non-match
Parameters:
Name Type Description
other string | PhoneNumber

other phone number to compare this one to

View Source PhoneNumber.js, line 1021

non-negative integer describing the percentage quality of the match. 100 means a very strong match (100%), and lower numbers are less and less strong, down to 0 meaning not at all a match.

number

# equals(other) → {boolean}

Determine whether or not the other phone number is exactly equal to the current one.

The difference between the compare method and the equals method is that the compare method compares normalized numbers with each other and returns the degree of match, whereas the equals operator returns true iff the two numbers contain the same fields and the fields are exactly the same. Functions and other non-phone number properties are not compared.

Parameters:
Name Type Description
other string | PhoneNumber

another phone number to compare to this one

View Source PhoneNumber.js, line 1148

true if the numbers are the same, false otherwise

boolean

# normalize(options) → {string|undefined}

This function normalizes the current phone number to a canonical format and returns a string with that phone number. If parts are missing, this function attempts to fill in those parts.

The options object contains a set of properties that can possibly help normalize this number by providing "extra" information to the algorithm. The options parameter may be null or an empty object if no hints can be determined before this call is made. If any particular hint is not available, it does not need to be present in the options object.

The following is a list of hints that the algorithm will look for in the options object:

  • mcc the mobile carrier code of the current network upon which this phone is operating. This is translated into an IDD country code. This is useful if the number being normalized comes from CNAP (callerid) and the MCC is known.
  • defaultAreaCode the area code of the phone number of the current device, if available. Local numbers in a person's contact list are most probably in this same area code.
  • country the 2 letter ISO 3166 code of the country if it is known from some other means such as parsing the physical address of the person associated with the phone number, or the from the domain name of the person's email address
  • networkType specifies whether the phone is currently connected to a CDMA network or a UMTS network. Valid values are the strings "cdma" and "umts". If one of those two strings are not specified, or if this property is left off completely, this method will assume UMTS.

The following are a list of options that control the behaviour of the normalization:

  • assistedDialing if this is set to true, the number will be normalized so that it can dialled directly on the type of network this phone is currently connected to. This allows customers to dial numbers or use numbers in their contact list that are specific to their "home" region when they are roaming and those numbers would not otherwise work with the current roaming carrier as they are. The home region is specified as the phoneRegion system preference that is settable in the regional settings app. With assisted dialling, this method will add or remove international direct dialling prefixes and country codes, as well as national trunk access codes, as required by the current roaming carrier and the home region in order to dial the number properly. If it is not possible to construct a full international dialling sequence from the options and hints given, this function will not modify the phone number, and will return "undefined". If assisted dialling is false or not specified, then this method will attempt to add all the information it can to the number so that it is as fully specified as possible. This allows two numbers to be compared more easily when those two numbers were otherwise only partially specified.
  • sms set this option to true for the following conditions:
    • assisted dialing is turned on
    • the phone number represents the destination of an SMS message
    • the phone is UMTS
    • the phone is SIM-locked to its carrier
    This enables special international direct dialling codes to route the SMS message to the correct carrier. If assisted dialling is not turned on, this option has no affect.
  • manualDialing set this option to true if the user is entering this number on the keypad directly, and false when the number comes from a stored location like a contact entry or a call log entry. When true, this option causes the normalizer to not perform any normalization on numbers that look like local numbers in the home country. If false, all numbers go through normalization. This option only has an effect when the assistedDialing option is true as well, otherwise it is ignored.

If both a set of options and a locale are given, and they offer conflicting information, the options will take precedence. The idea is that the locale tells you the region setting that the user has chosen (probably in firstuse), whereas the the hints are more current information such as where the phone is currently operating (the MCC).

This function performs the following types of normalizations with assisted dialling turned on:

  1. If the current location of the phone matches the home country, this is a domestic call.
    • Remove any iddPrefix and countryCode fields, as they are not needed
    • Add in a trunkAccess field that may be necessary to call a domestic numbers in the home country
  2. If the current location of the phone does not match the home country, attempt to form a whole international number.
    • Add in the area code if it is missing from the phone number and the area code of the current phone is available in the hints
    • Add the country dialling code for the home country if it is missing from the phone number
    • Add or replace the iddPrefix with the correct one for the current country. The phone number will have been parsed with the settings for the home country, so the iddPrefix may be incorrect for the current country. The iddPrefix for the current country can be "+" if the phone is connected to a UMTS network, and either a "+" or a country-dependent sequences of digits for CDMA networks.

This function performs the following types of normalization with assisted dialling turned off:

  • Normalize the international direct dialing prefix to be a plus or the international direct dialling access code for the current country, depending on the network type.
  • If a number is a local number (ie. it is missing its area code), use a default area code from the hints if available. CDMA phones always know their area code, and GSM/UMTS phones know their area code in many instances, but not always (ie. not on Vodaphone or Telcel phones). If the default area code is not available, do not add it.
  • In assisted dialling mode, if a number is missing its country code, use the current MCC number if it is available to figure out the current country code, and prepend that to the number. If it is not available, leave it off. Also, use that country's settings to parse the number instead of the current format locale.
  • For North American numbers with an area code but no trunk access code, add in the trunk access code.
  • For other countries, if the country code is added in step 3, remove the trunk access code when required by that country's conventions for international calls. If the country requires a trunk access code for international calls and it doesn't exist, add one.

This method modifies the current object, and also returns a string containing the normalized phone number that can be compared directly against other normalized numbers. The canonical format for phone numbers that is returned from thhomeLocaleis method is simply an uninterrupted and unformatted string of dialable digits.

Parameters:
Name Type Description
options Object

an object containing options to help in normalizing.

View Source PhoneNumber.js, line 1545

the normalized string, or undefined if the number could not be normalized

string | undefined

# static parseImsi(imsi, options) → {Object|undefined}

Parse an International Mobile Subscriber Identity (IMSI) number into its 3 constituent parts:

  1. mcc - Mobile Country Code, which identifies the country where the phone is currently receiving service.
  2. mnc - Mobile Network Code, which identifies the carrier which is currently providing service to the phone
  3. msin - Mobile Subscription Identifier Number. This is a unique number identifying the mobile phone on the network, which usually maps to an account/subscriber in the carrier's database.

Because this function may need to load data to identify the above parts, you can pass an options object that controls how the data is loaded. The options may contain any of the following properties:

  • onLoad - a callback function to call when the parsing is done. When the onLoad option is given, this method will attempt to load the locale data using the ilib loader callback. When it is done (even if the data is already preassembled), the onLoad function is called with the parsing results as a parameter, so this callback can be used with preassembled or dynamic, synchronous or asynchronous loading or a mix of the above.
  • sync - tell whether to load any missing locale data synchronously or asynchronously. If this option is given as "false", then the "onLoad" callback must be given, as the results returned from this constructor will not be usable for a while.
  • loadParams - an object containing parameters to pass to the loader callback function when locale data is missing. The parameters are not interpretted or modified in any way. They are simply passed along. The object may contain any property/value pairs as long as the calling code is in agreement with the loader callback function as to what those parameters mean.
Parameters:
Name Type Description
imsi string

IMSI number to parse

options Object

options controlling the loading of the locale data

View Source PhoneNumber.js, line 403

components of the IMSI number, when the locale data is loaded synchronously, or undefined if asynchronous

Object | undefined