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: add plugin advancedFormat validation #1566

Merged
merged 11 commits into from
Jul 7, 2021
7 changes: 6 additions & 1 deletion src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +11,11 @@ export default (o, c, d) => { // locale needed later
// extend en locale here
proto.format = function (formatStr) {
const locale = this.$locale()

imwh0im marked this conversation as resolved.
Show resolved Hide resolved
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) => {
Expand Down
4 changes: 4 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down