Created
May 16, 2022 11:03
-
-
Save jeroenvisser101/00de6ea2def21ceb296df6fb68372836 to your computer and use it in GitHub Desktop.
This file contains 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
// Put this in a folder together with the "svg" folder from Twemoji. Create a new folder next to it called "flags". | |
const fs = require("fs"); | |
const path = require("path"); | |
// country code regex | |
const CC_REGEX = /^[a-z]{2}$/i; | |
// offset between uppercase ascii and regional indicator symbols | |
const OFFSET = 127397; | |
/** | |
* convert country code to corresponding flag emoji | |
* @param {string} cc - country code string | |
* @returns {string} flag emoji | |
*/ | |
function countryCodeEmoji(cc) { | |
if (!CC_REGEX.test(cc)) { | |
const type = typeof cc; | |
throw new TypeError( | |
`cc argument must be an ISO 3166-1 alpha-2 string, but got '${ | |
type === "string" ? cc : type | |
}' instead.` | |
); | |
} | |
const codePoints = [...cc.toUpperCase()].map((c) => c.codePointAt() + OFFSET); | |
return codePoints.map((codepoints) => codepoints.toString(16)).join("-"); | |
} | |
const countryCodes = [ | |
"AF", | |
"AX", | |
"AL", | |
"DZ", | |
"AD", | |
"AO", | |
"AI", | |
"AG", | |
"AR", | |
"AM", | |
"AW", | |
"AC", | |
"AU", | |
"AT", | |
"AZ", | |
"BS", | |
"BH", | |
"BD", | |
"BB", | |
"BY", | |
"BE", | |
"BZ", | |
"BJ", | |
"BM", | |
"BT", | |
"BO", | |
"BA", | |
"BW", | |
"BV", | |
"BR", | |
"IO", | |
"BN", | |
"BG", | |
"BF", | |
"BI", | |
"KH", | |
"CA", | |
"CV", | |
"BQ", | |
"KY", | |
"CF", | |
"TD", | |
"CL", | |
"CN", | |
"CX", | |
"CC", | |
"CO", | |
"KM", | |
"CG", | |
"CD", | |
"CK", | |
"CR", | |
"HR", | |
"CU", | |
"CW", | |
"CY", | |
"CZ", | |
"CI", | |
"DK", | |
"DJ", | |
"DM", | |
"DO", | |
"EC", | |
"EG", | |
"SV", | |
"GQ", | |
"ER", | |
"EE", | |
"SZ", | |
"ET", | |
"FK", | |
"FO", | |
"FJ", | |
"FI", | |
"FR", | |
"GF", | |
"PF", | |
"TF", | |
"GA", | |
"GM", | |
"GE", | |
"DE", | |
"GH", | |
"GI", | |
"GR", | |
"GL", | |
"GD", | |
"GP", | |
"GT", | |
"GG", | |
"GN", | |
"GW", | |
"GY", | |
"HT", | |
"HM", | |
"VA", | |
"HN", | |
"HK", | |
"HU", | |
"IS", | |
"IN", | |
"ID", | |
"IR", | |
"IQ", | |
"IE", | |
"IM", | |
"IL", | |
"IT", | |
"JM", | |
"JP", | |
"JE", | |
"JO", | |
"KZ", | |
"KE", | |
"KI", | |
"KP", | |
"XK", | |
"KW", | |
"KG", | |
"LA", | |
"LV", | |
"LB", | |
"LS", | |
"LR", | |
"LY", | |
"LI", | |
"LT", | |
"LU", | |
"MO", | |
"MG", | |
"MW", | |
"MY", | |
"MV", | |
"ML", | |
"MT", | |
"MQ", | |
"MR", | |
"MU", | |
"YT", | |
"MX", | |
"MD", | |
"MC", | |
"MN", | |
"ME", | |
"MS", | |
"MA", | |
"MZ", | |
"MM", | |
"NA", | |
"NR", | |
"NP", | |
"NL", | |
"NC", | |
"NZ", | |
"NI", | |
"NE", | |
"NG", | |
"NU", | |
"NF", | |
"MK", | |
"NO", | |
"OM", | |
"PK", | |
"PS", | |
"PA", | |
"PG", | |
"PY", | |
"PE", | |
"PH", | |
"PN", | |
"PL", | |
"PT", | |
"QA", | |
"CM", | |
"RE", | |
"RO", | |
"RU", | |
"RW", | |
"BL", | |
"SH", | |
"KN", | |
"LC", | |
"MF", | |
"PM", | |
"WS", | |
"SM", | |
"ST", | |
"SA", | |
"SN", | |
"RS", | |
"SC", | |
"SL", | |
"SG", | |
"SX", | |
"SK", | |
"SI", | |
"SB", | |
"SO", | |
"ZA", | |
"GS", | |
"KR", | |
"SS", | |
"ES", | |
"LK", | |
"VC", | |
"SD", | |
"SR", | |
"SJ", | |
"SE", | |
"CH", | |
"SY", | |
"TW", | |
"TJ", | |
"TZ", | |
"TH", | |
"TL", | |
"TG", | |
"TK", | |
"TO", | |
"TT", | |
"TA", | |
"TN", | |
"TR", | |
"TM", | |
"TC", | |
"TV", | |
"UG", | |
"UA", | |
"AE", | |
"GB", | |
"US", | |
"UM", | |
"UY", | |
"UZ", | |
"VU", | |
"VE", | |
"VN", | |
"VG", | |
"WF", | |
"EH", | |
"YE", | |
"ZM", | |
"ZW", | |
]; | |
countryCodes.map((cc) => { | |
const pathname = path.resolve(`./svg/${countryCodeEmoji(cc)}.svg`); | |
fs.copyFileSync(pathname, path.resolve(`./flags/${cc}.svg`)); | |
return cc; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment