Skip to content

Commit

Permalink
fix: advancedFormat plugin invalid date check
Browse files Browse the repository at this point in the history
  • Loading branch information
imwh0im committed Jul 5, 2021
1 parent 4889ed5 commit 4554cbe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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()

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

0 comments on commit 4554cbe

Please sign in to comment.