Skip to content

Commit

Permalink
chore: draft add isoWeek plugin (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
hcwhan authored Mar 2, 2020
1 parent b9d64e1 commit e06a598
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/plugin/isoWeekOfYear/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default (o, c, d) => {
const days = function (day) {
const weekDay = day.day()
return weekDay === 0 ? 7 : weekDay
}

const getYearFirstThursday = function (year) {
const yearFirstDay = d().year(year).startOf('year')
let addDiffDays = 4 - days(yearFirstDay)
if (days(yearFirstDay) > 4) {
addDiffDays += 7
}
return yearFirstDay.add(addDiffDays, 'day')
}

const proto = c.prototype

proto.isoWeekYear = function () {
const nowWeekThursday = d(this).add((4 - days(this)), 'day')
return nowWeekThursday.year()
}

proto.isoWeek = function (isoWeek = null) {
if (isoWeek !== null) {
return this.add((isoWeek - this.isoWeek()) * 7, 'day')
}

const nowWeekThursday = d(this).add((4 - days(this)), 'day')
const diffWeekThursday = getYearFirstThursday(this.isoWeekYear())
return nowWeekThursday.diff(diffWeekThursday, 'week') + 1
}
}
96 changes: 96 additions & 0 deletions test/plugin/isoWeekOfYear.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import isoWeekOfYear from '../../src/plugin/isoWeekOfYear'

dayjs.extend(isoWeekOfYear)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('isoWeek of year', () => {
expect(dayjs().isoWeek(1).isoWeek()).toBe(1)
expect(dayjs().isoWeek(27).isoWeek()).toBe(27)


expect(dayjs('20191223').isoWeekYear()).toBe(2019)
expect(dayjs('20191223').isoWeek()).toBe(52)
expect(dayjs('20191224').isoWeekYear()).toBe(2019)
expect(dayjs('20191224').isoWeek()).toBe(52)
expect(dayjs('20191225').isoWeekYear()).toBe(2019)
expect(dayjs('20191225').isoWeek()).toBe(52)
expect(dayjs('20191226').isoWeekYear()).toBe(2019)
expect(dayjs('20191226').isoWeek()).toBe(52)
expect(dayjs('20191227').isoWeekYear()).toBe(2019)
expect(dayjs('20191227').isoWeek()).toBe(52)
expect(dayjs('20191228').isoWeekYear()).toBe(2019)
expect(dayjs('20191228').isoWeek()).toBe(52)
expect(dayjs('20191229').isoWeekYear()).toBe(2019)
expect(dayjs('20191229').isoWeek()).toBe(52)

expect(dayjs('20191230').isoWeekYear()).toBe(2020)
expect(dayjs('20191230').isoWeek()).toBe(1)
expect(dayjs('20191231').isoWeekYear()).toBe(2020)
expect(dayjs('20191231').isoWeek()).toBe(1)
expect(dayjs('20200101').isoWeekYear()).toBe(2020)
expect(dayjs('20200101').isoWeek()).toBe(1)
expect(dayjs('20200102').isoWeekYear()).toBe(2020)
expect(dayjs('20200102').isoWeek()).toBe(1)
expect(dayjs('20200103').isoWeekYear()).toBe(2020)
expect(dayjs('20200103').isoWeek()).toBe(1)
expect(dayjs('20200104').isoWeekYear()).toBe(2020)
expect(dayjs('20200104').isoWeek()).toBe(1)
expect(dayjs('20200105').isoWeekYear()).toBe(2020)
expect(dayjs('20200105').isoWeek()).toBe(1)

expect(dayjs('20200106').isoWeekYear()).toBe(2020)
expect(dayjs('20200106').isoWeek()).toBe(2)
expect(dayjs('20200107').isoWeekYear()).toBe(2020)
expect(dayjs('20200107').isoWeek()).toBe(2)


expect(dayjs('20201223').isoWeekYear()).toBe(2020)
expect(dayjs('20201223').isoWeek()).toBe(52)
expect(dayjs('20201224').isoWeekYear()).toBe(2020)
expect(dayjs('20201224').isoWeek()).toBe(52)
expect(dayjs('20201225').isoWeekYear()).toBe(2020)
expect(dayjs('20201225').isoWeek()).toBe(52)
expect(dayjs('20201226').isoWeekYear()).toBe(2020)
expect(dayjs('20201226').isoWeek()).toBe(52)
expect(dayjs('20201227').isoWeekYear()).toBe(2020)
expect(dayjs('20201227').isoWeek()).toBe(52)

expect(dayjs('20201228').isoWeekYear()).toBe(2020)
expect(dayjs('20201228').isoWeek()).toBe(53)
expect(dayjs('20201229').isoWeekYear()).toBe(2020)
expect(dayjs('20201229').isoWeek()).toBe(53)
expect(dayjs('20201230').isoWeekYear()).toBe(2020)
expect(dayjs('20201230').isoWeek()).toBe(53)
expect(dayjs('20201231').isoWeekYear()).toBe(2020)
expect(dayjs('20201231').isoWeek()).toBe(53)
expect(dayjs('20210101').isoWeekYear()).toBe(2020)
expect(dayjs('20210101').isoWeek()).toBe(53)
expect(dayjs('20210102').isoWeekYear()).toBe(2020)
expect(dayjs('20210102').isoWeek()).toBe(53)
expect(dayjs('20210103').isoWeekYear()).toBe(2020)
expect(dayjs('20210103').isoWeek()).toBe(53)

expect(dayjs('20210104').isoWeekYear()).toBe(2021)
expect(dayjs('20210104').isoWeek()).toBe(1)
expect(dayjs('20210105').isoWeekYear()).toBe(2021)
expect(dayjs('20210105').isoWeek()).toBe(1)
expect(dayjs('20210106').isoWeekYear()).toBe(2021)
expect(dayjs('20210106').isoWeek()).toBe(1)
expect(dayjs('20210107').isoWeekYear()).toBe(2021)
expect(dayjs('20210107').isoWeek()).toBe(1)
expect(dayjs('20210108').isoWeekYear()).toBe(2021)
expect(dayjs('20210108').isoWeek()).toBe(1)
expect(dayjs('20210109').isoWeekYear()).toBe(2021)
expect(dayjs('20210109').isoWeek()).toBe(1)
expect(dayjs('20210110').isoWeekYear()).toBe(2021)
expect(dayjs('20210110').isoWeek()).toBe(1)
})
13 changes: 13 additions & 0 deletions types/plugin/isoWeekOfYear.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
isoWeekYear(): number
isoWeek(): number

isoWeek(value: number): Dayjs
}
}

0 comments on commit e06a598

Please sign in to comment.