Skip to content

Commit

Permalink
fix: Update LocaleData plugin to support dayjs.months dayjs.weekdays API
Browse files Browse the repository at this point in the history
fix #779
  • Loading branch information
iamkun committed Jan 19, 2020
1 parent 287fed6 commit 144c2ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default (o, c, dayjs) => { // locale needed later
}
return locale[target]
}
const getDayjsLocaleObject = () => dayjs.Ls[dayjs.locale()]
const localeData = function () {
return {
months: instance =>
Expand All @@ -26,14 +27,24 @@ export default (o, c, dayjs) => { // locale needed later
}

dayjs.localeData = () => {
const localeObject = dayjs.Ls[dayjs.locale()]
const localeObject = getDayjsLocaleObject()
return {
firstDayOfWeek: () => localeObject.weekStart || 0,
weekdays: () => localeObject.weekdays,
weekdaysShort: () => getShort(localeObject, 'weekdaysShort', 'weekdays', 3),
weekdaysMin: () => getShort(localeObject, 'weekdaysMin', 'weekdays', 2),
months: () => localeObject.months,
monthsShort: () => getShort(localeObject, 'monthsShort', 'months', 3)
weekdays: () => dayjs.weekdays(),
weekdaysShort: () => dayjs.weekdaysShort(),
weekdaysMin: () => dayjs.weekdaysMin(),
months: () => dayjs.months(),
monthsShort: () => dayjs.monthsShort()
}
}

dayjs.months = () => getDayjsLocaleObject().months

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

dayjs.weekdays = () => getDayjsLocaleObject().weekdays

dayjs.weekdaysShort = () => getShort(getDayjsLocaleObject(), 'weekdaysShort', 'weekdays', 3)

dayjs.weekdaysMin = () => getShort(getDayjsLocaleObject(), 'weekdaysMin', 'weekdays', 2)
}
13 changes: 13 additions & 0 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ it('Global localeData', () => {
expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin())
})
})


it('Listing the months and weekdays', () => {
['zh-cn', 'en'].forEach((lo) => {
dayjs.locale(lo)
moment.locale(lo)
expect(dayjs.months()).toEqual(moment.months())
expect(dayjs.monthsShort()).toEqual(moment.monthsShort())
expect(dayjs.weekdays()).toEqual(moment.weekdays())
expect(dayjs.weekdaysShort()).toEqual(moment.weekdaysShort())
expect(dayjs.weekdaysMin()).toEqual(moment.weekdaysMin())
})
})

0 comments on commit 144c2ae

Please sign in to comment.