From 759456a2dabe164b4aea71afe0b34e7be7a01f89 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 24 Jun 2020 16:36:30 +0800 Subject: [PATCH] fix: LocaleData plugin supports locale order fix #936 --- src/plugin/localeData/index.js | 13 ++++++++----- test/plugin/localeData.test.js | 9 +++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/plugin/localeData/index.js b/src/plugin/localeData/index.js index f478d5a41..636f1557e 100644 --- a/src/plugin/localeData/index.js +++ b/src/plugin/localeData/index.js @@ -1,11 +1,14 @@ export default (o, c, dayjs) => { // locale needed later const proto = c.prototype const getLocalePart = part => (part && (part.indexOf ? part : part.s)) - const getShort = (ins, target, full, num) => { + const getShort = (ins, target, full, num, localeOrder) => { const locale = ins.name ? ins : ins.$locale() const targetLocale = getLocalePart(locale[target]) const fullLocale = getLocalePart(locale[full]) - return targetLocale || fullLocale.map(f => f.substr(0, num)) + const result = targetLocale || fullLocale.map(f => f.substr(0, num)) + if (!localeOrder) return result + const { weekStart } = locale || 0 + return result.map((_, index) => (result[(index + weekStart) % 7])) } const getDayjsLocaleObject = () => dayjs.Ls[dayjs.locale()] const localeData = function () { @@ -42,9 +45,9 @@ export default (o, c, dayjs) => { // locale needed later dayjs.monthsShort = () => getShort(getDayjsLocaleObject(), 'monthsShort', 'months', 3) - dayjs.weekdays = () => getDayjsLocaleObject().weekdays + dayjs.weekdays = localeOrder => getShort(getDayjsLocaleObject(), 'weekdays', null, null, localeOrder) - dayjs.weekdaysShort = () => getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3) + dayjs.weekdaysShort = localeOrder => getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3, localeOrder) - dayjs.weekdaysMin = () => getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2) + dayjs.weekdaysMin = localeOrder => getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2, localeOrder) } diff --git a/test/plugin/localeData.test.js b/test/plugin/localeData.test.js index 6dac0a97a..290f0c0e9 100644 --- a/test/plugin/localeData.test.js +++ b/test/plugin/localeData.test.js @@ -77,3 +77,12 @@ it('Month function', () => { expect(dayjs.months()).toEqual(moment.months()) expect(dayjs.monthsShort()).toEqual(moment.monthsShort()) }) + +it('Locale order', () => { + dayjs.locale('fr') + moment.locale('fr') + expect(dayjs.weekdays(true)).toEqual(moment.weekdays(true)) + expect(dayjs.weekdaysShort(true)).toEqual(moment.weekdaysShort(true)) + expect(dayjs.weekdaysMin(true)).toEqual(moment.weekdaysMin(true)) + expect(dayjs.weekdays()).not.toEqual(dayjs.weekdays(true)) +})