Skip to content
Permalink
Browse files

fix: Add .add('quarter') .startOf('quarter') through plugin quarterOf…

…Year

fix #537, fix #531
  • Loading branch information...
iamkun committed Mar 21, 2019
1 parent 1ac9e1e commit dde39e9660a7e9a511ab3ed58f151559b96e930b
@@ -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)
}
}
@@ -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$/, '')
}
@@ -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())
})
@@ -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('')
})

@@ -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

@@ -1,10 +1,20 @@
import { PluginFunc } from 'dayjs'
import { PluginFunc, QUnitType } from 'dayjs'

declare const plugin: PluginFunc
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
}
}

0 comments on commit dde39e9

Please sign in to comment.
You can’t perform that action at this time.