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: add IsoWeek plugin #811

Merged
merged 9 commits into from
Mar 2, 2020
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
51 changes: 51 additions & 0 deletions src/plugin/isoWeek/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { D, W, Y } from '../../constant'

const isoWeekPrettyUnit = 'isoweek'

export default (o, c, d) => {
const getYearFirstThursday = (year) => {
const yearFirstDay = d().year(year).startOf(Y)
let addDiffDays = 4 - yearFirstDay.isoWeekday()
if (yearFirstDay.isoWeekday() > 4) {
addDiffDays += 7
}
return yearFirstDay.add(addDiffDays, D)
}

const getCurrentWeekThursday = ins => ins.add((4 - ins.isoWeekday()), D)

const proto = c.prototype

proto.isoWeekYear = function () {
const nowWeekThursday = getCurrentWeekThursday(this)
return nowWeekThursday.year()
}

proto.isoWeek = function (week) {
if (!this.$utils().u(week)) {
return this.add((week - this.isoWeek()) * 7, D)
}
const nowWeekThursday = getCurrentWeekThursday(this)
const diffWeekThursday = getYearFirstThursday(this.isoWeekYear())
return nowWeekThursday.diff(diffWeekThursday, W) + 1
}

proto.isoWeekday = function (week) {
if (!this.$utils().u(week)) {
return this.day(this.day() % 7 ? week : week - 7)
}
return this.day() || 7
}

const oldStartOf = proto.startOf
proto.startOf = function (units, startOf) {
const utils = this.$utils()
const isStartOf = !utils.u(startOf) ? startOf : true
const unit = utils.p(units)
if (unit === isoWeekPrettyUnit) {
return isStartOf ? this.date(this.date() - (this.isoWeekday() - 1)).startOf('day') :
this.date((this.date() - 1 - (this.isoWeekday() - 1)) + 7).endOf('day')
}
return oldStartOf.bind(this)(units, startOf)
}
}
32 changes: 0 additions & 32 deletions src/plugin/isoWeekOfYear/index.js

This file was deleted.

33 changes: 31 additions & 2 deletions test/plugin/isoWeekOfYear.test.js → test/plugin/isoWeek.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import isoWeekOfYear from '../../src/plugin/isoWeekOfYear'
import isoWeek from '../../src/plugin/isoWeek'

dayjs.extend(isoWeekOfYear)
dayjs.extend(isoWeek)

beforeEach(() => {
MockDate.set(new Date())
Expand All @@ -12,6 +13,34 @@ afterEach(() => {
MockDate.reset()
})

it('get isoWeek', () => {
expect(dayjs().isoWeek()).toBe(moment().isoWeek())
})

it('set isoWeek', () => {
expect(dayjs().isoWeek(1).valueOf()).toBe(moment().isoWeek(1).valueOf())
expect(dayjs().isoWeek(52).valueOf()).toBe(moment().isoWeek(52).valueOf())
})

it('get isoWeekYear', () => {
expect(dayjs().isoWeekYear()).toBe(moment().isoWeekYear())
})

it('startOf/endOf isoWeek', () => {
const ISOWEEK = 'isoWeek'
expect(dayjs().startOf(ISOWEEK).valueOf()).toBe(moment().startOf(ISOWEEK).valueOf())
expect(dayjs().endOf(ISOWEEK).valueOf()).toBe(moment().endOf(ISOWEEK).valueOf())
})

it('isoWeekday', () => {
expect(dayjs().isoWeekday()).toBe(moment().isoWeekday())
expect(dayjs('20200301').isoWeekday(1).valueOf()).toBe(moment('20200301').isoWeekday(1).valueOf()) // Sunday this.day() -> 0
for (let i = 0; i < 7; i += 1) {
expect(dayjs().add(i, 'day').isoWeekday()).toBe(moment().add(i, 'day').isoWeekday())
expect(dayjs().isoWeekday(i).valueOf()).toBe(moment().isoWeekday(i).valueOf())
}
})

it('isoWeek of year', () => {
expect(dayjs().isoWeek(1).isoWeek()).toBe(1)
expect(dayjs().isoWeek(27).isoWeek()).toBe(27)
Expand Down
27 changes: 27 additions & 0 deletions types/plugin/isoWeek.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PluginFunc, QUnitType, ConfigType } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

type ISOUnitType = UnitType | 'isoWeek';

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

isoWeekday(): number
isoWeekday(value: number): Dayjs

startOf(unit: ISOUnitType): Dayjs

endOf(unit: ISOUnitType): Dayjs

isSame(date: ConfigType, unit?: ISOUnitType): boolean

isBefore(date: ConfigType, unit?: ISOUnitType): boolean

isAfter(date: ConfigType, unit?: ISOUnitType): boolean
}
}
13 changes: 0 additions & 13 deletions types/plugin/isoWeekOfYear.d.ts

This file was deleted.