parseIncompletePhoneNumber.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = parseIncompletePhoneNumber;
  6. exports.parsePhoneNumberCharacter = parsePhoneNumberCharacter;
  7. var _parseDigits = require("./helpers/parseDigits.js");
  8. 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."); }
  9. 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); }
  10. 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; }
  11. /**
  12. * Parses phone number characters from a string.
  13. * Drops all punctuation leaving only digits and the leading `+` sign (if any).
  14. * Also converts wide-ascii and arabic-indic numerals to conventional numerals.
  15. * E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
  16. * @param {string} string
  17. * @return {string}
  18. * @example
  19. * ```js
  20. * // Outputs '8800555'.
  21. * parseIncompletePhoneNumber('8 (800) 555')
  22. * // Outputs '+7800555'.
  23. * parseIncompletePhoneNumber('+7 800 555')
  24. * ```
  25. */
  26. function parseIncompletePhoneNumber(string) {
  27. var result = ''; // Using `.split('')` here instead of normal `for ... of`
  28. // because the importing application doesn't neccessarily include an ES6 polyfill.
  29. // The `.split('')` approach discards "exotic" UTF-8 characters
  30. // (the ones consisting of four bytes) but digits
  31. // (including non-European ones) don't fall into that range
  32. // so such "exotic" characters would be discarded anyway.
  33. for (var _iterator = _createForOfIteratorHelperLoose(string.split('')), _step; !(_step = _iterator()).done;) {
  34. var character = _step.value;
  35. result += parsePhoneNumberCharacter(character, result) || '';
  36. }
  37. return result;
  38. }
  39. /**
  40. * Parses next character while parsing phone number digits (including a `+`)
  41. * from text: discards everything except `+` and digits, and `+` is only allowed
  42. * at the start of a phone number.
  43. * For example, is used in `react-phone-number-input` where it uses
  44. * [`input-format`](https://gitlab.com/catamphetamine/input-format).
  45. * @param {string} character - Yet another character from raw input string.
  46. * @param {string?} prevParsedCharacters - Previous parsed characters.
  47. * @param {function?} emitEvent - An optional "emit event" function.
  48. * @return {string?} The parsed character.
  49. */
  50. function parsePhoneNumberCharacter(character, prevParsedCharacters, emitEvent) {
  51. // Only allow a leading `+`.
  52. if (character === '+') {
  53. // If this `+` is not the first parsed character
  54. // then discard it.
  55. if (prevParsedCharacters) {
  56. // `emitEvent` argument was added to this `export`ed function on Dec 26th, 2023.
  57. // Any 3rd-party code that used to `import` and call this function before that
  58. // won't be passing any `emitEvent` argument.
  59. //
  60. // The addition of the `emitEvent` argument was to fix the slightly-weird behavior
  61. // of parsing an input string when the user inputs something like `"2+7"
  62. // https://github.com/catamphetamine/react-phone-number-input/issues/437
  63. //
  64. // If the parser encounters an unexpected `+` in a string being parsed
  65. // then it simply discards that out-of-place `+` and any following characters.
  66. //
  67. if (typeof emitEvent === 'function') {
  68. emitEvent('end');
  69. }
  70. return;
  71. }
  72. return '+';
  73. } // Allow digits.
  74. return (0, _parseDigits.parseDigit)(character);
  75. }
  76. //# sourceMappingURL=parseIncompletePhoneNumber.js.map