Skip to content
Merged
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
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})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/
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
export const REGEX_FORMAT = /\[([^\]]+)]|YYYY|YY|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
10 changes: 5 additions & 5 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
MILLISECONDS_A_HOUR,
MILLISECONDS_A_MINUTE,
MILLISECONDS_A_SECOND,
MILLISECONDS_A_WEEK,
REGEX_FORMAT
MILLISECONDS_A_WEEK
} from '../../constant'

const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
const MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12

const durationRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/
const DURATION_REGEX_PARSE = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/
const DURATION_REGEX_FORMAT = /\[([^\]]+)]|YYYY|YY|Y|M{1,2}|D{1,2}|H{1,2}|m{1,2}|s{1,2}|SSS/g

const unitToMS = {
years: MILLISECONDS_A_YEAR,
Expand Down Expand Up @@ -81,7 +81,7 @@ class Duration {
return this
}
if (typeof input === 'string') {
const d = input.match(durationRegex)
const d = input.match(DURATION_REGEX_PARSE)
if (d) {
const properties = d.slice(2)
const numberD = properties.map(value => (value != null ? Number(value) : 0));
Expand Down Expand Up @@ -182,7 +182,7 @@ class Duration {
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]))
return str.replace(DURATION_REGEX_FORMAT, (match, $1) => $1 || String(matches[match]))
}

as(unit) {
Expand Down
2 changes: 2 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ it('Format invalid date', () => {
it('Format Year YY YYYY', () => {
expect(dayjs().format('YY')).toBe(moment().format('YY'))
expect(dayjs().format('YYYY')).toBe(moment().format('YYYY'))
expect(dayjs().format('Y')).toBe('Y')
expect(dayjs().format('YYY')).toBe(`${moment().format('YY')}Y`)
})

it('Format Month M MM MMM MMMM', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,9 @@ describe('Format', () => {
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')
})

test('formats YYY as YY + Y', () => {
const d = dayjs.duration(2, 'years')
expect(d.format('YYY')).toBe('022')
})
})
Loading