getIddPrefix.js 1.1 KB

12345678910111213141516171819202122232425
  1. import Metadata from '../metadata.js'
  2. /**
  3. * Pattern that makes it easy to distinguish whether a region has a single
  4. * international dialing prefix or not. If a region has a single international
  5. * prefix (e.g. 011 in USA), it will be represented as a string that contains
  6. * a sequence of ASCII digits, and possibly a tilde, which signals waiting for
  7. * the tone. If there are multiple available international prefixes in a
  8. * region, they will be represented as a regex string that always contains one
  9. * or more characters that are not ASCII digits or a tilde.
  10. */
  11. const SINGLE_IDD_PREFIX_REG_EXP = /^[\d]+(?:[~\u2053\u223C\uFF5E][\d]+)?$/
  12. // For regions that have multiple IDD prefixes
  13. // a preferred IDD prefix is returned.
  14. export default function getIddPrefix(country, callingCode, metadata) {
  15. const countryMetadata = new Metadata(metadata)
  16. countryMetadata.selectNumberingPlan(country, callingCode)
  17. if (countryMetadata.defaultIDDPrefix()) {
  18. return countryMetadata.defaultIDDPrefix()
  19. }
  20. if (SINGLE_IDD_PREFIX_REG_EXP.test(countryMetadata.IDDPrefix())) {
  21. return countryMetadata.IDDPrefix()
  22. }
  23. }