Skip to content

Commit 2db8631

Browse files
committedMar 28, 2019
fix: Add isoWeeksInYear plugin
1 parent 67c3086 commit 2db8631

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
 

‎src/plugin/isoWeeksInYear/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default (o, c) => {
2+
const proto = c.prototype
3+
proto.isoWeeksInYear = function () {
4+
const isLeapYear = this.isLeapYear()
5+
const last = this.endOf('y')
6+
const day = last.day()
7+
if (day === 4 || (isLeapYear && day === 5)) {
8+
return 53
9+
}
10+
return 52
11+
}
12+
}

‎test/plugin/isoWeeksInYear.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import MockDate from 'mockdate'
2+
import dayjs from '../../src'
3+
import isoWeeksInYear from '../../src/plugin/isoWeeksInYear'
4+
import isLeapYear from '../../src/plugin/isLeapYear'
5+
6+
dayjs.extend(isoWeeksInYear)
7+
dayjs.extend(isLeapYear)
8+
9+
beforeEach(() => {
10+
MockDate.set(new Date())
11+
})
12+
13+
afterEach(() => {
14+
MockDate.reset()
15+
})
16+
17+
it('isoWeeksInYear', () => {
18+
expect(dayjs('2004').isoWeeksInYear()).toBe(53)
19+
expect(dayjs('2005').isoWeeksInYear()).toBe(52)
20+
expect(dayjs('2006').isoWeeksInYear()).toBe(52)
21+
expect(dayjs('2007').isoWeeksInYear()).toBe(52)
22+
expect(dayjs('2008').isoWeeksInYear()).toBe(52)
23+
expect(dayjs('2009').isoWeeksInYear()).toBe(53)
24+
expect(dayjs('2010').isoWeeksInYear()).toBe(52)
25+
expect(dayjs('2011').isoWeeksInYear()).toBe(52)
26+
expect(dayjs('2012').isoWeeksInYear()).toBe(52)
27+
expect(dayjs('2013').isoWeeksInYear()).toBe(52)
28+
expect(dayjs('2014').isoWeeksInYear()).toBe(52)
29+
expect(dayjs('2015').isoWeeksInYear()).toBe(53)
30+
})

‎types/plugin/isoWeeksInYear.d.ts

Lines changed: 10 additions & 0 deletions
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+
isoWeeksInYear(): number
9+
}
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.