Skip to content

Commit

Permalink
fix: Update LocaleData plugin to support dayjs.localeData().weekdays(…
Browse files Browse the repository at this point in the history
…) API

fix #779
  • Loading branch information
iamkun committed Jan 19, 2020
1 parent a4979c7 commit 287fed6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default (o, c, dayjs) => { // locale needed later
const proto = c.prototype
const getShort = (ins, target, full, num) => {
const locale = ins.$locale()
const locale = ins.name ? ins : ins.$locale()
if (!locale[target]) {
return locale[full].map(f => f.substr(0, num))
}
Expand All @@ -28,7 +28,12 @@ export default (o, c, dayjs) => { // locale needed later
dayjs.localeData = () => {
const localeObject = dayjs.Ls[dayjs.locale()]
return {
firstDayOfWeek: () => localeObject.weekStart || 0
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)
}
}
}
22 changes: 12 additions & 10 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ it('Instance localeData', () => {


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())
['zh-cn', 'en'].forEach((lo) => {
dayjs.locale(lo)
moment.locale(lo)
const dayjsLocaleData = dayjs.localeData()
const momentLocaleData = moment.localeData()
expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek())
expect(dayjsLocaleData.months()).toEqual(momentLocaleData.months())
expect(dayjsLocaleData.monthsShort()).toEqual(momentLocaleData.monthsShort())
expect(dayjsLocaleData.weekdays()).toEqual(momentLocaleData.weekdays())
expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort())
expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin())
})
})

0 comments on commit 287fed6

Please sign in to comment.