Skip to content

Commit

Permalink
fix: Fix WeekOfYear plugin bug while using BadMutable plugin (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Apr 27, 2020
1 parent 0606f42 commit 2977438
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/plugin/weekOfYear/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { MS, Y, D, W } from '../../constant'

export default (o, c) => {
export default (o, c, d) => {
const proto = c.prototype
proto.week = function (week = null) {
if (week !== null) {
return this.add((week - this.week()) * 7, D)
}
const yearStart = this.$locale().yearStart || 1
if (this.month() === 11 && this.date() > 25) {
const nextYearStartDay = this.startOf(Y).add(1, Y).date(yearStart)
const thisEndOfWeek = this.endOf(W)
// d(this) is for badMutable
const nextYearStartDay = d(this).startOf(Y).add(1, Y).date(yearStart)
const thisEndOfWeek = d(this).endOf(W)
if (nextYearStartDay.isBefore(thisEndOfWeek)) {
return 1
}
}
const yearStartDay = this.startOf(Y).date(yearStart)
const yearStartDay = d(this).startOf(Y).date(yearStart)
const yearStartWeek = yearStartDay.startOf(W).subtract(1, MS)
const diffInWeek = this.diff(yearStartWeek, W, true)
if (diffInWeek < 0) {
return this.startOf('week').week()
return d(this).startOf('week').week()
}
return Math.ceil(diffInWeek)
}
Expand Down
9 changes: 9 additions & 0 deletions test/plugin/badMutable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import badMutable from '../../src/plugin/badMutable'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/zh-cn'

dayjs.extend(badMutable)
dayjs.extend(weekOfYear)

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -175,3 +177,10 @@ it('isAfter isBefore isSame', () => {
expect(d.format()).toBe(format)
expect(d.isAfter()).toBe(false)
})

it('WeekOfYear get week won"t change instance', () => {
const d = dayjs()
const format = d.format()
d.week()
expect(d.format()).toBe(format)
})

0 comments on commit 2977438

Please sign in to comment.