From 6981ab6decd8556d6c43042eb3ad88a6ee4151f7 Mon Sep 17 00:00:00 2001 From: iamkun Date: Sun, 10 Mar 2019 18:13:47 +0800 Subject: [PATCH] chore: Update AdvancedFormat plugin support wo format string fro week of the year --- src/locale/zh-cn.js | 10 +++++++++- src/plugin/advancedFormat/index.js | 7 ++++--- test/plugin/advancedFormat.test.js | 10 ++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js index bc28c7edf..83b92acd4 100644 --- a/src/locale/zh-cn.js +++ b/src/locale/zh-cn.js @@ -7,7 +7,15 @@ const locale = { weekdaysMin: '日_一_二_三_四_五_六'.split('_'), months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'), - ordinal: n => `${n}日`, + ordinal1: n => `${n}日`, + ordinal: (number, period) => { + switch (period) { + case 'W': + return `${number}周` + default: + return `${number}日` + } + }, weekStart: 1, formats: { LT: 'HH:mm', diff --git a/src/plugin/advancedFormat/index.js b/src/plugin/advancedFormat/index.js index 854f2b73b..9a1e30eb2 100644 --- a/src/plugin/advancedFormat/index.js +++ b/src/plugin/advancedFormat/index.js @@ -13,13 +13,14 @@ export default (o, c, d) => { // locale needed later const locale = this.$locale() const utils = this.$utils() const str = formatStr || FORMAT_DEFAULT - const result = str.replace(/Q|Do|X|x|k{1,2}|S/g, (match) => { + const result = str.replace(/Q|wo|Do|X|x|k{1,2}|S/g, (match) => { switch (match) { case 'Q': return Math.ceil((this.$M + 1) / 3) - case 'Do': { + case 'Do': return locale.ordinal(this.$D) - } + case 'wo': + return locale.ordinal(this.week(), 'W') // W for week case 'k': case 'kk': return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0') diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index 985ff50be..37c185576 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -2,7 +2,10 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' import advancedFormat from '../../src/plugin/advancedFormat' +import weekOfYear from '../../src/plugin/weekOfYear' +import '../../src/locale/zh-cn' +dayjs.extend(weekOfYear) dayjs.extend(advancedFormat) beforeEach(() => { @@ -65,3 +68,10 @@ it('Format Hour k kk 24-hour 1 - 24', () => { expect(dayjs(d).format('kk')).toBe('23') expect(dayjs(d).format('kk')).toBe(moment(d).format('kk')) }) + +it('Format Week of Year wo', () => { + const d = '2018-12-01' + expect(dayjs(d).format('wo')).toBe(moment(d).format('wo')) + expect(dayjs(d).locale('zh-cn').format('wo')) + .toBe(moment(d).locale('zh-cn').format('wo')) +})