moment-business-time
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -263,2 +263,25 @@ var moment = require('moment'), | ||
moment.fn.nextTransitionTime = function nextTransitionTime() { | ||
if (this.isWorkingDay()) { | ||
var segments = openingTimes(this); | ||
var openinghours, lastSegment; | ||
for(i = 0; i < segments.length; i++) { | ||
openinghours = segments[i]; | ||
lastSegment = i === segments.length -1; | ||
if (this.isBefore(openinghours[0])) { | ||
return {'transition': 'open','moment':openinghours[0]}; | ||
} else if (this.isBefore(openinghours[1])) { | ||
return {'transition':'close','moment':openinghours[1]}; | ||
} else if (this.isAfter(openinghours[1])) { | ||
if (!lastSegment) { | ||
continue; | ||
} | ||
return {'transition':'open','moment':openingTimes(this.nextWorkingDay())[0][0]}; | ||
} | ||
} | ||
} else { | ||
return {'transition':'open','moment':openingTimes(this.nextWorkingDay())[0][0]}; | ||
} | ||
} | ||
moment.fn.lastWorkingTime = function nextWorkingTime() { | ||
@@ -287,2 +310,25 @@ if (this.isWorkingDay()) { | ||
moment.fn.lastTransitionTime = function lastTransitionTime() { | ||
if (this.isWorkingDay()) { | ||
var segments = openingTimes(this); | ||
var openinghours, firstSegment; | ||
for(i = segments.length - 1; i >= 0; i--) { | ||
openinghours = segments[i]; | ||
firstSegment = i === 0; | ||
if (this.isAfter(openinghours[1])) { | ||
return {'transition':'close','moment':openinghours[1]}; | ||
} else if (this.isAfter(openinghours[0])) { | ||
return {'transition':'open','moment':openinghours[0]}; | ||
} else if (this.isBefore(openinghours[0])) { | ||
if (!firstSegment) { | ||
continue; | ||
} | ||
return {'transition':'close','moment':openingTimes(this.lastWorkingDay()).slice(-1)[0][1]}; | ||
} | ||
} | ||
} else { | ||
return {'transition':'close','moment':openingTimes(this.lastWorkingDay()).slice(-1)[0][1]}; | ||
} | ||
} | ||
moment.fn.workingDiff = function workingDiff(comparator, unit, detail) { | ||
@@ -289,0 +335,0 @@ unit = unit || 'milliseconds'; |
{ | ||
"name": "moment-business-time", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Query and manipulate moment objects within the context of business/working hours", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -70,2 +70,22 @@ # moment-business-time | ||
### `moment#nextTransitionTime` | ||
Returns: `object` | ||
Returns a new object with moment and transition properties representing the next moment when the business will transition between 'open' and 'closed' states. | ||
#### Example: | ||
```javascript | ||
moment('2015-02-26T17:30:00').nextTransitionTime(); | ||
// { | ||
// 'moment': Fri Feb 27 2015 09:00:00 GMT+0000 | ||
// 'transition': 'open' | ||
// } | ||
moment('2015-02-26T13:30:00').nextTransitionTime(); | ||
// { | ||
// 'moment': Thu Feb 26 2015 17:00:00 GMT+0000 | ||
// 'transition': 'close' | ||
// } | ||
``` | ||
### `moment#lastWorkingDay` | ||
@@ -99,2 +119,22 @@ | ||
### `moment#lastTransitionTime` | ||
Returns: `object` | ||
Returns a new object with moment and transition properties representing the most recent moment when the business transitioned between 'open' and 'closed' states. | ||
#### Example: | ||
```javascript | ||
moment('2015-02-26T17:30:00').lastTransitionTime(); | ||
// { | ||
// 'moment': Thu Feb 26 2015 17:00:00 GMT+0000 | ||
// 'transition': 'close' | ||
// } | ||
moment('2015-02-26T10:12:34').lastTransitionTime(); | ||
// { | ||
// 'moment': Thu Feb 26 2015 09:00:00 GMT+0000 | ||
// 'transition': 'open' | ||
// } | ||
``` | ||
### `moment#addWorkingTime` | ||
@@ -101,0 +141,0 @@ |
@@ -110,2 +110,77 @@ var moment = require('../lib/business-hours'); | ||
describe('nextTransitionTime', function () { | ||
it('returns the start of the next working period if not a current working period', function () { | ||
let res = moment(weekend).nextTransitionTime(); | ||
res.moment.format(full).should.equal('2015-03-02 09:00:00.000'); | ||
res.transition.should.equal('open'); | ||
}); | ||
it('returns the start of the next working day if called after closing time on a working day', function () { | ||
let res = moment('2015-02-26T17:30:00').nextTransitionTime(); | ||
res.moment.format(full).should.equal('2015-02-27 09:00:00.000'); | ||
res.transition.should.equal('open') | ||
}); | ||
it('returns the start of the next working period if called before opening time on a working day', function () { | ||
let res = moment('2015-02-26T08:30:00').nextTransitionTime(); | ||
res.moment.format(full).should.equal('2015-02-26 09:00:00.000'); | ||
res.transition.should.equal('open') | ||
}); | ||
it('returns the end of the current working period if called during working period', function () { | ||
let res = moment('2015-02-26T13:30:00').nextTransitionTime(); | ||
res.moment.format(full).should.equal('2015-02-26 17:00:00.000'); | ||
res.transition.should.equal('close'); | ||
}); | ||
}); | ||
describe('lastWorkingTime', function () { | ||
it('returns the end of the last working day if not a current working day', function () { | ||
moment(weekend).lastWorkingTime().format(full).should.equal('2015-02-27 17:00:00.000'); | ||
}); | ||
it('returns the end of the current working day if called after closing time on a working day', function () { | ||
moment('2015-02-26T17:30:00').lastWorkingTime().format(full).should.equal('2015-02-26 17:00:00.000'); | ||
}); | ||
it('returns the end of the previous working day if called before opening time on a working day', function () { | ||
moment('2015-02-26T07:30:00').lastWorkingTime().format(full).should.equal('2015-02-25 17:00:00.000'); | ||
}); | ||
it('returns the current time and date if called during opening hours', function () { | ||
moment(now).lastWorkingTime().format(full).should.equal('2015-02-26 10:12:34.000'); | ||
}); | ||
}); | ||
describe('lastTransitionTime', function () { | ||
it('returns the end of the last working day if not a current working day', function () { | ||
let res = moment(weekend).lastTransitionTime(); | ||
res.moment.format(full).should.equal('2015-02-27 17:00:00.000'); | ||
res.transition.should.equal('close') | ||
}); | ||
it('returns the end of the current working day if called after closing time on a working day', function () { | ||
let res = moment('2015-02-26T17:30:00').lastTransitionTime(); | ||
res.moment.format(full).should.equal('2015-02-26 17:00:00.000'); | ||
res.transition.should.equal('close') | ||
}); | ||
it('returns the end of the previous working day if called before opening time on a working day', function () { | ||
let res = moment('2015-02-26T07:30:00').lastTransitionTime(); | ||
res.moment.format(full).should.equal('2015-02-25 17:00:00.000'); | ||
res.transition.should.equal('close') | ||
}); | ||
it('returns the start of the current working period if called during opening hours', function () { | ||
let res = moment(now).lastTransitionTime(); | ||
res.moment.format(full).should.equal('2015-02-26 09:00:00.000'); | ||
res.transition.should.equal('open') | ||
}); | ||
}); | ||
describe('addWorkingTime', function () { | ||
@@ -112,0 +187,0 @@ |
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
50954
898
246