RFC3966.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  2. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  3. function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  4. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  5. function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  6. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  7. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  8. import isViablePhoneNumber from './isViablePhoneNumber.js'; // https://www.ietf.org/rfc/rfc3966.txt
  9. /**
  10. * @param {string} text - Phone URI (RFC 3966).
  11. * @return {object} `{ ?number, ?ext }`.
  12. */
  13. export function parseRFC3966(text) {
  14. var number;
  15. var ext; // Replace "tel:" with "tel=" for parsing convenience.
  16. text = text.replace(/^tel:/, 'tel=');
  17. for (var _iterator = _createForOfIteratorHelperLoose(text.split(';')), _step; !(_step = _iterator()).done;) {
  18. var part = _step.value;
  19. var _part$split = part.split('='),
  20. _part$split2 = _slicedToArray(_part$split, 2),
  21. name = _part$split2[0],
  22. value = _part$split2[1];
  23. switch (name) {
  24. case 'tel':
  25. number = value;
  26. break;
  27. case 'ext':
  28. ext = value;
  29. break;
  30. case 'phone-context':
  31. // Only "country contexts" are supported.
  32. // "Domain contexts" are ignored.
  33. if (value[0] === '+') {
  34. number = value + number;
  35. }
  36. break;
  37. }
  38. } // If the phone number is not viable, then abort.
  39. if (!isViablePhoneNumber(number)) {
  40. return {};
  41. }
  42. var result = {
  43. number: number
  44. };
  45. if (ext) {
  46. result.ext = ext;
  47. }
  48. return result;
  49. }
  50. /**
  51. * @param {object} - `{ ?number, ?extension }`.
  52. * @return {string} Phone URI (RFC 3966).
  53. */
  54. export function formatRFC3966(_ref) {
  55. var number = _ref.number,
  56. ext = _ref.ext;
  57. if (!number) {
  58. return '';
  59. }
  60. if (number[0] !== '+') {
  61. throw new Error("\"formatRFC3966()\" expects \"number\" to be in E.164 format.");
  62. }
  63. return "tel:".concat(number).concat(ext ? ';ext=' + ext : '');
  64. }
  65. //# sourceMappingURL=RFC3966.js.map