extractNationalNumber.js 5.4 KB

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