Move phonenumbery utils to libphonenumber object

Slowly whittling away at helpers.js...
This commit is contained in:
lilia 2014-10-11 17:19:16 -07:00
parent 9fc14cfa30
commit 81e4af5827
7 changed files with 301 additions and 58 deletions

View file

@ -129,6 +129,7 @@
<script type="text/javascript" src="js-deps/libphonenumber_api-compiled.js"></script>
<script type="text/javascript" src="js/helpers.js"></script>
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
<script type="text/javascript" src="js/webcrypto.js"></script>
<script type="text/javascript" src="js/crypto.js"></script>
<script type="text/javascript" src="js/models/messages.js"></script>

View file

@ -213,49 +213,6 @@ window.textsecure.protos = function() {
// Number formatting utils
window.textsecure.utils = function() {
var self = {};
/****************************************
*** Number conversion/checking stuff ***
****************************************/
self.getAllRegionCodes = function() {
return {"AD":"Andorra","AE":"United Arab Emirates","AF":"Afghanistan","AG":"Antigua and Barbuda","AI":"Anguilla","AL":"Albania","AM":"Armenia","AO":"Angola","AR":"Argentina","AS":"AmericanSamoa","AT":"Austria","AU":"Australia","AW":"Aruba","AX":"Åland Islands","AZ":"Azerbaijan","BA":"Bosnia and Herzegovina","BB":"Barbados","BD":"Bangladesh","BE":"Belgium","BF":"Burkina Faso","BG":"Bulgaria","BH":"Bahrain","BI":"Burundi","BJ":"Benin","BL":"Saint Barthélemy","BM":"Bermuda","BN":"Brunei Darussalam","BO":"Bolivia, Plurinational State of","BR":"Brazil","BS":"Bahamas","BT":"Bhutan","BW":"Botswana","BY":"Belarus","BZ":"Belize","CA":"Canada","CC":"Cocos (Keeling) Islands","CD":"Congo, The Democratic Republic of the","CF":"Central African Republic","CG":"Congo","CH":"Switzerland","CI":"Cote d'Ivoire","CK":"Cook Islands","CL":"Chile","CM":"Cameroon","CN":"China","CO":"Colombia","CR":"Costa Rica","CU":"Cuba","CV":"Cape Verde","CX":"Christmas Island","CY":"Cyprus","CZ":"Czech Republic","DE":"Germany","DJ":"Djibouti","DK":"Denmark","DM":"Dominica","DO":"Dominican Republic","DZ":"Algeria","EC":"Ecuador","EE":"Estonia","EG":"Egypt","ER":"Eritrea","ES":"Spain","ET":"Ethiopia","FI":"Finland","FJ":"Fiji","FK":"Falkland Islands (Malvinas)","FM":"Micronesia, Federated States of","FO":"Faroe Islands","FR":"France","GA":"Gabon","GB":"United Kingdom","GD":"Grenada","GE":"Georgia","GF":"French Guiana","GG":"Guernsey","GH":"Ghana","GI":"Gibraltar","GL":"Greenland","GM":"Gambia","GN":"Guinea","GP":"Guadeloupe","GQ":"Equatorial Guinea","GR":"Ελλάδα","GT":"Guatemala","GU":"Guam","GW":"Guinea-Bissau","GY":"Guyana","HK":"Hong Kong","HN":"Honduras","HR":"Croatia","HT":"Haiti","HU":"Magyarország","ID":"Indonesia","IE":"Ireland","IL":"Israel","IM":"Isle of Man","IN":"India","IO":"British Indian Ocean Territory","IQ":"Iraq","IR":"Iran, Islamic Republic of","IS":"Iceland","IT":"Italy","JE":"Jersey","JM":"Jamaica","JO":"Jordan","JP":"Japan","KE":"Kenya","KG":"Kyrgyzstan","KH":"Cambodia","KI":"Kiribati","KM":"Comoros","KN":"Saint Kitts and Nevis","KP":"Korea, Democratic People's Republic of","KR":"Korea, Republic of","KW":"Kuwait","KY":"Cayman Islands","KZ":"Kazakhstan","LA":"Lao People's Democratic Republic","LB":"Lebanon","LC":"Saint Lucia","LI":"Liechtenstein","LK":"Sri Lanka","LR":"Liberia","LS":"Lesotho","LT":"Lithuania","LU":"Luxembourg","LV":"Latvia","LY":"Libyan Arab Jamahiriya","MA":"Morocco","MC":"Monaco","MD":"Moldova, Republic of","ME":"Црна Гора","MF":"Saint Martin","MG":"Madagascar","MH":"Marshall Islands","MK":"Macedonia, The Former Yugoslav Republic of","ML":"Mali","MM":"Myanmar","MN":"Mongolia","MO":"Macao","MP":"Northern Mariana Islands","MQ":"Martinique","MR":"Mauritania","MS":"Montserrat","MT":"Malta","MU":"Mauritius","MV":"Maldives","MW":"Malawi","MX":"Mexico","MY":"Malaysia","MZ":"Mozambique","NA":"Namibia","NC":"New Caledonia","NE":"Niger","NF":"Norfolk Island","NG":"Nigeria","NI":"Nicaragua","NL":"Netherlands","NO":"Norway","NP":"Nepal","NR":"Nauru","NU":"Niue","NZ":"New Zealand","OM":"Oman","PA":"Panama","PE":"Peru","PF":"French Polynesia","PG":"Papua New Guinea","PH":"Philippines","PK":"Pakistan","PL":"Polska","PM":"Saint Pierre and Miquelon","PR":"Puerto Rico","PS":"Palestinian Territory, Occupied","PT":"Portugal","PW":"Palau","PY":"Paraguay","QA":"Qatar","RE":"Réunion","RO":"Romania","RS":"Србија","RU":"Russia","RW":"Rwanda","SA":"Saudi Arabia","SB":"Solomon Islands","SC":"Seychelles","SD":"Sudan","SE":"Sweden","SG":"Singapore","SH":"Saint Helena, Ascension and Tristan Da Cunha","SI":"Slovenia","SJ":"Svalbard and Jan Mayen","SK":"Slovakia","SL":"Sierra Leone","SM":"San Marino","SN":"Senegal","SO":"Somalia","SR":"Suriname","ST":"Sao Tome and Principe","SV":"El Salvador","SY":"Syrian Arab Republic","SZ":"Swaziland","TC":"Turks and Caicos Islands","TD":"Chad","TG":"Togo","TH":"Thailand","TJ":"Tajikistan","TK":"Tokelau","TL":"Timor-Leste","TM":"Turkmenistan","TN":"Tunisia","TO":"Tonga","TR":"Turkey","TT":"Trinidad and Tobago","TV":"Tuvalu","TW":"Taiwan, Province of China","TZ":"Tanzania, United Republic of","UA":"Ukraine","UG":"Uganda","US":"United States","UY":"Uruguay","UZ":"Uzbekistan","VA":"Holy See (Vatican City State)","VC":"Saint Vincent and the Grenadines","VE":"Venezuela","VG":"Virgin Islands, British","VI":"Virgin Islands, U.S.","VN":"Viet Nam","VU":"Vanuatu","WF":"Wallis and Futuna","WS":"Samoa","YE":"Yemen","YT":"Mayotte","ZA":"South Africa","ZM":"Zambia","ZW":"Zimbabwe"};
};
self.getRegionCodeForCountryCode = function(countryCode) {
return libphonenumber.getRegionCodeForCountryCode(countryCode);
};
self.getRegionCodeForNumber = function(number) {
try {
var parsedNumber = libphonenumber.parse(number);
return libphonenumber.getRegionCodeForNumber(parsedNumber);
} catch(e) {
return "ZZ";
}
};
self.getCountryCodeForRegion = function(regionCode) {
var cc = libphonenumber.getCountryCodeForRegion(regionCode);
return (cc != 0) ? cc : "";
};
self.verifyNumber = function(number, regionCode) {
var parsedNumber = libphonenumber.parse(number, regionCode);
if(!regionCode || regionCode == 'ZZ')
regionCode = libphonenumber.getRegionCodeForNumber(parsedNumber);
var isValidNumber = libphonenumber.isValidNumber(parsedNumber);
var isValidNumberForRegion = libphonenumber.isValidNumberForRegion(parsedNumber, regionCode);
if (isValidNumber && isValidNumberForRegion) {
return libphonenumber.format(parsedNumber, libphonenumber.PhoneNumberFormat.E164);
} else {
throw new Error("The number seems not to be valid.");
}
};
self.unencodeNumber = function(number) {
return number.split(".");
};
@ -471,7 +428,7 @@ window.textsecure.storage = function() {
var haveMe = false;
var finalNumbers = [];
for (var i in numbers) {
var number = textsecure.utils.verifyNumber(numbers[i]);
var number = libphonenumber.util.verifyNumber(numbers[i]);
if (number == me)
haveMe = true;
if (finalNumbers.indexOf(number) < 0) {
@ -502,7 +459,7 @@ window.textsecure.storage = function() {
return undefined;
try {
number = textsecure.utils.verifyNumber(number);
number = libphonenumber.util.verifyNumber(number);
} catch (e) {
return group.numbers;
}
@ -527,7 +484,7 @@ window.textsecure.storage = function() {
return undefined;
for (var i in numbers) {
var number = textsecure.utils.verifyNumber(numbers[i]);
var number = libphonenumber.util.verifyNumber(numbers[i]);
if (group.numbers.indexOf(number) < 0) {
group.numbers.push(number);
addGroupToNumber(groupId, number);
@ -768,7 +725,7 @@ window.textsecure.registerSingleDevice = function(number, verificationCode, step
return textsecure.api.confirmCode(number, verificationCode, password, signalingKey, registrationId, true).then(function() {
var numberId = number + ".1";
textsecure.storage.putUnencrypted("number_id", numberId);
textsecure.storage.putUnencrypted("regionCode", textsecure.utils.getRegionCodeForNumber(number));
textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegionCodeForNumber(number));
stepDone(1);
return textsecure.crypto.generateKeys().then(function(keys) {
@ -803,7 +760,7 @@ window.textsecure.registerSecondDevice = function(encodedDeviceInit, cryptoInfo,
return textsecure.api.confirmCode(number, identityKey.provisioningCode, password, signalingKey, registrationId, false).then(function(result) {
var numberId = number + "." + result;
textsecure.storage.putUnencrypted("number_id", numberId);
textsecure.storage.putUnencrypted("regionCode", textsecure.utils.getRegionCodeForNumber(number));
textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegion(number));
stepDone(2);
return textsecure.crypto.generateKeys().then(function(keys) {

284
js/libphonenumber-util.js Normal file
View file

@ -0,0 +1,284 @@
/*
* This file extends the libphonenumber object with a set of phonenumbery
* utility functions. libphonenumber must be included before you call these
* functions, but the order of the files/script-tags doesn't matter.
*/
;(function() {
"use strict";
window.libphonenumber = window.libphonenumber || {};
window.libphonenumber.util = {
getRegionCodeForNumber: function(number) {
try {
var parsedNumber = libphonenumber.parse(number);
return libphonenumber.getRegionCodeForNumber(parsedNumber);
} catch(e) {
return "ZZ";
}
},
getCountryCode: function(regionCode) {
var cc = libphonenumber.getCountryCodeForRegion(regionCode);
return (cc != 0) ? cc : "";
},
verifyNumber: function(number, regionCode) {
var parsedNumber = libphonenumber.parse(number, regionCode);
if(!regionCode || regionCode == 'ZZ') {
regionCode = libphonenumber.getRegionCodeForNumber(parsedNumber);
}
var isValidNumber = libphonenumber.isValidNumber(parsedNumber);
var isValidNumberForRegion = libphonenumber.isValidNumberForRegion(parsedNumber, regionCode);
if (isValidNumber && isValidNumberForRegion) {
return libphonenumber.format(parsedNumber, libphonenumber.PhoneNumberFormat.E164);
} else {
throw new Error("The number seems not to be valid.");
}
},
getAllRegionCodes: function() {
return {
"AD":"Andorra",
"AE":"United Arab Emirates",
"AF":"Afghanistan",
"AG":"Antigua and Barbuda",
"AI":"Anguilla",
"AL":"Albania",
"AM":"Armenia",
"AO":"Angola",
"AR":"Argentina",
"AS":"AmericanSamoa",
"AT":"Austria",
"AU":"Australia",
"AW":"Aruba",
"AX":"Åland Islands",
"AZ":"Azerbaijan",
"BA":"Bosnia and Herzegovina",
"BB":"Barbados",
"BD":"Bangladesh",
"BE":"Belgium",
"BF":"Burkina Faso",
"BG":"Bulgaria",
"BH":"Bahrain",
"BI":"Burundi",
"BJ":"Benin",
"BL":"Saint Barthélemy",
"BM":"Bermuda",
"BN":"Brunei Darussalam",
"BO":"Bolivia, Plurinational State of",
"BR":"Brazil",
"BS":"Bahamas",
"BT":"Bhutan",
"BW":"Botswana",
"BY":"Belarus",
"BZ":"Belize",
"CA":"Canada",
"CC":"Cocos (Keeling) Islands",
"CD":"Congo, The Democratic Republic of the",
"CF":"Central African Republic",
"CG":"Congo",
"CH":"Switzerland",
"CI":"Cote d'Ivoire",
"CK":"Cook Islands",
"CL":"Chile",
"CM":"Cameroon",
"CN":"China",
"CO":"Colombia",
"CR":"Costa Rica",
"CU":"Cuba",
"CV":"Cape Verde",
"CX":"Christmas Island",
"CY":"Cyprus",
"CZ":"Czech Republic",
"DE":"Germany",
"DJ":"Djibouti",
"DK":"Denmark",
"DM":"Dominica",
"DO":"Dominican Republic",
"DZ":"Algeria",
"EC":"Ecuador",
"EE":"Estonia",
"EG":"Egypt",
"ER":"Eritrea",
"ES":"Spain",
"ET":"Ethiopia",
"FI":"Finland",
"FJ":"Fiji",
"FK":"Falkland Islands (Malvinas)",
"FM":"Micronesia, Federated States of",
"FO":"Faroe Islands",
"FR":"France",
"GA":"Gabon",
"GB":"United Kingdom",
"GD":"Grenada",
"GE":"Georgia",
"GF":"French Guiana",
"GG":"Guernsey",
"GH":"Ghana",
"GI":"Gibraltar",
"GL":"Greenland",
"GM":"Gambia",
"GN":"Guinea",
"GP":"Guadeloupe",
"GQ":"Equatorial Guinea",
"GR":"Ελλάδα",
"GT":"Guatemala",
"GU":"Guam",
"GW":"Guinea-Bissau",
"GY":"Guyana",
"HK":"Hong Kong",
"HN":"Honduras",
"HR":"Croatia",
"HT":"Haiti",
"HU":"Magyarország",
"ID":"Indonesia",
"IE":"Ireland",
"IL":"Israel",
"IM":"Isle of Man",
"IN":"India",
"IO":"British Indian Ocean Territory",
"IQ":"Iraq",
"IR":"Iran, Islamic Republic of",
"IS":"Iceland",
"IT":"Italy",
"JE":"Jersey",
"JM":"Jamaica",
"JO":"Jordan",
"JP":"Japan",
"KE":"Kenya",
"KG":"Kyrgyzstan",
"KH":"Cambodia",
"KI":"Kiribati",
"KM":"Comoros",
"KN":"Saint Kitts and Nevis",
"KP":"Korea, Democratic People's Republic of",
"KR":"Korea, Republic of",
"KW":"Kuwait",
"KY":"Cayman Islands",
"KZ":"Kazakhstan",
"LA":"Lao People's Democratic Republic",
"LB":"Lebanon",
"LC":"Saint Lucia",
"LI":"Liechtenstein",
"LK":"Sri Lanka",
"LR":"Liberia",
"LS":"Lesotho",
"LT":"Lithuania",
"LU":"Luxembourg",
"LV":"Latvia",
"LY":"Libyan Arab Jamahiriya",
"MA":"Morocco",
"MC":"Monaco",
"MD":"Moldova, Republic of",
"ME":"Црна Гора",
"MF":"Saint Martin",
"MG":"Madagascar",
"MH":"Marshall Islands",
"MK":"Macedonia, The Former Yugoslav Republic of",
"ML":"Mali",
"MM":"Myanmar",
"MN":"Mongolia",
"MO":"Macao",
"MP":"Northern Mariana Islands",
"MQ":"Martinique",
"MR":"Mauritania",
"MS":"Montserrat",
"MT":"Malta",
"MU":"Mauritius",
"MV":"Maldives",
"MW":"Malawi",
"MX":"Mexico",
"MY":"Malaysia",
"MZ":"Mozambique",
"NA":"Namibia",
"NC":"New Caledonia",
"NE":"Niger",
"NF":"Norfolk Island",
"NG":"Nigeria",
"NI":"Nicaragua",
"NL":"Netherlands",
"NO":"Norway",
"NP":"Nepal",
"NR":"Nauru",
"NU":"Niue",
"NZ":"New Zealand",
"OM":"Oman",
"PA":"Panama",
"PE":"Peru",
"PF":"French Polynesia",
"PG":"Papua New Guinea",
"PH":"Philippines",
"PK":"Pakistan",
"PL":"Polska",
"PM":"Saint Pierre and Miquelon",
"PR":"Puerto Rico",
"PS":"Palestinian Territory, Occupied",
"PT":"Portugal",
"PW":"Palau",
"PY":"Paraguay",
"QA":"Qatar",
"RE":"Réunion",
"RO":"Romania",
"RS":"Србија",
"RU":"Russia",
"RW":"Rwanda",
"SA":"Saudi Arabia",
"SB":"Solomon Islands",
"SC":"Seychelles",
"SD":"Sudan",
"SE":"Sweden",
"SG":"Singapore",
"SH":"Saint Helena, Ascension and Tristan Da Cunha",
"SI":"Slovenia",
"SJ":"Svalbard and Jan Mayen",
"SK":"Slovakia",
"SL":"Sierra Leone",
"SM":"San Marino",
"SN":"Senegal",
"SO":"Somalia",
"SR":"Suriname",
"ST":"Sao Tome and Principe",
"SV":"El Salvador",
"SY":"Syrian Arab Republic",
"SZ":"Swaziland",
"TC":"Turks and Caicos Islands",
"TD":"Chad",
"TG":"Togo",
"TH":"Thailand",
"TJ":"Tajikistan",
"TK":"Tokelau",
"TL":"Timor-Leste",
"TM":"Turkmenistan",
"TN":"Tunisia",
"TO":"Tonga",
"TR":"Turkey",
"TT":"Trinidad and Tobago",
"TV":"Tuvalu",
"TW":"Taiwan, Province of China",
"TZ":"Tanzania, United Republic of",
"UA":"Ukraine",
"UG":"Uganda",
"US":"United States",
"UY":"Uruguay",
"UZ":"Uzbekistan",
"VA":"Holy See (Vatican City State)",
"VC":"Saint Vincent and the Grenadines",
"VE":"Venezuela",
"VG":"Virgin Islands, British",
"VI":"Virgin Islands, U.S.",
"VN":"Viet Nam",
"VU":"Vanuatu",
"WF":"Wallis and Futuna",
"WS":"Samoa",
"YE":"Yemen",
"YT":"Mayotte",
"ZA":"South Africa",
"ZM":"Zambia",
"ZW":"Zimbabwe"
};
} // getAllRegionCodes
}; // libphonenumber.util
})();

View file

@ -18,7 +18,7 @@
function updateNumberColors() {
try {
if($('#number').val() != "" && $('#regionCode').val() != "")
textsecure.utils.verifyNumber($('#number').val(), $('#regionCode').val());
libphonenumber.util.verifyNumber($('#number').val(), $('#regionCode').val());
$('#countrycode').removeClass('invalid');
$('#number').removeClass('invalid');
} catch (numberInvalidError) {
@ -44,7 +44,7 @@
$('#init-go-single-client').click(function() {
try {
var parsedNumber = textsecure.utils.verifyNumber($('#number').val(), $('#regionCode').val());
var parsedNumber = libphonenumber.util.verifyNumber($('#number').val(), $('#regionCode').val());
} catch(e) {
alert("Please enter a valid phone number first.");
return false;
@ -68,7 +68,7 @@
});
$('#init-go').click(function() {
var parsedNumber = textsecure.utils.verifyNumber($('#number').val(), $('#regionCode').val());
var parsedNumber = libphonenumber.util.verifyNumber($('#number').val(), $('#regionCode').val());
if (!isCodeValid()) {
updateCodeColor();
return;
@ -110,7 +110,7 @@
if (!isRegistrationDone()) {
$('#init-setup').show();
var countrys = textsecure.utils.getAllRegionCodes();
var countrys = libphonenumber.util.getAllRegionCodes();
$.each(countrys, function (regionCode, countryName) {
$('#regionCode').append($('<option>', {
value: regionCode,
@ -119,19 +119,19 @@
});
$('#regionCode').change(function(){
$('#countrycode').val(textsecure.utils.getCountryCodeForRegion(this.value));
$('#countrycode').val(libphonenumber.util.getCountryCodeForRegion(this.value));
updateNumberColors();
});
$('#countrycode').keyup(function(){
$('#regionCode').val(textsecure.utils.getRegionCodeForCountryCode($('#countrycode').val()));
$('#regionCode').val(libphonenumber.util.getRegionCodeForCountryCode($('#countrycode').val()));
updateNumberColors();
});
$('#number').change(updateNumberColors);
// handle form data cached by the browser (after a page ref
$('#regionCode').val(textsecure.utils.getRegionCodeForCountryCode($('#countrycode').val()));
$('#regionCode').val(libphonenumber.util.getRegionCodeForCountryCode($('#countrycode').val()));
updateNumberColors();
textsecure.crypto.prepareTempWebsocket().then(function(cryptoInfo) {

View file

@ -14,7 +14,7 @@ var Whisper = Whisper || {};
verifyNumber: function(item) {
try {
if (textsecure.utils.verifyNumber(this.$el.val())) {
if (libphonenumber.util.verifyNumber(this.$el.val())) {
this.removeError();
return;
}
@ -42,7 +42,7 @@ var Whisper = Whisper || {};
e.preventDefault();
var number = this.input.val();
try {
if (textsecure.utils.verifyNumber(number)) {
if (libphonenumber.util.verifyNumber(number)) {
var thread = Whisper.Threads.findOrCreateForRecipient(number);
var message_input = this.$el.find('input.send-message');
thread.sendMessage(message_input.val());

View file

@ -10,7 +10,7 @@ var Whisper = Whisper || {};
tagClass: function(item) {
try {
if (textsecure.utils.verifyNumber(item)) {
if (libphonenumber.util.verifyNumber(item)) {
return;
}
} catch(ex) {}

View file

@ -77,6 +77,7 @@
<script type="text/javascript" src="js-deps/qrcode.min.js"></script>
<script type="text/javascript" src="js/helpers.js"></script>
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
<script type="text/javascript" src="js/webcrypto.js"></script>
<script type="text/javascript" src="js/crypto.js"></script>
<script type="text/javascript" src="js/models/messages.js"></script>