From b5176222b7b07f42fc13cfd5212b1128b4564eb3 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 26 Apr 2018 00:40:58 +0800 Subject: [PATCH] chore: chore --- src/index.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/index.js b/src/index.js index 2b966378c..c8ba41752 100644 --- a/src/index.js +++ b/src/index.js @@ -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() }