getCountryByCallingCode.js 903 B

123456789101112131415161718192021222324252627282930
  1. import getCountryByNationalNumber from './getCountryByNationalNumber.js'
  2. const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
  3. export default function getCountryByCallingCode(callingCode, {
  4. nationalNumber: nationalPhoneNumber,
  5. defaultCountry,
  6. metadata
  7. }) {
  8. /* istanbul ignore if */
  9. if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
  10. if (metadata.isNonGeographicCallingCode(callingCode)) {
  11. return '001'
  12. }
  13. }
  14. const possibleCountries = metadata.getCountryCodesForCallingCode(callingCode)
  15. if (!possibleCountries) {
  16. return
  17. }
  18. // If there's just one country corresponding to the country code,
  19. // then just return it, without further phone number digits validation.
  20. if (possibleCountries.length === 1) {
  21. return possibleCountries[0]
  22. }
  23. return getCountryByNationalNumber(nationalPhoneNumber, {
  24. countries: possibleCountries,
  25. defaultCountry,
  26. metadata: metadata.metadata
  27. })
  28. }