parseIncompletePhoneNumber.js 4.1 KB

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