Skip to content

Commit 19affc8

Browse files
guillaumebridayiamkun
authored andcommitted
fix: Return null in toJSON if not valid (#633)
* fix: toJSON returns null if not valid * fix: typo in locale * fix: adding test on toJSON
1 parent fef135e commit 19affc8

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class Dayjs {
374374
}
375375

376376
toJSON() {
377-
return this.toISOString()
377+
return this.isValid() ? this.toISOString() : null
378378
}
379379

380380
toISOString() {

src/locale/pt-br.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import dayjs from 'dayjs'
33
const locale = {
44
name: 'pt-br',
55
weekdays: 'Domingo_Segunda-feira_Terça-feira_Quarta-feira_Quinta-feira_Sexta-feira_Sábado'.split('_'),
6-
weekdaysMin : 'Do_2ª_3ª_4ª_5ª_6ª_Sá'.split('_'),
6+
weekdaysMin: 'Do_2ª_3ª_4ª_5ª_6ª_Sá'.split('_'),
77
weekStart: 1,
88
months: 'Janeiro_Fevereiro_Março_Abril_Maio_Junho_Julho_Agosto_Setembro_Outubro_Novembro_Dezembro'.split('_'),
99
ordinal: n => `${n}º`,

test/display.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ it('As Javascript Date -> toDate', () => {
248248

249249
it('As JSON -> toJSON', () => {
250250
expect(dayjs().toJSON()).toBe(moment().toJSON())
251+
expect(dayjs('otherString').toJSON()).toBe(moment('otherString').toJSON())
252+
expect(dayjs('otherString').toJSON()).toBe(null)
251253
})
252254

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

0 commit comments

Comments
 (0)