@18f/us-federal-holidays
Advanced tools
Comparing version 1.7.0 to 2.0.0
@@ -25,7 +25,7 @@ ## Welcome! | ||
Make your changes to `src/index.js`. If you're changing or adding behaviors, | ||
modify or add tests to `index.test.js`. Run your tests with `npm test`. Once | ||
your changes are finished and all tests pass, rebuild with `npm run build`, | ||
which will update `bin/index.js`. Commit all your changes and push them to | ||
your fork and open a pull request into this repo. | ||
Make your changes to `index.js`. If you're changing or adding behaviors, modify | ||
or add tests to `index.test.js`. Run your tests with `npm test`. Once your | ||
changes are finished and all tests pass, commit all your changes and push them | ||
to your fork and open a pull request into this repo. Be sure to update | ||
`package.json` to add yourself to the list of contributors if you want! | ||
@@ -32,0 +32,0 @@ When you open your pull request, please tell us a little about what you're |
@@ -1,4 +0,4 @@ | ||
const tap = require('tap'); | ||
const tap = require("tap"); | ||
const federalHolidays = require('./src/index'); | ||
const federalHolidays = require("./index"); | ||
@@ -9,22 +9,22 @@ const getDate = dateString => new Date(`${dateString} 00:00:00`); | ||
tap.test('handles standard federal holidays', async tests => { | ||
tap.test("handles standard federal holidays", async tests => { | ||
tests.test( | ||
'gets observed holidays, accounting for actual holidays on weekends', | ||
"gets observed holidays, accounting for actual holidays on weekends", | ||
async test => { | ||
[ | ||
'2010-12-31', // New Year's Day falls on a Saturday, observed before | ||
'2012-01-02', // New Year's Day falls on a Sunday, observed after | ||
'2014-01-01', | ||
'2014-01-20', | ||
'2014-02-17', | ||
'2014-05-26', | ||
'2014-07-04', | ||
'2014-09-01', | ||
'2014-10-13', | ||
'2014-11-11', | ||
'2014-11-27', | ||
'2014-12-25', | ||
'2015-07-03', // Independence Day falls on a Saturday, observed before | ||
'2016-12-26', // Christmas Day falls on a Sunday, observed after | ||
'2017-12-25' | ||
"2010-12-31", // New Year's Day falls on a Saturday, observed before | ||
"2012-01-02", // New Year's Day falls on a Sunday, observed after | ||
"2014-01-01", | ||
"2014-01-20", | ||
"2014-02-17", | ||
"2014-05-26", | ||
"2014-07-04", | ||
"2014-09-01", | ||
"2014-10-13", | ||
"2014-11-11", | ||
"2014-11-27", | ||
"2014-12-25", | ||
"2015-07-03", // Independence Day falls on a Saturday, observed before | ||
"2016-12-26", // Christmas Day falls on a Sunday, observed after | ||
"2017-12-25" | ||
].forEach(dateString => { | ||
@@ -48,9 +48,9 @@ const date = getDate(dateString); | ||
tests.test( | ||
'actual holidays on weekends are not listed as observed holidays', | ||
"actual holidays on weekends are not listed as observed holidays", | ||
async test => { | ||
[ | ||
'2011-01-01', // New Year's Day falls on a Saturday, so is not a holiday | ||
'2012-01-01', // New Year's Day falls on a Sunday, so is not a holiday | ||
'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 | ||
"2011-01-01", // New Year's Day falls on a Saturday, so is not a holiday | ||
"2012-01-01", // New Year's Day falls on a Sunday, so is not a holiday | ||
"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 | ||
].forEach(dateString => { | ||
@@ -73,7 +73,42 @@ const date = getDate(dateString); | ||
tests.test("Juneteenth is only included from 2021 onwards", async test => { | ||
test.notOk( | ||
federalHolidays.isAHoliday(getDate("2020-06-19")), | ||
"Juneteenth is not a holiday in 2020" | ||
); | ||
// In 2021, Juneteenth fell on a Saturday, so the observed holiday was the | ||
// 18th instead of the 19th. | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate("2021-06-18")), | ||
"Juneteenth is a holiday in 2021" | ||
); | ||
// 2023 is the first year that the observation of Juneteenth falls on the | ||
// actual holiday. | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate("2023-06-19")), | ||
"Juneteenth is a holiday in 2023" | ||
); | ||
test.notOk( | ||
federalHolidays | ||
.allForYear(2020) | ||
.some(({ name }) => name === "Juneteenth National Independence Day"), | ||
"Juneteenth is not included in the list of holidays for 2020" | ||
); | ||
test.ok( | ||
federalHolidays | ||
.allForYear(2021) | ||
.some(({ name }) => name === "Juneteenth National Independence Day"), | ||
"Juneteenth is included in the list of holidays for 2021" | ||
); | ||
}); | ||
tests.test( | ||
'honors requests not to shift holidays on weekends', | ||
"honors requests not to shift holidays on weekends", | ||
async test => { | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate('2011-01-01'), { | ||
federalHolidays.isAHoliday(getDate("2011-01-01"), { | ||
shiftSaturdayHolidays: false | ||
@@ -84,3 +119,3 @@ }), | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate('2012-01-01'), { | ||
federalHolidays.isAHoliday(getDate("2012-01-01"), { | ||
shiftSundayHolidays: false | ||
@@ -91,3 +126,3 @@ }), | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate('2015-07-04'), { | ||
federalHolidays.isAHoliday(getDate("2015-07-04"), { | ||
shiftSaturdayHolidays: false | ||
@@ -98,3 +133,3 @@ }), | ||
test.ok( | ||
federalHolidays.isAHoliday(getDate('2016-12-25'), { | ||
federalHolidays.isAHoliday(getDate("2016-12-25"), { | ||
shiftSundayHolidays: false | ||
@@ -108,7 +143,7 @@ }), | ||
tests.test( | ||
'handles federal holidays within a range (Saturday and Sunday holidays shifted)', | ||
"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') | ||
new Date("2015-07-03"), | ||
new Date("2016-12-26") | ||
); | ||
@@ -125,3 +160,3 @@ holidays.forEach(holiday => { | ||
tests.test( | ||
'handles federal holidays within a range (Saturday shifted only)', | ||
"handles federal holidays within a range (Saturday shifted only)", | ||
async test => { | ||
@@ -131,4 +166,4 @@ const shiftSaturdayHolidays = true; | ||
const holidays = federalHolidays.inRange( | ||
new Date('2015-07-03'), | ||
new Date('2016-12-26'), | ||
new Date("2015-07-03"), | ||
new Date("2016-12-26"), | ||
{ shiftSaturdayHolidays, shiftSundayHolidays } | ||
@@ -149,3 +184,3 @@ ); | ||
tests.test( | ||
'handles federal holidays within a range (Sunday shifted only)', | ||
"handles federal holidays within a range (Sunday shifted only)", | ||
async test => { | ||
@@ -155,4 +190,4 @@ const shiftSaturdayHolidays = false; | ||
const holidays = federalHolidays.inRange( | ||
new Date('2015-07-03'), | ||
new Date('2016-12-26'), | ||
new Date("2015-07-03"), | ||
new Date("2016-12-26"), | ||
{ shiftSaturdayHolidays, shiftSundayHolidays } | ||
@@ -173,3 +208,3 @@ ); | ||
tests.test( | ||
'handles federal holidays within a range (none shifted)', | ||
"handles federal holidays within a range (none shifted)", | ||
async test => { | ||
@@ -179,4 +214,4 @@ const shiftSaturdayHolidays = false; | ||
const holidays = federalHolidays.inRange( | ||
new Date('2015-07-03'), | ||
new Date('2016-12-26'), | ||
new Date("2015-07-03"), | ||
new Date("2016-12-26"), | ||
{ shiftSaturdayHolidays, shiftSundayHolidays } | ||
@@ -195,2 +230,82 @@ ); | ||
); | ||
tests.test("handles default dates and ranges", async defaultTests => { | ||
const testYear = 2000; | ||
let GlobalDate; | ||
// Create a proxy for the global Date object. This way we can control the | ||
// date that is created for default arguments. | ||
defaultTests.beforeEach(() => { | ||
GlobalDate = global.Date; | ||
const ProxiedDate = new Proxy(Date, { | ||
construct: (_, args) => { | ||
// We only want to override the constructor if there aren't any | ||
// arguments. In that case, use our magic date of July 1. Otherwise, | ||
// pass the arguments on to the real Date constructor. | ||
if (args.length === 0) { | ||
return new GlobalDate(`${testYear}-07-01T00:00:00.000Z`); | ||
} | ||
return new GlobalDate(...args); | ||
} | ||
}); | ||
global.Date = ProxiedDate; | ||
}); | ||
defaultTests.afterEach(() => { | ||
// Put the real Date object back. | ||
global.Date = GlobalDate; | ||
}); | ||
// We've already tested isAHoliday and allForYear with args, so let's assume | ||
// they're correct. If not, our earlier tests should have caught that. If | ||
// they didn't... uhoh. | ||
defaultTests.test("indicates whether today is a holiday", async test => { | ||
// July 1 should not be a holiday in any year. | ||
test.same(federalHolidays.isAHoliday(), false, "is not a holiday"); | ||
}); | ||
defaultTests.test( | ||
"fetches all holidays for the current year", | ||
async test => { | ||
const expected = federalHolidays.allForYear(testYear); | ||
const holidays = federalHolidays.allForYear(); | ||
test.match(holidays, expected, "gets the expected holidays"); | ||
test.same( | ||
holidays.length, | ||
expected.length, | ||
"gets exactly the expected holidays" | ||
); | ||
} | ||
); | ||
defaultTests.test( | ||
"defaults to a range from now to one year from now", | ||
async test => { | ||
const holidays = federalHolidays.inRange(); | ||
// Safe-ify these tests against changing test years. In 2021, Juneteenth | ||
// was added to the holiday calendar. If the test year is before 2020, | ||
// there are 4 holidays preceding July 1, so we start our slice at | ||
// holiday #5 (index 4). From 2021 onwards, there are 5 holidays before | ||
// July 1, so we start our slice at holiday #6 (index 5). | ||
const slice = testYear > 2020 ? 5 : 4; | ||
const expected = [ | ||
...federalHolidays.allForYear(testYear).slice(slice), | ||
...federalHolidays.allForYear(testYear + 1).slice(0, slice) | ||
]; | ||
test.match(holidays, expected, "get the expected holidays"); | ||
test.same( | ||
holidays.length, | ||
expected.length, | ||
"gets exactly the expected holidays" | ||
); | ||
} | ||
); | ||
}); | ||
}); |
{ | ||
"name": "@18f/us-federal-holidays", | ||
"version": "1.7.0", | ||
"version": "2.0.0", | ||
"description": "All about US federal holidays", | ||
"main": "bin/index.js", | ||
"main": "index.js", | ||
"keywords": [ | ||
@@ -17,3 +17,4 @@ "federal", | ||
"CreateThis.com (https://github.com/createthis)", | ||
"Alexandr Rodik (https://github.com/arodik)" | ||
"Alexandr Rodik (https://github.com/arodik)", | ||
"Ian Speers (https://github.com/ian-speers)" | ||
], | ||
@@ -24,25 +25,20 @@ "repository": { | ||
}, | ||
"engines": { | ||
"node": ">=10.0.0" | ||
}, | ||
"scripts": { | ||
"lint": "eslint index.test.js 'src/**/*.js'", | ||
"build": "babel src -d bin --no-comments", | ||
"lint": "eslint index.test.js index.js", | ||
"test": "tap --reporter=spec index.test.js" | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"@babel/preset-env" | ||
] | ||
}, | ||
"license": "CC0-1.0", | ||
"devDependencies": { | ||
"@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": "^4.2.0", | ||
"eslint": "^7.30.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
"prettier": "^1.17.0", | ||
"tap": "^13.1.8" | ||
"prettier": "^2.3.2", | ||
"tap": "^15.0.9" | ||
}, | ||
"dependencies": {} | ||
"dependencies": {}, | ||
"prettier": {} | ||
} |
# US Federal Holidays | ||
Builds and returns a list of all US federal holidays for a given year, and provides a helper method to determine if a given date is a US federal holiday. Handles shifting holidays to the nearest weekday if the holiday falls on a weekend. | ||
Builds and returns a list of all US federal holidays for a given year, and | ||
provides a helper method to determine if a given date is a US federal holiday. | ||
Handles shifting holidays to the nearest weekday if the holiday falls on a | ||
weekend. | ||
@@ -13,5 +16,8 @@ US federal holidays are [as defined by OPM](https://www.opm.gov/policy-data-oversight/pay-leave/federal-holidays/). | ||
Requires Node.js 10 or higher. | ||
### Usage | ||
To get a list of all US federal holidays in a given year, use the `allForYear` method. If no year is passed in, uses the current year. | ||
To get a list of all US federal holidays in a given year, use the `allForYear` | ||
method. If no year is passed in, uses the current year. | ||
@@ -61,3 +67,5 @@ ```javascript | ||
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. | ||
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. | ||
@@ -121,6 +129,7 @@ ```javascript | ||
To determine if a date is a federal holiday, use the `isAHoliday` method. If no argument is provided, defaults to the current date: | ||
To determine if a date is a federal holiday, use the `isAHoliday` method. If no | ||
argument is provided, defaults to the current date: | ||
```javascript | ||
const fedHolidays = require('@18f/us-federal-holidays'); | ||
const fedHolidays = require("@18f/us-federal-holidays"); | ||
@@ -136,3 +145,4 @@ const options = { | ||
All three 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: | ||
@@ -173,6 +183,11 @@ ```javascript | ||
This project is in the worldwide [public domain](LICENSE.md). As stated in [CONTRIBUTING](CONTRIBUTING.md): | ||
This project is in the worldwide [public domain](LICENSE.md). As stated in | ||
[CONTRIBUTING](CONTRIBUTING.md): | ||
> This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/). | ||
> This project is in the public domain within the United States, and copyright | ||
> and related rights in the work worldwide are waived through the | ||
> [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/). | ||
> | ||
> All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest. | ||
> All contributions to this project will be released under the CC0 dedication. | ||
> By submitting a pull request, you are agreeing to comply with this waiver of | ||
> copyright interest. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6
189
27375
9
475
1