Skip to content

Commit 8d63d88

Browse files
committed
fix: API .locale() with no argument should return current locale name string
1 parent 600d547 commit 8d63d88

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ class Dayjs {
346346
}
347347

348348
locale(preset, object) {
349+
if (!preset) return this.$L
349350
const that = this.clone()
350351
that.$L = parseLocale(preset, object, true)
351352
return that

test/locale.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import MockDate from 'mockdate'
2+
import moment from 'moment'
23
import dayjs from '../src'
34
import es from '../src/locale/es'
45

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

48+
it('get instance locale name', () => {
49+
expect(dayjs().locale()).toBe('en')
50+
expect(dayjs().locale()).toBe(moment().locale())
51+
expect(dayjs().locale('es').locale()).toBe('es')
52+
expect(dayjs().locale('es').locale()).toBe(moment().locale('es').locale())
53+
dayjs.locale(es)
54+
moment.locale('es')
55+
expect(dayjs().locale()).toBe('es')
56+
expect(dayjs().locale()).toBe(moment().locale())
57+
})
58+
4759
it('immutable instance locale', () => {
4860
dayjs.locale('en')
4961
const origin = dayjs('2018-4-28')

types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ declare namespace dayjs {
8585

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

88+
locale(): string
89+
8890
locale(preset: string | { name: string, [key: string]: any }, object?: { [key: string]: any }): Dayjs
8991
}
9092

0 commit comments

Comments
 (0)