From 49477efd2a78abc51b2524cf9fc224f7c6f13bdd Mon Sep 17 00:00:00 2001 From: Jerry O'Brien Date: Tue, 4 Dec 2018 14:09:13 -0600 Subject: [PATCH] Fix infinite loop caused by fraction value in businessAdd Fixes #55 * Fix: Fractional day causes infinite loop in businessAdd #55 * Add absolute number rounding in businessAdd * Fix tests for fractional days --- index.js | 13 ++++++++++--- tests/test.js | 26 +++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index a6d7876..1744532 100644 --- a/index.js +++ b/index.js @@ -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()) { diff --git a/tests/test.js b/tests/test.js index 85cfdf7..bcaf214 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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'