diff --git a/README.md b/README.md index 4b893e5..513fa23 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ This program was written for the 2022 Hackathon. ## Releases -* [`v1.0.4 (Current Release)`](https://github.com/r1zyn/MatarikiProgram/releases/tag/v1.0.4) +* [`v1.0.5 (Current release)`](https://github.com/r1zyn/MatarikiProgram/releases/tag/v1.0.5) +* [`v1.0.4`](https://github.com/r1zyn/MatarikiProgram/releases/tag/v1.0.4) * [`v1.0.3`](https://github.com/r1zyn/MatarikiProgram/releases/tag/v1.0.3) * [`v1.0.2`](https://github.com/r1zyn/MatarikiProgram/releases/tag/v1.0.2) * [`v1.0.1`](https://github.com/r1zyn/MatarikiProgram/releases/tag/v1.0.1) @@ -29,7 +30,7 @@ If you prefer to use [`npm`](https://npmjs.com/), be sure to delete [`yarn.lock` The script for running the program is defined in the `scripts` property in [`package.json`](./package.json) ```cmd -npm run start +npm start # or yarn start ``` @@ -46,26 +47,31 @@ You should see something like this: ## How it works The program will request you enter a valid year (has to be able to be converted to a number) and will in result output its predictions of when the Matariki holiday for the given year will be. -TypeScript files will be compiled to the `build` directory (only generated once `npm run start` or `yarn start` is executed). +TypeScript files will be compiled to the `build` directory (only generated once `npm start` or `yarn start` is executed). ## Method To see the original method, view the [`archived`](https://github.com/r1zyn/MatarikiProgram/tree/archived) branch. -The current method is yet to be added. +It is known that the Tangaroa period occurs 22 days after the new moon in May-June. Moreover, the Chinese Lunar calendar is also determined by the moon’s lunar cycle, where the first of the month is the beginning of the new moon. The Chinese lunar month in the Gregorian May-June-July period is May (五月). Therefore, the Tangaroa period begins on the 23rd of May in the Chinese lunar calendar as it is 22 days after the beginning of May. We can convert the lunar date of May 23rd into the Gregorian date, and we can determine the Matariki holiday using this date, by getting the closest Friday. + +If the date we use to determine the Matariki holiday is on a weekend, the holiday will be on the previous Friday. Otherwise, the holiday will occur on the next Friday. +Since the Georgian calendar is a solar calendar, we can always predict the Matariki cluster becomes visible at dawn on and after 19 June every year. ## Contributing See the [contributing guide](.github/CONTRIBUTING.md). ## Todo -* Resolve +* Nothing to do. ## Contributors -* [@r1zyn](https://github.com/r1zyn) -* [@JiachenHH](https://github.com/JiachenHH) -* [@SILentASSassinE](https://github.com/SILentASSassinE) -* [@tommy-duan-macleans](https://github.com/tommy-duan-macleans) +* [@r1zyn](https://github.com/r1zyn) - getMatariki and parseDate functions, commenting +* [@JiachenHH](https://github.com/JiachenHH) - Date utility functions +* [@SILentASSassinE](https://github.com/SILentASSassinE) - Method concept, testing (Jest and manual) +* [@tommy-duan-macleans](https://github.com/tommy-duan-macleans) - index and types files + +Note: due to 3 members unfamiliar with `git`, all members contributed via the [`Live Share extension`](https://code.visualstudio.com/learn/collaboration/live-share) for Visual Studio Code, and one member then pushed commits to the repository. ## Import links diff --git a/package.json b/package.json index b436a80..b01c88b 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "chalk": "4.1.2", "jest": "^28.1.3", "lunar": "^0.0.3", + "lune": "^0.4.0", "nodemon": "^2.0.19" }, "devDependencies": { diff --git a/src/utils/closestFriday.ts b/src/utils/closestFriday.ts index 1043c28..c41dcd5 100644 --- a/src/utils/closestFriday.ts +++ b/src/utils/closestFriday.ts @@ -4,44 +4,49 @@ * @returns {Date} */ export default function closestFriday(fromDate: Date): Date { - if (fromDate.getMonth() === 5 && fromDate.getDate() < 19) { - fromDate.setDate(fromDate.getDate() + 7); + if (fromDate.getMonth() === 5 && fromDate.getDate() <= 19) { + fromDate.setDate(fromDate.getDate() + 7); // If the pointer date is before or on the 19th of June, shift it a week forward (due to June 19th rule) } + if (fromDate.getDay() === 5) { + return fromDate; // If the day is Friday, keep it as it is + } + + /** + * The new Friday date to return. + * @type {Date} + */ const returnValue: Date = new Date(fromDate); - const difference: number = getDifferenceBetweenDays(5, fromDate.getDay()); - returnValue.setDate(returnValue.getDate() + difference); + const difference: number = getDifferenceBetweenDays(5, fromDate.getDay()); // Get the difference between Friday and the day + returnValue.setDate(returnValue.getDate() + difference); // Alter the date accordingly - return returnValue; + return returnValue; // Return the Friday dtae } /** - * Function that gets the difference between two days. - * @param {number} start The starting date - * @param {number} end The ending date - * @param {number} weekLength The length of a week + * Function that gets the difference between two days. + * @param {number} start The starting day + * @param {number} end The ending day * @returns {number} */ export function getDifferenceBetweenDays( start: number, end: number - // weekLength: number = 7 ): number { + /** + * The weekend days for Date.getDay(). + * @type {number[]} + */ const weekends: number[] = [0, 6]; - if (weekends.includes(end)) { + + if (weekends.includes(end)) { // Checks if the day is a weekend, as if the first day of the Tangaroa period is a weekend, Matariki will be the Friday before, and vice-versa if (end === 0) { - end = 7; + end = 7; // Change Sunday to index of 7 so we can get the day difference } - return -(end - start); + return -(end - start); // Get the difference between the day and the previous Friday } else { - return start - end; + return start - end; // Else, retun the difference between the day and the next Friday } - - // if (end <= 1) { - // return -(weekLength - start + end); - // } else { - // return start - end; - // } } diff --git a/src/utils/getDateSuffix.ts b/src/utils/getDateSuffix.ts index 294babb..953961c 100644 --- a/src/utils/getDateSuffix.ts +++ b/src/utils/getDateSuffix.ts @@ -4,15 +4,17 @@ * @returns {string} */ export default function getDateSuffix(date: number): string { - if (date > 3 && date < 21) return "th"; - switch (date % 10) { + if (date > 3 && date < 21) return "th"; // Return the "th" suffix for corresponding numbers + switch ( + date % 10 // Uses modulo division to get the last digit of the date to determine the suffix + ) { case 1: - return "st"; + return "st"; // Return the "st" suffix for numbers ending with 1 case 2: - return "nd"; + return "nd"; // Return the "nd" suffix for numbers ending with 2 case 3: - return "rd"; + return "rd"; // Return the "rd" suffix for numbers ending with 3 default: - return "th"; + return "th"; // Return the "th" suffix for numbers ending with 4, 5, 6, 7, 8, 9 } } diff --git a/src/utils/getMatariki.ts b/src/utils/getMatariki.ts index 8e63503..db3d1a6 100644 --- a/src/utils/getMatariki.ts +++ b/src/utils/getMatariki.ts @@ -1,26 +1,36 @@ import type { MatarikiResult } from "../types"; import closestFriday from "./closestFriday"; +// import lune from "lune"; import lunar from "lunar"; /** * Function that returns the Matariki public holiday for a given year - * and it's pointer date. + * and the first day of the Tangaroa date (or referrenced in the program as "pointer" date) * @param {string} year The year to find the Matariki public holiday * @returns */ export default function getMatariki(year: string): MatarikiResult { + /** + * The date of the first day of the Tangaroa period. + * @type {Date} + */ + // const pointer: Date = lune.phase_hunt( + // new Date(parseInt(year), 4, 18) + // ).nextnew_date; + // pointer.setDate(pointer.getDate() + 22); + /** * Pointer date we can use to predict the Matariki public holiday. + * AKA the date of the first day of the Tangaroa period. * @type {Date} */ - const pointer: Date = lunar([parseInt(year), 4, 25, true]).toDate(); + const pointer: Date = lunar([parseInt(year), 4, 23, true]).toDate(); /** * Date object representing the Matariki public holiday. * @type {Date} */ - const holiday: Date = - pointer.getDay() === 5 ? pointer : closestFriday(pointer); + const holiday: Date = closestFriday(pointer); return { holiday, diff --git a/test/getMatariki.test.ts b/test/getMatariki.test.ts index 0369503..f2fe050 100644 --- a/test/getMatariki.test.ts +++ b/test/getMatariki.test.ts @@ -6,7 +6,7 @@ describe("Get the date for the Matariki public holiday", (): void => { let mockHoliday: Date; beforeEach((): void => { - mockPointer = new Date(2022, 5, 22); + mockPointer = new Date(2022, 5, 20); mockHoliday = new Date(2022, 5, 24); }); diff --git a/yarn.lock b/yarn.lock index b7f3e9c..128b836 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2418,6 +2418,11 @@ lunar@^0.0.3: dependencies: lodash "^3.7.0" +lune@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/lune/-/lune-0.4.0.tgz#4ac3faaa6cd351236815212e8db7ecd8fb5e2306" + integrity sha512-rMdQf3UWY+fga8vo2MjXkpvqesliljvqIS2AjkEHZLhj7fpgT30S11hoEfug/yNPLy1jWLr2G2esQIzlQjbfpQ== + make-dir@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"