From 4c862f682e763c5ad27ebc2492f7a40d0030ea4f Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 27 Aug 2019 12:56:29 +0800 Subject: [PATCH] chore: Update localeData plugin logic --- src/locale/en.js | 6 ++---- src/plugin/localeData/index.js | 15 +++++++++++---- test/display.test.js | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/locale/en.js b/src/locale/en.js index 76cc04fbd..a9eb16369 100644 --- a/src/locale/en.js +++ b/src/locale/en.js @@ -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('_') } diff --git a/src/plugin/localeData/index.js b/src/plugin/localeData/index.js index c49a1117e..c8008a22d 100644 --- a/src/plugin/localeData/index.js +++ b/src/plugin/localeData/index.js @@ -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 () { diff --git a/test/display.test.js b/test/display.test.js index 6c546d923..42975c3cb 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -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) })