moment-business-time
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,24 +0,14 @@ | ||
var moment = require('moment'); | ||
var moment = require('moment'), | ||
minimatch = require('minimatch'); | ||
var locale = moment.locale(), | ||
localeData; | ||
var localeData = require('../locale/default'); | ||
moment.locale(moment.locale(), localeData); | ||
try { | ||
localeData = require('../locale/' + locale); | ||
} catch(e) { | ||
if (e.code === 'MODULE_NOT_FOUND') { | ||
localeData = require('../locale/default'); | ||
} else { | ||
throw e; | ||
} | ||
function getLocaleData(key) { | ||
return moment.localeData()['_' + key]; | ||
} | ||
moment.locale(locale, { | ||
workinghours: localeData.HOURS, | ||
holidays: localeData.HOLIDAYS | ||
}); | ||
function openingTimes(d) { | ||
d = d.clone(); | ||
var hours = moment.localeData()._workinghours; | ||
var hours = getLocaleData('workinghours'); | ||
if (!d.isWorkingDay()) { | ||
@@ -147,3 +137,3 @@ return null; | ||
moment.fn.isBusinessDay = function isBusinessDay() { | ||
var hours = moment.localeData()._workinghours; | ||
var hours = getLocaleData('workinghours'); | ||
return !!hours[this.day()] && !this.isHoliday(); | ||
@@ -163,3 +153,10 @@ }; | ||
moment.fn.isHoliday = function isHoliday() { | ||
return false; | ||
var isHoliday = false, | ||
today = this.format('YYYY-MM-DD'); | ||
getLocaleData('holidays').forEach(function (holiday) { | ||
if (minimatch(today, holiday)) { | ||
isHoliday = true; | ||
} | ||
}); | ||
return isHoliday; | ||
}; | ||
@@ -219,2 +216,4 @@ | ||
comparator = moment(comparator); | ||
if (['year', 'month', 'week'].indexOf(unit) > -1) { | ||
@@ -221,0 +220,0 @@ return this.diff(comparator, unit, detail); |
module.exports = { | ||
HOURS: { | ||
0: null, | ||
1: ['09:00:00', '17:00:00'], | ||
2: ['09:00:00', '17:00:00'], | ||
3: ['09:00:00', '17:00:00'], | ||
4: ['09:00:00', '17:00:00'], | ||
5: ['09:00:00', '17:00:00'], | ||
6: null | ||
workinghours: { | ||
0: null, | ||
1: ['09:00:00', '17:00:00'], | ||
2: ['09:00:00', '17:00:00'], | ||
3: ['09:00:00', '17:00:00'], | ||
4: ['09:00:00', '17:00:00'], | ||
5: ['09:00:00', '17:00:00'], | ||
6: null | ||
}, | ||
HOLIDAYS: [] | ||
holidays: [] | ||
}; |
{ | ||
"name": "moment-business-time", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Query and manipulate moment objects within the context of business/working hours", | ||
@@ -28,2 +28,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"minimatch": "^2.0.1", | ||
"moment": "^2.9.0" | ||
@@ -30,0 +31,0 @@ }, |
@@ -148,2 +148,4 @@ # moment-business-time | ||
### Working hours | ||
The working hours used for a locale can be modified using moment's `locale` method. The default working hours are 09:00-17:00, Mon-Fri. | ||
@@ -170,2 +172,31 @@ | ||
### Holidays | ||
Holidays which should not be considered as working days can be configured by passing them as locale information. | ||
Example: | ||
```javascript | ||
moment.locale('en', { | ||
holidays: [ | ||
'2015-05-04' | ||
] | ||
}); | ||
moment('2015-05-04').isWorkingDay() // false | ||
``` | ||
Recurring holidays can also be set with wildcard parameters. | ||
```javascript | ||
moment.locale('en', { | ||
holidays: [ | ||
'*-12-25' | ||
] | ||
}); | ||
moment('2015-12-25').isWorkingDay() // false | ||
moment('2016-12-25').isWorkingDay() // false | ||
moment('2017-12-25').isWorkingDay() // false | ||
moment('2018-12-25').isWorkingDay() // false | ||
``` | ||
## Running tests | ||
@@ -172,0 +203,0 @@ |
var moment = require('../lib/business-hours'); | ||
var localeData = require('../locale/default'); | ||
@@ -12,3 +13,11 @@ describe('moment.business-hours', function () { | ||
beforeEach(function () { | ||
moment.locale('en'); | ||
}); | ||
afterEach(function () { | ||
moment.locale('en', localeData); | ||
}); | ||
describe('isWorkingDay', function () { | ||
@@ -294,13 +303,2 @@ | ||
beforeEach(function () { | ||
moment.locale('en'); | ||
}); | ||
afterEach(function () { | ||
localeData = require('../locale/default'); | ||
moment.locale(moment.locale(), { | ||
workinghours: localeData.HOURS | ||
}); | ||
}); | ||
it('handles inconsistent opening hours', function () { | ||
@@ -429,2 +427,43 @@ moment.locale('en', { | ||
describe('holidays', function () { | ||
beforeEach(function () { | ||
moment.locale('en'); | ||
moment.locale('en', { | ||
holidays: [ | ||
'2015-02-27', | ||
'*-12-25' | ||
] | ||
}); | ||
}); | ||
afterEach(function () { | ||
moment.locale('en', { | ||
holidays: [] | ||
}); | ||
}); | ||
it('does not count holidays as working days', function () { | ||
moment('2015-02-27').isWorkingDay().should.be.false; | ||
}); | ||
it('does not include holidays when adding working time', function () { | ||
moment('2015-02-26').addWorkingTime(3, 'days').format(date).should.equal('2015-03-04'); | ||
moment('2015-02-26T12:00:00Z').addWorkingTime(8, 'hours').format(full).should.equal('2015-03-02 12:00:00.000'); | ||
}); | ||
it('does not include holidays when adding calculating diffs', function () { | ||
moment('2015-03-02T12:00:00Z').workingDiff('2015-02-26T12:00:00Z', 'hours').should.equal(8); | ||
}); | ||
it('supports holidays as wildcards', function () { | ||
moment('2015-12-25').isWorkingDay().should.be.false; | ||
moment('2016-12-25').isWorkingDay().should.be.false; | ||
moment('2017-12-25').isWorkingDay().should.be.false; | ||
moment('2018-12-25').isWorkingDay().should.be.false; | ||
moment('2019-12-25').isWorkingDay().should.be.false; | ||
}); | ||
}); | ||
}); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
33988
590
205
0
2
+ Addedminimatch@^2.0.1
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedminimatch@2.0.10(transitive)