From 8d63d885fdd72b51bfdf01cbd6f45c773f71363a Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 7 Mar 2019 13:16:14 +0800 Subject: [PATCH] fix: API .locale() with no argument should return current locale name string --- src/index.js | 1 + test/locale.test.js | 12 ++++++++++++ types/index.d.ts | 2 ++ 3 files changed, 15 insertions(+) diff --git a/src/index.js b/src/index.js index 4f96f9c91..413b5becc 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/test/locale.test.js b/test/locale.test.js index 06560b6ba..8438bd095 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -1,4 +1,5 @@ import MockDate from 'mockdate' +import moment from 'moment' import dayjs from '../src' import es from '../src/locale/es' @@ -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') diff --git a/types/index.d.ts b/types/index.d.ts index 22ea11726..d6f547d22 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 }