parseDigits.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.DIGITS = void 0;
  6. exports["default"] = parseDigits;
  7. exports.parseDigit = parseDigit;
  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. // These mappings map a character (key) to a specific digit that should
  12. // replace it for normalization purposes. Non-European digits that
  13. // may be used in phone numbers are mapped to a European equivalent.
  14. //
  15. // E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
  16. //
  17. var DIGITS = {
  18. '0': '0',
  19. '1': '1',
  20. '2': '2',
  21. '3': '3',
  22. '4': '4',
  23. '5': '5',
  24. '6': '6',
  25. '7': '7',
  26. '8': '8',
  27. '9': '9',
  28. "\uFF10": '0',
  29. // Fullwidth digit 0
  30. "\uFF11": '1',
  31. // Fullwidth digit 1
  32. "\uFF12": '2',
  33. // Fullwidth digit 2
  34. "\uFF13": '3',
  35. // Fullwidth digit 3
  36. "\uFF14": '4',
  37. // Fullwidth digit 4
  38. "\uFF15": '5',
  39. // Fullwidth digit 5
  40. "\uFF16": '6',
  41. // Fullwidth digit 6
  42. "\uFF17": '7',
  43. // Fullwidth digit 7
  44. "\uFF18": '8',
  45. // Fullwidth digit 8
  46. "\uFF19": '9',
  47. // Fullwidth digit 9
  48. "\u0660": '0',
  49. // Arabic-indic digit 0
  50. "\u0661": '1',
  51. // Arabic-indic digit 1
  52. "\u0662": '2',
  53. // Arabic-indic digit 2
  54. "\u0663": '3',
  55. // Arabic-indic digit 3
  56. "\u0664": '4',
  57. // Arabic-indic digit 4
  58. "\u0665": '5',
  59. // Arabic-indic digit 5
  60. "\u0666": '6',
  61. // Arabic-indic digit 6
  62. "\u0667": '7',
  63. // Arabic-indic digit 7
  64. "\u0668": '8',
  65. // Arabic-indic digit 8
  66. "\u0669": '9',
  67. // Arabic-indic digit 9
  68. "\u06F0": '0',
  69. // Eastern-Arabic digit 0
  70. "\u06F1": '1',
  71. // Eastern-Arabic digit 1
  72. "\u06F2": '2',
  73. // Eastern-Arabic digit 2
  74. "\u06F3": '3',
  75. // Eastern-Arabic digit 3
  76. "\u06F4": '4',
  77. // Eastern-Arabic digit 4
  78. "\u06F5": '5',
  79. // Eastern-Arabic digit 5
  80. "\u06F6": '6',
  81. // Eastern-Arabic digit 6
  82. "\u06F7": '7',
  83. // Eastern-Arabic digit 7
  84. "\u06F8": '8',
  85. // Eastern-Arabic digit 8
  86. "\u06F9": '9' // Eastern-Arabic digit 9
  87. };
  88. exports.DIGITS = DIGITS;
  89. function parseDigit(character) {
  90. return DIGITS[character];
  91. }
  92. /**
  93. * Parses phone number digits from a string.
  94. * Drops all punctuation leaving only digits.
  95. * Also converts wide-ascii and arabic-indic numerals to conventional numerals.
  96. * E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
  97. * @param {string} string
  98. * @return {string}
  99. * @example
  100. * ```js
  101. * parseDigits('8 (800) 555')
  102. * // Outputs '8800555'.
  103. * ```
  104. */
  105. function parseDigits(string) {
  106. var result = ''; // Using `.split('')` here instead of normal `for ... of`
  107. // because the importing application doesn't neccessarily include an ES6 polyfill.
  108. // The `.split('')` approach discards "exotic" UTF-8 characters
  109. // (the ones consisting of four bytes) but digits
  110. // (including non-European ones) don't fall into that range
  111. // so such "exotic" characters would be discarded anyway.
  112. for (var _iterator = _createForOfIteratorHelperLoose(string.split('')), _step; !(_step = _iterator()).done;) {
  113. var character = _step.value;
  114. var digit = parseDigit(character);
  115. if (digit) {
  116. result += digit;
  117. }
  118. }
  119. return result;
  120. }
  121. //# sourceMappingURL=parseDigits.js.map