moment-holiday
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "moment-holiday", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A Moment.js plugin for handling holidays.", | ||
@@ -8,4 +8,3 @@ "keywords": [ | ||
"momentjs", | ||
"holiday", | ||
"holidays" | ||
"holiday" | ||
], | ||
@@ -12,0 +11,0 @@ "author": { |
@@ -8,6 +8,20 @@ # Changelog | ||
## 1.0.0 - TBA | ||
## 1.1.0 - 2017-07-17 | ||
### Added | ||
- `previousHoliday` and `nextHoliday` functions. | ||
- `holidays` alias function for `holiday`. | ||
### Changed | ||
- Moved holidays object from `moment.fn.holidays` to `moment.holidays`. | ||
- Fixed bug where `holidaysBetween` function would only return holidays for the start year. | ||
- `holidaysBetween` function now returns an array of moment objects rather than an object. | ||
- `isHoliday` now accepts a `holidays` parameter to get a `true` response. | ||
## 1.0.0 - 2017-07-16 | ||
### Added | ||
- Initial release. | ||
[Unreleased]: https://github.com/kodie/moment-holiday/compare/v1.0.0...HEAD | ||
[1.1.0]: https://github.com/kodie/moment-holiday/compare/v1.0.0...v1.1.0 |
//! moment-holiday.js | ||
//! version : 1.0.0 | ||
//! version : 1.1.0 | ||
//! author : Kodie Grantham | ||
@@ -10,3 +10,3 @@ //! license : MIT | ||
moment.fn.holidays = { | ||
moment.holidays = { | ||
"New Year's Day": { | ||
@@ -130,3 +130,3 @@ date: '1/1', | ||
var findHoliday = function(self, holiday, adjust, parse) { | ||
var h = moment.fn.holidays; | ||
var h = moment.holidays; | ||
var pt = {}; | ||
@@ -189,3 +189,3 @@ var wn = []; | ||
var getAllHolidays = function(self, adjust) { | ||
var h = moment.fn.holidays; | ||
var h = moment.holidays; | ||
var d = {}; | ||
@@ -201,4 +201,49 @@ | ||
var holidayLoop = function(self, count, forward, adjust) { | ||
if (!count) { count = 1; } | ||
var h = getAllHolidays(self, adjust); | ||
var l = moment(self); | ||
var y = self.year(); | ||
var w = []; | ||
for (i = 0; i < count; i++) { | ||
var d = moment(l); | ||
while (true) { | ||
var b = false; | ||
if (forward) { | ||
d.add(1, 'day'); | ||
} else { | ||
d.subtract(1, 'day'); | ||
} | ||
if (d.year() !== y) { | ||
h = getAllHolidays(d, adjust); | ||
y = d.year(); | ||
} | ||
if (!Object.keys(h).length) { b = true; break; } | ||
for (var hd in h) { | ||
if (d.isSame(h[hd], 'day')) { | ||
w.push(h[hd]); | ||
l = moment(d); | ||
b = true; | ||
break; | ||
} | ||
} | ||
if (b) { break; } | ||
} | ||
} | ||
if (!w.length) { return false; } | ||
return w; | ||
}; | ||
moment.fn.holiday = function(holidays, adjust) { | ||
var h = moment.fn.holidays; | ||
var h = moment.holidays; | ||
var d = {}; | ||
@@ -226,8 +271,24 @@ var single = false; | ||
moment.fn.isHoliday = function(adjust) { | ||
var h = getAllHolidays(this, adjust); | ||
moment.fn.holidays = function(holidays, adjust) { | ||
return this.holiday(holidays, adjust); | ||
}; | ||
for (var hd in h) { | ||
if (!h.hasOwnProperty(hd)) { continue; } | ||
if (this.isSame(h[hd], 'day')) { return hd; } | ||
moment.fn.isHoliday = function(holidays, adjust) { | ||
if (holidays) { | ||
if (holidays.constructor !== Array) { holidays = [holidays]; } | ||
var h = this.holiday(holidays, adjust); | ||
if (!h) { return false; } | ||
for (var hd in h) { | ||
if (!h.hasOwnProperty(hd)) { continue; } | ||
if (this.isSame(h[hd], 'day')) { return true; } | ||
} | ||
} else { | ||
var h = getAllHolidays(this, adjust); | ||
for (var hd in h) { | ||
if (!h.hasOwnProperty(hd)) { continue; } | ||
if (this.isSame(h[hd], 'day')) { return hd; } | ||
} | ||
} | ||
@@ -238,14 +299,48 @@ | ||
moment.fn.previousHoliday = function(count, adjust) { | ||
return holidayLoop(this, count, false, adjust); | ||
}; | ||
moment.fn.previousHolidays = function(count, adjust) { | ||
return this.previousHoliday(count, adjust); | ||
}; | ||
moment.fn.nextHoliday = function(count, adjust) { | ||
return holidayLoop(this, count, true, adjust); | ||
}; | ||
moment.fn.nextHolidays = function(count, adjust) { | ||
return this.nextHoliday(count, adjust); | ||
}; | ||
moment.fn.holidaysBetween = function(date, adjust) { | ||
if (!date) { date = new Date(); } | ||
date = moment(date).subtract(1, 'day'); | ||
var h = getAllHolidays(this, adjust); | ||
var d = moment(this); | ||
var y = d.year(); | ||
var w = []; | ||
for (var hd in h) { | ||
if (!h.hasOwnProperty(hd)) { continue; } | ||
if (h[hd].isBefore(this) || h[hd].isAfter(date)) { delete(h[hd]); } | ||
for (i = 0; i < date.diff(this, 'days'); i++) { | ||
d.add(1, 'day'); | ||
if (d.year() !== y) { | ||
h = getAllHolidays(d, adjust); | ||
y = d.year(); | ||
} | ||
if (!Object.keys(h).length) { break; } | ||
for (var hd in h) { | ||
if (d.isSame(h[hd], 'day')) { | ||
w.push(h[hd]); | ||
break; | ||
} | ||
} | ||
} | ||
if (!Object.keys(h).length) { return false; } | ||
if (!w.length) { return false; } | ||
return h; | ||
return w; | ||
}; | ||
@@ -263,8 +358,8 @@ | ||
for (var hd in moment.fn.holidays) { | ||
if (!moment.fn.holidays.hasOwnProperty(hd)) { continue; } | ||
if (hs.indexOf(hd) === -1) { delete(moment.fn.holidays[hd]); } | ||
for (var hd in moment.holidays) { | ||
if (!moment.holidays.hasOwnProperty(hd)) { continue; } | ||
if (hs.indexOf(hd) === -1) { delete(moment.holidays[hd]); } | ||
} | ||
} else { | ||
moment.fn.holidays = holidays; | ||
moment.holidays = holidays; | ||
} | ||
@@ -274,3 +369,3 @@ }, | ||
add: function(holidays) { | ||
moment.fn.holidays = Object.assign({}, moment.fn.holidays, holidays); | ||
moment.holidays = Object.assign({}, moment.holidays, holidays); | ||
}, | ||
@@ -283,3 +378,3 @@ | ||
var d = findHoliday(this, holidays[i], null, false); | ||
if (d) { delete(moment.fn.holidays[d]); } | ||
if (d) { delete(moment.holidays[d]); } | ||
} | ||
@@ -286,0 +381,0 @@ } |
{ | ||
"name": "moment-holiday", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A Moment.js plugin for handling holidays.", | ||
@@ -8,4 +8,3 @@ "keywords": [ | ||
"momentjs", | ||
"holiday", | ||
"holidays" | ||
"holiday" | ||
], | ||
@@ -12,0 +11,0 @@ "author": { |
199
README.md
@@ -33,6 +33,22 @@ # moment-holiday [![npm version](https://badge.fury.io/js/moment-holiday.svg)](https://badge.fury.io/js/moment-holiday) [![Build Status](https://travis-ci.org/kodie/moment-holiday.svg?branch=master)](https://travis-ci.org/kodie/moment-holiday) | ||
## Examples | ||
## Functions | ||
### holiday(holidays, adjust) | ||
### holiday | ||
*or `holidays`* | ||
Searches for holiday(s) by keywords. Returns a single moment object, an object containing moment objects with the holiday names as keys, or `false` if no holidays were found. | ||
#### Use | ||
```javascript | ||
moment().holiday(holidays, adjust); | ||
//or | ||
moment().holidays(holidays, adjust); | ||
``` | ||
#### Parameters | ||
* **holidays** - The holiday(s) to search for. Can be a string to search for a single holiday or an array to search for multiple. Defaults to all holidays. | ||
* **adjust** - See [global parameters](#global-parameters). | ||
#### Examples | ||
```javascript | ||
moment().holiday('Memorial Day'); | ||
@@ -47,7 +63,7 @@ //moment("2017-05-29T00:00:00.000") | ||
moment().holiday(['Turkey Day', 'New Years Eve']); | ||
moment().holidays(['Turkey Day', 'New Years Eve']); | ||
//{ 'Thanksgiving Day': moment("2017-11-23T00:00:00.000"), | ||
// 'New Year\'s Eve': moment("2017-12-31T00:00:00.000") } | ||
moment().holiday(['Not actually a holiday', 'Mothers Day']); | ||
moment().holidays(['Not actually a holiday', 'Mothers Day']); | ||
//{ 'Mother\'s Day': moment("2017-05-14T00:00:00.000") } | ||
@@ -61,8 +77,21 @@ | ||
moment().holiday(); | ||
moment().holidays(); | ||
//Returns all holidays | ||
``` | ||
### isHoliday(adjust) | ||
### isHoliday | ||
Returns the name of the holiday (or `true` if `holidays` parameter is used) if the given date is in fact a holiday or `false` if it isn't. | ||
#### Use | ||
```javascript | ||
moment().isHoliday(holidays, adjust); | ||
``` | ||
#### Parameters | ||
* **holidays** - Holidays to check for. Will cause function to return `true` if there is a match. Can be a string to compare with a single holiday or an array for multiple. Defaults to all holidays. | ||
* **adjust** - See [global parameters](#global-parameters). | ||
#### Examples | ||
```javascript | ||
moment('2017-12-25').isHoliday(); | ||
@@ -74,57 +103,137 @@ //Christmas Day | ||
moment('2009-10-31').isHoliday('Halloween'); | ||
//true | ||
moment('2017-12-31').isHoliday(); | ||
//New Year's Eve | ||
moment('2017-12-31').isHoliday(true); | ||
moment('2017-12-31').isHoliday(null, true); | ||
//false | ||
``` | ||
### holidaysBetween(date, adjust) | ||
### previousHoliday | ||
*or `previousHolidays`* | ||
Returns an array containing the previous holidays before the given date. | ||
#### Use | ||
```javascript | ||
moment().previousHoliday(count, adjust); | ||
//or | ||
moment().previousHolidays(count, adjust); | ||
``` | ||
#### Parameters | ||
* **count** - The number of previous holidays to fetch. Defaults to `1`. | ||
* **adjust** - See [global parameters](#global-parameters). | ||
#### Examples | ||
```javascript | ||
moment().previousHoliday(); | ||
//[ moment("2017-07-04T00:00:00.000") ] | ||
moment('2001-02-14').previousHolidays(5); | ||
//[ moment("2001-01-15T00:00:00.000"), | ||
// moment("2001-01-01T00:00:00.000"), | ||
// moment("2000-12-31T00:00:00.000"), | ||
// moment("2000-12-25T00:00:00.000"), | ||
// moment("2000-12-24T00:00:00.000") ] | ||
moment('2001-02-14').previousHolidays(5, true); | ||
//[ moment("2001-01-15T00:00:00.000"), | ||
// moment("2001-01-01T00:00:00.000"), | ||
// moment("2000-12-25T00:00:00.000"), | ||
// moment("2000-11-24T00:00:00.000"), | ||
// moment("2000-11-23T00:00:00.000") ] | ||
``` | ||
### nextHoliday | ||
*or `nextHolidays`* | ||
Returns an array containing the next holidays after the given date. | ||
#### Use | ||
```javascript | ||
moment().nextHoliday(count, adjust); | ||
//or | ||
moment().nextHolidays(count, adjust); | ||
``` | ||
#### Parameters | ||
* **count** - The number of upcoming holidays to fetch. Defaults to `1`. | ||
* **adjust** - See [global parameters](#global-parameters). | ||
#### Examples | ||
```javascript | ||
moment().nextHoliday(); | ||
//[ moment("2017-09-04T00:00:00.000") ] | ||
moment('2010-05-23').nextHolidays(5); | ||
//[ moment("2010-05-31T00:00:00.000"), | ||
// moment("2010-06-20T00:00:00.000"), | ||
// moment("2010-07-04T00:00:00.000"), | ||
// moment("2010-09-06T00:00:00.000"), | ||
// moment("2010-10-11T00:00:00.000") ] | ||
moment('2010-05-23').nextHolidays(5, true); | ||
//[ moment("2010-05-31T00:00:00.000"), | ||
// moment("2010-06-21T00:00:00.000"), | ||
// moment("2010-07-05T00:00:00.000"), | ||
// moment("2010-09-06T00:00:00.000"), | ||
// moment("2010-10-11T00:00:00.000") ] | ||
``` | ||
### holidaysBetween | ||
Returns an array containing the holidays between the given date and the `date` parameter or `false` if no dates were found. | ||
#### Use | ||
```javascript | ||
moment().holidaysBetween(date, adjust); | ||
``` | ||
#### Parameters | ||
* **date** - The end date range for holidays to get. Can be any string that moment accepts or a moment object. Defaults to today. | ||
* **adjust** - See [global parameters](#global-parameters). | ||
#### Examples | ||
```javascript | ||
moment().holidaysBetween(moment().endOf('year')); | ||
//{ 'Labor Day': moment("2017-09-04T00:00:00.000"), | ||
// 'Columbus Day': moment("2017-10-09T00:00:00.000"), | ||
// Halloween: moment("2017-10-31T00:00:00.000"), | ||
// 'Veteran\'s Day': moment("2017-11-11T00:00:00.000"), | ||
// 'Thanksgiving Day': moment("2017-11-23T00:00:00.000"), | ||
// 'Day after Thanksgiving': moment("2017-11-24T00:00:00.000"), | ||
// 'Christmas Eve': moment("2017-12-24T00:00:00.000"), | ||
// 'Christmas Day': moment("2017-12-25T00:00:00.000"), | ||
// 'New Year\'s Eve': moment("2017-12-31T00:00:00.000") } | ||
//[ moment("2017-09-04T00:00:00.000"), | ||
// moment("2017-10-09T00:00:00.000"), | ||
// moment("2017-10-31T00:00:00.000"), | ||
// moment("2017-11-11T00:00:00.000"), | ||
// moment("2017-11-23T00:00:00.000"), | ||
// moment("2017-11-24T00:00:00.000"), | ||
// moment("2017-12-24T00:00:00.000"), | ||
// moment("2017-12-25T00:00:00.000") ] | ||
moment('2011-11-01').holidaysBetween('2011-12-31'); | ||
//{ 'Veteran\'s Day': moment("2011-11-11T00:00:00.000"), | ||
// 'Thanksgiving Day': moment("2011-11-24T00:00:00.000"), | ||
// 'Day after Thanksgiving': moment("2011-11-25T00:00:00.000"), | ||
// 'Christmas Eve': moment("2011-12-24T00:00:00.000"), | ||
// 'Christmas Day': moment("2011-12-25T00:00:00.000"), | ||
// 'New Year\'s Eve': moment("2011-12-31T00:00:00.000") } | ||
//[ moment("2011-11-11T00:00:00.000"), | ||
// moment("2011-11-24T00:00:00.000"), | ||
// moment("2011-11-25T00:00:00.000"), | ||
// moment("2011-12-24T00:00:00.000"), | ||
// moment("2011-12-25T00:00:00.000") ] | ||
moment('2011-11-01').holidaysBetween('2011-12-31', true); | ||
//{ 'Veteran\'s Day': moment("2011-11-11T00:00:00.000"), | ||
// 'Thanksgiving Day': moment("2011-11-24T00:00:00.000"), | ||
// 'Day after Thanksgiving': moment("2011-11-25T00:00:00.000"), | ||
// 'Christmas Eve': moment("2011-12-23T00:00:00.000"), | ||
// 'Christmas Day': moment("2011-12-26T00:00:00.000"), | ||
// 'New Year\'s Eve': moment("2011-12-30T00:00:00.000") } | ||
//[ moment("2011-11-11T00:00:00.000"), | ||
// moment("2011-11-24T00:00:00.000"), | ||
// moment("2011-11-25T00:00:00.000"), | ||
// moment("2011-12-23T00:00:00.000"), | ||
// moment("2011-12-26T00:00:00.000"), | ||
// moment("2011-12-30T00:00:00.000") ] | ||
moment('2017-01-01').holidaysBetween(); | ||
//{ 'New Year\'s Day': moment("2017-01-01T00:00:00.000"), | ||
// 'Martin Luther King Jr. Day': moment("2017-01-16T00:00:00.000"), | ||
// 'Valentine\'s Day': moment("2017-02-14T00:00:00.000"), | ||
// 'Washington\'s Birthday': moment("2017-02-20T00:00:00.000"), | ||
// 'Saint Patrick\'s Day': moment("2017-03-17T00:00:00.000"), | ||
// 'Memorial Day': moment("2017-05-29T00:00:00.000"), | ||
// 'Mother\'s Day': moment("2017-05-14T00:00:00.000"), | ||
// 'Father\'s Day': moment("2017-06-18T00:00:00.000"), | ||
// 'Independence Day': moment("2017-07-04T00:00:00.000") } | ||
//[ moment("2017-01-16T00:00:00.000"), | ||
// moment("2017-02-14T00:00:00.000"), | ||
// moment("2017-02-20T00:00:00.000"), | ||
// moment("2017-03-17T00:00:00.000"), | ||
// moment("2017-05-14T00:00:00.000"), | ||
// moment("2017-05-29T00:00:00.000"), | ||
// moment("2017-06-18T00:00:00.000"), | ||
// moment("2017-07-04T00:00:00.000") ] | ||
``` | ||
#### Parameters | ||
* **holidays** (`holiday` function only) - The holiday(s) you would like to find. Can be a string to return a single moment object, or an array of strings to return an object of moment objects with the holiday names as keys. Defaults to all holidays. | ||
* **date** (`holidaysBetween` function only) - The end date range to find holidays in. Accepts a moment object or a string that moment would accept. Defaults to today. | ||
### Global Parameters | ||
* **adjust** - Set to `true` to make all holidays that land on a Saturday go to the prior Friday and all holidays that land on a Sunday go to the following Monday. Defaults to `false`. | ||
All parameters are optional. | ||
## The Holidays | ||
@@ -159,3 +268,3 @@ The following holidays are built-in: | ||
moment().holiday(); // Returns all holidays | ||
moment().holidays(); // Returns all holidays | ||
//{ 'New Year\'s Day': moment("2017-01-01T00:00:00.000"), | ||
@@ -176,3 +285,3 @@ // 'Memorial Day': moment("2017-05-29T00:00:00.000"), | ||
moment().holiday(); // Returns all holidays | ||
moment().holidays(); // Returns all holidays | ||
//{ 'My Birthday': moment("2017-11-17T00:00:00.000"), | ||
@@ -179,0 +288,0 @@ // 'Last Friday of the year': moment("2017-12-29T00:00:00.000") } |
55
test.js
@@ -12,3 +12,3 @@ import test from 'ava'; | ||
test('holiday_2', function(t){ | ||
var w = moment('2010-03-25').holiday(['Turkey Day', 'Christmas']); | ||
var w = moment('2010-03-25').holidays(['Turkey Day', 'Christmas']); | ||
var k = Object.keys(w); | ||
@@ -27,5 +27,5 @@ t.is(typeof w, 'object'); | ||
test('holiday_4', function(t){ | ||
var w = moment().holiday(); | ||
var w = moment().holidays(); | ||
t.is(typeof w, 'object'); | ||
t.is(Object.keys(w).length, Object.keys(moment.fn.holidays).length); | ||
t.is(Object.keys(w).length, Object.keys(moment.holidays).length); | ||
}); | ||
@@ -50,11 +50,44 @@ | ||
test('isHoliday_3', function(t){ | ||
var w = moment('2018-11-12').isHoliday(true); | ||
var w = moment('2018-11-12').isHoliday(null, true); | ||
t.is(w, "Veteran's Day"); | ||
}); | ||
test('isHoliday_4', function(t){ | ||
var w = moment('2018-03-17').isHoliday('St Paddys Day'); | ||
t.true(w); | ||
}); | ||
test('previousHoliday_1', function(t){ | ||
var w = moment('2002-06-15').previousHoliday(6); | ||
t.is(w.constructor, Array); | ||
t.is(w.length, 6); | ||
t.true(w[4].isHoliday("Valentine's Day")); | ||
}); | ||
test('previousHoliday_2', function(t){ | ||
var w = moment('2012-02-06').previousHolidays(7, true); | ||
t.is(w.constructor, Array); | ||
t.is(w.length, 7); | ||
t.true(w[2].isHoliday("New Year's Eve", true)); | ||
}); | ||
test('nextHoliday_1', function(t){ | ||
var w = moment('2001-04-20').nextHolidays(5); | ||
t.is(w.constructor, Array); | ||
t.is(w.length, 5); | ||
t.is(w[2].isHoliday(), "Father's Day"); | ||
}); | ||
test('nextHoliday_2', function(t){ | ||
var w = moment('2011-11-02').nextHoliday(8, true); | ||
t.is(w.constructor, Array); | ||
t.is(w.length, 8); | ||
t.true(w[6].isHoliday("New Year's Day", true)); | ||
}); | ||
test('holidaysBetween_1', function(t){ | ||
var w = moment('2011-11-01').holidaysBetween('2011-12-31'); | ||
t.is(typeof w, 'object'); | ||
t.is(Object.keys(w).length, 6); | ||
t.is(w[Object.keys(w)[4]].isHoliday(), 'Christmas Day'); | ||
var w = moment('2011-11-01').holidaysBetween('2012-05-15'); | ||
t.is(w.constructor, Array); | ||
t.is(w.length, 12); | ||
t.is(w[11].isHoliday(), "Mother's Day"); | ||
}); | ||
@@ -69,5 +102,5 @@ | ||
var w = moment('2011-11-01').holidaysBetween('2011-12-31', true); | ||
t.is(typeof w, 'object'); | ||
t.is(Object.keys(w).length, 6); | ||
t.true(w[Object.keys(w)[5]].isSame('2011-12-30', 'day')); | ||
t.is(w.constructor, Array); | ||
t.is(w.length, 6); | ||
t.true(w[5].isSame('2011-12-30', 'day')); | ||
}); | ||
@@ -74,0 +107,0 @@ |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
25439
462
322
0