Skip to content

Commit

Permalink
fix: Return null in toJSON if not valid (#633)
Browse files Browse the repository at this point in the history
* fix: toJSON returns null if not valid

* fix: typo in locale

* fix: adding test on toJSON
  • Loading branch information
guillaumebriday authored and iamkun committed Jul 16, 2019
1 parent fef135e commit 19affc8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class Dayjs {
}

toJSON() {
return this.toISOString()
return this.isValid() ? this.toISOString() : null
}

toISOString() {
Expand Down
2 changes: 1 addition & 1 deletion src/locale/pt-br.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import dayjs from 'dayjs'
const locale = {
name: 'pt-br',
weekdays: 'Domingo_Segunda-feira_Terça-feira_Quarta-feira_Quinta-feira_Sexta-feira_Sábado'.split('_'),
weekdaysMin : 'Do_2ª_3ª_4ª_5ª_6ª_Sá'.split('_'),
weekdaysMin: 'Do_2ª_3ª_4ª_5ª_6ª_Sá'.split('_'),
weekStart: 1,
months: 'Janeiro_Fevereiro_Março_Abril_Maio_Junho_Julho_Agosto_Setembro_Outubro_Novembro_Dezembro'.split('_'),
ordinal: n => `${n}º`,
Expand Down
2 changes: 2 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ it('As Javascript Date -> toDate', () => {

it('As JSON -> toJSON', () => {
expect(dayjs().toJSON()).toBe(moment().toJSON())
expect(dayjs('otherString').toJSON()).toBe(moment('otherString').toJSON())
expect(dayjs('otherString').toJSON()).toBe(null)
})

it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
Expand Down

0 comments on commit 19affc8

Please sign in to comment.