extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import extractPhoneContext, { isPhoneContextValid, PLUS_SIGN, RFC3966_PREFIX_, RFC3966_PHONE_CONTEXT_, RFC3966_ISDN_SUBADDRESS_ } from './extractPhoneContext.js';
  2. import ParseError from '../ParseError.js';
  3. /**
  4. * @param {string} numberToParse
  5. * @param {string} nationalNumber
  6. * @return {}
  7. */
  8. export default function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, _ref) {
  9. var extractFormattedPhoneNumber = _ref.extractFormattedPhoneNumber;
  10. var phoneContext = extractPhoneContext(numberToParse);
  11. if (!isPhoneContextValid(phoneContext)) {
  12. throw new ParseError('NOT_A_NUMBER');
  13. }
  14. var phoneNumberString;
  15. if (phoneContext === null) {
  16. // Extract a possible number from the string passed in.
  17. // (this strips leading characters that could not be the start of a phone number)
  18. phoneNumberString = extractFormattedPhoneNumber(numberToParse) || '';
  19. } else {
  20. phoneNumberString = ''; // If the phone context contains a phone number prefix, we need to capture
  21. // it, whereas domains will be ignored.
  22. if (phoneContext.charAt(0) === PLUS_SIGN) {
  23. phoneNumberString += phoneContext;
  24. } // Now append everything between the "tel:" prefix and the phone-context.
  25. // This should include the national number, an optional extension or
  26. // isdn-subaddress component. Note we also handle the case when "tel:" is
  27. // missing, as we have seen in some of the phone number inputs.
  28. // In that case, we append everything from the beginning.
  29. var indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_);
  30. var indexOfNationalNumber; // RFC 3966 "tel:" prefix is preset at this stage because
  31. // `isPhoneContextValid()` requires it to be present.
  32. /* istanbul ignore else */
  33. if (indexOfRfc3966Prefix >= 0) {
  34. indexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length;
  35. } else {
  36. indexOfNationalNumber = 0;
  37. }
  38. var indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_);
  39. phoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext);
  40. } // Delete the isdn-subaddress and everything after it if it is present.
  41. // Note extension won't appear at the same time with isdn-subaddress
  42. // according to paragraph 5.3 of the RFC3966 spec.
  43. var indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_);
  44. if (indexOfIsdn > 0) {
  45. phoneNumberString = phoneNumberString.substring(0, indexOfIsdn);
  46. } // If both phone context and isdn-subaddress are absent but other
  47. // parameters are present, the parameters are left in nationalNumber.
  48. // This is because we are concerned about deleting content from a potential
  49. // number string when there is no strong evidence that the number is
  50. // actually written in RFC3966.
  51. if (phoneNumberString !== '') {
  52. return phoneNumberString;
  53. }
  54. }
  55. //# sourceMappingURL=extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map