From 48687152cf5bee6a4c1b8ceea4bda8b9bab9be10 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 6 Mar 2020 11:37:06 +0800 Subject: [PATCH] fix: Fix unsupported locale fallback to previous one (#819) --- src/index.js | 4 ++-- test/locale.test.js | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 9c4b93c12..8d0863559 100644 --- a/src/index.js +++ b/src/index.js @@ -24,8 +24,8 @@ const parseLocale = (preset, object, isLocal) => { Ls[name] = preset l = name } - if (!isLocal) L = l - return l + if (!isLocal && l) L = l + return l || (!isLocal && L) } const dayjs = (date, c, pl) => { diff --git a/test/locale.test.js b/test/locale.test.js index 48e1f5666..7faab1d08 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -12,6 +12,7 @@ afterEach(() => { }) const format = 'dddd D, MMMM' +const NOT_SUPPORTED_LOCALE_STRING = 'not_supported_locale_string' it('Uses spanish locale through constructor', () => { // not recommend expect(dayjs('2018-4-28', { locale: es }) @@ -123,10 +124,24 @@ describe('Instance locale inheritance', () => { }) -it('Not supported locale string fallback to previous one', () => { +it('Not supported locale string fallback to previous one (instance)', () => { const D = dayjs() - const DFormat = D.format() - const D2 = D.locale('not_supported_locale_string') - const D2Format = D2.format() - expect(DFormat).toBe(D2Format) + expect(D.locale()).toBe('en') + const D2 = D.locale(NOT_SUPPORTED_LOCALE_STRING) + expect(D2.locale()).toBe('en') + expect(D2.format()).toBe(D.format()) + const D3 = D2.locale('es') + expect(D3.locale()).toBe('es') + const D4 = D3.locale(NOT_SUPPORTED_LOCALE_STRING) + expect(D4.locale()).toBe('es') +}) + +it('Not supported locale string fallback to previous one (global)', () => { + expect(dayjs().locale()).toBe('en') + dayjs.locale(NOT_SUPPORTED_LOCALE_STRING) + expect(dayjs().locale()).toBe('en') + dayjs.locale('es') + expect(dayjs().locale()).toBe('es') + dayjs.locale(NOT_SUPPORTED_LOCALE_STRING) + expect(dayjs().locale()).toBe('es') })