parseDigits.js 4.0 KB

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