Skip to content

Commit

Permalink
fix: add invalid date string override (#1465) (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teajey committed May 26, 2021
1 parent b246210 commit 06f88f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Expand Up @@ -251,11 +251,12 @@ class Dayjs {
}

format(formatStr) {
if (!this.isValid()) return C.INVALID_DATE_STRING
const locale = this.$locale()

if (!this.isValid()) return locale.invalidDate || C.INVALID_DATE_STRING

const str = formatStr || C.FORMAT_DEFAULT
const zoneStr = Utils.z(this)
const locale = this.$locale()
const { $H, $m, $M } = this
const {
weekdays, months, meridiem
Expand Down
11 changes: 11 additions & 0 deletions test/plugin/updateLocale.test.js
Expand Up @@ -68,4 +68,15 @@ describe('Update locale', () => {
expect(dayjs().format(formatString))
.toEqual(moment().format(formatString))
})

it('Update invalid date string', () => {
const locale = 'en'
const localeSetting = { invalidDate: 'bad date' }
dayjs.updateLocale(locale, localeSetting)
moment.updateLocale(locale, localeSetting)
dayjs.locale(locale)
moment.locale(locale)
expect(dayjs('').format()).toBe(moment('').format())
expect(dayjs('otherString').format()).toBe(moment('otherString').format())
})
})

0 comments on commit 06f88f4

Please sign in to comment.