Skip to content

Commit 40ab6d9

Browse files
authored
fix: Fix BadMutable plugin bug in .diff (#1023)
1 parent a09d259 commit 40ab6d9

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const monthDiff = (a, b) => {
1818
// function from moment.js in order to keep the same result
1919
if (a.date() < b.date()) return -monthDiff(b, a)
2020
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
21-
const anchor = a.add(wholeMonthDiff, C.M)
21+
const anchor = a.clone().add(wholeMonthDiff, C.M)
2222
const c = b - anchor < 0
23-
const anchor2 = a.add(wholeMonthDiff + (c ? -1 : 1), C.M)
23+
const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), C.M)
2424
return +(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) :
2525
(anchor2 - anchor)))) || 0)
2626
}

test/plugin/badMutable.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ it('Locale', () => {
164164
expect(d.format(format)).toBe(m.format(format))
165165
})
166166

167+
it('Diff', () => {
168+
const d = dayjs()
169+
const m = moment()
170+
const unit = 'year'
171+
const d2 = d.clone().add(1, unit)
172+
const m2 = m.clone().add(1, unit)
173+
expect(d.diff(d2, unit)).toBe(-1)
174+
expect(m.diff(m2, unit)).toBe(-1)
175+
})
176+
167177
it('isAfter isBefore isSame', () => {
168178
const d = dayjs()
169179
const format = dayjs().format()

0 commit comments

Comments
 (0)