Skip to content

Commit

Permalink
chore: Update AdvancedFormat plugin support wo format string fro week…
Browse files Browse the repository at this point in the history
… of the year
  • Loading branch information
iamkun committed Mar 10, 2019
1 parent 1ddf633 commit 6981ab6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/locale/zh-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 4 additions & 3 deletions src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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'))
})

0 comments on commit 6981ab6

Please sign in to comment.