From a8926082ee11979866e1328214443ae1823f5f07 Mon Sep 17 00:00:00 2001 From: iamkun Date: Sun, 10 Mar 2019 18:32:14 +0800 Subject: [PATCH] fix: Add WeekYear plugin --- src/locale/zh-cn.js | 1 - src/plugin/weekYear/index.js | 12 ++++++++++++ test/plugin/weekYear.test.js | 30 ++++++++++++++++++++++++++++++ types/plugin/weekOfYear.d.ts | 2 ++ types/plugin/weekYear.d.ts | 10 ++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/plugin/weekYear/index.js create mode 100644 test/plugin/weekYear.test.js create mode 100644 types/plugin/weekYear.d.ts diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js index 83b92acd4..e3e754432 100644 --- a/src/locale/zh-cn.js +++ b/src/locale/zh-cn.js @@ -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': diff --git a/src/plugin/weekYear/index.js b/src/plugin/weekYear/index.js new file mode 100644 index 000000000..1fa7577b7 --- /dev/null +++ b/src/plugin/weekYear/index.js @@ -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 + } +} diff --git a/test/plugin/weekYear.test.js b/test/plugin/weekYear.test.js new file mode 100644 index 000000000..a23709e50 --- /dev/null +++ b/test/plugin/weekYear.test.js @@ -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()) + }) +}) diff --git a/types/plugin/weekOfYear.d.ts b/types/plugin/weekOfYear.d.ts index 360219556..d98801455 100644 --- a/types/plugin/weekOfYear.d.ts +++ b/types/plugin/weekOfYear.d.ts @@ -6,5 +6,7 @@ export = plugin declare module 'dayjs' { interface Dayjs { week(): number + + week(value : number): Dayjs } } diff --git a/types/plugin/weekYear.d.ts b/types/plugin/weekYear.d.ts new file mode 100644 index 000000000..df2533123 --- /dev/null +++ b/types/plugin/weekYear.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + weekYear(): number + } +}