Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
// includes
2
goog.require('i18n.phonenumbers.PhoneNumberFormat');
3
goog.require('i18n.phonenumbers.PhoneNumberUtil');
4
 
5
 
6
// format the given number to the given format
7
function formatNumber(number, countryCode, format) {
8
  try {
9
    var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
10
    var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
11
    if (phoneUtil.isPossibleNumber(numberObj)) {
12
        format = (typeof format == "undefined") ? i18n.phonenumbers.PhoneNumberFormat.E164 : format;
13
        return phoneUtil.format(numberObj, format);
14
    } else {
15
        return number;
16
    }
17
  } catch (e) {
18
    return number;
19
  }
20
}
21
 
22
 
23
// get an example number for the given country code
24
function getExampleNumber(countryCode, national, numberType) {
25
  try {
26
    var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
27
    var numberObj = phoneUtil.getExampleNumberForType(countryCode, numberType);
28
    var format = (national) ? i18n.phonenumbers.PhoneNumberFormat.NATIONAL : i18n.phonenumbers.PhoneNumberFormat.INTERNATIONAL;
29
    return phoneUtil.format(numberObj, format);
30
  } catch (e) {
31
    return "";
32
  }
33
}
34
 
35
 
36
// get the extension from the given number
37
function getExtension(number, countryCode) {
38
  try {
39
    var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
40
    var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
41
    return numberObj.getExtension();
42
  } catch (e) {
43
    return "";
44
  }
45
}
46
 
47
 
48
// get the type of the given number e.g. fixed-line/mobile
49
function getNumberType(number, countryCode) {
50
  try {
51
    var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
52
    var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
53
    return phoneUtil.getNumberType(numberObj);
54
  } catch (e) {
55
    // broken
56
    return -99;
57
  }
58
}
59
 
60
 
61
// get more info if the validation has failed e.g. too long/too short
62
// NOTE that isPossibleNumberWithReason returns a i18n.phonenumbers.PhoneNumberUtil.ValidationResult
63
function getValidationError(number, countryCode) {
64
  try {
65
    var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
66
    var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
67
    return phoneUtil.isPossibleNumberWithReason(numberObj);
68
  } catch (e) {
69
    //console.log(e);
70
 
71
    // here I convert thrown errors into ValidationResult enums (if possible)
72
    // errors are from i18n.phonenumbers.Error in the file https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js
73
    if (e.message == i18n.phonenumbers.Error.INVALID_COUNTRY_CODE) {
74
      return i18n.phonenumbers.PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE;
75
    }
76
    if (e.message == i18n.phonenumbers.Error.TOO_SHORT_AFTER_IDD || e.message == i18n.phonenumbers.Error.TOO_SHORT_NSN) {
77
      return i18n.phonenumbers.PhoneNumberUtil.ValidationResult.TOO_SHORT;
78
    }
79
    if (e.message == i18n.phonenumbers.Error.TOO_LONG) {
80
      return i18n.phonenumbers.PhoneNumberUtil.ValidationResult.TOO_LONG;
81
    }
82
 
83
    // broken
84
    return -99;
85
  }
86
}
87
 
88
 
89
// check if given number is valid
90
function isValidNumber(number, countryCode) {
91
  try {
92
    var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
93
    var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
94
    return phoneUtil.isValidNumber(numberObj);
95
  } catch (e) {
96
    return false;
97
  }
98
}
99
 
100
 
101
// copied this from i18n.phonenumbers.PhoneNumberFormat in the file https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js
102
var numberFormat = {
103
  "E164": 0,
104
  "INTERNATIONAL": 1,
105
  "NATIONAL": 2,
106
  "RFC3966": 3
107
};
108
 
109
 
110
// copied this from i18n.phonenumbers.PhoneNumberType in https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js and put the keys in quotes to force closure compiler to preserve the keys
111
// TODO: there must be a way to just tell closure compiler to preserve the keys on i18n.phonenumbers.PhoneNumberType and just export that
112
var numberType = {
113
  "FIXED_LINE": 0,
114
  "MOBILE": 1,
115
  "FIXED_LINE_OR_MOBILE": 2,
116
  "TOLL_FREE": 3,
117
  "PREMIUM_RATE": 4,
118
  "SHARED_COST": 5,
119
  "VOIP": 6,
120
  "PERSONAL_NUMBER": 7,
121
  "PAGER": 8,
122
  "UAN": 9,
123
  "VOICEMAIL": 10,
124
  "UNKNOWN": -1
125
};
126
 
127
 
128
// copied this from i18n.phonenumbers.PhoneNumberUtil.ValidationResult in https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js and again put the keys in quotes.
129
var validationError = {
130
  "IS_POSSIBLE": 0,
131
  "INVALID_COUNTRY_CODE": 1,
132
  "TOO_SHORT": 2,
133
  "TOO_LONG": 3,
134
  "IS_POSSIBLE_LOCAL_ONLY": 4,
135
  "INVALID_LENGTH": 5,
136
};
137
 
138
 
139
// exports
140
goog.exportSymbol('intlTelInputUtils', {});
141
goog.exportSymbol('intlTelInputUtils.formatNumber', formatNumber);
142
goog.exportSymbol('intlTelInputUtils.getExampleNumber', getExampleNumber);
143
goog.exportSymbol('intlTelInputUtils.getExtension', getExtension);
144
goog.exportSymbol('intlTelInputUtils.getNumberType', getNumberType);
145
goog.exportSymbol('intlTelInputUtils.getValidationError', getValidationError);
146
goog.exportSymbol('intlTelInputUtils.isValidNumber', isValidNumber);
147
// enums
148
goog.exportSymbol('intlTelInputUtils.numberFormat', numberFormat);
149
goog.exportSymbol('intlTelInputUtils.numberType', numberType);
150
goog.exportSymbol('intlTelInputUtils.validationError', validationError);