Skip to content

Commit

Permalink
fix: Add isoWeeksInYear plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Mar 28, 2019
1 parent 67c3086 commit 2db8631
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/plugin/isoWeeksInYear/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.isoWeeksInYear = function () {
const isLeapYear = this.isLeapYear()
const last = this.endOf('y')
const day = last.day()
if (day === 4 || (isLeapYear && day === 5)) {
return 53
}
return 52
}
}
30 changes: 30 additions & 0 deletions test/plugin/isoWeeksInYear.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import isoWeeksInYear from '../../src/plugin/isoWeeksInYear'
import isLeapYear from '../../src/plugin/isLeapYear'

dayjs.extend(isoWeeksInYear)
dayjs.extend(isLeapYear)

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

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

it('isoWeeksInYear', () => {
expect(dayjs('2004').isoWeeksInYear()).toBe(53)
expect(dayjs('2005').isoWeeksInYear()).toBe(52)
expect(dayjs('2006').isoWeeksInYear()).toBe(52)
expect(dayjs('2007').isoWeeksInYear()).toBe(52)
expect(dayjs('2008').isoWeeksInYear()).toBe(52)
expect(dayjs('2009').isoWeeksInYear()).toBe(53)
expect(dayjs('2010').isoWeeksInYear()).toBe(52)
expect(dayjs('2011').isoWeeksInYear()).toBe(52)
expect(dayjs('2012').isoWeeksInYear()).toBe(52)
expect(dayjs('2013').isoWeeksInYear()).toBe(52)
expect(dayjs('2014').isoWeeksInYear()).toBe(52)
expect(dayjs('2015').isoWeeksInYear()).toBe(53)
})
10 changes: 10 additions & 0 deletions types/plugin/isoWeeksInYear.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 {
isoWeeksInYear(): number
}
}

0 comments on commit 2db8631

Please sign in to comment.