@18f/us-federal-holidays
Advanced tools
Comparing version 1.3.0 to 1.4.0
123
bin/index.js
"use strict"; | ||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } | ||
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } | ||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } | ||
function getNthDayOf(n, day, month, year) { | ||
var firstOfMonth = new Date(Date.parse(month + "/1/" + year + " GMT")); | ||
var firstOfMonth = new Date(Date.parse("".concat(month, "/1/").concat(year, " GMT"))); | ||
var dayOffset = firstOfMonth.getUTCDay() - day; | ||
var dayOffset = firstOfMonth.getUTCDay() - day; | ||
if (dayOffset > 0) { | ||
@@ -12,6 +20,6 @@ dayOffset = 7 - dayOffset; | ||
} | ||
var initialDay = firstOfMonth.getUTCDate() + dayOffset; | ||
var finalDay = initialDay + 7 * (n - 1); | ||
return new Date(Date.parse(month + "/" + finalDay + "/" + year + " GMT")); | ||
return new Date(Date.parse("".concat(month, "/").concat(finalDay, "/").concat(year, " GMT"))); | ||
} | ||
@@ -22,4 +30,4 @@ | ||
var daysInMonth = new Date(year, month, 0).getUTCDate() - 7; | ||
var lastOfDay = firstOfDay; | ||
var lastOfDay = firstOfDay; | ||
while (lastOfDay <= daysInMonth) { | ||
@@ -29,3 +37,3 @@ lastOfDay += 7; | ||
return new Date(Date.parse(month + "/" + lastOfDay + "/" + year + " GMT")); | ||
return new Date(Date.parse("".concat(month, "/").concat(lastOfDay, "/").concat(year, " GMT"))); | ||
} | ||
@@ -38,13 +46,11 @@ | ||
_ref$shiftSaturdayHol = _ref.shiftSaturdayHolidays, | ||
shiftSaturdayHolidays = _ref$shiftSaturdayHol === undefined ? true : _ref$shiftSaturdayHol, | ||
shiftSaturdayHolidays = _ref$shiftSaturdayHol === void 0 ? true : _ref$shiftSaturdayHol, | ||
_ref$shiftSundayHolid = _ref.shiftSundayHolidays, | ||
shiftSundayHolidays = _ref$shiftSundayHolid === undefined ? true : _ref$shiftSundayHolid; | ||
shiftSundayHolidays = _ref$shiftSundayHolid === void 0 ? true : _ref$shiftSundayHolid; | ||
var holidays = []; | ||
holidays.push({ | ||
name: "New Year's Day", | ||
date: new Date(Date.parse("1/1/" + year + " GMT")) | ||
date: new Date(Date.parse("1/1/".concat(year, " GMT"))) | ||
}); | ||
holidays.push({ | ||
@@ -54,3 +60,2 @@ name: "Birthday of Martin Luther King, Jr.", | ||
}); | ||
holidays.push({ | ||
@@ -60,3 +65,2 @@ name: "Washington's Birthday", | ||
}); | ||
holidays.push({ | ||
@@ -66,8 +70,6 @@ name: "Memorial Day", | ||
}); | ||
holidays.push({ | ||
name: "Independence Day", | ||
date: new Date(Date.parse("7/4/" + year + " GMT")) | ||
date: new Date(Date.parse("7/4/".concat(year, " GMT"))) | ||
}); | ||
holidays.push({ | ||
@@ -77,3 +79,2 @@ name: "Labor Day", | ||
}); | ||
holidays.push({ | ||
@@ -83,8 +84,6 @@ name: "Columbus Day", | ||
}); | ||
holidays.push({ | ||
name: "Veterans Day", | ||
date: new Date(Date.parse("11/11/" + year + " GMT")) | ||
date: new Date(Date.parse("11/11/".concat(year, " GMT"))) | ||
}); | ||
holidays.push({ | ||
@@ -94,6 +93,5 @@ name: "Thanksgiving Day", | ||
}); | ||
holidays.push({ | ||
name: "Christmas Day", | ||
date: new Date(Date.parse("12/25/" + year + " GMT")) | ||
date: new Date(Date.parse("12/25/".concat(year, " GMT"))) | ||
}); | ||
@@ -103,3 +101,2 @@ | ||
holidays.forEach(function (holiday) { | ||
var dow = holiday.date.getUTCDay(); | ||
@@ -112,40 +109,68 @@ | ||
} | ||
holiday.dateString = holiday.date.getUTCFullYear() + "-" + (holiday.date.getUTCMonth() + 1) + "-" + holiday.date.getUTCDate(); | ||
}); | ||
} | ||
holidays.forEach(function (holiday) { | ||
holiday.dateString = "".concat(holiday.date.getUTCFullYear(), "-").concat(holiday.date.getUTCMonth() + 1, "-").concat(holiday.date.getUTCDate()); | ||
}); | ||
return holidays; | ||
} | ||
module.exports = { | ||
isAHoliday: function isAHoliday() { | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
function isAHoliday() { | ||
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref2$shiftSaturdayHo = _ref2.shiftSaturdayHolidays, | ||
shiftSaturdayHolidays = _ref2$shiftSaturdayHo === undefined ? true : _ref2$shiftSaturdayHo, | ||
_ref2$shiftSundayHoli = _ref2.shiftSundayHolidays, | ||
shiftSundayHolidays = _ref2$shiftSundayHoli === undefined ? true : _ref2$shiftSundayHoli, | ||
_ref2$utc = _ref2.utc, | ||
utc = _ref2$utc === undefined ? false : _ref2$utc; | ||
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref2$shiftSaturdayHo = _ref2.shiftSaturdayHolidays, | ||
shiftSaturdayHolidays = _ref2$shiftSaturdayHo === void 0 ? true : _ref2$shiftSaturdayHo, | ||
_ref2$shiftSundayHoli = _ref2.shiftSundayHolidays, | ||
shiftSundayHolidays = _ref2$shiftSundayHoli === void 0 ? true : _ref2$shiftSundayHoli, | ||
_ref2$utc = _ref2.utc, | ||
utc = _ref2$utc === void 0 ? false : _ref2$utc; | ||
var year = utc ? date.getUTCFullYear() : date.getFullYear(); | ||
var shift = { shiftSaturdayHolidays: shiftSaturdayHolidays, shiftSundayHolidays: shiftSundayHolidays }; | ||
var year = utc ? date.getUTCFullYear() : date.getFullYear(); | ||
var shift = { | ||
shiftSaturdayHolidays: shiftSaturdayHolidays, | ||
shiftSundayHolidays: shiftSundayHolidays | ||
}; | ||
var allForYear = allFederalHolidaysForYear(year, shift); | ||
var nextYear = allFederalHolidaysForYear(year + 1, shift); | ||
var allForYear = allFederalHolidaysForYear(year, shift); | ||
var nextYear = allFederalHolidaysForYear(year + 1, shift); | ||
if (nextYear[0].date.getUTCFullYear() === year) { | ||
allForYear.push(nextYear[0]); | ||
} | ||
if (nextYear[0].date.getUTCFullYear() === year) { | ||
allForYear.push(nextYear[0]); | ||
} | ||
var mm = utc ? date.getUTCMonth() : date.getMonth(); | ||
var dd = utc ? date.getUTCDate() : date.getDate(); | ||
var mm = utc ? date.getUTCMonth() : date.getMonth(); | ||
var dd = utc ? date.getUTCDate() : date.getDate(); | ||
return allForYear.some(function (holiday) { | ||
return holiday.date.getUTCMonth() === mm && holiday.date.getUTCDate() === dd; | ||
}); | ||
} | ||
return allForYear.some(function (holiday) { | ||
return holiday.date.getUTCMonth() === mm && holiday.date.getUTCDate() === dd; | ||
}); | ||
}, | ||
function getOneYearFromNow() { | ||
var future = new Date(); | ||
future.setUTCFullYear(new Date().getUTCFullYear() + 1); | ||
return future; | ||
} | ||
allForYear: allFederalHolidaysForYear | ||
function federalHolidaysInRange() { | ||
var startDate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Date(); | ||
var endDate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : getOneYearFromNow(); | ||
var options = arguments.length > 2 ? arguments[2] : undefined; | ||
var startYear = startDate.getFullYear(); | ||
var endYear = endDate.getFullYear(); | ||
var candidates = []; | ||
for (var year = startYear; year <= endYear; year += 1) { | ||
candidates.push.apply(candidates, _toConsumableArray(allFederalHolidaysForYear(year, options))); | ||
} | ||
return candidates.filter(function (h) { | ||
return h.date >= startDate && h.date <= endDate; | ||
}); | ||
} | ||
module.exports = { | ||
isAHoliday: isAHoliday, | ||
allForYear: allFederalHolidaysForYear, | ||
inRange: federalHolidaysInRange | ||
}; |
@@ -100,2 +100,84 @@ const tap = require('tap'); | ||
); | ||
tests.test( | ||
'handles federal holidays within a range (Saturday and Sunday holidays shifted)', | ||
async test => { | ||
const holidays = federalHolidays.inRange( | ||
new Date('2015-07-03'), | ||
new Date('2016-12-26') | ||
); | ||
holidays.forEach(holiday => { | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate(holiday.dateString)), | ||
`${holiday.dateString} is a holiday (observed)` | ||
); | ||
}); | ||
} | ||
); | ||
tests.test( | ||
'handles federal holidays within a range (Saturday shifted only)', | ||
async test => { | ||
const shiftSaturdayHolidays = true; | ||
const shiftSundayHolidays = false; | ||
const holidays = federalHolidays.inRange( | ||
new Date('2015-07-03'), | ||
new Date('2016-12-26'), | ||
{ shiftSaturdayHolidays, shiftSundayHolidays } | ||
); | ||
holidays.forEach(holiday => { | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate(holiday.dateString), { | ||
shiftSaturdayHolidays, | ||
shiftSundayHolidays | ||
}), | ||
`${holiday.dateString} is a holiday (observed)` | ||
); | ||
}); | ||
} | ||
); | ||
tests.test( | ||
'handles federal holidays within a range (Sunday shifted only)', | ||
async test => { | ||
const shiftSaturdayHolidays = false; | ||
const shiftSundayHolidays = true; | ||
const holidays = federalHolidays.inRange( | ||
new Date('2015-07-03'), | ||
new Date('2016-12-26'), | ||
{ shiftSaturdayHolidays, shiftSundayHolidays } | ||
); | ||
holidays.forEach(holiday => { | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate(holiday.dateString), { | ||
shiftSaturdayHolidays, | ||
shiftSundayHolidays | ||
}), | ||
`${holiday.dateString} is a holiday (observed)` | ||
); | ||
}); | ||
} | ||
); | ||
tests.test( | ||
'handles federal holidays within a range (none shifted)', | ||
async test => { | ||
const shiftSaturdayHolidays = false; | ||
const shiftSundayHolidays = false; | ||
const holidays = federalHolidays.inRange( | ||
new Date('2015-07-03'), | ||
new Date('2016-12-26'), | ||
{ shiftSaturdayHolidays, shiftSundayHolidays } | ||
); | ||
holidays.forEach(holiday => { | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate(holiday.dateString), { | ||
shiftSaturdayHolidays, | ||
shiftSundayHolidays | ||
}), | ||
`${holiday.dateString} is a holiday (observed)` | ||
); | ||
}); | ||
} | ||
); | ||
}); |
{ | ||
"name": "@18f/us-federal-holidays", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "All about US federal holidays", | ||
@@ -24,3 +24,3 @@ "main": "bin/index.js", | ||
"presets": [ | ||
"es2015" | ||
"@babel/preset-env" | ||
] | ||
@@ -30,12 +30,13 @@ }, | ||
"devDependencies": { | ||
"babel-cli": "^6.5.1", | ||
"babel-preset-es2015": "^6.5.0", | ||
"eslint": "^5.11.1", | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-config-prettier": "^3.3.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"prettier": "^1.15.3", | ||
"tap": "^12.0.1" | ||
"eslint-config-prettier": "^4.2.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
"prettier": "^1.17.0", | ||
"tap": "^13.1.8" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -56,2 +56,53 @@ # US Federal Holidays | ||
To get a list of all US federal holidays within a date range, use the `inRange` method. If no `start` date is provided in, uses the current date. If the end date is omitted, one year from the current date is used. | ||
```javascript | ||
const fedHolidays = require('@18f/us-federal-holidays'); | ||
const start = new Date('2016-02-13'); | ||
const end = new Date('2017-07-23'); | ||
const options = { shiftSaturdayHolidays: true, shiftSundayHolidays: true }; | ||
const holidays = fedHolidays.federalHolidaysInRange(start, end, options); | ||
// Returns | ||
[ { name: 'Washington\'s Birthday', | ||
date: 2016-02-15T00:00:00.000Z, | ||
dateString: '2016-2-15' }, | ||
{ name: 'Memorial Day', | ||
date: 2016-05-30T00:00:00.000Z, | ||
dateString: '2016-5-30' }, | ||
{ name: 'Independence Day', | ||
date: 2016-07-04T00:00:00.000Z, | ||
dateString: '2016-7-4' }, | ||
{ name: 'Labor Day', | ||
date: 2016-09-05T00:00:00.000Z, | ||
dateString: '2016-9-5' }, | ||
{ name: 'Columbus Day', | ||
date: 2016-10-10T00:00:00.000Z, | ||
dateString: '2016-10-10' }, | ||
{ name: 'Veterans Day', | ||
date: 2016-11-11T00:00:00.000Z, | ||
dateString: '2016-11-11' }, | ||
{ name: 'Thanksgiving Day', | ||
date: 2016-11-24T00:00:00.000Z, | ||
dateString: '2016-11-24' }, | ||
{ name: 'Christmas Day', | ||
date: 2016-12-26T00:00:00.000Z, | ||
dateString: '2016-12-26' }, | ||
{ name: 'New Year\'s Day', | ||
date: 2017-01-02T00:00:00.000Z, | ||
dateString: '2017-1-2' }, | ||
{ name: 'Birthday of Martin Luther King, Jr.', | ||
date: 2017-01-16T00:00:00.000Z, | ||
dateString: '2017-1-16' }, | ||
{ name: 'Washington\'s Birthday', | ||
date: 2017-02-20T00:00:00.000Z, | ||
dateString: '2017-2-20' }, | ||
{ name: 'Memorial Day', | ||
date: 2017-05-29T00:00:00.000Z, | ||
dateString: '2017-5-29' }, | ||
{ name: 'Independence Day', | ||
date: 2017-07-04T00:00:00.000Z, | ||
dateString: '2017-7-4' } ] | ||
``` | ||
To determine if a date is a federal holiday, use the `isAHoliday` method. If no argument is provided, defaults to the current date: | ||
@@ -62,3 +113,7 @@ | ||
const options = { shiftSaturdayHolidays: true, shiftSundayHolidays: true, utc: false }; | ||
const options = { | ||
shiftSaturdayHolidays: true, | ||
shiftSundayHolidays: true, | ||
utc: false | ||
}; | ||
const isAHoliday = fedHolidays.isAHoliday(myDate, options); | ||
@@ -68,3 +123,3 @@ // Returns true or false | ||
Both methods take `options` as a second argument. This argument is a plain object which accepts the following properties: | ||
All three methods take `options` as a second argument. This argument is a plain object which accepts the following properties: | ||
@@ -74,5 +129,5 @@ ```javascript | ||
// Whether or not holidays that fall on Saturdays should be | ||
// shifted to Friday observance. Defaults to true, but if | ||
// you don't follow the US federal standard for observing | ||
// holidays on weekends, you can adjust! | ||
// shifted to Friday observance. If you don't follow the | ||
// US federal standard for observing holidays on weekends, | ||
// you can adjust by setting this value to false. | ||
// Default value is true. | ||
@@ -82,8 +137,14 @@ shiftSaturdayHolidays: boolean, | ||
// Whether or not holidays that fall on Sundays should be | ||
// shifted to Monday observance. Defaults to true, but if | ||
// you don't follow the US federal standard for observing | ||
// holidays on weekends, you can adjust! | ||
// shifted to Monday observance. If you don't follow the | ||
// US federal standard for observing holidays on weekends, | ||
// you can adjust by setting this value to false. | ||
// Default value is true. | ||
shiftSundayHolidays: boolean, | ||
shiftSundayHolidays: boolean | ||
} | ||
``` | ||
Additionally, `isAHoliday` takes an `options.utc` parameter: | ||
```javascript | ||
{ | ||
// Whether to treat the first argument as a UTC date instead | ||
@@ -95,3 +156,3 @@ // of the local time. Defaults to false. This is useful if | ||
// This option only applies to the isAHoliday method. | ||
utc: boolean | ||
utc: boolean; | ||
} | ||
@@ -98,0 +159,0 @@ ``` |
@@ -129,42 +129,64 @@ function getNthDayOf(n, day, month, year) { | ||
} | ||
holiday.dateString = `${holiday.date.getUTCFullYear()}-${holiday.date.getUTCMonth() + | ||
1}-${holiday.date.getUTCDate()}`; | ||
}); | ||
} | ||
holidays.forEach(holiday => { | ||
holiday.dateString = `${holiday.date.getUTCFullYear()}-${holiday.date.getUTCMonth() + | ||
1}-${holiday.date.getUTCDate()}`; | ||
}); | ||
return holidays; | ||
} | ||
module.exports = { | ||
isAHoliday( | ||
date = new Date(), | ||
{ | ||
shiftSaturdayHolidays = true, | ||
shiftSundayHolidays = true, | ||
utc = false | ||
} = {} | ||
) { | ||
const year = utc ? date.getUTCFullYear() : date.getFullYear(); | ||
const shift = { shiftSaturdayHolidays, shiftSundayHolidays }; | ||
function isAHoliday( | ||
date = new Date(), | ||
{ shiftSaturdayHolidays = true, shiftSundayHolidays = true, utc = false } = {} | ||
) { | ||
const year = utc ? date.getUTCFullYear() : date.getFullYear(); | ||
const shift = { shiftSaturdayHolidays, shiftSundayHolidays }; | ||
// Get the holidays this year, plus check if New Year's Day of next year is | ||
// observed on December 31 and if so, add it to this year's list. | ||
const allForYear = allFederalHolidaysForYear(year, shift); | ||
const nextYear = allFederalHolidaysForYear(year + 1, shift); | ||
if (nextYear[0].date.getUTCFullYear() === year) { | ||
allForYear.push(nextYear[0]); | ||
} | ||
// Get the holidays this year, plus check if New Year's Day of next year is | ||
// observed on December 31 and if so, add it to this year's list. | ||
const allForYear = allFederalHolidaysForYear(year, shift); | ||
const nextYear = allFederalHolidaysForYear(year + 1, shift); | ||
if (nextYear[0].date.getUTCFullYear() === year) { | ||
allForYear.push(nextYear[0]); | ||
} | ||
const mm = utc ? date.getUTCMonth() : date.getMonth(); | ||
const dd = utc ? date.getUTCDate() : date.getDate(); | ||
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 | ||
); | ||
}, | ||
allForYear: allFederalHolidaysForYear | ||
// 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 | ||
); | ||
} | ||
function getOneYearFromNow() { | ||
const future = new Date(); | ||
future.setUTCFullYear(new Date().getUTCFullYear() + 1); | ||
return future; | ||
} | ||
function federalHolidaysInRange( | ||
startDate = new Date(), | ||
endDate = getOneYearFromNow(), | ||
options | ||
) { | ||
const startYear = startDate.getFullYear(); | ||
const endYear = endDate.getFullYear(); | ||
const candidates = []; | ||
for (let year = startYear; year <= endYear; year += 1) { | ||
candidates.push(...allFederalHolidaysForYear(year, options)); | ||
} | ||
return candidates.filter(h => h.date >= startDate && h.date <= endDate); | ||
} | ||
module.exports = { | ||
isAHoliday, | ||
allForYear: allFederalHolidaysForYear, | ||
inRange: federalHolidaysInRange | ||
}; |
29468
474
162
9