moment-feiertage
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -18,9 +18,50 @@ (function (root, factory) { | ||
moment.fn.isHoliday = function (state) { | ||
moment.fn.isHoliday = function (_states) { | ||
// call backwords compatible function | ||
if (typeof _states === 'string' || _states == null) { | ||
// call backwards compatible function | ||
return isHoliday106(this, _states); | ||
} | ||
// return Object if argument contains an Array | ||
if (_states instanceof Array) { | ||
var allStates = ['BW', 'BY', 'BE', 'BB', 'HB', 'HH', 'HE', 'MV', 'NI', 'NW', 'RP', 'SL', 'SN', 'ST', 'SH', 'TH']; | ||
var testStates = prepareArray(_states); | ||
var result = { | ||
allStates: false, | ||
holidayName: '', | ||
holidayStates: [], | ||
testedStates: testStates | ||
}; | ||
// check in all states if no states passed | ||
if (testStates.length < 1) { | ||
testStates = allStates; | ||
} | ||
// loop tested states and validate holiday | ||
for (i = 0; i < testStates.length; i++) { | ||
var holiday = isHoliday106(this, testStates[i]); | ||
if (holiday) { | ||
// add validated states to result object | ||
result.holidayName = holiday; | ||
result.holidayStates.push(testStates[i]); | ||
} | ||
} | ||
// set allStates value | ||
if (isHoliday106(this)) { | ||
result.allStates = true; | ||
} | ||
result.testedStates = testStates; | ||
return result; | ||
} | ||
} | ||
isHoliday106 = function (momentObj, state) { | ||
// EXCEPTION: 2017 Reformationstag is holiday in all states | ||
if (this.isSame(moment('2017-10-31'), 'day')) { | ||
if (momentObj.isSame(moment('2017-10-31'), 'day')) { | ||
return 'Reformationstag'; | ||
} | ||
var year = this.year(); | ||
var year = momentObj.year(); | ||
var easter = moment(calculateEasterDate(year)).format(); | ||
@@ -99,3 +140,3 @@ var holidays = { | ||
for (var holiday in holidays) { | ||
if (this.isSame(holidays[holiday].date, 'day')) { | ||
if (momentObj.isSame(holidays[holiday].date, 'day')) { | ||
var states = holidays[holiday].state | ||
@@ -106,3 +147,3 @@ if (states.length === 0) { // if state empty -> holiday for every state | ||
// check if it is a holiday in state (param) | ||
if (state !== undefined && state !== "" && state.length === 2 && states.indexOf(state) > -1) { | ||
if (state != null && state !== "" && state.length === 2 && states.indexOf(state) > -1) { | ||
return holiday; | ||
@@ -115,3 +156,2 @@ } else { | ||
} | ||
return false; | ||
@@ -147,3 +187,20 @@ } | ||
} | ||
prepareArray = function (array) { | ||
// sort out false values | ||
var preparedArray = []; | ||
for (i = 0; i < array.length; i++) { | ||
if (array[i] != null && array[i] !== '' && array[i].length === 2) { | ||
preparedArray.push(array[i]); | ||
} | ||
} | ||
// sort array | ||
preparedArray.sort(); | ||
// remove duplicates | ||
var uniqueArray = preparedArray.filter(function (item, pos) { | ||
return preparedArray.indexOf(item) == pos; | ||
}); | ||
return uniqueArray; | ||
} | ||
return moment; | ||
})); |
{ | ||
"name": "moment-feiertage", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Moment.js Plugin for german holidays; check if a given Date is a german holiday", | ||
@@ -8,3 +8,3 @@ "main": "moment-feiertage.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha" | ||
}, | ||
@@ -36,3 +36,6 @@ "repository": { | ||
"moment": "^2.15.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^5.2.0" | ||
} | ||
} |
# moment-feiertage | ||
moment-feiertage is a [Moment.js](http://momentjs.com/) plugin to determine if a date is a german holiday. Holidays are taken from [Wikipedia (de)](https://de.wikipedia.org/wiki/Gesetzliche_Feiertage_in_Deutschland). | ||
moment-feiertage is a [Moment.js](http://momentjs.com/) plugin to determine if a date is a german holiday. Holidays are taken from [Wikipedia (de)](https://de.wikipedia.org/wiki/Gesetzliche_Feiertage_in_Deutschland). Feel free to contribute! | ||
## How to use? | ||
1. Add moment-feiertage to your package.json by runing `npm install moment-feiertage --save` | ||
1. Add moment-feiertage to your package.json by running `npm install moment-feiertage --save` | ||
2. Import moment and moment-feiertage | ||
@@ -16,25 +16,76 @@ ```javascript | ||
```` | ||
3. `isHoliday()` on any moment object is returning `false` (boolean) or name of holiday (string) | ||
3. call `isHoliday()` on any moment object. Check examples for supported arguments and return values | ||
## Examples | ||
## Examples since version 1.1.0 | ||
From version `1.1.0` on `isHoliday()` supports Arrays. Pass an empty Array to test agains all states, or pass an Array of state codes (e.g. `['BY', 'SH']`) to test agains passed states. The return value is an Object: | ||
```javascript | ||
/** | ||
* isHoliday() checks if moment object is a german holiday in all states. | ||
* isHoliday('<state code>') checks if moment object is a german holiday in specific state) | ||
* @param {string} input any state code | ||
* @returns {number|string} false | name of holiday | ||
*/ | ||
{ | ||
allStates: boolean, // true if holiday in all 16 states (default: false) | ||
holidayName: string, // name of holiday (default: '') | ||
holidayStates: Array<string>, // states, which celebrate this holiday (default: []) | ||
testedStates: Array<string> // tested states default: ['BW','BY','BE','BB','HB','HH','HE','MV','NI','NW','RP','SL','SN','ST','SH','TH'] | ||
} | ||
``` | ||
Tests Christmas in all states and all states celebrate this holiday: | ||
```javascript | ||
const christmasInAllStates = moment('2018-12-25').isHoliday([]); | ||
/* returns { | ||
allStates: true, | ||
holidayName: '1. Weihnachtsfeiertag', | ||
holidayStates: ...allStates, | ||
testedStates: ...allStates | ||
}*/ | ||
``` | ||
Tests some holiday in all states but not all states celebrate this holiday: | ||
```javascript | ||
const someDateInAllStates = moment('2018-11-01').isHoliday([]); | ||
/* returns { | ||
allStates: false, | ||
holidayName: 'Allerheiligen', | ||
holidayStates: [ 'BW', 'BY', 'NW', 'RP', 'SL' ], | ||
testedStates: ...allStates | ||
}*/ | ||
``` | ||
Tests multiple states for a holiday. This checks if the passed states celebrate this holiday: | ||
```javascript | ||
const someDateInSomeStates = moment('2018-11-01').isHoliday(['BW', 'SH', 'TH']); | ||
/* returns { | ||
allStates: false, | ||
holidayName: 'Allerheiligen', | ||
holidayStates: [ 'BW' ], | ||
testedStates: [ 'BW', 'SH', 'TH' ] | ||
}*/ | ||
``` | ||
Tests some date in all states and no state is celebrating this date: | ||
```javascript | ||
const someDateInAllStates = moment('2018-12-12').isHoliday([]); | ||
/* returns { | ||
allStates: false, | ||
holidayName: '', | ||
holidayStates: [], | ||
testedStates: ...allStates | ||
}*/ | ||
``` | ||
## Working Examples since version `1.0.0` | ||
```javascript | ||
const nowIsHoliday = moment().isHoliday(); | ||
// returns name of holiday (string) if date is a holiday | ||
// retruns false (boolean) if date is not a holiday | ||
const someDateIsHoliday = moment('2019-12-25').isHoliday(); | ||
// returns '1. Weihnachtsfeiertag' - is holiday in all states | ||
// returns '1. Weihnachtsfeiertag' - is a holiday in all states | ||
const isHolidayInAllStates = moment('2017-08-15').isHoliday(); | ||
// returns false - is not holiday in all states | ||
// returns false - is not a holiday in all states | ||
const isHolidayInBavaria = moment('2017-08-15').isHoliday('BY'); | ||
// returns false - is not holiday in BY | ||
// returns false - is not a holiday in BY | ||
const isHolidayInSaarland = moment('2017-08-15').isHoliday('SL'); | ||
// returns 'Mariä Himmelfahrt' - is holiday in SL | ||
// returns 'Mariä Himmelfahrt' - is a holiday in SL | ||
@@ -41,0 +92,0 @@ // state codes: |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
11857
4
247
0
108
1