Skip to content

Commit

Permalink
fix: Fix startOf week bug while week start is not Sunday
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Feb 11, 2019
1 parent 85396c2 commit 5eaf77b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,18 @@ class Dayjs {
(isStartOf ? argumentStart : argumentEnd).slice(slice)
), this)
}

const { $W, $M, $D } = this
switch (unit) {
case C.Y:
return isStartOf ? instanceFactory(1, 0) :
instanceFactory(31, 11)
case C.M:
return isStartOf ? instanceFactory(1, this.$M) :
instanceFactory(0, this.$M + 1)
return isStartOf ? instanceFactory(1, $M) :
instanceFactory(0, $M + 1)
case C.W: {
const weekStart = this.$locale().weekStart || 0
return isStartOf ? instanceFactory(this.$D - (this.$W - weekStart), this.$M) :
instanceFactory(this.$D + (6 - (this.$W - weekStart)), this.$M)
const gap = ($W < weekStart ? $W + 7 : $W) - weekStart
return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M)
}
case C.D:
case C.DATE:
Expand Down
1 change: 1 addition & 0 deletions src/locale/ar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const locale = {
name: 'ar',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekStart: 6,
relativeTime: {
future: 'بعد %s',
past: 'منذ %s',
Expand Down
13 changes: 9 additions & 4 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ describe('StartOf EndOf', () => {
})

it('StartOf week with locale', () => {
const testArr = ['zh-cn']
testArr.forEach((l) => {
expect(dayjs().locale(l).startOf('week').date()).toBe(moment().locale(l).startOf('week').date())
expect(dayjs().locale(l).endOf('week').date()).toBe(moment().locale(l).endOf('week').date())
const testDate = [undefined, '2019-02-10', '2019-02-11', '2019-02-12', '2019-02-13', '2019-02-14', '2019-02-15', '2019-02-16']
const testLocale = ['zh-cn', 'ar', 'en']
testDate.forEach((d) => {
testLocale.forEach((l) => {
expect(dayjs(d).locale(l).startOf('week').date())
.toBe(moment(d).locale(l).startOf('week').date())
expect(dayjs(d).locale(l).endOf('week').date())
.toBe(moment(d).locale(l).endOf('week').date())
})
})
})
})
Expand Down

0 comments on commit 5eaf77b

Please sign in to comment.