• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @license
3 * Copyright (C) 2010 The Libphonenumber Authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @fileoverview  Phone Number Parser Demo.
20 *
21 * @author Nikolaos Trogkanis
22 */
23goog.provide('i18n.phonenumbers.demo');
24
25goog.require('goog.dom');
26goog.require('goog.proto2.ObjectSerializer');
27goog.require('goog.string.StringBuffer');
28goog.require('i18n.phonenumbers.AsYouTypeFormatter');
29goog.require('i18n.phonenumbers.PhoneNumberFormat');
30goog.require('i18n.phonenumbers.PhoneNumberType');
31goog.require('i18n.phonenumbers.PhoneNumberUtil');
32goog.require('i18n.phonenumbers.PhoneNumberUtil.ValidationResult');
33goog.require('i18n.phonenumbers.ShortNumberInfo');
34
35
36/**
37 * @const
38 * @type {!i18n.phonenumbers.PhoneNumberUtil}
39 * @private
40 */
41var phoneUtil_ = i18n.phonenumbers.PhoneNumberUtil.getInstance();
42
43function phoneNumberParser() {
44  var $ = goog.dom.getElement;
45  var phoneNumber = $('phoneNumber').value;
46  var regionCode = $('defaultCountry').value.toUpperCase();
47  var carrierCode = $('carrierCode').value;
48  var output = new goog.string.StringBuffer();
49  try {
50    var number = phoneUtil_.parseAndKeepRawInput(phoneNumber, regionCode);
51    output.append('****Parsing Result:****\n');
52    output.append(JSON.stringify(
53        new goog.proto2
54            .ObjectSerializer(goog.proto2.ObjectSerializer.KeyOption.NAME)
55            .serialize(number)));
56    output.append('\n\n****Validation Results:****');
57    var isPossible = phoneUtil_.isPossibleNumber(number);
58    output.append('\nResult from isPossibleNumber(): ');
59    output.append(isPossible);
60    var validationResult = i18n.phonenumbers.PhoneNumberUtil.ValidationResult;
61    var isPossibleReason = phoneUtil_.isPossibleNumberWithReason(number)
62    var hasRegionCode = regionCode && regionCode != 'ZZ';
63    if (isPossible) {
64      // Checking as isValid() fails if possible local only.
65      if (isPossibleReason == validationResult.IS_POSSIBLE_LOCAL_ONLY) {
66        output.append('\nResult from isPossibleNumberWithReason(): ');
67        output.append('IS_POSSIBLE_LOCAL_ONLY');
68        output.append(
69            '\nNumber is considered invalid as it is ' +
70            'not a possible national number.');
71      } else {
72        var isNumberValid = phoneUtil_.isValidNumber(number);
73        output.append('\nResult from isValidNumber(): ');
74        output.append(isNumberValid);
75        if (isNumberValid && hasRegionCode) {
76          output.append('\nResult from isValidNumberForRegion(): ');
77          output.append(phoneUtil_.isValidNumberForRegion(number, regionCode));
78        }
79        output.append('\nPhone Number region: ');
80        output.append(phoneUtil_.getRegionCodeForNumber(number));
81        output.append('\nResult from getNumberType(): ');
82        output.append(getNumberTypeString(number));
83      }
84    } else {
85      output.append('\nResult from isPossibleNumberWithReason(): ');
86      switch (isPossibleReason) {
87        case validationResult.INVALID_COUNTRY_CODE:
88          output.append('INVALID_COUNTRY_CODE');
89          break;
90        case validationResult.TOO_SHORT:
91          output.append('TOO_SHORT');
92          break;
93        case validationResult.TOO_LONG:
94          output.append('TOO_LONG');
95          break;
96        case validationResult.INVALID_LENGTH:
97          output.append('INVALID_LENGTH');
98          break;
99      }
100      // IS_POSSIBLE shouldn't happen, since we only call this if _not_
101      // possible.
102      output.append(
103          '\nNote: Numbers that are not possible have type UNKNOWN,' +
104          ' an unknown region, and are considered invalid.');
105    }
106    if (!isNumberValid) {
107      var shortInfo = i18n.phonenumbers.ShortNumberInfo.getInstance();
108      output.append('\n\n****ShortNumberInfo Results:****');
109      output.append('\nResult from isPossibleShortNumber: ');
110      output.append(shortInfo.isPossibleShortNumber(number));
111      output.append('\nResult from isValidShortNumber: ');
112      output.append(shortInfo.isValidShortNumber(number));
113      if (hasRegionCode) {
114        output.append('\nResult from isPossibleShortNumberForRegion: ');
115        output.append(
116            shortInfo.isPossibleShortNumberForRegion(number, regionCode));
117        output.append('\nResult from isValidShortNumberForRegion: ');
118        output.append(
119            shortInfo.isValidShortNumberForRegion(number, regionCode));
120      }
121    }
122
123    var PNF = i18n.phonenumbers.PhoneNumberFormat;
124    output.append('\n\n****Formatting Results:**** ');
125    output.append('\nE164 format: ');
126    output.append(
127        isNumberValid ? phoneUtil_.format(number, PNF.E164) : 'invalid');
128    output.append('\nOriginal format: ');
129    output.append(phoneUtil_.formatInOriginalFormat(number, regionCode));
130    output.append('\nNational format: ');
131    output.append(phoneUtil_.format(number, PNF.NATIONAL));
132    output.append('\nInternational format: ');
133    output.append(
134        isNumberValid ? phoneUtil_.format(number, PNF.INTERNATIONAL) :
135                        'invalid');
136    output.append('\nOut-of-country format from US: ');
137    output.append(
138        isNumberValid ?
139            phoneUtil_.formatOutOfCountryCallingNumber(number, 'US') :
140            'invalid');
141    output.append('\nOut-of-country format from Switzerland: ');
142    output.append(
143        isNumberValid ?
144            phoneUtil_.formatOutOfCountryCallingNumber(number, 'CH') :
145            'invalid');
146    if (carrierCode.length > 0) {
147      output.append('\nNational format with carrier code: ');
148      output.append(
149          phoneUtil_.formatNationalNumberWithCarrierCode(number, carrierCode));
150    }
151    output.append('\nFormat for mobile dialing (calling from US): ');
152    output.append(
153        isNumberValid ?
154            phoneUtil_.formatNumberForMobileDialing(number, 'US', true) :
155            'invalid');
156    output.append('\nFormat for national dialing with preferred carrier code and empty fallback carrier code: ');
157    output.append(
158        isNumberValid ?
159            phoneUtil_.formatNationalNumberWithPreferredCarrierCode(number, '') :
160            'invalid');
161    output.append('\n\n****AsYouTypeFormatter Results****');
162    var formatter = new i18n.phonenumbers.AsYouTypeFormatter(regionCode);
163    var phoneNumberLength = phoneNumber.length;
164    for (var i = 0; i < phoneNumberLength; ++i) {
165      var inputChar = phoneNumber.charAt(i);
166      output.append('\nChar entered: ');
167      output.append(inputChar);
168      output.append(' Output: ');
169      output.append(formatter.inputDigit(inputChar));
170    }
171  } catch (e) {
172    output.append('\n' + e.toString());
173  }
174  $('output').value = output.toString();
175  return false;
176}
177
178function getNumberTypeString(number) {
179  switch (phoneUtil_.getNumberType(number)) {
180    case i18n.phonenumbers.PhoneNumberType.FIXED_LINE:
181      return 'FIXED_LINE';
182    case i18n.phonenumbers.PhoneNumberType.MOBILE:
183      return 'MOBILE'
184    case i18n.phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE:
185      return 'FIXED_LINE_OR_MOBILE';
186    case i18n.phonenumbers.PhoneNumberType.TOLL_FREE:
187      return 'TOLL_FREE';
188    case i18n.phonenumbers.PhoneNumberType.PREMIUM_RATE:
189      return 'PREMIUM_RATE';
190    case i18n.phonenumbers.PhoneNumberType.SHARED_COST:
191      return 'SHARED_COST';
192    case i18n.phonenumbers.PhoneNumberType.VOIP:
193      return 'VOIP';
194    case i18n.phonenumbers.PhoneNumberType.PERSONAL_NUMBER:
195      return 'PERSONAL_NUMBER';
196    case i18n.phonenumbers.PhoneNumberType.PAGER:
197      return 'PAGER';
198    case i18n.phonenumbers.PhoneNumberType.UAN:
199      return 'UAN';
200    case i18n.phonenumbers.PhoneNumberType.UNKNOWN:
201      return 'UNKNOWN';
202  }
203}
204
205goog.exportSymbol('phoneNumberParser', phoneNumberParser);
206