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: Handle locale in WeekOfYear plugin #658

Merged
merged 2 commits into from
Aug 19, 2019
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
13 changes: 11 additions & 2 deletions src/plugin/weekOfYear/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ export default (o, c, d) => {
if (week !== null) {
return this.add((week - this.week()) * 7, 'day')
}

const weekStart = this.$locale().weekStart || 0

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const weekStart = this.$locale().weekStart || 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Already update.

// d(this) clone is for badMutable plugin
const endOfYear = d(this).endOf(Y)
if (endOfYear.day() !== 6 && this.month() === 11 && (31 - this.date()) <= endOfYear.day()) {
if (
weekStart === 0 &&
endOfYear.day() !== 6 &&
this.month() === 11 &&
31 - this.date() <= endOfYear.day()
) {
return 1
}

const startOfYear = d(this).startOf(Y)
const compareDay = startOfYear.subtract(startOfYear.day(), D).subtract(1, MS)
const compareDay = startOfYear.subtract(startOfYear.day() - weekStart, D).subtract(1, MS)
const diffInWeek = this.diff(compareDay, W, true)
return Math.ceil(diffInWeek)
}
Expand Down
15 changes: 15 additions & 0 deletions test/plugin/weekOfYear.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/en-gb'

dayjs.extend(weekOfYear)

Expand All @@ -14,6 +15,8 @@ afterEach(() => {
})

it('Week of year', () => {
dayjs.locale('en')

const day = '2018-12-31T10:59:09+08:00'
const week = 27
expect(dayjs(day).week()).toBe(moment(day).week())
Expand All @@ -24,3 +27,15 @@ it('Week of year', () => {
expect(dayjs().weeks(55).week()).toBe(moment().weeks(55).week())
expect(dayjs().weeks()).toBe(moment().weeks())
})

it('Week of year with locale', () => {
dayjs.locale('en-gb')
moment.locale('en-gb')

const day = '2019-07-28'
expect(dayjs(day).week()).toBe(moment(day).week())

// Edges
expect(dayjs('2018-12-30').week()).toBe(moment('2018-12-30').week())
expect(dayjs('2019-12-29').week()).toBe(moment('2019-12-29').week())
})