Skip to content

Commit

Permalink
feat: add invalid date string override (#1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teajey committed Apr 21, 2021
1 parent 0fdac93 commit 1c375fd
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 1c375fd

Please sign in to comment.