Skip to content

Commit dde39e9

Browse files
committed
fix: Add .add('quarter') .startOf('quarter') through plugin quarterOfYear
fix #537, fix #531
1 parent 1ac9e1e commit dde39e9

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

src/plugin/quarterOfYear/index.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
1+
import { Q, M, D } from '../../constant'
2+
13
export default (o, c) => {
24
const proto = c.prototype
3-
proto.quarter = function () {
5+
proto.quarter = function (quarter) {
6+
if (!this.$utils().u(quarter)) {
7+
return this.add((quarter - 1) * 3, M)
8+
}
49
return Math.ceil((this.month() + 1) / 3)
510
}
11+
12+
const oldAdd = proto.add
13+
proto.add = function (number, units) {
14+
number = Number(number) // eslint-disable-line no-param-reassign
15+
const unit = this.$utils().p(units)
16+
if (unit === Q) {
17+
return this.add(number * 3, M)
18+
}
19+
return oldAdd.bind(this)(number, units)
20+
}
21+
22+
const oldStartOf = proto.startOf
23+
proto.startOf = function (units, startOf) {
24+
const utils = this.$utils()
25+
const isStartOf = !utils.u(startOf) ? startOf : true
26+
const unit = utils.p(units)
27+
if (unit === Q) {
28+
const quarter = this.quarter() - 1
29+
return isStartOf ? this.month(quarter * 3)
30+
.startOf(M).startOf(D) :
31+
this.month((quarter * 3) + 2).endOf(M).endOf(D)
32+
}
33+
return oldStartOf.bind(this)(units, startOf)
34+
}
635
}

src/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const prettyUnit = (u) => {
3535
h: C.H,
3636
m: C.MIN,
3737
s: C.S,
38-
ms: C.MS
38+
ms: C.MS,
39+
Q: C.Q
3940
}
4041
return special[u] || String(u || '').toLowerCase().replace(/s$/, '')
4142
}

test/plugin/quarterOfYear.test.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import MockDate from 'mockdate'
2+
import moment from 'moment'
23
import dayjs from '../../src'
34
import quarterOfYear from '../../src/plugin/quarterOfYear'
45

@@ -12,7 +13,7 @@ afterEach(() => {
1213
MockDate.reset()
1314
})
1415

15-
it('QuarterOfYear', () => {
16+
it('get QuarterOfYear', () => {
1617
expect(dayjs('2013-01-01T00:00:00.000').quarter()).toBe(1)
1718
expect(dayjs('2013-04-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(1)
1819
expect(dayjs('2013-04-01T00:00:00.000').quarter()).toBe(2)
@@ -22,3 +23,28 @@ it('QuarterOfYear', () => {
2223
expect(dayjs('2013-10-01T00:00:00.000').quarter()).toBe(4)
2324
expect(dayjs('2014-01-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(4)
2425
})
26+
27+
it('set QuarterOfYear', () => {
28+
const d1 = '2013-01-01T00:00:00.000'
29+
expect(dayjs(d1).quarter(2).valueOf()).toBe(1364745600000)
30+
expect(dayjs(d1).quarter(2).format())
31+
.toBe(moment(d1).quarter(2).format())
32+
const d2 = '2013-02-05T05:06:07.000'
33+
expect(dayjs(d2).quarter(2).valueOf()).toBe(1367701567000)
34+
expect(dayjs(d2).quarter(2).format())
35+
.toBe(moment(d2).quarter(2).format())
36+
})
37+
38+
it('add subtract quarter', () => {
39+
expect(dayjs().add(2, 'quarter').format())
40+
.toBe(moment().add(2, 'quarter').format())
41+
expect(dayjs().subtract(2, 'quarter').format())
42+
.toBe(moment().subtract(2, 'quarter').format())
43+
})
44+
45+
it('startOf endOf quarter', () => {
46+
expect(dayjs().startOf('quarter').format())
47+
.toBe(moment().startOf('quarter').format())
48+
expect(dayjs().endOf('quarter').format())
49+
.toBe(moment().endOf('quarter').format())
50+
})

test/utils.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ it('PrettyUnit', () => {
88
expect(prettyUnit('Days')).toBe('day')
99
expect(prettyUnit('days')).toBe('day')
1010
expect(prettyUnit('day')).toBe('day')
11+
expect(prettyUnit('Q')).toBe('quarter')
12+
expect(prettyUnit('quarter')).toBe('quarter')
13+
expect(prettyUnit('quarters')).toBe('quarter')
1114
expect(prettyUnit()).toBe('')
1215
})
1316

types/index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare namespace dayjs {
1010
export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date' | UnitTypeShort;
1111

1212
export type OpUnitType = UnitType | "week" | 'w';
13+
export type QUnitType = UnitType | "quarter" | 'Q';
1314

1415
class Dayjs {
1516
constructor (config?: ConfigType)
@@ -62,7 +63,7 @@ declare namespace dayjs {
6263

6364
format(template?: string): string
6465

65-
diff(date: ConfigType, unit: OpUnitType | 'quarter', float?: boolean): number
66+
diff(date: ConfigType, unit: QUnitType, float?: boolean): number
6667

6768
valueOf(): number
6869

types/plugin/quarterOfYear.d.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
import { PluginFunc } from 'dayjs'
1+
import { PluginFunc, QUnitType } from 'dayjs'
22

33
declare const plugin: PluginFunc
44
export = plugin
55

66
declare module 'dayjs' {
77
interface Dayjs {
88
quarter(): number
9+
10+
quarter(quarter: number): Dayjs
11+
12+
add(value: number, unit: QUnitType): Dayjs
13+
14+
subtract(value: number, unit: QUnitType): Dayjs
15+
16+
startOf(unit: QUnitType): Dayjs
17+
18+
endOf(unit: QUnitType): Dayjs
919
}
1020
}

0 commit comments

Comments
 (0)