Skip to content

Commit

Permalink
Merge pull request #373 from ruimarinho/support/update-libphonenumber…
Browse files Browse the repository at this point in the history
…-8-13-18

Update [email protected]
  • Loading branch information
ruimarinho authored Aug 9, 2023
2 parents c3474c8 + 8f4c61f commit 93141c8
Show file tree
Hide file tree
Showing 6 changed files with 942 additions and 732 deletions.
514 changes: 258 additions & 256 deletions src/demo-compiled.js

Large diffs are not rendered by default.

383 changes: 192 additions & 191 deletions src/metadata.js

Large diffs are not rendered by default.

383 changes: 192 additions & 191 deletions src/metadatalite.js

Large diffs are not rendered by default.

268 changes: 208 additions & 60 deletions src/phonenumberutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ goog.addSingletonGetter(i18n.phonenumbers.PhoneNumberUtil);
*/
i18n.phonenumbers.Error = {
INVALID_COUNTRY_CODE: 'Invalid country calling code',
// This generally indicates the string passed in had less than 3 digits in it.
// This indicates the string passed is not a valid number. Either the string
// had less than 3 digits in it or had an invalid phone-context parameter.
// More specifically, the number failed to match the regular expression
// VALID_PHONE_NUMBER.
// VALID_PHONE_NUMBER, RFC3966_GLOBAL_NUMBER_DIGITS, or RFC3966_DOMAINNAME.
NOT_A_NUMBER: 'The string supplied did not seem to be a phone number',
// This indicates the string started with an international dialing prefix, but
// after this was stripped from the number, had less digits than any valid
Expand Down Expand Up @@ -730,6 +731,90 @@ i18n.phonenumbers.PhoneNumberUtil.VALID_PHONE_NUMBER_ =
*/
i18n.phonenumbers.PhoneNumberUtil.DEFAULT_EXTN_PREFIX_ = ' ext. ';

/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_VISUAL_SEPARATOR_ = '[\\-\\.\\(\\)]?';

/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_DIGIT_ = '(['
+ i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + ']|'
+ i18n.phonenumbers.PhoneNumberUtil.RFC3966_VISUAL_SEPARATOR_ + ')';

/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_GLOBAL_NUMBER_DIGITS_ = '^\\'
+ i18n.phonenumbers.PhoneNumberUtil.PLUS_SIGN
+ i18n.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_DIGIT_ + '*['
+ i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_ + ']'
+ i18n.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_DIGIT_ + '*$';

/**
* Regular expression of valid global-number-digits for the phone-context
* parameter, following the syntax defined in RFC3966.
*
* @const
* @type {RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ =
new RegExp(i18n.phonenumbers.PhoneNumberUtil.RFC3966_GLOBAL_NUMBER_DIGITS_);

/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.ALPHANUM_ =
i18n.phonenumbers.PhoneNumberUtil.VALID_ALPHA_
+ i18n.phonenumbers.PhoneNumberUtil.VALID_DIGITS_;

/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_DOMAINLABEL_ = '['
+ i18n.phonenumbers.PhoneNumberUtil.ALPHANUM_ + ']+((\\-)*['
+ i18n.phonenumbers.PhoneNumberUtil.ALPHANUM_ + '])*';

/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_TOPLABEL_ = '['
+ i18n.phonenumbers.PhoneNumberUtil.VALID_ALPHA_ + ']+((\\-)*['
+ i18n.phonenumbers.PhoneNumberUtil.ALPHANUM_ + '])*';

/**
* @const
* @type {string}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_DOMAINNAME_ = '^('
+ i18n.phonenumbers.PhoneNumberUtil.RFC3966_DOMAINLABEL_ + '\\.)*'
+ i18n.phonenumbers.PhoneNumberUtil.RFC3966_TOPLABEL_ + '\\.?$';

/**
* Regular expression of valid domainname for the phone-context parameter,
* following the syntax defined in RFC3966.
*
* @const
* @type {RegExp}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.RFC3966_DOMAINNAME_PATTERN_ =
new RegExp(i18n.phonenumbers.PhoneNumberUtil.RFC3966_DOMAINNAME_);

/**
* Helper method for constructing regular expressions for parsing. Creates
Expand Down Expand Up @@ -4298,6 +4383,74 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.parseHelper_ =
};


/**
* Extracts the value of the phone-context parameter of numberToExtractFrom,
* following the syntax defined in RFC3966.
* @param {?string} numberToExtractFrom
* @return {string|null} the extracted string (possibly empty), or null if no
* phone-context parameter is found.
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.extractPhoneContext_ =
function (numberToExtractFrom) {
/** @type {number} */
var indexOfPhoneContext = numberToExtractFrom.indexOf(i18n
.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_CONTEXT_);
// If no phone-context parameter is present
if (indexOfPhoneContext === -1) {
return null;
}

/** @type {number} */
var phoneContextStart = indexOfPhoneContext + i18n
.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_CONTEXT_.length;
// If phone-context parameter is empty
if (phoneContextStart >= numberToExtractFrom.length) {
return "";
}

/** @type {number} */
var phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart);
// If phone-context is not the last parameter
if (phoneContextEnd !== -1) {
return numberToExtractFrom.substring(phoneContextStart,
phoneContextEnd);
} else {
return numberToExtractFrom.substring(phoneContextStart);
}
}


/**
* Returns whether the value of phoneContext follows the syntax defined in
* RFC3966.
*
* @param {string|null} phoneContext
* @return {boolean}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.isPhoneContextValid_ =
function (phoneContext) {
if (phoneContext == null) {
return true;
}

if (phoneContext.length === 0) {
return false;
}

var globalNumberDigitsMatcher =
i18n.phonenumbers.PhoneNumberUtil.RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.exec(
phoneContext);
var domainnameMatcher =
i18n.phonenumbers.PhoneNumberUtil.RFC3966_DOMAINNAME_PATTERN_.exec(
phoneContext);
// Does phone-context value match pattern of global-number-digits or
// domainname
return globalNumberDigitsMatcher !== null || domainnameMatcher !== null;
}


/**
* Converts numberToParse to a form that we can parse and write it to
* nationalNumber if it is written in RFC3966; otherwise extract a possible
Expand All @@ -4308,71 +4461,66 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.parseHelper_ =
* extension.
* @param {!goog.string.StringBuffer} nationalNumber a string buffer for storing
* the national significant number.
* @throws {Error}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.buildNationalNumberForParsing_ =
function(numberToParse, nationalNumber) {
function (numberToParse, nationalNumber) {
var phoneContext =
i18n.phonenumbers.PhoneNumberUtil.prototype.extractPhoneContext_(
numberToParse);

if (!i18n.phonenumbers.PhoneNumberUtil.prototype.isPhoneContextValid_(
phoneContext)) {
throw new Error(i18n.phonenumbers.Error.NOT_A_NUMBER);
}
if (phoneContext != null) {
// If the phone context contains a phone number prefix, we need to capture
// it, whereas domains will be ignored.
if (phoneContext.charAt(0) ===
i18n.phonenumbers.PhoneNumberUtil.PLUS_SIGN) {
nationalNumber.append(phoneContext);
}

/** @type {number} */
var indexOfPhoneContext = numberToParse.indexOf(
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_CONTEXT_);
if (indexOfPhoneContext >= 0) {
var phoneContextStart = indexOfPhoneContext +
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_CONTEXT_.length;
// If the phone context contains a phone number prefix, we need to capture
// it, whereas domains will be ignored.
// No length check is necessary, as per C++ or Java, since out-of-bounds
// requests to charAt return an empty string.
if (numberToParse.charAt(phoneContextStart) ==
i18n.phonenumbers.PhoneNumberUtil.PLUS_SIGN) {
// Additional parameters might follow the phone context. If so, we will
// remove them here because the parameters after phone context are not
// important for parsing the phone number.
var phoneContextEnd = numberToParse.indexOf(';', phoneContextStart);
if (phoneContextEnd > 0) {
nationalNumber.append(numberToParse.substring(phoneContextStart,
phoneContextEnd));
// Now append everything between the "tel:" prefix and the phone-context.
// This should include the national number, an optional extension or
// isdn-subaddress component. Note we also handle the case when "tel:" is
// missing, as we have seen in some of the phone number inputs.
// In that case, we append everything from the beginning.
var indexOfRfc3966Prefix = numberToParse.indexOf(
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PREFIX_);
var indexOfNationalNumber = (indexOfRfc3966Prefix >= 0) ?
indexOfRfc3966Prefix +
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PREFIX_.length : 0;
var indexOfPhoneContext = numberToParse.indexOf(
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PHONE_CONTEXT_);
nationalNumber.append(numberToParse.substring(indexOfNationalNumber,
indexOfPhoneContext));
} else {
nationalNumber.append(numberToParse.substring(phoneContextStart));
// Extract a possible number from the string passed in (this strips leading
// characters that could not be the start of a phone number.)
nationalNumber.append(
i18n.phonenumbers.PhoneNumberUtil.extractPossibleNumber(
numberToParse ?? ""));
}
}

// Now append everything between the "tel:" prefix and the phone-context.
// This should include the national number, an optional extension or
// isdn-subaddress component. Note we also handle the case when "tel:" is
// missing, as we have seen in some of the phone number inputs.
// In that case, we append everything from the beginning.
var indexOfRfc3966Prefix = numberToParse.indexOf(
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PREFIX_);
var indexOfNationalNumber = (indexOfRfc3966Prefix >= 0) ?
indexOfRfc3966Prefix +
i18n.phonenumbers.PhoneNumberUtil.RFC3966_PREFIX_.length : 0;
nationalNumber.append(numberToParse.substring(indexOfNationalNumber,
indexOfPhoneContext));
} else {
// Extract a possible number from the string passed in (this strips leading
// characters that could not be the start of a phone number.)
nationalNumber.append(
i18n.phonenumbers.PhoneNumberUtil.extractPossibleNumber(numberToParse));
}

// Delete the isdn-subaddress and everything after it if it is present.
// Note extension won't appear at the same time with isdn-subaddress
// according to paragraph 5.3 of the RFC3966 spec,
/** @type {string} */
var nationalNumberStr = nationalNumber.toString();
var indexOfIsdn = nationalNumberStr.indexOf(
i18n.phonenumbers.PhoneNumberUtil.RFC3966_ISDN_SUBADDRESS_);
if (indexOfIsdn > 0) {
nationalNumber.clear();
nationalNumber.append(nationalNumberStr.substring(0, indexOfIsdn));
}
// If both phone context and isdn-subaddress are absent but other
// parameters are present, the parameters are left in nationalNumber. This
// is because we are concerned about deleting content from a potential
// number string when there is no strong evidence that the number is
// actually written in RFC3966.
};
// Delete the isdn-subaddress and everything after it if it is present.
// Note extension won't appear at the same time with isdn-subaddress
// according to paragraph 5.3 of the RFC3966 spec,
/** @type {string} */
var nationalNumberStr = nationalNumber.toString();
var indexOfIsdn = nationalNumberStr.indexOf(
i18n.phonenumbers.PhoneNumberUtil.RFC3966_ISDN_SUBADDRESS_);
if (indexOfIsdn > 0) {
nationalNumber.clear();
nationalNumber.append(nationalNumberStr.substring(0, indexOfIsdn));
}
// If both phone context and isdn-subaddress are absent but other
// parameters are present, the parameters are left in nationalNumber. This
// is because we are concerned about deleting content from a potential
// number string when there is no strong evidence that the number is
// actually written in RFC3966.
};


/**
Expand Down
Loading

0 comments on commit 93141c8

Please sign in to comment.