extractNationalNumber.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = extractNationalNumber;
  6. var _extractNationalNumberFromPossiblyIncompleteNumber = _interopRequireDefault(require("./extractNationalNumberFromPossiblyIncompleteNumber.js"));
  7. var _matchesEntirely = _interopRequireDefault(require("./matchesEntirely.js"));
  8. var _checkNumberLength = _interopRequireDefault(require("./checkNumberLength.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  10. /**
  11. * Strips national prefix and carrier code from a complete phone number.
  12. * The difference from the non-"FromCompleteNumber" function is that
  13. * it won't extract national prefix if the resultant number is too short
  14. * to be a complete number for the selected phone numbering plan.
  15. * @param {string} number — Complete phone number digits.
  16. * @param {Metadata} metadata — Metadata with a phone numbering plan selected.
  17. * @return {object} `{ nationalNumber: string, carrierCode: string? }`.
  18. */
  19. function extractNationalNumber(number, metadata) {
  20. // Parsing national prefixes and carrier codes
  21. // is only required for local phone numbers
  22. // but some people don't understand that
  23. // and sometimes write international phone numbers
  24. // with national prefixes (or maybe even carrier codes).
  25. // http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
  26. // Google's original library forgives such mistakes
  27. // and so does this library, because it has been requested:
  28. // https://github.com/catamphetamine/libphonenumber-js/issues/127
  29. var _extractNationalNumbe = (0, _extractNationalNumberFromPossiblyIncompleteNumber["default"])(number, metadata),
  30. carrierCode = _extractNationalNumbe.carrierCode,
  31. nationalNumber = _extractNationalNumbe.nationalNumber;
  32. if (nationalNumber !== number) {
  33. if (!shouldHaveExtractedNationalPrefix(number, nationalNumber, metadata)) {
  34. // Don't strip the national prefix.
  35. return {
  36. nationalNumber: number
  37. };
  38. } // Check the national (significant) number length after extracting national prefix and carrier code.
  39. // Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
  40. if (metadata.possibleLengths()) {
  41. // The number remaining after stripping the national prefix and carrier code
  42. // should be long enough to have a possible length for the country.
  43. // Otherwise, don't strip the national prefix and carrier code,
  44. // since the original number could be a valid number.
  45. // This check has been copy-pasted "as is" from Google's original library:
  46. // https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
  47. // It doesn't check for the "possibility" of the original `number`.
  48. // I guess it's fine not checking that one. It works as is anyway.
  49. if (!isPossibleIncompleteNationalNumber(nationalNumber, metadata)) {
  50. // Don't strip the national prefix.
  51. return {
  52. nationalNumber: number
  53. };
  54. }
  55. }
  56. }
  57. return {
  58. nationalNumber: nationalNumber,
  59. carrierCode: carrierCode
  60. };
  61. } // In some countries, the same digit could be a national prefix
  62. // or a leading digit of a valid phone number.
  63. // For example, in Russia, national prefix is `8`,
  64. // and also `800 555 35 35` is a valid number
  65. // in which `8` is not a national prefix, but the first digit
  66. // of a national (significant) number.
  67. // Same's with Belarus:
  68. // `82004910060` is a valid national (significant) number,
  69. // but `2004910060` is not.
  70. // To support such cases (to prevent the code from always stripping
  71. // national prefix), a condition is imposed: a national prefix
  72. // is not extracted when the original number is "viable" and the
  73. // resultant number is not, a "viable" national number being the one
  74. // that matches `national_number_pattern`.
  75. function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberAfter, metadata) {
  76. // The equivalent in Google's code is:
  77. // https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
  78. if ((0, _matchesEntirely["default"])(nationalNumberBefore, metadata.nationalNumberPattern()) && !(0, _matchesEntirely["default"])(nationalNumberAfter, metadata.nationalNumberPattern())) {
  79. return false;
  80. } // This "is possible" national number (length) check has been commented out
  81. // because it's superceded by the (effectively) same check done in the
  82. // `extractNationalNumber()` function after it calls `shouldHaveExtractedNationalPrefix()`.
  83. // In other words, why run the same check twice if it could only be run once.
  84. // // Check the national (significant) number length after extracting national prefix and carrier code.
  85. // // Fixes a minor "weird behavior" bug: https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
  86. // // (Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature).
  87. // if (metadata.possibleLengths()) {
  88. // if (isPossibleIncompleteNationalNumber(nationalNumberBefore, metadata) &&
  89. // !isPossibleIncompleteNationalNumber(nationalNumberAfter, metadata)) {
  90. // return false
  91. // }
  92. // }
  93. return true;
  94. }
  95. function isPossibleIncompleteNationalNumber(nationalNumber, metadata) {
  96. switch ((0, _checkNumberLength["default"])(nationalNumber, metadata)) {
  97. case 'TOO_SHORT':
  98. case 'INVALID_LENGTH':
  99. // This library ignores "local-only" phone numbers (for simplicity).
  100. // See the readme for more info on what are "local-only" phone numbers.
  101. // case 'IS_POSSIBLE_LOCAL_ONLY':
  102. return false;
  103. default:
  104. return true;
  105. }
  106. }
  107. //# sourceMappingURL=extractNationalNumber.js.map