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 DayOfYear plugin when using BadMutable plugin #1511

Merged
merged 1 commit into from
Jun 1, 2021
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
5 changes: 3 additions & 2 deletions src/plugin/dayOfYear/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default (o, c) => {
export default (o, c, d) => {
const proto = c.prototype
proto.dayOfYear = function (input) {
const dayOfYear = Math.round((this.startOf('day') - this.startOf('year')) / 864e5) + 1
// d(this) is for badMutable
const dayOfYear = Math.round((d(this).startOf('day') - d(this).startOf('year')) / 864e5) + 1
return input == null ? dayOfYear : this.add(input - dayOfYear, 'day')
}
}
11 changes: 10 additions & 1 deletion test/plugin/badMutable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import badMutable from '../../src/plugin/badMutable'
import dayOfYear from '../../src/plugin/dayOfYear'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/zh-cn'

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

beforeEach(() => {
Expand Down Expand Up @@ -188,7 +190,14 @@ it('isAfter isBefore isSame', () => {
expect(d.isAfter()).toBe(false)
})

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

it('WeekOfYear get week won\'t change instance', () => {
const d = dayjs()
const format = d.format()
d.week()
Expand Down