From 790cd1a2afe8bc65cf39d501efba660a721361d9 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 22 May 2019 10:20:04 +0800 Subject: [PATCH] fix: Fix dayjs.locale() returns current global locale (#602) * fix: Fix dayjs.locale() returns current global locale * update localeData plugin --- src/index.js | 4 ++-- src/plugin/localeData/index.js | 9 ++++++++- test/locale.test.js | 10 ++++++++++ test/plugin/localeData.test.js | 16 +++++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 03359c2d9..434f60362 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,7 @@ const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-def const parseLocale = (preset, object, isLocal) => { let l - if (!preset) return null + if (!preset) return L if (typeof preset === 'string') { if (Ls[preset]) { l = preset @@ -66,7 +66,7 @@ const parseDate = (cfg) => { class Dayjs { constructor(cfg) { - this.$L = this.$L || parseLocale(cfg.locale, null, true) || L + this.$L = this.$L || parseLocale(cfg.locale, null, true) this.parse(cfg) // for plugin } diff --git a/src/plugin/localeData/index.js b/src/plugin/localeData/index.js index b85a838a4..dae8f7b65 100644 --- a/src/plugin/localeData/index.js +++ b/src/plugin/localeData/index.js @@ -1,4 +1,4 @@ -export default (o, c) => { // locale needed later +export default (o, c, dayjs) => { // locale needed later const proto = c.prototype const localeData = function () { return { @@ -12,5 +12,12 @@ export default (o, c) => { // locale needed later proto.localeData = function () { return localeData.bind(this)() } + + dayjs.localeData = () => { + const localeObject = dayjs.Ls[dayjs.locale()] + return { + firstDayOfWeek: () => localeObject.weekStart || 0 + } + } } diff --git a/test/locale.test.js b/test/locale.test.js index 8438bd095..1ac0aa9c2 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -110,4 +110,14 @@ describe('Instance locale inheritance', () => { expect(esDayjs.add(1, 'minute').format(format)) .toBe('sábado 28, Abril') }) + + it('dayjs.locale() returns locale name', () => { + dayjs.locale(es) + moment.locale('es') + expect(dayjs.locale()).toBe(moment.locale()) + + dayjs.locale('en') + moment.locale('en') + expect(dayjs.locale()).toBe(moment.locale()) + }) }) diff --git a/test/plugin/localeData.test.js b/test/plugin/localeData.test.js index 5e5e1cb95..7b42a69bf 100644 --- a/test/plugin/localeData.test.js +++ b/test/plugin/localeData.test.js @@ -2,6 +2,7 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' import localeData from '../../src/plugin/localeData' +import '../../src/locale/zh-cn' dayjs.extend(localeData) @@ -13,7 +14,7 @@ afterEach(() => { MockDate.reset() }) -it('localeData', () => { +it('instance localeData', () => { const d = dayjs() const m = moment() const dayjsLocaleData = dayjs().localeData() @@ -24,3 +25,16 @@ it('localeData', () => { expect(dayjsLocaleData.weekdaysMin(d)).toBe(momentLocaleData.weekdaysMin(m)) expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m)) }) + +it('global localeData', () => { + dayjs.locale('zh-cn') + moment.locale('zh-cn') + let dayjsLocaleData = dayjs.localeData() + let momentLocaleData = moment.localeData() + expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek()) + dayjs.locale('en') + moment.locale('en') + dayjsLocaleData = dayjs.localeData() + momentLocaleData = moment.localeData() + expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek()) +})