Created
October 23, 2019 21:40
-
-
Save Abelhawk/909e320c39a9580f862112c8e9b3262f to your computer and use it in GitHub Desktop.
A custom Angular pipe for abbreviating US state names
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
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ name: 'stateAbbreviation' }) | |
export class StateAbbreviationPipe implements PipeTransform { | |
states = [ | |
{ state: 'Alabama', abbr: 'AL' }, | |
{ state: 'Alaska', abbr: 'AK' }, | |
{ state: 'Arizona', abbr: 'AZ' }, | |
{ state: 'Arkansas', abbr: 'AR' }, | |
{ state: 'California', abbr: 'CA' }, | |
{ state: 'Colorado', abbr: 'CO' }, | |
{ state: 'Connecticut', abbr: 'CT' }, | |
{ state: 'Delaware', abbr: 'DE' }, | |
{ state: 'Florida', abbr: 'FL' }, | |
{ state: 'Georgia', abbr: 'GA' }, | |
{ state: 'Hawaii', abbr: 'HI' }, | |
{ state: 'Idaho', abbr: 'ID' }, | |
{ state: 'Illinois', abbr: 'IL' }, | |
{ state: 'Indiana', abbr: 'IN' }, | |
{ state: 'Iowa', abbr: 'IA' }, | |
{ state: 'Kansas', abbr: 'KS' }, | |
{ state: 'Kentucky', abbr: 'KY' }, | |
{ state: 'Louisiana', abbr: 'LA' }, | |
{ state: 'Maine', abbr: 'ME' }, | |
{ state: 'Maryland', abbr: 'MD' }, | |
{ state: 'Massachusetts', abbr: 'MA' }, | |
{ state: 'Michigan', abbr: 'MI' }, | |
{ state: 'Minnesota', abbr: 'MN' }, | |
{ state: 'Mississippi', abbr: 'MS' }, | |
{ state: 'Missouri', abbr: 'MO' }, | |
{ state: 'Montana', abbr: 'MT' }, | |
{ state: 'Nebraska', abbr: 'NE' }, | |
{ state: 'Nevada', abbr: 'NV' }, | |
{ state: 'New Hampshire', abbr: 'NH' }, | |
{ state: 'New Jersey', abbr: 'NJ' }, | |
{ state: 'New Mexico', abbr: 'NM' }, | |
{ state: 'New York', abbr: 'NY' }, | |
{ state: 'North Carolina', abbr: 'NC' }, | |
{ state: 'North Dakota', abbr: 'ND' }, | |
{ state: 'Ohio', abbr: 'OH' }, | |
{ state: 'Oklahoma', abbr: 'OK' }, | |
{ state: 'Oregon', abbr: 'OR' }, | |
{ state: 'Pennsylvania', abbr: 'PA' }, | |
{ state: 'Rhode Island', abbr: 'RI' }, | |
{ state: 'South Carolina', abbr: 'SC' }, | |
{ state: 'South Dakota', abbr: 'SD' }, | |
{ state: 'Tennessee', abbr: 'TN' }, | |
{ state: 'Texas', abbr: 'TX' }, | |
{ state: 'Utah', abbr: 'UT' }, | |
{ state: 'Vermont', abbr: 'VT' }, | |
{ state: 'Virginia', abbr: 'VA' }, | |
{ state: 'Washington', abbr: 'WA' }, | |
{ state: 'West Virginia', abbr: 'WV' }, | |
{ state: 'Wisconsin', abbr: 'WI' }, | |
{ state: 'Wyoming', abbr: 'WY' } | |
]; | |
transform(state: string): string { | |
for (const s in this.states) { | |
if (state === this.states[s].state) { | |
return this.states[s].abbr; | |
} | |
} | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment