This command-line application was created by Stefan Klinkusch at Digital Career Institute in Berlin, Germany. It is a recapture of the node-worldtime repository and consumes the World Time API.
It has three main modes:
- showing a thank you message
- showing a list of timezones
- showing the current time for a given timezone
- JavaScript
- Node.js
- Clone the repository using
git clone git@github.com:sklinkusch/node-walkthrough.git(SSH) orgit clone https://github.com/sklinkusch/node-walkthrough(HTTPS). - Move into the directory
node-walkthroughand runnpm installoryarn.
Type: node index.js --help. The output is
Thank you for using this app.
Type node index.js --option. The output is a list of timezones:
{ area: 'Africa', city: 'Abidjan' }
{ area: 'Africa', city: 'Accra' }
{ area: 'Africa', city: 'Algiers' }
{ area: 'Africa', city: 'Bissau' }
{ area: 'Africa', city: 'Cairo' }
...
{ area: 'Pacific', city: 'Tarawa' }
{ area: 'Pacific', city: 'Tongatapu' }
{ area: 'Pacific', city: 'Wake' }
{ area: 'Pacific', city: 'Wallis' }
{ area: 'WET', city: undefined }
Type node index.js $AREA $CITY with $AREA as a continent/area from the list and $CITY as a matching city from the list (area and city should appear as a pair), e.g. node index.js europe berlin. The app is not sensitive to the case. The output should look like
11.07.2019 10:46:39
-
In a new file called
index.jswrite a CLI application that accepts a user parameter and logs it to the console. -
Push your result to a new GitHub repo called node-walkthrough and post the link to #exercises on Slack. We're going to do this after each exercise because fearlessness is built from backups. Here's a workflow that works for me:
a) At the start of each exercise,
checkouta new branch andcommitworking code at each step as you complete it.b)
checkout masterandmergeyour feature branch.pushonce you finish each one to help me keep an eye on progress as you go.c) Test that your code definitely works on master, then delete unused branches.
-
In a new file called
messaging.jswrite a new function calledshowHelp.a) This function should print information to the console if the term
--helpappears anywhere in the argument list.b) Connect this function to
index.jsusing CommonJS Module syntax. Think about your control flow before writing code.Remember: Git is your friend.
-
In a new file called
formatting.jswrite a new function calledremoveWhitespace. This function should take a string as a parameter and:a) Trim whitespace from the beginning and end of the param
b) Collapse spaces (ensure only one space appears at a time, no doubles)
Example output:
- ' sao tome' => 'sao tome'
- 'kuala lumpur ' => 'kuala lumpur'
Tip: Use console.log to check your output as you go. Remove any test calls to console.log once you're happy with the output.
Remember: Do the Git dance.
-
Still in
formatting.jswrite a new function calledcapitalizeInitial. This function should take a string as a parameter, and return it with only the first letter capitalized.Example output:
- 'europe' => 'Europe'
- 'BERLIN' => 'Berlin'
- 'aSIa' => 'Asia'
Remember: Have you committed lately?
-
Still in
formatting.jswrite a new function calledprepareStringthat puts an input string throughcapitalizeInitialandremoveWhitespacethen replaces any spaces with underscores.Example output:
- 'kuala lumpur' => 'Kuala_Lumpur'
- ' puerto rico' => 'Puerto_Rico'
- ' tokyo' => 'Tokyo'
There are a million other ways we should sanitize user input before using it in a production app, but this will do for now.
Remember: Hmmm end of the exercise. Wasn't there something to remember...?
- Start a new file called
client.jsto handle your API call (similar to what we did yesterday).
Remember: Did you remember on your own this time?
- Write a function to format the API response before presenting it to the user, so it's easier to read. (To decide where to write this function, think about how your control flow will go through your files and functions between input and ouput).
Remember: What happens before the next exercise?
- Handle common errors with helpful messages for the user
Remember: You know the drill.
- Pick another endpoint to be handled by the same app. At which points will your existing control flow need to change to accommodate this? Go through the steps of preparing input, performing the call and formatting output. Keep committing as you complete small steps toward this.