Skip to content

Commit

Permalink
add setWeek fucntion to weekOfYear (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquaminer authored and iamkun committed Feb 5, 2019
1 parent bdfb7c8 commit 142b763
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/plugin/weekOfYear/index.js
Expand Up @@ -2,7 +2,10 @@ import { MS, Y, D, W } from '../../constant'

export default (o, c, d) => {
const proto = c.prototype
proto.week = function () {
proto.week = function (week = null) {
if (week !== null) {
return this.add((week - this.week()) * 7, 'day')
}
const endOfYear = this.endOf(Y)
if (endOfYear.day() !== 6 && this.month() === 11 && (31 - this.date()) <= endOfYear.day()) {
return 1
Expand All @@ -12,4 +15,7 @@ export default (o, c, d) => {
const diffInWeek = this.diff(compareDay, W, true)
return Math.ceil(diffInWeek)
}
proto.weeks = function (week = null) {
return this.week(week)
}
}
5 changes: 5 additions & 0 deletions test/plugin/weekOfYear.test.js
Expand Up @@ -15,6 +15,11 @@ afterEach(() => {

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

0 comments on commit 142b763

Please sign in to comment.