Skip to content

Commit

Permalink
Add previous business day
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanaugustoos committed May 25, 2021
1 parent 0520d8b commit 30fc624
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -37,6 +37,10 @@ Returns if the supplied date is a business day.

Returns the next business day after `day`.

## previousBusinessDay(country, date, options)

Returns the previous business day before `day`.

# License

Check [here](LICENSE).
Expand Down
17 changes: 17 additions & 0 deletions lib/index.js
Expand Up @@ -73,6 +73,23 @@ exports.nextBusinessDay = function(country, anchorDate, options) {
}
};

exports.previousBusinessDay = function(country, anchorDate, options) {
const day = moment(sanitizeDateInput(anchorDate)).subtract(1, 'd');

return iterate(day);

function iterate(day) {
return exports.isBusinessDay(country, day, options)
.then(function(isBusinessDay) {
if (isBusinessDay) {
return day.toDate();
}

return iterate(day.subtract(1, 'd'));
});
}
};

function getDateFromInformation(info, date) {
var key = date.format('YYYY-MM-DD');

Expand Down
7 changes: 7 additions & 0 deletions test/query.js
Expand Up @@ -30,5 +30,12 @@ describe('Query', function() {
return expect(bm.nextBusinessDay('brazil', new Date(2016, 1, 5, 10))).to.eventually.eql(new Date(2016, 1, 10, 10));
});
});

describe('previousBusinessDay', function() {
it('should return the correct day', function() {
return expect(bm.previousBusinessDay('brazil', new Date(2021, 5, 4, 10)))
.to.eventually.eql(new Date(2021, 5, 2, 10));
});
});
});

0 comments on commit 30fc624

Please sign in to comment.