Created
May 22, 2015 18:31
-
-
Save ericdke/c9aa9bccd160eb8afabf to your computer and use it in GitHub Desktop.
Transform a country acronym to an emoji flag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func flag(country: String) -> String { | |
let base = 127397 // Root Unicode flags index | |
var usv = String.UnicodeScalarView() // Prepare a Unicode string | |
for i in country.utf16 { // Loop over the country acronym letters | |
let inc = Int(i) // The letter's ASCII index | |
let code = UnicodeScalar(base + inc) // Shift the letter index to the flags index | |
usv.append(code) // Append the grapheme to the Unicode string | |
} | |
return String(usv) | |
} | |
let flagged = flag("FR") // prints "🇫🇷" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment