extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = extractFormattedPhoneNumberFromPossibleRfc3966NumberUri;
  7. var _extractPhoneContext = _interopRequireWildcard(require("./extractPhoneContext.js"));
  8. var _ParseError = _interopRequireDefault(require("../ParseError.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  10. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  11. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  12. /**
  13. * @param {string} numberToParse
  14. * @param {string} nationalNumber
  15. * @return {}
  16. */
  17. function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, _ref) {
  18. var extractFormattedPhoneNumber = _ref.extractFormattedPhoneNumber;
  19. var phoneContext = (0, _extractPhoneContext["default"])(numberToParse);
  20. if (!(0, _extractPhoneContext.isPhoneContextValid)(phoneContext)) {
  21. throw new _ParseError["default"]('NOT_A_NUMBER');
  22. }
  23. var phoneNumberString;
  24. if (phoneContext === null) {
  25. // Extract a possible number from the string passed in.
  26. // (this strips leading characters that could not be the start of a phone number)
  27. phoneNumberString = extractFormattedPhoneNumber(numberToParse) || '';
  28. } else {
  29. phoneNumberString = ''; // If the phone context contains a phone number prefix, we need to capture
  30. // it, whereas domains will be ignored.
  31. if (phoneContext.charAt(0) === _extractPhoneContext.PLUS_SIGN) {
  32. phoneNumberString += phoneContext;
  33. } // Now append everything between the "tel:" prefix and the phone-context.
  34. // This should include the national number, an optional extension or
  35. // isdn-subaddress component. Note we also handle the case when "tel:" is
  36. // missing, as we have seen in some of the phone number inputs.
  37. // In that case, we append everything from the beginning.
  38. var indexOfRfc3966Prefix = numberToParse.indexOf(_extractPhoneContext.RFC3966_PREFIX_);
  39. var indexOfNationalNumber; // RFC 3966 "tel:" prefix is preset at this stage because
  40. // `isPhoneContextValid()` requires it to be present.
  41. /* istanbul ignore else */
  42. if (indexOfRfc3966Prefix >= 0) {
  43. indexOfNationalNumber = indexOfRfc3966Prefix + _extractPhoneContext.RFC3966_PREFIX_.length;
  44. } else {
  45. indexOfNationalNumber = 0;
  46. }
  47. var indexOfPhoneContext = numberToParse.indexOf(_extractPhoneContext.RFC3966_PHONE_CONTEXT_);
  48. phoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext);
  49. } // Delete the isdn-subaddress and everything after it if it is present.
  50. // Note extension won't appear at the same time with isdn-subaddress
  51. // according to paragraph 5.3 of the RFC3966 spec.
  52. var indexOfIsdn = phoneNumberString.indexOf(_extractPhoneContext.RFC3966_ISDN_SUBADDRESS_);
  53. if (indexOfIsdn > 0) {
  54. phoneNumberString = phoneNumberString.substring(0, indexOfIsdn);
  55. } // If both phone context and isdn-subaddress are absent but other
  56. // parameters are present, the parameters are left in nationalNumber.
  57. // This is because we are concerned about deleting content from a potential
  58. // number string when there is no strong evidence that the number is
  59. // actually written in RFC3966.
  60. if (phoneNumberString !== '') {
  61. return phoneNumberString;
  62. }
  63. }
  64. //# sourceMappingURL=extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map