extractNationalNumberFromPossiblyIncompleteNumber.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = extractNationalNumberFromPossiblyIncompleteNumber;
  6. /**
  7. * Strips any national prefix (such as 0, 1) present in a
  8. * (possibly incomplete) number provided.
  9. * "Carrier codes" are only used in Colombia and Brazil,
  10. * and only when dialing within those countries from a mobile phone to a fixed line number.
  11. * Sometimes it won't actually strip national prefix
  12. * and will instead prepend some digits to the `number`:
  13. * for example, when number `2345678` is passed with `VI` country selected,
  14. * it will return `{ number: "3402345678" }`, because `340` area code is prepended.
  15. * @param {string} number — National number digits.
  16. * @param {object} metadata — Metadata with country selected.
  17. * @return {object} `{ nationalNumber: string, nationalPrefix: string? carrierCode: string? }`. Even if a national prefix was extracted, it's not necessarily present in the returned object, so don't rely on its presence in the returned object in order to find out whether a national prefix has been extracted or not.
  18. */
  19. function extractNationalNumberFromPossiblyIncompleteNumber(number, metadata) {
  20. if (number && metadata.numberingPlan.nationalPrefixForParsing()) {
  21. // See METADATA.md for the description of
  22. // `national_prefix_for_parsing` and `national_prefix_transform_rule`.
  23. // Attempt to parse the first digits as a national prefix.
  24. var prefixPattern = new RegExp('^(?:' + metadata.numberingPlan.nationalPrefixForParsing() + ')');
  25. var prefixMatch = prefixPattern.exec(number);
  26. if (prefixMatch) {
  27. var nationalNumber;
  28. var carrierCode; // https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
  29. // If a `national_prefix_for_parsing` has any "capturing groups"
  30. // then it means that the national (significant) number is equal to
  31. // those "capturing groups" transformed via `national_prefix_transform_rule`,
  32. // and nothing could be said about the actual national prefix:
  33. // what is it and was it even there.
  34. // If a `national_prefix_for_parsing` doesn't have any "capturing groups",
  35. // then everything it matches is a national prefix.
  36. // To determine whether `national_prefix_for_parsing` matched any
  37. // "capturing groups", the value of the result of calling `.exec()`
  38. // is looked at, and if it has non-undefined values where there're
  39. // "capturing groups" in the regular expression, then it means
  40. // that "capturing groups" have been matched.
  41. // It's not possible to tell whether there'll be any "capturing gropus"
  42. // before the matching process, because a `national_prefix_for_parsing`
  43. // could exhibit both behaviors.
  44. var capturedGroupsCount = prefixMatch.length - 1;
  45. var hasCapturedGroups = capturedGroupsCount > 0 && prefixMatch[capturedGroupsCount];
  46. if (metadata.nationalPrefixTransformRule() && hasCapturedGroups) {
  47. nationalNumber = number.replace(prefixPattern, metadata.nationalPrefixTransformRule()); // If there's more than one captured group,
  48. // then carrier code is the second one.
  49. if (capturedGroupsCount > 1) {
  50. carrierCode = prefixMatch[1];
  51. }
  52. } // If there're no "capturing groups",
  53. // or if there're "capturing groups" but no
  54. // `national_prefix_transform_rule`,
  55. // then just strip the national prefix from the number,
  56. // and possibly a carrier code.
  57. // Seems like there could be more.
  58. else {
  59. // `prefixBeforeNationalNumber` is the whole substring matched by
  60. // the `national_prefix_for_parsing` regular expression.
  61. // There seem to be no guarantees that it's just a national prefix.
  62. // For example, if there's a carrier code, it's gonna be a
  63. // part of `prefixBeforeNationalNumber` too.
  64. var prefixBeforeNationalNumber = prefixMatch[0];
  65. nationalNumber = number.slice(prefixBeforeNationalNumber.length); // If there's at least one captured group,
  66. // then carrier code is the first one.
  67. if (hasCapturedGroups) {
  68. carrierCode = prefixMatch[1];
  69. }
  70. } // Tries to guess whether a national prefix was present in the input.
  71. // This is not something copy-pasted from Google's library:
  72. // they don't seem to have an equivalent for that.
  73. // So this isn't an "officially approved" way of doing something like that.
  74. // But since there seems no other existing method, this library uses it.
  75. var nationalPrefix;
  76. if (hasCapturedGroups) {
  77. var possiblePositionOfTheFirstCapturedGroup = number.indexOf(prefixMatch[1]);
  78. var possibleNationalPrefix = number.slice(0, possiblePositionOfTheFirstCapturedGroup); // Example: an Argentinian (AR) phone number `0111523456789`.
  79. // `prefixMatch[0]` is `01115`, and `$1` is `11`,
  80. // and the rest of the phone number is `23456789`.
  81. // The national number is transformed via `9$1` to `91123456789`.
  82. // National prefix `0` is detected being present at the start.
  83. // if (possibleNationalPrefix.indexOf(metadata.numberingPlan.nationalPrefix()) === 0) {
  84. if (possibleNationalPrefix === metadata.numberingPlan.nationalPrefix()) {
  85. nationalPrefix = metadata.numberingPlan.nationalPrefix();
  86. }
  87. } else {
  88. nationalPrefix = prefixMatch[0];
  89. }
  90. return {
  91. nationalNumber: nationalNumber,
  92. nationalPrefix: nationalPrefix,
  93. carrierCode: carrierCode
  94. };
  95. }
  96. }
  97. return {
  98. nationalNumber: number
  99. };
  100. }
  101. //# sourceMappingURL=extractNationalNumberFromPossiblyIncompleteNumber.js.map