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

fix: update week support in duration plugin #2648

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 17 additions & 8 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class Duration {
$ms %= MILLISECONDS_A_YEAR
this.$d.months = roundNumber($ms / MILLISECONDS_A_MONTH)
$ms %= MILLISECONDS_A_MONTH
this.$d.weeks = roundNumber($ms / MILLISECONDS_A_WEEK)
$ms %= MILLISECONDS_A_WEEK
this.$d.days = roundNumber($ms / MILLISECONDS_A_DAY)
$ms %= MILLISECONDS_A_DAY
this.$d.hours = roundNumber($ms / MILLISECONDS_A_HOUR)
Expand All @@ -128,10 +130,7 @@ class Duration {
const Y = getNumberUnitFormat(this.$d.years, 'Y')
const M = getNumberUnitFormat(this.$d.months, 'M')

let days = +this.$d.days || 0
if (this.$d.weeks) {
days += this.$d.weeks * 7
}
const days = this.getTotalDays()

const D = getNumberUnitFormat(days, 'D')
const H = getNumberUnitFormat(this.$d.hours, 'H')
Expand Down Expand Up @@ -166,14 +165,17 @@ class Duration {

format(formatStr) {
const str = formatStr || 'YYYY-MM-DDTHH:mm:ss'

const days = this.getTotalDays()

const matches = {
Y: this.$d.years,
YY: $u.s(this.$d.years, 2, '0'),
YYYY: $u.s(this.$d.years, 4, '0'),
M: this.$d.months,
MM: $u.s(this.$d.months, 2, '0'),
D: this.$d.days,
DD: $u.s(this.$d.days, 2, '0'),
D: days,
DD: $u.s(days, 2, '0'),
H: this.$d.hours,
HH: $u.s(this.$d.hours, 2, '0'),
m: this.$d.minutes,
Expand All @@ -185,6 +187,14 @@ class Duration {
return str.replace(REGEX_FORMAT, (match, $1) => $1 || String(matches[match]))
}

getTotalDays() {
let days = +this.$d.days || 0
if (this.$d.weeks) {
days += this.$d.weeks * 7
}
return days
}

as(unit) {
return this.$ms / (unitToMS[prettyUnit(unit)])
}
Expand All @@ -194,8 +204,6 @@ class Duration {
const pUnit = prettyUnit(unit)
if (pUnit === 'milliseconds') {
base %= 1000
} else if (pUnit === 'weeks') {
base = roundNumber(base / unitToMS[pUnit])
} else {
base = this.$d[pUnit]
}
Expand Down Expand Up @@ -261,6 +269,7 @@ class Duration {
const manipulateDuration = (date, duration, k) =>
date.add(duration.years() * k, 'y')
.add(duration.months() * k, 'M')
.add(duration.weeks() * k, 'w')
.add(duration.days() * k, 'd')
.add(duration.hours() * k, 'h')
.add(duration.minutes() * k, 'm')
Expand Down
13 changes: 9 additions & 4 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ describe('Add to a dayjs()', () => {

test('Add duration', () => {
const a = dayjs('2020-10-01')
const days = dayjs.duration(2, 'days')
expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03')
const days = dayjs.duration({ weeks: 2, days: 2 })
expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-17')

const b = dayjs('2023-02-01 00:00:00')
const p = dayjs.duration('P1Y1M1DT1H1M1S')
Expand All @@ -219,8 +219,8 @@ describe('Subtract', () => {

test('Subtract duration', () => {
const a = dayjs('2020-10-20')
const days = dayjs.duration(2, 'days')
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18')
const days = dayjs.duration({ weeks: 2, days: 2 })
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-04')

const b = dayjs('2023-03-02 02:02:02')
const p = dayjs.duration('P1Y1M1DT1H1M1S')
Expand Down Expand Up @@ -296,6 +296,11 @@ describe('Format', () => {
.add(22, 'years')
expect(d.format()).toBe('0022-10-16T13:35:15')
})
test('format for weeks', () => {
const d = dayjs.duration(2, 'weeks').add(3, 'days')
expect(d.format('D')).toBe('17')
expect(d.format('DD')).toBe('17')
})

test('with formatStr for all tokens', () => {
const d = dayjs.duration(1, 'seconds')
Expand Down