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

[Code improvement] utcPad constant #2683

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
37 changes: 21 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ class Dayjs {
return Utils
}

$getUtcPadUnits() {
const utcPad = `set${this.$u ? 'UTC' : ''}`
return {
[C.D]: `${utcPad}Date`,
[C.DATE]: `${utcPad}Date`,
[C.M]: `${utcPad}Month`,
[C.Y]: `${utcPad}FullYear`,
[C.H]: `${utcPad}Hours`,
[C.MIN]: `${utcPad}Minutes`,
[C.S]: `${utcPad}Seconds`,
[C.MS]: `${utcPad}Milliseconds`
}
}

isValid() {
return !(this.$d.toString() === C.INVALID_DATE_STRING)
}
Expand Down Expand Up @@ -160,7 +174,7 @@ class Dayjs {
), this)
}
const { $W, $M, $D } = this
const utcPad = `set${this.$u ? 'UTC' : ''}`
const utcPadUnits = this.$getUtcPadUnits()
switch (unit) {
case C.Y:
return isStartOf ? instanceFactory(1, 0) :
Expand All @@ -175,13 +189,13 @@ class Dayjs {
}
case C.D:
case C.DATE:
return instanceFactorySet(`${utcPad}Hours`, 0)
return instanceFactorySet(utcPadUnits[C.H], 0)
case C.H:
return instanceFactorySet(`${utcPad}Minutes`, 1)
return instanceFactorySet(utcPadUnits[C.MIN], 1)
case C.MIN:
return instanceFactorySet(`${utcPad}Seconds`, 2)
return instanceFactorySet(utcPadUnits[C.MIN], 2)
case C.S:
return instanceFactorySet(`${utcPad}Milliseconds`, 3)
return instanceFactorySet(utcPadUnits[C.MS], 3)
default:
return this.clone()
}
Expand All @@ -193,17 +207,8 @@ class Dayjs {

$set(units, int) { // private set
const unit = Utils.p(units)
const utcPad = `set${this.$u ? 'UTC' : ''}`
const name = {
[C.D]: `${utcPad}Date`,
[C.DATE]: `${utcPad}Date`,
[C.M]: `${utcPad}Month`,
[C.Y]: `${utcPad}FullYear`,
[C.H]: `${utcPad}Hours`,
[C.MIN]: `${utcPad}Minutes`,
[C.S]: `${utcPad}Seconds`,
[C.MS]: `${utcPad}Milliseconds`
}[unit]
const utcPadUnits = this.$getUtcPadUnits()
const name = utcPadUnits[unit]
const arg = unit === C.D ? this.$D + (int - this.$W) : int

if (unit === C.M || unit === C.Y) {
Expand Down