Lab03 Holidays
Background
Rationale
Everybody loves holidays, and your client is no exception !
You have been approached with a problem - your client wishes to know which day of the week Christmas, Easter and Valentine's day would fall on in a particular year, regardless of whether it is in the past, present or future.
Being a clever and efficient software engineer, you knew that writing a date-time library yourself could only bring forth pain and suffering. Fortunately, the wheels do not need to be re-invented for you have recently acquired infinite wisdom through the Node Package Manager (npm).
Getting Started
Please make sure you have completed lab03_password prior.
Copy the SSH clone link from Gitlab and clone this repository on either VLAB or your local machine.
In your terminal, change your directory (using the cd command) into the newly cloned lab. To check if you have done this correctly, type ls in this new directory to see if you can see the relevant files (including holidays.js).
Package Installation
1. Open package.json and look under the key "devDependencies" . We have added these development packages from lab03_password for you.
2. Quickly install the packages with the command:
$ npm install # shortcut: npm i
3. Under "scripts" , make the following changes:
1 "scripts": {
2 "test": "jest",
3 }
For this exercise, we will be using the following libraries:
· date-fns for parsing dates
· date-fns-holiday-us for useful holiday functions
· prompt-sync for reading user input from the command line
1. You can install all three packages in one command with:
1 $ npm i date-fns date-fns-holiday-us prompt-sync
2. Open package.json and ensure that these packages appear under the key "dependencies" , e.g
1 // Note: Your version number may differ
2 "dependencies": {
"date-fns": "^2.28.0", "date-fns-holiday-us": "^0.2.1", "prompt-sync": "^4.2.0"
3 }
3. Use git status, add, commit and push your package.json and package-lock.json.
Task
Writing Tests
In holidays.test.js, complete a test suite for holidaysInRange . You should aim to cover different cases for the function. You do not need to write tests for the main function.
You may find the following resource useful:
https://www.calendar-365.com/holidays/1970.html
Try changing 1970 to a different year in the link above
Implementation
In holidays.js, implement the function holidaysInRange according to its documentation.
This exercise is difficult to complete without using the libraries provided. You may want to spend some time reading the documentation for date-fns
and date-fns-holiday-us before starting.
Ensure that your code passes all of the written tests before submitting it.
Main - Reading Inputs from Commandline
In the main function, use prompt-sync to read the start and end year from the user and print the output of holidaysInRange to stdout . This function is imported and called in main.js.
Note that prompt-sync reads inputs as strings, so you will need to use convert them to integers using parseInt() !
Here are a few examples (it is fine if your output differs by white space):
1 $ node main.js
Enter start: 1970 # NOTE: user enters 1970
2 Enter end: 1972 # NOTE: user enters 1972
3 [
4 {
5 valentinesDay: 'Saturday, 14.02.1970',
6 easter: 'Sunday, 29.03.1970',
7 christmas: 'Friday, 25.12.1970'
8 }, {
9 valentinesDay: 'Sunday, 14.02.1971',
10 easter: 'Sunday, 11.04.1971',
11 christmas: 'Saturday, 25.12.1971'
12 }, {
13 valentinesDay: 'Monday, 14.02.1972',
14 easter: 'Sunday, 02.04.1972',
15 christmas: 'Monday, 25.12.1972'
16
17 }
18 ]
Start is less than 325:
1 $ node main.js
2 Enter start: 324
3 Enter end: 325
4 []
Tips
1. Avoid manually parsing the date string format yourself - use the provided libraries!
2. You may not use any libraries other than the ones listed unless mentioned otherwise by our course staff.
Submission
Use git to add , commit , and push your changes on your master branch.
Check that your code has been uploaded to your Gitlab repository on this website (you may need to refresh the page).
If you have pushed your latest changes to master on Gitlab no further action is required! At the due date and time, we automatically collect your work from what's on your master branch on Gitlab.
Additional Information
Sample package.json
Click to view our sample package.json