From 4574b93443a22bcf31e9bdc266c4da86a9c47281 Mon Sep 17 00:00:00 2001 From: lucianm05 Date: Wed, 25 Jan 2023 11:44:10 +0200 Subject: [PATCH] fix: add/subtract business days infinite loop --- src/index.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index ac36bc7..a4992fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,11 +88,18 @@ const businessTime = ( ) { let daysToIterate = numberOfDays; let day = date.clone(); + let internalAction = action; + + if (numberOfDays < 0) { + if (action === 'add') internalAction = 'subtract'; + if (action === 'subtract') internalAction = 'add'; + } while (daysToIterate) { - day = day[action](1, 'day'); + day = day[internalAction](1, 'day'); if (day.isBusinessDay()) { - daysToIterate = daysToIterate - 1; + if (daysToIterate < 0) daysToIterate = daysToIterate + 1; + else daysToIterate = daysToIterate - 1; } } @@ -374,7 +381,7 @@ const businessTime = ( break; } else if (from.isSameOrAfter(start) && from.isSameOrBefore(end)) { diff += end.diff(from, 'minutes'); - } + } } return diff ? diff * multiplier : 0;