Skip to content

Commit

Permalink
fix: Add WeekYear plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Mar 10, 2019
1 parent 6981ab6 commit a892608
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/locale/zh-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const locale = {
weekdaysMin: '日_一_二_三_四_五_六'.split('_'),
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
ordinal1: n => `${n}日`,
ordinal: (number, period) => {
switch (period) {
case 'W':
Expand Down
12 changes: 12 additions & 0 deletions src/plugin/weekYear/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default (o, c) => {
const proto = c.prototype
proto.weekYear = function () {
const month = this.month()
const weekOfYear = this.week()
const year = this.year()
if (weekOfYear === 1 && month === 11) {
return year + 1
}
return year
}
}
30 changes: 30 additions & 0 deletions test/plugin/weekYear.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import weekYear from '../../src/plugin/weekYear'
import weekOfYear from '../../src/plugin/weekOfYear'

dayjs.extend(weekYear)
dayjs.extend(weekOfYear)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('Week Year', () => {
const daySet = [
['2018-12-01', 2018],
['2018-12-30', 2019],
['2018-12-31', 2019],
['2019-01-01', 2019]
]
daySet.forEach((d) => {
const [day, result] = d
expect(dayjs(day).weekYear()).toBe(result)
expect(dayjs(day).weekYear()).toBe(moment(day).weekYear())
})
})
2 changes: 2 additions & 0 deletions types/plugin/weekOfYear.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ export = plugin
declare module 'dayjs' {
interface Dayjs {
week(): number

week(value : number): Dayjs
}
}
10 changes: 10 additions & 0 deletions types/plugin/weekYear.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
weekYear(): number
}
}

0 comments on commit a892608

Please sign in to comment.