From 2db8631bd530a2d25528bf99d31ee678816936d1 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 28 Mar 2019 18:59:43 +0800 Subject: [PATCH] fix: Add isoWeeksInYear plugin --- src/plugin/isoWeeksInYear/index.js | 12 ++++++++++++ test/plugin/isoWeeksInYear.test.js | 30 ++++++++++++++++++++++++++++++ types/plugin/isoWeeksInYear.d.ts | 10 ++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/plugin/isoWeeksInYear/index.js create mode 100644 test/plugin/isoWeeksInYear.test.js create mode 100644 types/plugin/isoWeeksInYear.d.ts diff --git a/src/plugin/isoWeeksInYear/index.js b/src/plugin/isoWeeksInYear/index.js new file mode 100644 index 000000000..644f934fb --- /dev/null +++ b/src/plugin/isoWeeksInYear/index.js @@ -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 + } +} diff --git a/test/plugin/isoWeeksInYear.test.js b/test/plugin/isoWeeksInYear.test.js new file mode 100644 index 000000000..061120ca1 --- /dev/null +++ b/test/plugin/isoWeeksInYear.test.js @@ -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) +}) diff --git a/types/plugin/isoWeeksInYear.d.ts b/types/plugin/isoWeeksInYear.d.ts new file mode 100644 index 000000000..2bc02cddf --- /dev/null +++ b/types/plugin/isoWeeksInYear.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isoWeeksInYear(): number + } +}