Skip to content

Commit

Permalink
fix: API .locale() with no argument should return current locale name…
Browse files Browse the repository at this point in the history
… string
  • Loading branch information
iamkun committed Mar 7, 2019
1 parent 600d547 commit 8d63d88
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ class Dayjs {
}

locale(preset, object) {
if (!preset) return this.$L
const that = this.clone()
that.$L = parseLocale(preset, object, true)
return that
Expand Down
12 changes: 12 additions & 0 deletions test/locale.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../src'
import es from '../src/locale/es'

Expand Down Expand Up @@ -44,6 +45,17 @@ it('set global locale', () => {
.toBe('Saturday 28, April')
})

it('get instance locale name', () => {
expect(dayjs().locale()).toBe('en')
expect(dayjs().locale()).toBe(moment().locale())
expect(dayjs().locale('es').locale()).toBe('es')
expect(dayjs().locale('es').locale()).toBe(moment().locale('es').locale())
dayjs.locale(es)
moment.locale('es')
expect(dayjs().locale()).toBe('es')
expect(dayjs().locale()).toBe(moment().locale())
})

it('immutable instance locale', () => {
dayjs.locale('en')
const origin = dayjs('2018-4-28')
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ declare namespace dayjs {

isAfter(date: ConfigType, unit?: OpUnitType): boolean

locale(): string

locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }): Dayjs
}

Expand Down

0 comments on commit 8d63d88

Please sign in to comment.