Skip to content

Commit

Permalink
fix: add duration.format to format a Duration (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
verzac authored Nov 24, 2020
1 parent b8d2e32 commit 9a859a1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export const INVALID_DATE_STRING = 'Invalid Date'

// regex
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d+)?$/
export const REGEX_FORMAT = /\[([^\]]+)]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
23 changes: 22 additions & 1 deletion src/plugin/duration/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK } from '../../constant'
import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK, REGEX_FORMAT } from '../../constant'

const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
const MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30
Expand Down Expand Up @@ -105,6 +105,27 @@ class Duration {
return this.toISOString()
}

format(formatStr) {
const str = formatStr || 'YYYY-MM-DDTHH:mm:ss'
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'),
H: this.$d.hours,
HH: $u.s(this.$d.hours, 2, '0'),
m: this.$d.minutes,
mm: $u.s(this.$d.minutes, 2, '0'),
s: this.$d.seconds,
ss: $u.s(this.$d.seconds, 2, '0'),
SSS: $u.s(this.$d.milliseconds, 3, '0')
}
return str.replace(REGEX_FORMAT, (match, $1) => $1 || String(matches[match]))
}

as(unit) {
return this.$ms / (unitToMS[prettyUnit(unit)])
}
Expand Down
24 changes: 24 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,27 @@ describe('prettyUnit', () => {
m: 12
}).toISOString()).toBe('P12MT12M')
})

describe('Format', () => {
test('no formatStr', () => {
const d = dayjs.duration(15, 'seconds')
.add(13, 'hours')
.add(35, 'minutes')
.add(16, 'days')
.add(10, 'months')
.add(22, 'years')
expect(d.format()).toBe('0022-10-16T13:35:15')
})

test('with formatStr for all tokens', () => {
const d = dayjs.duration(1, 'seconds')
.add(8, 'hours')
.add(5, 'minutes')
.add(6, 'days')
.add(9, 'months')
.add(2, 'years')
.add(10, 'milliseconds')
expect(d.format('Y/YY.YYYYTESTM:MM:D:DD:H:HH:m:mm:s:ss:SSS'))
.toBe('2/02.0002TEST9:09:6:06:8:08:5:05:1:01:010')
})
})
2 changes: 2 additions & 0 deletions types/plugin/duration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ declare namespace plugin {

toISOString(): string

format(formatStr?: string): string

locale(locale: string): Duration
}
}
Expand Down

0 comments on commit 9a859a1

Please sign in to comment.