Skip to content

Commit

Permalink
fix: Fix unsupported locale fallback to previous one (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Mar 6, 2020
1 parent b54150e commit 4868715
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
25 changes: 20 additions & 5 deletions test/locale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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')
})

0 comments on commit 4868715

Please sign in to comment.