Typescript Today: How I create function in Elixir using Typescript?

A witty way to write code when you are lazy ๐Ÿ˜…

Intro

Some day this April, I need to write a function that checks if an input is a valid US state abbreviation.

But I was thinking, there are 50 states in America so I need to write 50 repeating codes with different outcomes/values. Something like this in Elixir.


def state_data("TX"), do: ["tx", "texas"]

Problem

You know what it is. I am lazy and I don't want to type 50 line of codes with the same style ๐Ÿฅน

Solution

Use Typescript! ๐Ÿ˜ˆ haha

So I already have this given variables written in typescript that holds 50 states.


export enum Location {
  AL = "AL",
  AK = "AK",
  AS = "AS",
  AZ = "AZ",
  AR = "AR",
  CA = "CA",
  CO = "CO",
  CT = "CT",
  DE = "DE",
  DC = "DC",
  FM = "FM",
  FL = "FL",
  GA = "GA",
  GU = "GU",
  HI = "HI",
  ID = "ID",
  IL = "IL",
  IN = "IN",
  IA = "IA",
  KS = "KS",
  KY = "KY",
  LA = "LA",
  ME = "ME",
  MH = "MH",
  MD = "MD",
  MA = "MA",
  MI = "MI",
  MN = "MN",
  MS = "MS",
  MO = "MO",
  MT = "MT",
  NE = "NE",
  NV = "NV",
  NH = "NH",
  NJ = "NJ",
  NM = "NM",
  NY = "NY",
  NC = "NC",
  ND = "ND",
  MP = "MP",
  OH = "OH",
  OK = "OK",
  OR = "OR",
  PW = "PW",
  PA = "PA",
  PR = "PR",
  RI = "RI",
  SC = "SC",
  SD = "SD",
  TN = "TN",
  TX = "TX",
  UT = "UT",
  VT = "VT",
  VI = "VI",
  VA = "VA",
  WA = "WA",
  WV = "WV",
  WI = "WI",
  WY = "WY",
}

export const locationOptions = [
  { label: "Alabama", value: Location.AL },
  { label: "Alaska", value: Location.AK },
  { label: "American Samoa", value: Location.AS },
  { label: "Arizona", value: Location.AZ },
  { label: "Arkansas", value: Location.AR },
  { label: "California", value: Location.CA },
  { label: "Colorado", value: Location.CO },
  { label: "Connecticut", value: Location.CT },
  { label: "Delaware", value: Location.DE },
  { label: "District Of Columbia", value: Location.DC },
  { label: "Federated States Of Micronesia", value: Location.FM },
  { label: "Florida", value: Location.FL },
  { label: "Georgia", value: Location.GA },
  { label: "Guam", value: Location.GU },
  { label: "Hawaii", value: Location.HI },
  { label: "Idaho", value: Location.ID },
  { label: "Illinois", value: Location.IL },
  { label: "Indiana", value: Location.IN },
  { label: "Iowa", value: Location.IA },
  { label: "Kansas", value: Location.KS },
  { label: "Kentucky", value: Location.KY },
  { label: "Louisiana", value: Location.LA },
  { label: "Maine", value: Location.ME },
  { label: "Marshall Islands", value: Location.MH },
  { label: "Maryland", value: Location.MD },
  { label: "Massachusetts", value: Location.MA },
  { label: "Michigan", value: Location.MI },
  { label: "Minnesota", value: Location.MN },
  { label: "Mississippi", value: Location.MS },
  { label: "Missouri", value: Location.MO },
  { label: "Montana", value: Location.MT },
  { label: "Nebraska", value: Location.NE },
  { label: "Nevada", value: Location.NV },
  { label: "New Hampshire", value: Location.NH },
  { label: "New Jersey", value: Location.NJ },
  { label: "New Mexico", value: Location.NM },
  { label: "New York", value: Location.NY },
  { label: "North Carolina", value: Location.NC },
  { label: "North Dakota", value: Location.ND },
  { label: "Northern Mariana Islands", value: Location.MP },
  { label: "Ohio", value: Location.OH },
  { label: "Oklahoma", value: Location.OK },
  { label: "Oregon", value: Location.OR },
  { label: "Palau", value: Location.PW },
  { label: "Pennsylvania", value: Location.PA },
  { label: "Puerto Rico", value: Location.PR },
  { label: "Rhode Island", value: Location.RI },
  { label: "South Carolina", value: Location.SC },
  { label: "South Dakota", value: Location.SD },
  { label: "Tennessee", value: Location.TN },
  { label: "Texas", value: Location.TX },
  { label: "Utah", value: Location.UT },
  { label: "Vermont", value: Location.VT },
  { label: "Virgin Islands", value: Location.VI },
  { label: "Virginia", value: Location.VA },
  { label: "Washington", value: Location.WA },
  { label: "West Virginia", value: Location.WV },
  { label: "Wisconsin", value: Location.WI },
  { label: "Wyoming", value: Location.WY },
];

So what I did was...

  1. create a file generate.ts, use the variables given ^^^ and create a function
function generate() {
    locationOptions.forEach((location) => {
      console.log(`def state_data("${location.value}"), do: ["${location.value}", "${location.label}"]
    })
}

generate()
  1. Install typescript
> npm install -g typescript
  1. Run the program
> tsc generate.ts
  1. It will generate a js file. Now, run the js file using node
> node generate.js

Output

def state_data("AL"), do: ["al", "alabama"]
def state_data("AK"), do: ["ak", "alaska"]
def state_data("AS"), do: ["as", "american samoa"]
def state_data("AZ"), do: ["az", "arizona"]
def state_data("AR"), do: ["ar", "arkansas"]
def state_data("CA"), do: ["ca", "california"]
def state_data("CO"), do: ["co", "colorado"]
def state_data("CT"), do: ["ct", "connecticut"]
def state_data("DE"), do: ["de", "delaware"]
def state_data("DC"), do: ["dc", "district of columbia"]
def state_data("FM"), do: ["fm", "federated states of micronesia"]
def state_data("FL"), do: ["fl", "florida"]
def state_data("GA"), do: ["ga", "georgia"]
def state_data("GU"), do: ["gu", "guam"]
def state_data("HI"), do: ["hi", "hawaii"]
def state_data("ID"), do: ["id", "idaho"]
def state_data("IL"), do: ["il", "illinois"]
def state_data("IN"), do: ["in", "indiana"]
def state_data("IA"), do: ["ia", "iowa"]
def state_data("KS"), do: ["ks", "kansas"]
def state_data("KY"), do: ["ky", "kentucky"]
def state_data("LA"), do: ["la", "louisiana"]
def state_data("ME"), do: ["me", "maine"]
def state_data("MH"), do: ["mh", "marshall islands"]
def state_data("MD"), do: ["md", "maryland"]
def state_data("MA"), do: ["ma", "massachusetts"]
def state_data("MI"), do: ["mi", "michigan"]
def state_data("MN"), do: ["mn", "minnesota"]
def state_data("MS"), do: ["ms", "mississippi"]
def state_data("MO"), do: ["mo", "missouri"]
def state_data("MT"), do: ["mt", "montana"]
def state_data("NE"), do: ["ne", "nebraska"]
def state_data("NV"), do: ["nv", "nevada"]
def state_data("NH"), do: ["nh", "new hampshire"]
def state_data("NJ"), do: ["nj", "new jersey"]
def state_data("NM"), do: ["nm", "new mexico"]
def state_data("NY"), do: ["ny", "new york"]
def state_data("NC"), do: ["nc", "north carolina"]
def state_data("ND"), do: ["nd", "north dakota"]
def state_data("MP"), do: ["mp", "northern mariana islands"]
def state_data("OH"), do: ["oh", "ohio"]
def state_data("OK"), do: ["ok", "oklahoma"]
def state_data("OR"), do: ["or", "oregon"]
def state_data("PW"), do: ["pw", "palau"]
def state_data("PA"), do: ["pa", "pennsylvania"]
def state_data("PR"), do: ["pr", "puerto rico"]
def state_data("RI"), do: ["ri", "rhode island"]
def state_data("SC"), do: ["sc", "south carolina"]
def state_data("SD"), do: ["sd", "south dakota"]
def state_data("TN"), do: ["tn", "tennessee"]
def state_data("TX"), do: ["tx", "texas"]
def state_data("UT"), do: ["ut", "utah"]
def state_data("VT"), do: ["vt", "vermont"]
def state_data("VI"), do: ["vi", "virgin islands"]
def state_data("VA"), do: ["va", "virginia"]
def state_data("WA"), do: ["wa", "washington"]
def state_data("WV"), do: ["wv", "west virginia"]
def state_data("WI"), do: ["wi", "wisconsin"]
def state_data("WY"), do: ["wy", "wyoming"]

And Voila! Using typescript I don't need to write more than 50 lines of code

Conclusion

It does pay off to know different languages that will help you achieve what you want. ๐Ÿ‘‰๐Ÿ‘ˆ

Happy Coding!

ย