Skip to content

Commit

Permalink
chore: Update localeData plugin logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Aug 27, 2019
1 parent 0e45b0a commit 4c862f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/locale/en.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// We don't need weekdaysShort, weekdaysMin, monthsShort in en.js locale
export default {
name: 'en',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_')
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_')
}
15 changes: 11 additions & 4 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
export default (o, c, dayjs) => { // locale needed later
const proto = c.prototype
const getShort = (ins, target, full, num) => {
const locale = ins.$locale()
if (!locale[target]) {
return locale[full].map(f => f.substr(0, num))
}
return locale[target]
}
const localeData = function () {
return {
months: instance =>
(instance ? instance.format('MMMM') : this.$locale().months),
(instance ? instance.format('MMMM') : getShort(this, 'months')),
monthsShort: instance =>
(instance ? instance.format('MMM') : this.$locale().monthsShort),
(instance ? instance.format('MMM') : getShort(this, 'monthsShort', 'months', 3)),
firstDayOfWeek: () => this.$locale().weekStart || 0,
weekdaysMin: instance =>
(instance ? instance.format('dd') : this.$locale().weekdaysMin),
(instance ? instance.format('dd') : getShort(this, 'weekdaysMin', 'weekdays', 2)),
weekdaysShort: instance =>
(instance ? instance.format('ddd') : this.$locale().weekdaysShort)
(instance ? instance.format('ddd') : getShort(this, 'weekdaysShort', 'weekdays', 3))
}
}
proto.localeData = function () {
Expand Down
1 change: 1 addition & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ it('As Javascript Date -> toDate', () => {

it('As JSON -> toJSON', () => {
expect(dayjs().toJSON()).toBe(moment().toJSON())
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toJSON()).toBe(moment('otherString').toJSON())
expect(dayjs('otherString').toJSON()).toBe(null)
})
Expand Down

0 comments on commit 4c862f6

Please sign in to comment.