Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix WeekOfYear plugin bug while using BadMutable plugin #884

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})