@18f/us-federal-holidays
Advanced tools
Comparing version 2.0.1 to 2.0.2
109
index.js
@@ -1,30 +0,41 @@ | ||
const pad = n => `${n < 10 ? "0" : ""}${n}`; | ||
const dayjs = require("dayjs"); | ||
const utcPlugin = require("dayjs/plugin/utc"); | ||
dayjs.extend(utcPlugin); | ||
const getDateFor = ({ day = 1, month, year }) => | ||
new Date(`${year}-${pad(month)}-${pad(day)}T00:00:00.000Z`); | ||
dayjs(`${year}-${month}-${day}`, "YYYY-MM-DD"); | ||
const getNthDayOf = (n, day, month, year) => { | ||
const firstOfMonth = getDateFor({ month, year }); | ||
let result = dayjs(getDateFor({ month, year })).day(day); | ||
let dayOffset = firstOfMonth.getUTCDay() - day; | ||
if (dayOffset > 0) { | ||
dayOffset = 7 - dayOffset; | ||
} else { | ||
dayOffset = -dayOffset; | ||
// dayjs.day(x) can return a time in the past (relative to the date being | ||
// operated on), because it returns a time from within the operand date's | ||
// current week. E.g.: | ||
// | ||
// date = July 1, 2021 # Thursday | ||
// dayjs(date).day(0) # Get Sunday | ||
// # returns June 27, 2021 | ||
if (result.month() !== month - 1) { | ||
result = result.add(1, "week"); | ||
} | ||
const initialDay = firstOfMonth.getUTCDate() + dayOffset; | ||
const finalDay = initialDay + 7 * (n - 1); | ||
return getDateFor({ day: finalDay, month, year }); | ||
result = result.add(n - 1, "week"); | ||
return result; | ||
}; | ||
const getLastDayOf = (day, month, year) => { | ||
const firstOfDay = getNthDayOf(1, day, month, year).getUTCDate(); | ||
const daysInMonth = new Date(year, month, 0).getUTCDate() - 7; | ||
const daysInMonth = dayjs(getDateFor({ month, year })).daysInMonth(); | ||
const lastDayOfMonth = dayjs(`${year}-${month}-${daysInMonth}`, "YYYY-MM-DD"); | ||
let lastOfDay = firstOfDay; | ||
while (lastOfDay <= daysInMonth) { | ||
lastOfDay += 7; | ||
let result = lastDayOfMonth.day(day); | ||
// See above comment for more details. TL;DR is dayjs.day(x) is not | ||
// constrained to the same month as the operand object. | ||
if (result.month() !== month - 1) { | ||
result = result.subtract(1, "week"); | ||
} | ||
return getDateFor({ day: lastOfDay, month, year }); | ||
return result; | ||
}; | ||
@@ -113,39 +124,23 @@ | ||
if (shiftSaturdayHolidays || shiftSundayHolidays) { | ||
holidays.forEach(holiday => { | ||
// Allow the holiday objects to be modified inside this loop: | ||
/* eslint-disable no-param-reassign */ | ||
return holidays.map(holiday => { | ||
let date = dayjs(holiday.date); | ||
const dow = holiday.date.getUTCDay(); | ||
if (date.day() === 0 && shiftSundayHolidays) { | ||
// Actual holiday falls on Sunday. Shift the observed date forward to | ||
// Monday. | ||
date = date.add(1, "day"); | ||
} | ||
if (dow === 0 && shiftSundayHolidays) { | ||
// Actual holiday falls on Sunday. Shift the observed date forward to | ||
// Monday. | ||
holiday.date = new Date( | ||
Date.UTC( | ||
holiday.date.getUTCFullYear(), | ||
holiday.date.getUTCMonth(), | ||
holiday.date.getUTCDate() + 1 | ||
) | ||
); | ||
} else if (dow === 6 && shiftSaturdayHolidays) { | ||
// Actual holiday falls on Saturday. Shift the observed date backward | ||
// to Friday. | ||
holiday.date = new Date( | ||
Date.UTC( | ||
holiday.date.getUTCFullYear(), | ||
holiday.date.getUTCMonth(), | ||
holiday.date.getUTCDate() - 1 | ||
) | ||
); | ||
} | ||
}); | ||
} | ||
if (date.day() === 6 && shiftSaturdayHolidays) { | ||
// Actual holiday falls on Saturday. Shift the observed date backward | ||
// to Friday. | ||
date = date.subtract(1, "day"); | ||
} | ||
holidays.forEach(holiday => { | ||
holiday.dateString = `${holiday.date.getUTCFullYear()}-${holiday.date.getUTCMonth() + | ||
1}-${holiday.date.getUTCDate()}`; | ||
return { | ||
name: holiday.name, | ||
date: date.toDate(), | ||
dateString: date.format("YYYY-MM-DD") | ||
}; | ||
}); | ||
return holidays; | ||
}; | ||
@@ -157,3 +152,5 @@ | ||
) => { | ||
const year = utc ? date.getUTCFullYear() : date.getFullYear(); | ||
const newDate = utc ? dayjs.utc(date) : dayjs(date); | ||
const year = newDate.year(); | ||
const shift = { shiftSaturdayHolidays, shiftSundayHolidays }; | ||
@@ -165,14 +162,8 @@ | ||
const nextYear = allFederalHolidaysForYear(year + 1, shift); | ||
if (nextYear[0].date.getUTCFullYear() === year) { | ||
allForYear.push(nextYear[0]); | ||
} | ||
allForYear.push(nextYear[0]); | ||
const mm = utc ? date.getUTCMonth() : date.getMonth(); | ||
const dd = utc ? date.getUTCDate() : date.getDate(); | ||
// If any dates in this year's holiday list match the one passed in, then | ||
// the passed-in date is a holiday. Otherwise, it is not. | ||
return allForYear.some( | ||
holiday => | ||
holiday.date.getUTCMonth() === mm && holiday.date.getUTCDate() === dd | ||
holiday => holiday.dateString === newDate.format("YYYY-MM-DD") | ||
); | ||
@@ -179,0 +170,0 @@ }; |
@@ -28,3 +28,4 @@ const tap = require("tap"); | ||
"2016-12-26", // Christmas Day falls on a Sunday, observed after | ||
"2017-12-25" | ||
"2017-12-25", | ||
"2021-05-31" // https://github.com/18F/us-federal-holidays/issues/28 | ||
].forEach(dateString => { | ||
@@ -54,3 +55,4 @@ const date = getDate(dateString); | ||
"2015-07-04", // Independence Day falls on a Saturday, so is not a holiday | ||
"2016-12-25" // Christmas Day falls on a Sunday, so is not a holiday | ||
"2016-12-25", // Christmas Day falls on a Sunday, so is not a holiday | ||
"2021-05-24" // https://github.com/18F/us-federal-holidays/issues/28 | ||
].forEach(dateString => { | ||
@@ -57,0 +59,0 @@ const date = getDate(dateString); |
{ | ||
"name": "@18f/us-federal-holidays", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "All about US federal holidays", | ||
@@ -30,2 +30,3 @@ "main": "index.js", | ||
"lint": "eslint index.test.js index.js", | ||
"format": "prettier --write index.test.js index.js", | ||
"test": "tap --reporter=spec index.test.js" | ||
@@ -42,4 +43,12 @@ }, | ||
}, | ||
"dependencies": {}, | ||
"prettier": {} | ||
"dependencies": { | ||
"dayjs": "^1.10.6" | ||
}, | ||
"prettier": { | ||
"trailingComma": "none", | ||
"tabWidth": 2, | ||
"semi": true, | ||
"singleQuote": false, | ||
"arrowParens": "avoid" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27799
1
465
+ Addeddayjs@^1.10.6
+ Addeddayjs@1.11.13(transitive)