formatNationalNumberUsingFormat.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.FIRST_GROUP_PATTERN = void 0;
  6. exports["default"] = formatNationalNumberUsingFormat;
  7. var _applyInternationalSeparatorStyle = _interopRequireDefault(require("./applyInternationalSeparatorStyle.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  9. // This was originally set to $1 but there are some countries for which the
  10. // first group is not used in the national pattern (e.g. Argentina) so the $1
  11. // group does not match correctly. Therefore, we use `\d`, so that the first
  12. // group actually used in the pattern will be matched.
  13. var FIRST_GROUP_PATTERN = /(\$\d)/;
  14. exports.FIRST_GROUP_PATTERN = FIRST_GROUP_PATTERN;
  15. function formatNationalNumberUsingFormat(number, format, _ref) {
  16. var useInternationalFormat = _ref.useInternationalFormat,
  17. withNationalPrefix = _ref.withNationalPrefix,
  18. carrierCode = _ref.carrierCode,
  19. metadata = _ref.metadata;
  20. var formattedNumber = number.replace(new RegExp(format.pattern()), useInternationalFormat ? format.internationalFormat() : // This library doesn't use `domestic_carrier_code_formatting_rule`,
  21. // because that one is only used when formatting phone numbers
  22. // for dialing from a mobile phone, and this is not a dialing library.
  23. // carrierCode && format.domesticCarrierCodeFormattingRule()
  24. // // First, replace the $CC in the formatting rule with the desired carrier code.
  25. // // Then, replace the $FG in the formatting rule with the first group
  26. // // and the carrier code combined in the appropriate way.
  27. // ? format.format().replace(FIRST_GROUP_PATTERN, format.domesticCarrierCodeFormattingRule().replace('$CC', carrierCode))
  28. // : (
  29. // withNationalPrefix && format.nationalPrefixFormattingRule()
  30. // ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())
  31. // : format.format()
  32. // )
  33. withNationalPrefix && format.nationalPrefixFormattingRule() ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule()) : format.format());
  34. if (useInternationalFormat) {
  35. return (0, _applyInternationalSeparatorStyle["default"])(formattedNumber);
  36. }
  37. return formattedNumber;
  38. }
  39. //# sourceMappingURL=formatNationalNumberUsingFormat.js.map