Skip to content

Commit

Permalink
fix: respect time, not just dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleavely committed Apr 9, 2023
1 parent 71c81fe commit 0da5db0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = exports = ({
const nextMonthly = ({
from = new Date('0000-01-31T00:00:00.000Z'),
now = Date.now(),
} = {}) => {
Expand Down Expand Up @@ -27,5 +27,10 @@ module.exports = exports = ({
_from.getUTCMilliseconds(),
))

// if nextDate is on the same date as _now and has already passed
if (nextDate <= _now) return nextMonthly({ from, now: nextDate.valueOf() + (86400 * 1000) })

return nextDate
}

module.exports = exports = nextMonthly
14 changes: 14 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ it('returns 29th on leap year when target is 31st', () => {
expect(output.toJSON()).toBe('2000-02-29T00:00:00.000Z')
})

it('respects time, not just dates', () => {
// one millisecond after - yields next month
expect(nextMonthly({
from: new Date('1990-01-07T01:29:03.999Z'),
now: new Date('2023-01-07T01:29:04.000Z'),
}).toJSON()).toBe('2023-02-07T01:29:03.999Z')

// a few ms before - yields today
expect(nextMonthly({
from: new Date('1990-01-07T01:29:03.999Z'),
now: new Date('2023-01-07T01:29:03.888Z'),
}).toJSON()).toBe('2023-01-07T01:29:03.999Z')
})

it('behaves like README', () => {
const nextBillingDate = nextMonthly({
from: new Date('1990-01-07T01:29:03.999Z'),
Expand Down

0 comments on commit 0da5db0

Please sign in to comment.