Skip to content

Instantly share code, notes, and snippets.

@baggednismo
Last active January 25, 2023 14:33
Show Gist options
  • Save baggednismo/52069cfd1641b4af7064f97379b08941 to your computer and use it in GitHub Desktop.
Save baggednismo/52069cfd1641b4af7064f97379b08941 to your computer and use it in GitHub Desktop.
This class defines all the United States, Canada and Mexico states/provinces and allows for conversion from the full name to abbreviation.
import java.text.Normalizer
enum class State(val stateName: String) {
AK("Alaska"),
AL("Alabama"),
AR("Arkansas"),
AZ("Arizona"),
CA("California"),
CO("Colorado"),
CT("Connecticut"),
DE("Delaware"),
FL("Florida"),
GA("Georgia"),
HI("Hawaii"),
IA("Iowa"),
ID("Idaho"),
IL("Illinois"),
IN("Indiana"),
KS("Kansas"),
KY("Kentucky"),
LA("Louisiana"),
MA("Massachusetts"),
MD("Maryland"),
ME("Maine"),
MI("Michigan"),
MN("Minnesota"),
MO("Missouri"),
MS("Mississippi"),
MT("Montana"),
NC("North Carolina"),
ND("North Dakota"),
NE("Nebraska"),
NH("New Hampshire"),
NJ("New Jersey"),
NM("New Mexico"),
NV("Nevada"),
NY("New York"),
OH("Ohio"),
OK("Oklahoma"),
OR("Oregon"),
PA("Pennsylvania"),
RI("Rhode Island"),
SC("South Carolina"),
SD("South Dakota"),
TN("Tennessee"),
TX("Texas"),
UT("Utah"),
VA("Virginia"),
VT("Vermont"),
WA("Washington"),
WI("Wisconsin"),
WV("West Virginia"),
WY("Wyoming"),
GU("Guam"),
VI("Virgin Islands"),
PR("Puerto Rico"),
AE("Armed forces - Europe"),
AA("Armed forces - America"),
AP("Armed forces - Pacific"),
// Canada Codes
AB("Alberta"),
BC("British Columbia"),
MB("Manitoba"),
NB("New Brunswick"),
NL("Newfoundland and Labrador"),
NT("Northwest Territories"),
NS("Nova Scotia"),
NU("Nunavut"),
ON("Ontario"),
PE("Prince Edward Island"),
QC("Quebec"),
SK("Saskatchewan"),
YT("Yukon"),
// México Codes
AGU("Aguascalientes"),
BCN("Baja California"),
BCS("Baja California Sur"),
CAM("Campeche"),
CHP("Chiapas"),
CHH("Chihuahua"),
COA("Coahuila"),
COL("Colima"),
DIF("Distrito Federal"),
DUR("Durango"),
GUA("Guanajuato"),
GRO("Guerrero"),
HID("Hidalgo"),
JAL("Jalisco"),
MEX("México"),
MIC("Michoacán"),
MOR("Morelos"),
NAY("Nayarit"),
NLE("Nuevo León"),
OAX("Oaxaca"),
PUE("Puebla"),
QUE("Querétaro"),
ROO("Quintana Roo"),
SLP("San Luis Potosí"),
SIN("Sinaloa"),
SON("Sonora"),
TAB("Tabasco"),
TAM("Tamaulipas"),
TLA("Tlaxcala"),
VER("Veracruz"),
YUC("Yucatán"),
ZAC("Zacatecas")
}
fun String.abbreviation(): String? = State::stateName.find(this)?.name
fun String.abbreviation(charCount: Int): String? = this.abbreviation()?.take(charCount)
fun String.abbreviationNormalized(): String? = State::stateName.findNormalized(this)?.name
fun String.abbreviationNormalized(charCount: Int): String? = this.abbreviationNormalized()?.take(charCount)
/*
* This function is rather universal to find an Enum by its value.
* It is assumed that the Enum has a single value
*/
inline fun <reified T : Enum<T>, V> ((T) -> V).find(value: V): T? {
return enumValues<T>().firstOrNull { this(it) == value }
}
/*
* This function assumes the Enum has a single String value.
* We use this to compare accented characters to their non-accented counterpart.
*/
inline fun <reified T : Enum<T>, V> ((T) -> V).findNormalized(value: V): T? {
return enumValues<T>().firstOrNull { this(it).toString().normalize() == value }
}
/*
* Extension function to remove all accented characters and replace with its non-accented counterpart.
*/
fun String.normalize(): String = Normalizer.normalize(this, Normalizer.Form.NFD).replace(Regex("\\p{M}"), "").trim()
import org.junit.Assert
import org.junit.Test
class PostalConversionsTest {
@Test
fun testGetStateAbbreviation() {
Assert.assertEquals("NY", "New York".abbreviation())
Assert.assertEquals(null, "NewYork".abbreviation())
Assert.assertEquals("QUE", "Querétaro".abbreviation())
Assert.assertEquals("QU", "Querétaro".abbreviation(2))
Assert.assertEquals(null, "Querétaroasdf".abbreviation(2))
}
@Test
fun testGetStateAbbreviationFromNormalizedText() {
Assert.assertEquals("QUE", "Queretaro".abbreviationNormalized())
Assert.assertEquals("SLP", "San Luis Potosi".abbreviationNormalized())
Assert.assertEquals("QU", "Queretaro".abbreviationNormalized(2))
Assert.assertEquals("SL", "San Luis Potosi".abbreviationNormalized(2))
Assert.assertEquals(null, "Queretaroasdf".abbreviationNormalized(2))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment