Skip to content

Commit a892608

Browse files
committed
fix: Add WeekYear plugin
1 parent 6981ab6 commit a892608

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

src/locale/zh-cn.js

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const locale = {
77
weekdaysMin: '日_一_二_三_四_五_六'.split('_'),
88
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
99
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
10-
ordinal1: n => `${n}日`,
1110
ordinal: (number, period) => {
1211
switch (period) {
1312
case 'W':

src/plugin/weekYear/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default (o, c) => {
2+
const proto = c.prototype
3+
proto.weekYear = function () {
4+
const month = this.month()
5+
const weekOfYear = this.week()
6+
const year = this.year()
7+
if (weekOfYear === 1 && month === 11) {
8+
return year + 1
9+
}
10+
return year
11+
}
12+
}

test/plugin/weekYear.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import moment from 'moment'
2+
import MockDate from 'mockdate'
3+
import dayjs from '../../src'
4+
import weekYear from '../../src/plugin/weekYear'
5+
import weekOfYear from '../../src/plugin/weekOfYear'
6+
7+
dayjs.extend(weekYear)
8+
dayjs.extend(weekOfYear)
9+
10+
beforeEach(() => {
11+
MockDate.set(new Date())
12+
})
13+
14+
afterEach(() => {
15+
MockDate.reset()
16+
})
17+
18+
it('Week Year', () => {
19+
const daySet = [
20+
['2018-12-01', 2018],
21+
['2018-12-30', 2019],
22+
['2018-12-31', 2019],
23+
['2019-01-01', 2019]
24+
]
25+
daySet.forEach((d) => {
26+
const [day, result] = d
27+
expect(dayjs(day).weekYear()).toBe(result)
28+
expect(dayjs(day).weekYear()).toBe(moment(day).weekYear())
29+
})
30+
})

types/plugin/weekOfYear.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ export = plugin
66
declare module 'dayjs' {
77
interface Dayjs {
88
week(): number
9+
10+
week(value : number): Dayjs
911
}
1012
}

types/plugin/weekYear.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
weekYear(): number
9+
}
10+
}

0 commit comments

Comments
 (0)