diff --git a/src/index.js b/src/index.js index a80fcd3ac..ffdbe9671 100644 --- a/src/index.js +++ b/src/index.js @@ -344,8 +344,7 @@ class Dayjs { } daysInMonth() { - // clone is for badMutable plugin - return this.clone().endOf(C.M).$D + return this.endOf(C.M).$D } $locale() { // get locale object diff --git a/src/plugin/badMutable/index.js b/src/plugin/badMutable/index.js index 6d28124b4..f95d581c6 100644 --- a/src/plugin/badMutable/index.js +++ b/src/plugin/badMutable/index.js @@ -29,5 +29,25 @@ export default (o, c) => { // locale needed later this.$L = oldLocale.bind(this)(preset, object).$L return this } + + const oldDaysInMonth = proto.daysInMonth + proto.daysInMonth = function () { + return oldDaysInMonth.bind(this.clone())() + } + + const oldIsSame = proto.isSame + proto.isSame = function () { + return oldIsSame.bind(this.clone())() + } + + const oldIsBefore = proto.isBefore + proto.isBefore = function () { + return oldIsBefore.bind(this.clone())() + } + + const oldIsAfter = proto.isAfter + proto.isAfter = function () { + return oldIsAfter.bind(this.clone())() + } } diff --git a/test/plugin/badMutable.test.js b/test/plugin/badMutable.test.js index be9333251..e68128914 100644 --- a/test/plugin/badMutable.test.js +++ b/test/plugin/badMutable.test.js @@ -161,3 +161,14 @@ it('Locale', () => { expect(d.locale()).toBe(m.locale()) expect(d.format(format)).toBe(m.format(format)) }) + +it('isAfter isBefore isSame', () => { + const d = dayjs() + const format = dayjs().format() + d.isSame(dayjs, 'year') + expect(d.format()).toBe(format) + d.isBefore(dayjs, 'hour') + expect(d.format()).toBe(format) + d.isAfter(dayjs, 'month') + expect(d.format()).toBe(format) +})