Skip to content

Commit

Permalink
fix: Fix BadMutable plugin bug in .diff (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Aug 31, 2020
1 parent a09d259 commit 40ab6d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const monthDiff = (a, b) => {
// function from moment.js in order to keep the same result
if (a.date() < b.date()) return -monthDiff(b, a)
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
const anchor = a.add(wholeMonthDiff, C.M)
const anchor = a.clone().add(wholeMonthDiff, C.M)
const c = b - anchor < 0
const anchor2 = a.add(wholeMonthDiff + (c ? -1 : 1), C.M)
const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), C.M)
return +(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) :
(anchor2 - anchor)))) || 0)
}
Expand Down
10 changes: 10 additions & 0 deletions test/plugin/badMutable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ it('Locale', () => {
expect(d.format(format)).toBe(m.format(format))
})

it('Diff', () => {
const d = dayjs()
const m = moment()
const unit = 'year'
const d2 = d.clone().add(1, unit)
const m2 = m.clone().add(1, unit)
expect(d.diff(d2, unit)).toBe(-1)
expect(m.diff(m2, unit)).toBe(-1)
})

it('isAfter isBefore isSame', () => {
const d = dayjs()
const format = dayjs().format()
Expand Down

0 comments on commit 40ab6d9

Please sign in to comment.