From 9a859a147ba223a1eeff0f2bb6f33d97e0ccc6c7 Mon Sep 17 00:00:00 2001 From: Benjamin Tanone Date: Tue, 24 Nov 2020 21:55:46 +1100 Subject: [PATCH] fix: add duration.format to format a Duration (#1202) --- src/constant.js | 2 +- src/plugin/duration/index.js | 23 ++++++++++++++++++++++- test/plugin/duration.test.js | 24 ++++++++++++++++++++++++ types/plugin/duration.d.ts | 2 ++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/constant.js b/src/constant.js index a4f034fa..22c1319e 100644 --- a/src/constant.js +++ b/src/constant.js @@ -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 diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index 50f66048..02f12bcb 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -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 @@ -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)]) } diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index 5e544765..15e60b92 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -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') + }) +}) diff --git a/types/plugin/duration.d.ts b/types/plugin/duration.d.ts index c09dc9f0..3695eeaa 100644 --- a/types/plugin/duration.d.ts +++ b/types/plugin/duration.d.ts @@ -51,6 +51,8 @@ declare namespace plugin { toISOString(): string + format(formatStr?: string): string + locale(locale: string): Duration } }