formatNationalNumberUsingFormat.js 1.9 KB

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