Skip to content

Commit

Permalink
fix: LocaleData plugin supports locale order (#938)
Browse files Browse the repository at this point in the history
fix #936
  • Loading branch information
iamkun authored Jun 30, 2020
1 parent 72cdb20 commit 62f429d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

0 comments on commit 62f429d

Please sign in to comment.