From 4554cbedb26bdbe232a9cebaf4521a4c1afe987c Mon Sep 17 00:00:00 2001 From: imwh0im Date: Tue, 6 Jul 2021 01:46:29 +0900 Subject: [PATCH] fix: advancedFormat plugin invalid date check --- src/plugin/advancedFormat/index.js | 7 ++++++- test/plugin/advancedFormat.test.js | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/plugin/advancedFormat/index.js b/src/plugin/advancedFormat/index.js index 6ebd3f43b..5b1591071 100644 --- a/src/plugin/advancedFormat/index.js +++ b/src/plugin/advancedFormat/index.js @@ -1,4 +1,4 @@ -import { FORMAT_DEFAULT } from '../../constant' +import { FORMAT_DEFAULT, INVALID_DATE_STRING } from '../../constant' export default (o, c, d) => { // locale needed later const proto = c.prototype @@ -11,6 +11,11 @@ export default (o, c, d) => { // locale needed later // extend en locale here proto.format = function (formatStr) { const locale = this.$locale() + + if (!this.isValid()) { + return locale.invalidDate || INVALID_DATE_STRING + } + const utils = this.$utils() const str = formatStr || FORMAT_DEFAULT const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, (match) => { diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index d9512e086..955f94032 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -24,6 +24,10 @@ afterEach(() => { MockDate.reset() }) +it('Format of invalid date', () => { + expect(dayjs(null).format('z').toLowerCase()).toEqual(moment(null).format('z').toLowerCase()) +}) + it('Format empty string', () => { expect(dayjs().format()).toBe(moment().format()) })