Skip to content

Commit

Permalink
Rename functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tsekityam committed Aug 3, 2021
1 parent 5451e6d commit 13960a9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
10 changes: 5 additions & 5 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import tap from "tap";

import { getNthDayOf, getLastDayOf } from "./src/common/util";
import { getNthDay, getLastDay } from "./src/common/util";
import { isHoliday, getHolidays } from "./src/index";

tap.test("nyse-holidays/common/util", async (tests) => {
tests.test("getNthDayOf", async (test) => {
tests.test("getNthDay", async (test) => {
[
{ n: 1, year: 2020, month: 1, day: 1, expected: "Mon Jan 06 2020" },
{ n: 2, year: 2020, month: 2, day: 2, expected: "Tue Feb 11 2020" },
Expand All @@ -13,12 +13,12 @@ tap.test("nyse-holidays/common/util", async (tests) => {
{ n: 5, year: 2020, month: 5, day: 5, expected: "Fri May 29 2020" },
].forEach((param) => {
const { expected } = param;
const dateString = getNthDayOf(param).toDate().toDateString();
const dateString = getNthDay(param).toDate().toDateString();
test.ok(dateString === expected, `${dateString} === ${expected}`);
});
});

tests.test("getLastDayOf", async (test) => {
tests.test("getLastDay", async (test) => {
[
{ year: 2020, month: 1, day: 1, expected: "Mon Jan 27 2020" },
{ year: 2020, month: 2, day: 2, expected: "Tue Feb 25 2020" },
Expand All @@ -27,7 +27,7 @@ tap.test("nyse-holidays/common/util", async (tests) => {
{ year: 2020, month: 5, day: 5, expected: "Fri May 29 2020" },
].forEach((param) => {
const { expected } = param;
const dateString = getLastDayOf(param).toDate().toDateString();
const dateString = getLastDay(param).toDate().toDateString();
test.ok(dateString === expected, `${dateString} === ${expected}`);
});
});
Expand Down
18 changes: 9 additions & 9 deletions src/common/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from "dayjs";

export const getDateFor = (param: {
export const getDate = (param: {
year: number;
month: number;
day?: number;
Expand All @@ -9,15 +9,15 @@ export const getDateFor = (param: {
return dayjs(`${year}-${month}-${day ?? 1}`, "YYYY-MM-DD");
};

export const getNthDayOf = (param: {
export const getNthDay = (param: {
n: number;
year: number;
month: number;
day: number;
}): dayjs.Dayjs => {
const { n, year, month, day } = param;

let result = getDateFor({ month, year }).day(day);
let result = getDate({ month, year }).day(day);

if (result.month() !== month - 1) {
result = result.add(1, "week");
Expand All @@ -28,14 +28,14 @@ export const getNthDayOf = (param: {
return result;
};

export const getLastDayOf = (param: {
export const getLastDay = (param: {
year: number;
month: number;
day: number;
}): dayjs.Dayjs => {
const { year, month, day } = param;

const daysInMonth = dayjs(getDateFor({ month, year })).daysInMonth();
const daysInMonth = dayjs(getDate({ month, year })).daysInMonth();
const lastDayOfMonth = dayjs(`${year}-${month}-${daysInMonth}`, "YYYY-MM-DD");

let result = lastDayOfMonth.day(day);
Expand All @@ -47,7 +47,7 @@ export const getLastDayOf = (param: {
return result;
};

const getEasterSundayOf = (param: { year: number }): dayjs.Dayjs => {
const getEasterSunday = (param: { year: number }): dayjs.Dayjs => {
const { year } = param;

/* credit: https://gist.github.com/johndyer/0dffbdd98c2046f41180c051f378f343 */
Expand All @@ -66,10 +66,10 @@ const getEasterSundayOf = (param: { year: number }): dayjs.Dayjs => {
month = 3 + f((L + 40) / 44),
day = L + 28 - 31 * f(month / 4);

return getDateFor({ year, month, day });
return getDate({ year, month, day });
};

export const getGoodFridayOf = (param: { year: number }): dayjs.Dayjs => {
export const getGoodFriday = (param: { year: number }): dayjs.Dayjs => {
const { year } = param;
return getEasterSundayOf({ year }).subtract(2, "days");
return getEasterSunday({ year }).subtract(2, "days");
};
25 changes: 10 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import dayjs from "dayjs";
import {
getDateFor,
getGoodFridayOf,
getLastDayOf,
getNthDayOf,
} from "./common/util";
import { getDate, getGoodFriday, getLastDay, getNthDay } from "./common/util";

export type Holiday = {
name: string;
Expand All @@ -14,27 +9,27 @@ export type Holiday = {

export const getHolidays = (year: number): Holiday[] =>
[
{ name: "New Years Day", date: getDateFor({ year, month: 1, day: 1 }) },
{ name: "New Years Day", date: getDate({ year, month: 1, day: 1 }) },
{
name: "Martin Luther King, Jr. Day",
date: getNthDayOf({ n: 3, year, month: 1, day: 1 }),
date: getNthDay({ n: 3, year, month: 1, day: 1 }),
},
{
name: "Washington's Birthday",
date: getNthDayOf({ n: 3, year, month: 2, day: 1 }),
date: getNthDay({ n: 3, year, month: 2, day: 1 }),
},
{
name: "Good Friday",
date: getGoodFridayOf({ year }),
date: getGoodFriday({ year }),
},
{ name: "Memorial Day", date: getLastDayOf({ year, month: 5, day: 1 }) },
{ name: "Independence Day", date: getDateFor({ year, month: 7, day: 4 }) },
{ name: "Labor Day", date: getNthDayOf({ n: 1, year, month: 9, day: 1 }) },
{ name: "Memorial Day", date: getLastDay({ year, month: 5, day: 1 }) },
{ name: "Independence Day", date: getDate({ year, month: 7, day: 4 }) },
{ name: "Labor Day", date: getNthDay({ n: 1, year, month: 9, day: 1 }) },
{
name: "Thanksgiving Day",
date: getNthDayOf({ n: 4, year, month: 11, day: 4 }),
date: getNthDay({ n: 4, year, month: 11, day: 4 }),
},
{ name: "Christmas Day", date: getDateFor({ year, month: 12, day: 25 }) },
{ name: "Christmas Day", date: getDate({ year, month: 12, day: 25 }) },
]
.map((holiday) => {
if (holiday.date.day() === 0) {
Expand Down

0 comments on commit 13960a9

Please sign in to comment.