Purpose: Show JavaScript and regular expression generation, auto-generate routine mappings
- Create a new file as phone.js
code phone.js
- Prompt Copilot to create a function with a regular expression to validate a US phone number. You can use the CMD+I interface and just Accept the results.
create a function to validate any global phone number using a regular expression
- Let's tell it to document the function by highlighting the code, invoking CMD+I and /doc. You can just Accept the results.
- Now let's see how Copilot can generate some data and mappings for us automatically. Enter the prompt below in the main/separate chat text entry area.
create a mapping of all 50 states to area codes where
the key is the state abbreviation and the value
is an array of area codes with max 10
- After running this, Copilot will generate the start of a list as shown below. Hover over the output area and click to insert the updates at the cursor in the phone.js file. (This assumes the cursor is below the previous function in the file.)
- You can scroll to the bottom to confirm if you got entries for all the states. If you didn't, you could create additional prompts for specific ranges of states, change the number of values downward, etc. You could then copy these into your file if you want. In the past GitHub Copilot Chat may have also added the disclaimer at the bottom of the output that these may not be actual values.
- Let's verify whether or not we have the actual area codes. We can ask Copilot whether or not there the area codes are the actual values. Enter the following in the chat interface.
Are those the actual area codes or, is that fabricated data?
This is another exmaple of GitHub Copilot providing additional resoruces to help you verify the latest information. In this case, it's suggesting you go to the North American Numbering Plan Administration (NANPA) where you can find the most up-to-date information on area codes. You can go to that source and see if the area codes generated by Copilot are accurate. Copilot has evolved to compensate for the fact that the LLM may not have the most up-to-date information.