Skip to content

Commit

Permalink
Fix infinite loop caused by fraction value in businessAdd
Browse files Browse the repository at this point in the history
Fixes #55

* Fix: Fractional day causes infinite loop in businessAdd #55
* Add absolute number rounding in businessAdd
* Fix tests for fractional days
  • Loading branch information
avenue19 authored and mcdado committed Dec 4, 2018
1 parent 30cddac commit 49477ef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,22 @@ moment.fn.businessDiff = function (param) {
};

moment.fn.businessAdd = function (number, period) {
var day = this.clone();
var day = this.clone().startOf('day');
if (!day.isValid()) {
return day;
}

if (number < 0) {
number = Math.round(-1 * number) * -1;
} else {
number = Math.round(number);
}

var signal = number < 0 ? -1 : 1;
var remaining = Math.abs(number);
period = typeof period !== 'undefined' ? period : 'days';

while (remaining) {
var remaining = Math.abs(number);
while (remaining > 0) {
day.add(signal, period);

if (day.isBusinessDay()) {
Expand Down
26 changes: 23 additions & 3 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,37 @@ describe('Moment Business Days', function () {
});
});
describe('On Tuesday, November 3rd 2015', function () {
it('adds business days only, excluding weekends, even over 2 weeks ', function (done) {
it('adds business days only, excluding weekends, even over 2 weeks', function (done) {
var newBusinessDay = moment('11-03-2015', 'MM-DD-YYYY').businessAdd(5);
expect(newBusinessDay.format('D')).to.eql('10');
done();
});
it('adds business days only, excluding weekends ', function (done) {
it('adds business days only, excluding weekends', function (done) {
var newBusinessDay = moment('11-03-2015', 'MM-DD-YYYY').businessAdd(10);
expect(newBusinessDay.format('D')).to.eql('17');
done();
});
it('adds business hours only, excluding weekends ', function (done) {
it('adds business days only, excluding weekends, rounding down fractional day values', function (done) {
var newBusinessDay = moment('11-03-2015 12:42:00', 'MM-DD-YYYY hh-mm-ss').businessAdd(10.4);
expect(newBusinessDay.format('D')).to.eql('17');
done();
});
it('adds business days only, excluding weekends, rounding up fractional day values', function (done) {
var newBusinessDay = moment('11-03-2015 12:42:00', 'MM-DD-YYYY hh-mm-ss').businessAdd(10.5);
expect(newBusinessDay.format('D')).to.eql('18');
done();
});
it('subtracts business days when negative values are added, excluding weekends, rounding down fractional day values', function (done) {
var newBusinessDay = moment('11-03-2015 12:42:00', 'MM-DD-YYYY hh-mm-ss').businessAdd(-10.4);
expect(newBusinessDay.format('D')).to.eql('20');
done();
});
it('subtracts business days when negative values are added, excluding weekends, rounding up fractional day values', function (done) {
var newBusinessDay = moment('11-03-2015 12:42:00', 'MM-DD-YYYY hh-mm-ss').businessAdd(-10.5);
expect(newBusinessDay.format('D')).to.eql('19');
done();
});
it('adds business hours only, excluding weekends', function (done) {
var newBusinessDay = moment('11-06-2015', 'MM-DD-YYYY').businessAdd(
36,
'hours'
Expand Down

0 comments on commit 49477ef

Please sign in to comment.