Coder Social home page Coder Social logo

isocountrycodes's Introduction

IsoCountryCodes for Swift

Platform version Swift Version

Iso country codes - that thing that we discussed that defines codes for the names of countries, dependent territories, and special areas of geographical interest.

What?

This is a iOS Swift library/class files that does a simple lookup depending on a alpha2, alpha3 or numeric value you give it. Currently it holds all the 249 countries, territories, or areas of geographical interest that are assigned official codes in ISO 3166-1.

Features:

This library returns ISO codes, names and currencies for countries.

  • Find by alpha-2, alpha-3 or numeric (String - yes I know...)
  • Search by (partial) name, case- and diacritic insensitive
  • Search by currency code
  • Search by phone dialing code (+31 for Netherlands, +1 for USA, etc...)
  • Retrieve a corresponding emoji flag for a country code.
  • Retrieve a fraction digits or decimal places for a country's currency. This can be useful when converting an amount in a certain currency to digits (ex 1 euro = 100 cent, 1 KD = 1000 fils)

Usage:

You can search via numeric, alpha-2 or alpha-3 format. Searching an ISO code returns a struct.

print(IsoCountryCodes.find(key: "020").name) //Andorra
print(IsoCountryCodes.find(key: "TK").name) //Tokelau
print(IsoCountryCodes.find(key: "TKL").currency) //NZD

You can also search by country name, currency or calling/dialing code:

dump(IsoCountryCodes.searchByName("Netherlands")
print(IsoCountryCodes.searchByCurrency("EUR").count ) // 31
print(IsoCountryCodes.searchByCallingCode("+31").first ) // Netherlands

let country = IsoCountryCodes.searchByName("Netherlands")
dump(country) // This dumps the full struct in console

This returns a IsoCountryInfo struct:

 IsoCountryCodes.IsoCountryInfo
    - name: Netherlands
    - numeric: 528
    - alpha2: NL
    - alpha3: NLD
    - calling: +31
    - currency: EUR
    - continent: EU

Searching by name is case- and diacritic insensitive:

dump(IsoCountryCodes.searchByName("netherlands"))
dump(IsoCountryCodes.searchByName("NETHERLANDS"))

dump(IsoCountryCodes.searchByName("Réunion"))
dump(IsoCountryCodes.searchByName("Reunion"))

If no full match is found, a partial match is tried:

// Full name is "Venezuela, Bolivarian Republic of"
dump(IsoCountryCodes.searchByName("Venezuela"))

but nothing is returned if the search results would be ambiguous:

// There are two Virgin Islands country codes:
// "Virgin Islands, British" and "Virgin Islands, U.S."
dump(IsoCountryCodes.searchByName("Virgin Islands"))

Fun with flags

Retrieve a corresponding emoji flag from a country code. (Thanks to @lorismaz for this addition)

let emojiString = IsoCountries.flag(countryCode: "NL")
// Prints 🇳🇱

or

let emojiString = IsoCountryCodes.find(key: "USA").flag
// Prints 🇺🇸

Retrieve fractional digits

Retrieve fractional digits or decimal places for a country's currency. This can be useful when converting an amount in a certain currency to digits. For example calculating how many cents go into 1 euro (100 cent), or in Kuwaiti dinar; 1 KD = 1000 fils. This is convenient when working with banks or payment providers. (Thanks to @mohameditani for this addition).

func transformToDigits(price: Double, country: IsoCountryInfo) -> Double {
  let fractionDigits = country.fractionDigits
  let amount = price * pow(10.0, Double(fractionDigits))
  return amount
}

let priceOfCoffee = 2.05
let country = IsoCountryCodes.searchByName("Italy") // fractionDigits for Italy is 2
let digits = transformToDigits(priceOfCoffee, country)
// 205 cents

Usage:

Copy/add files to your project

Licence:

The 'do-whatever-you-please-with-it licence. Use it or abuse it. Just keep my name at the top of the files.

isocountrycodes's People

Contributors

basthomas avatar brianhenryie avatar dmazzoni avatar funky-monkey avatar icesco avatar kassemitani avatar lorismaz avatar marcblanchet avatar matthijsotterloo avatar mohameditani avatar wacumov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

isocountrycodes's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.