Skip to content

Commit

Permalink
refactor: switch refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
李权威 committed Apr 17, 2023
1 parent 80ee36f commit 209d694
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,36 @@ class Dayjs {
const diff = this - that
const getMonth = () => Utils.m(this, that)

let result = {
[C.Y]: () => getMonth() / 12,
[C.M]: () => getMonth(),
[C.Q]: () => getMonth() / 3,
[C.W]: () => (diff - zoneDelta) / C.MILLISECONDS_A_WEEK,
[C.D]: () => (diff - zoneDelta) / C.MILLISECONDS_A_DAY,
[C.H]: () => diff / C.MILLISECONDS_A_HOUR,
[C.MIN]: () => diff / C.MILLISECONDS_A_MINUTE,
[C.S]: () => diff / C.MILLISECONDS_A_SECOND
}[unit] || (() => diff) // milliseconds

result = result()
let result
switch (unit) {
case C.Y:
result = getMonth() / 12
break
case C.M:
result = getMonth()
break
case C.Q:
result = getMonth() / 3
break
case C.W:
result = (diff - zoneDelta) / C.MILLISECONDS_A_WEEK
break
case C.D:
result = (diff - zoneDelta) / C.MILLISECONDS_A_DAY
break
case C.H:
result = diff / C.MILLISECONDS_A_HOUR
break
case C.MIN:
result = diff / C.MILLISECONDS_A_MINUTE
break
case C.S:
result = diff / C.MILLISECONDS_A_SECOND
break
default:
result = diff // milliseconds
break
}

return float ? result : Utils.a(result)
}
Expand Down

0 comments on commit 209d694

Please sign in to comment.