Skip to content

Commit 62f429d

Browse files
authored
fix: LocaleData plugin supports locale order (#938)
fix #936
1 parent 72cdb20 commit 62f429d

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/plugin/localeData/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
export default (o, c, dayjs) => { // locale needed later
22
const proto = c.prototype
33
const getLocalePart = part => (part && (part.indexOf ? part : part.s))
4-
const getShort = (ins, target, full, num) => {
4+
const getShort = (ins, target, full, num, localeOrder) => {
55
const locale = ins.name ? ins : ins.$locale()
66
const targetLocale = getLocalePart(locale[target])
77
const fullLocale = getLocalePart(locale[full])
8-
return targetLocale || fullLocale.map(f => f.substr(0, num))
8+
const result = targetLocale || fullLocale.map(f => f.substr(0, num))
9+
if (!localeOrder) return result
10+
const { weekStart } = locale || 0
11+
return result.map((_, index) => (result[(index + weekStart) % 7]))
912
}
1013
const getDayjsLocaleObject = () => dayjs.Ls[dayjs.locale()]
1114
const localeData = function () {
@@ -42,9 +45,9 @@ export default (o, c, dayjs) => { // locale needed later
4245

4346
dayjs.monthsShort = () => getShort(getDayjsLocaleObject(), 'monthsShort', 'months', 3)
4447

45-
dayjs.weekdays = () => getDayjsLocaleObject().weekdays
48+
dayjs.weekdays = localeOrder => getShort(getDayjsLocaleObject(), 'weekdays', null, null, localeOrder)
4649

47-
dayjs.weekdaysShort = () => getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3)
50+
dayjs.weekdaysShort = localeOrder => getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3, localeOrder)
4851

49-
dayjs.weekdaysMin = () => getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2)
52+
dayjs.weekdaysMin = localeOrder => getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2, localeOrder)
5053
}

test/plugin/localeData.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ it('Month function', () => {
7777
expect(dayjs.months()).toEqual(moment.months())
7878
expect(dayjs.monthsShort()).toEqual(moment.monthsShort())
7979
})
80+
81+
it('Locale order', () => {
82+
dayjs.locale('fr')
83+
moment.locale('fr')
84+
expect(dayjs.weekdays(true)).toEqual(moment.weekdays(true))
85+
expect(dayjs.weekdaysShort(true)).toEqual(moment.weekdaysShort(true))
86+
expect(dayjs.weekdaysMin(true)).toEqual(moment.weekdaysMin(true))
87+
expect(dayjs.weekdays()).not.toEqual(dayjs.weekdays(true))
88+
})

0 commit comments

Comments
 (0)