Skip to content

Commit

Permalink
chore: chore
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Apr 25, 2018
1 parent 4ba4fa2 commit b517622
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,38 +91,37 @@ class Dayjs {

startOf(units, isStartOf = true) { // isStartOf -> endOf
const unit = Utils.prettyUnit(units)
const instanceFactory = (d, m, y = this.$y, isEnd = false) => {
const instanceFactory = (d, m, y = this.$y) => {
const ins = new Dayjs(new Date(y, m, d))
return isEnd ? ins.endOf(C.D) : ins
return isStartOf ? ins : ins.endOf(C.D)
}
const instanceFactorySet = (method, slice) => {
const argumentStart = [0, 0, 0, 0]
const argumentEnd = [23, 59, 59, 999]
return new Dayjs(Date()[method].apply(
this.toDate(),
isStartOf ? argumentStart.slice(slice) : argumentEnd.slice(slice)
))
}
switch (unit) {
case C.Y:
return isStartOf ? instanceFactory(1, 0) :
instanceFactory(31, 11, this.$y, true)
instanceFactory(31, 11, this.$y)
case C.M:
return isStartOf ? instanceFactory(1, this.$M) :
instanceFactory(0, this.$M + 1, this.$y, true)
instanceFactory(0, this.$M + 1, this.$y)
case C.W:
return isStartOf ? instanceFactory(this.$D - this.$W, this.$M) :
instanceFactory(this.$D + (6 - this.$W), this.$M, this.$y, true)
instanceFactory(this.$D + (6 - this.$W), this.$M, this.$y)
case C.D:
case C.DATE:
return new Dayjs(Date().setHours.apply(
this.toDate(),
isStartOf ? [0, 0, 0, 0] : [23, 59, 59, 999]
))
return instanceFactorySet('setHours', 0)
case C.H:
return new Dayjs(Date().setMinutes.apply(
this.toDate(),
isStartOf ? [0, 0, 0] : [59, 59, 999]
))
return instanceFactorySet('setMinutes', 1)
case C.MIN:
return new Dayjs(Date().setSeconds.apply(
this.toDate(),
isStartOf ? [0, 0] : [59, 999]
))
return instanceFactorySet('setSeconds', 2)
case C.S:
return new Dayjs(this.toDate().setMilliseconds(isStartOf ? 0 : 999))
return instanceFactorySet('setMilliseconds', 3)
default:
return this.clone()
}
Expand Down

0 comments on commit b517622

Please sign in to comment.