From 19affc84bbec84bad840e310b390db5f92b2499a Mon Sep 17 00:00:00 2001 From: Guillaume Briday Date: Tue, 16 Jul 2019 10:19:34 +0200 Subject: [PATCH] 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 --- src/index.js | 2 +- src/locale/pt-br.js | 2 +- test/display.test.js | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 434f6036..7e3af1c5 100644 --- a/src/index.js +++ b/src/index.js @@ -374,7 +374,7 @@ class Dayjs { } toJSON() { - return this.toISOString() + return this.isValid() ? this.toISOString() : null } toISOString() { diff --git a/src/locale/pt-br.js b/src/locale/pt-br.js index 5133a72f..286a5458 100644 --- a/src/locale/pt-br.js +++ b/src/locale/pt-br.js @@ -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}º`, diff --git a/test/display.test.js b/test/display.test.js index c8fe1935..6c546d92 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -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', () => {