Last active
February 12, 2023 23:39
-
-
Save glowcap/92032a925b9e8cfb40d03730579bac6e to your computer and use it in GitHub Desktop.
Swift State and Territory enums
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
| /// includes USStateAndTerritory enum and USState enum | |
| /// required for retreiving the full name from the enum cases | |
| extension String { | |
| /// Splits a camelCase string and capitalizes each word | |
| /// - Returns: self split into words and capitalized | |
| /// - Note: In its current configuration, it does not | |
| /// follow proper title formatting. | |
| /// (ie: capitalizes short verbs, prepositions, etc.) | |
| func titleCased() -> String { | |
| return self.replacingOccurrences( | |
| of: "([A-Z])", | |
| with: " $1", | |
| options: .regularExpression, | |
| range: self.range(of: self) | |
| ) | |
| .trimmingCharacters(in: .whitespacesAndNewlines) | |
| .capitalized | |
| } | |
| } | |
| enum USStateAndTerritory: String, CaseIterable { | |
| case alabama = "AL" | |
| case alaska = "AK" | |
| case arizona = "AZ" | |
| case arkansas = "AR" | |
| case americanSamoa = "AS" | |
| case california = "CA" | |
| case colorado = "CO" | |
| case connecticut = "CT" | |
| case delaware = "DE" | |
| case districtOfColumbia = "DC" | |
| case florida = "FL" | |
| case georgia = "GA" | |
| case guam = "GU" | |
| case hawaii = "HI" | |
| case idaho = "ID" | |
| case illinois = "IL" | |
| case indiana = "IN" | |
| case iowa = "IA" | |
| case kansas = "KS" | |
| case kentucky = "KY" | |
| case louisiana = "LA" | |
| case maine = "ME" | |
| case maryland = "MD" | |
| case massachusetts = "MA" | |
| case michigan = "MI" | |
| case minnesota = "MN" | |
| case mississippi = "MS" | |
| case missouri = "MO" | |
| case montana = "MT" | |
| case nebraska = "NE" | |
| case nevada = "NV" | |
| case newHampshire = "NH" | |
| case newJersey = "NJ" | |
| case newMexico = "NM" | |
| case newYork = "NY" | |
| case northCarolina = "NC" | |
| case northDakota = "ND" | |
| case northernMarianaIslands = "MP" | |
| case ohio = "OH" | |
| case oklahoma = "OK" | |
| case oregon = "OR" | |
| case pennsylvania = "PA" | |
| case puertoRico = "PR" | |
| case rhodeIsland = "RI" | |
| case southCarolina = "SC" | |
| case southDakota = "SD" | |
| case tennessee = "TN" | |
| case texas = "TX" | |
| case trustTerritories = "TT" | |
| case utah = "UT" | |
| case vermont = "VT" | |
| case virginia = "VA" | |
| case virginIslands = "VI" | |
| case washington = "WA" | |
| case westVirginia = "WV" | |
| case wisconsin = "WI" | |
| case wyoming = "WY" | |
| var abbreviated: String { | |
| self.rawValue | |
| } | |
| var fullName: String { | |
| String(describing: self).titleCased() | |
| } | |
| } | |
| /// ! USState enum ! | |
| enum USState: String, CaseIterable { | |
| case alabama = "AL" | |
| case alaska = "AK" | |
| case arizona = "AZ" | |
| case arkansas = "AR" | |
| case california = "CA" | |
| case colorado = "CO" | |
| case connecticut = "CT" | |
| case delaware = "DE" | |
| case districtOfColumbia = "DC" | |
| case florida = "FL" | |
| case georgia = "GA" | |
| case hawaii = "HI" | |
| case idaho = "ID" | |
| case illinois = "IL" | |
| case indiana = "IN" | |
| case iowa = "IA" | |
| case kansas = "KS" | |
| case kentucky = "KY" | |
| case louisiana = "LA" | |
| case maine = "ME" | |
| case maryland = "MD" | |
| case massachusetts = "MA" | |
| case michigan = "MI" | |
| case minnesota = "MN" | |
| case mississippi = "MS" | |
| case missouri = "MO" | |
| case montana = "MT" | |
| case nebraska = "NE" | |
| case nevada = "NV" | |
| case newHampshire = "NH" | |
| case newJersey = "NJ" | |
| case newMexico = "NM" | |
| case newYork = "NY" | |
| case northCarolina = "NC" | |
| case northDakota = "ND" | |
| case ohio = "OH" | |
| case oklahoma = "OK" | |
| case oregon = "OR" | |
| case pennsylvania = "PA" | |
| case rhodeIsland = "RI" | |
| case southCarolina = "SC" | |
| case southDakota = "SD" | |
| case tennessee = "TN" | |
| case texas = "TX" | |
| case utah = "UT" | |
| case vermont = "VT" | |
| case virginia = "VA" | |
| case washington = "WA" | |
| case westVirginia = "WV" | |
| case wisconsin = "WI" | |
| case wyoming = "WY" | |
| var abbreviated: String { | |
| self.rawValue | |
| } | |
| var fullName: String { | |
| String(describing: self).titleCased() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment