From dde39e9660a7e9a511ab3ed58f151559b96e930b Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 21 Mar 2019 12:28:39 +0800 Subject: [PATCH] fix: Add .add('quarter') .startOf('quarter') through plugin quarterOfYear fix #537, fix #531 --- src/plugin/quarterOfYear/index.js | 31 ++++++++++++++++++++++++++++++- src/utils.js | 3 ++- test/plugin/quarterOfYear.test.js | 28 +++++++++++++++++++++++++++- test/utils.test.js | 3 +++ types/index.d.ts | 3 ++- types/plugin/quarterOfYear.d.ts | 12 +++++++++++- 6 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/plugin/quarterOfYear/index.js b/src/plugin/quarterOfYear/index.js index 755d608b9..0ec48906b 100644 --- a/src/plugin/quarterOfYear/index.js +++ b/src/plugin/quarterOfYear/index.js @@ -1,6 +1,35 @@ +import { Q, M, D } from '../../constant' + export default (o, c) => { const proto = c.prototype - proto.quarter = function () { + proto.quarter = function (quarter) { + if (!this.$utils().u(quarter)) { + return this.add((quarter - 1) * 3, M) + } return Math.ceil((this.month() + 1) / 3) } + + const oldAdd = proto.add + proto.add = function (number, units) { + number = Number(number) // eslint-disable-line no-param-reassign + const unit = this.$utils().p(units) + if (unit === Q) { + return this.add(number * 3, M) + } + return oldAdd.bind(this)(number, units) + } + + const oldStartOf = proto.startOf + proto.startOf = function (units, startOf) { + const utils = this.$utils() + const isStartOf = !utils.u(startOf) ? startOf : true + const unit = utils.p(units) + if (unit === Q) { + const quarter = this.quarter() - 1 + return isStartOf ? this.month(quarter * 3) + .startOf(M).startOf(D) : + this.month((quarter * 3) + 2).endOf(M).endOf(D) + } + return oldStartOf.bind(this)(units, startOf) + } } diff --git a/src/utils.js b/src/utils.js index cd98b07cd..01921d46e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -35,7 +35,8 @@ const prettyUnit = (u) => { h: C.H, m: C.MIN, s: C.S, - ms: C.MS + ms: C.MS, + Q: C.Q } return special[u] || String(u || '').toLowerCase().replace(/s$/, '') } diff --git a/test/plugin/quarterOfYear.test.js b/test/plugin/quarterOfYear.test.js index 857dd4d9a..a1fe0e360 100644 --- a/test/plugin/quarterOfYear.test.js +++ b/test/plugin/quarterOfYear.test.js @@ -1,4 +1,5 @@ import MockDate from 'mockdate' +import moment from 'moment' import dayjs from '../../src' import quarterOfYear from '../../src/plugin/quarterOfYear' @@ -12,7 +13,7 @@ afterEach(() => { MockDate.reset() }) -it('QuarterOfYear', () => { +it('get QuarterOfYear', () => { expect(dayjs('2013-01-01T00:00:00.000').quarter()).toBe(1) expect(dayjs('2013-04-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(1) expect(dayjs('2013-04-01T00:00:00.000').quarter()).toBe(2) @@ -22,3 +23,28 @@ it('QuarterOfYear', () => { expect(dayjs('2013-10-01T00:00:00.000').quarter()).toBe(4) expect(dayjs('2014-01-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(4) }) + +it('set QuarterOfYear', () => { + const d1 = '2013-01-01T00:00:00.000' + expect(dayjs(d1).quarter(2).valueOf()).toBe(1364745600000) + expect(dayjs(d1).quarter(2).format()) + .toBe(moment(d1).quarter(2).format()) + const d2 = '2013-02-05T05:06:07.000' + expect(dayjs(d2).quarter(2).valueOf()).toBe(1367701567000) + expect(dayjs(d2).quarter(2).format()) + .toBe(moment(d2).quarter(2).format()) +}) + +it('add subtract quarter', () => { + expect(dayjs().add(2, 'quarter').format()) + .toBe(moment().add(2, 'quarter').format()) + expect(dayjs().subtract(2, 'quarter').format()) + .toBe(moment().subtract(2, 'quarter').format()) +}) + +it('startOf endOf quarter', () => { + expect(dayjs().startOf('quarter').format()) + .toBe(moment().startOf('quarter').format()) + expect(dayjs().endOf('quarter').format()) + .toBe(moment().endOf('quarter').format()) +}) diff --git a/test/utils.test.js b/test/utils.test.js index 5c07f7796..f49dcf50e 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -8,6 +8,9 @@ it('PrettyUnit', () => { expect(prettyUnit('Days')).toBe('day') expect(prettyUnit('days')).toBe('day') expect(prettyUnit('day')).toBe('day') + expect(prettyUnit('Q')).toBe('quarter') + expect(prettyUnit('quarter')).toBe('quarter') + expect(prettyUnit('quarters')).toBe('quarter') expect(prettyUnit()).toBe('') }) diff --git a/types/index.d.ts b/types/index.d.ts index a04c66eaa..f32c4c003 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,6 +10,7 @@ declare namespace dayjs { export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date' | UnitTypeShort; export type OpUnitType = UnitType | "week" | 'w'; + export type QUnitType = UnitType | "quarter" | 'Q'; class Dayjs { constructor (config?: ConfigType) @@ -62,7 +63,7 @@ declare namespace dayjs { format(template?: string): string - diff(date: ConfigType, unit: OpUnitType | 'quarter', float?: boolean): number + diff(date: ConfigType, unit: QUnitType, float?: boolean): number valueOf(): number diff --git a/types/plugin/quarterOfYear.d.ts b/types/plugin/quarterOfYear.d.ts index e8aad14b8..f77f032c0 100644 --- a/types/plugin/quarterOfYear.d.ts +++ b/types/plugin/quarterOfYear.d.ts @@ -1,4 +1,4 @@ -import { PluginFunc } from 'dayjs' +import { PluginFunc, QUnitType } from 'dayjs' declare const plugin: PluginFunc export = plugin @@ -6,5 +6,15 @@ export = plugin declare module 'dayjs' { interface Dayjs { quarter(): number + + quarter(quarter: number): Dayjs + + add(value: number, unit: QUnitType): Dayjs + + subtract(value: number, unit: QUnitType): Dayjs + + startOf(unit: QUnitType): Dayjs + + endOf(unit: QUnitType): Dayjs } }