Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 dde39e9
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
31 changes: 30 additions & 1 deletion 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)
}
}
3 changes: 2 additions & 1 deletion src/utils.js
Expand Up @@ -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$/, '')
}
Expand Down
28 changes: 27 additions & 1 deletion 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'

Expand All @@ -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)
Expand All @@ -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())
})
3 changes: 3 additions & 0 deletions test/utils.test.js
Expand Up @@ -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('')
})

Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion types/plugin/quarterOfYear.d.ts
@@ -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.