-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.