Hide currency flags shared by more than 3 countries
XCD, XOF, and XAF each have 6-8 issuing countries, which is too many flags to cram side by side next to a currency code. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FXcrACM5sok3M1KQJ8kByy
This commit is contained in:
@@ -12,17 +12,22 @@ private val MULTI_COUNTRY_CURRENCY_REGION_CODES: Map<String, List<String>> = map
|
|||||||
"XPF" to listOf("NC", "PF", "WF"),
|
"XPF" to listOf("NC", "PF", "WF"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/** Above this many issuing countries, showing every flag side by side is too visually noisy. */
|
||||||
|
private const val MAX_FLAGS_PER_CURRENCY = 3
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A country flag emoji for [code], derived from the first two letters of the ISO 4217 code —
|
* A country flag emoji for [code], derived from the first two letters of the ISO 4217 code —
|
||||||
* which double as the issuing country's ISO 3166-1 alpha-2 code for ordinary national
|
* which double as the issuing country's ISO 3166-1 alpha-2 code for ordinary national
|
||||||
* currencies — or null when there's no flag to show. [MULTI_COUNTRY_CURRENCY_REGION_CODES]
|
* currencies — or null when there's no flag to show. [MULTI_COUNTRY_CURRENCY_REGION_CODES]
|
||||||
* lists every country sharing a currency with no single issuer, so those show all their flags
|
* lists every country sharing a currency with no single issuer, so those show all their flags
|
||||||
* side by side; the remaining "X"-prefixed codes are precious metals, testing codes, and other
|
* side by side, unless there are more than [MAX_FLAGS_PER_CURRENCY] of them; the remaining
|
||||||
* non-national codes (XAU, XTS, XXX, ...), none of which has a country to show. EUR is a special
|
* "X"-prefixed codes are precious metals, testing codes, and other non-national codes (XAU, XTS,
|
||||||
* case handled separately, using the EU's own flag.
|
* XXX, ...), none of which has a country to show. EUR is a special case handled separately, using
|
||||||
|
* the EU's own flag.
|
||||||
*/
|
*/
|
||||||
fun currencyFlagEmoji(code: String): String? {
|
fun currencyFlagEmoji(code: String): String? {
|
||||||
MULTI_COUNTRY_CURRENCY_REGION_CODES[code]?.let { regionCodes ->
|
MULTI_COUNTRY_CURRENCY_REGION_CODES[code]?.let { regionCodes ->
|
||||||
|
if (regionCodes.size > MAX_FLAGS_PER_CURRENCY) return null
|
||||||
return regionCodes.joinToString(" ") { regionFlagEmoji(it) }
|
return regionCodes.joinToString(" ") { regionFlagEmoji(it) }
|
||||||
}
|
}
|
||||||
if (code.startsWith("X")) return null
|
if (code.startsWith("X")) return null
|
||||||
|
|||||||
@@ -19,13 +19,19 @@ class CurrencyFlagTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun currencyFlagEmoji_showsAllFlagsForCurrenciesSharedByMultipleCountries() {
|
fun currencyFlagEmoji_showsAllFlagsForCurrenciesSharedByFewCountries() {
|
||||||
assertEquals("🇦🇬 🇦🇮 🇩🇲 🇬🇩 🇰🇳 🇱🇨 🇲🇸 🇻🇨", currencyFlagEmoji("XCD"))
|
assertEquals("🇨🇼 🇸🇽", currencyFlagEmoji("XCG"))
|
||||||
assertEquals("🇧🇫 🇧🇯 🇨🇮 🇬🇼 🇲🇱 🇳🇪 🇸🇳 🇹🇬", currencyFlagEmoji("XOF"))
|
|
||||||
assertEquals("🇨🇫 🇨🇬 🇨🇲 🇬🇦 🇬🇶 🇹🇩", currencyFlagEmoji("XAF"))
|
|
||||||
assertEquals("🇳🇨 🇵🇫 🇼🇫", currencyFlagEmoji("XPF"))
|
assertEquals("🇳🇨 🇵🇫 🇼🇫", currencyFlagEmoji("XPF"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun currencyFlagEmoji_hidesFlagsForCurrenciesSharedByManyCountries() {
|
||||||
|
// Showing every flag side by side gets visually noisy past a few countries.
|
||||||
|
assertNull(currencyFlagEmoji("XCD")) // 8 countries
|
||||||
|
assertNull(currencyFlagEmoji("XOF")) // 8 countries
|
||||||
|
assertNull(currencyFlagEmoji("XAF")) // 6 countries
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun currencyFlagEmoji_returnsNullForNonNationalCodes() {
|
fun currencyFlagEmoji_returnsNullForNonNationalCodes() {
|
||||||
// ISO 4217 reserves the remaining "X"-prefixed codes for precious metals, testing codes,
|
// ISO 4217 reserves the remaining "X"-prefixed codes for precious metals, testing codes,
|
||||||
|
|||||||
Reference in New Issue
Block a user