RFC3966.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.formatRFC3966 = formatRFC3966;
  6. exports.parseRFC3966 = parseRFC3966;
  7. var _isViablePhoneNumber = _interopRequireDefault(require("./isViablePhoneNumber.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  9. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  10. 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."); }
  11. 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; }
  12. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  13. 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."); }
  14. 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); }
  15. 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; }
  16. // https://www.ietf.org/rfc/rfc3966.txt
  17. /**
  18. * @param {string} text - Phone URI (RFC 3966).
  19. * @return {object} `{ ?number, ?ext }`.
  20. */
  21. function parseRFC3966(text) {
  22. var number;
  23. var ext; // Replace "tel:" with "tel=" for parsing convenience.
  24. text = text.replace(/^tel:/, 'tel=');
  25. for (var _iterator = _createForOfIteratorHelperLoose(text.split(';')), _step; !(_step = _iterator()).done;) {
  26. var part = _step.value;
  27. var _part$split = part.split('='),
  28. _part$split2 = _slicedToArray(_part$split, 2),
  29. name = _part$split2[0],
  30. value = _part$split2[1];
  31. switch (name) {
  32. case 'tel':
  33. number = value;
  34. break;
  35. case 'ext':
  36. ext = value;
  37. break;
  38. case 'phone-context':
  39. // Only "country contexts" are supported.
  40. // "Domain contexts" are ignored.
  41. if (value[0] === '+') {
  42. number = value + number;
  43. }
  44. break;
  45. }
  46. } // If the phone number is not viable, then abort.
  47. if (!(0, _isViablePhoneNumber["default"])(number)) {
  48. return {};
  49. }
  50. var result = {
  51. number: number
  52. };
  53. if (ext) {
  54. result.ext = ext;
  55. }
  56. return result;
  57. }
  58. /**
  59. * @param {object} - `{ ?number, ?extension }`.
  60. * @return {string} Phone URI (RFC 3966).
  61. */
  62. function formatRFC3966(_ref) {
  63. var number = _ref.number,
  64. ext = _ref.ext;
  65. if (!number) {
  66. return '';
  67. }
  68. if (number[0] !== '+') {
  69. throw new Error("\"formatRFC3966()\" expects \"number\" to be in E.164 format.");
  70. }
  71. return "tel:".concat(number).concat(ext ? ';ext=' + ext : '');
  72. }
  73. //# sourceMappingURL=RFC3966.js.map