Skip to content

Commit 616bf1a

Browse files
suspectpartiamkun
authored andcommitted
Add plugin advancedCompare (#404)
1 parent 24ae06b commit 616bf1a

File tree

10 files changed

+1347
-55
lines changed

10 files changed

+1347
-55
lines changed

index.d.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare namespace dayjs {
88
export type OptionType = { locale: string }
99

1010
export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date'
11-
11+
1212
interface DayjsObject {
1313
years: number
1414
months: number
@@ -18,68 +18,68 @@ declare namespace dayjs {
1818
seconds: number
1919
milliseconds: number
2020
}
21-
21+
2222
class Dayjs {
2323
constructor (config?: ConfigType)
24-
24+
2525
clone(): Dayjs
26-
26+
2727
isValid(): boolean
28-
28+
2929
year(): number
30-
30+
3131
month(): number
32-
32+
3333
date(): number
34-
34+
3535
day(): number
36-
36+
3737
hour(): number
38-
38+
3939
minute(): number
40-
40+
4141
second(): number
42-
42+
4343
millisecond(): number
44-
44+
4545
set(unit: UnitType, value: number): Dayjs
46-
46+
4747
add(value: number, unit: UnitType): Dayjs
48-
48+
4949
subtract(value: number, unit: UnitType): Dayjs
50-
50+
5151
startOf(unit: UnitType): Dayjs
52-
52+
5353
endOf(unit: UnitType): Dayjs
54-
54+
5555
format(template?: string): string
56-
56+
5757
diff(dayjs: Dayjs, unit: UnitType, float?: boolean): number
58-
58+
5959
valueOf(): number
60-
60+
6161
unix(): number
62-
62+
6363
daysInMonth(): number
64-
64+
6565
toDate(): Date
66-
66+
6767
toArray(): number[]
68-
68+
6969
toJSON(): string
70-
70+
7171
toISOString(): string
72-
72+
7373
toObject(): DayjsObject
74-
74+
7575
toString(): string
76-
77-
isBefore(dayjs: Dayjs): boolean
78-
79-
isSame(dayjs: Dayjs): boolean
80-
81-
isAfter(dayjs: Dayjs): boolean
82-
76+
77+
isBefore(dayjs: Dayjs, unit?: UnitType): boolean
78+
79+
isSame(dayjs: Dayjs, unit?: UnitType): boolean
80+
81+
isAfter(dayjs: Dayjs, unit?: UnitType): boolean
82+
8383
isLeapYear(): boolean
8484

8585
locale(arg1: any, arg2?: any): Dayjs

src/index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,17 @@ class Dayjs {
9292
return !(this.$d.toString() === 'Invalid Date')
9393
}
9494

95-
$compare(that) {
96-
return this.valueOf() - dayjs(that).valueOf()
95+
isSame(that, units) {
96+
const other = dayjs(that)
97+
return this.startOf(units) <= other && other <= this.endOf(units)
9798
}
9899

99-
isSame(that) {
100-
return this.$compare(that) === 0
100+
isAfter(that, units) {
101+
return dayjs(that) < this.startOf(units)
101102
}
102103

103-
isBefore(that) {
104-
return this.$compare(that) < 0
105-
}
106-
107-
isAfter(that) {
108-
return this.$compare(that) > 0
104+
isBefore(that, units) {
105+
return this.endOf(units) < dayjs(that)
109106
}
110107

111108
year() {

src/plugin/isBetween/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
export default (o, c, d) => {
2-
const proto = c.prototype
3-
proto.isBetween = function (a, b) {
2+
c.prototype.isBetween = function (a, b, u) {
43
const dA = d(a)
54
const dB = d(b)
65

7-
return (this.isAfter(dA) && this.isBefore(dB)) || (this.isBefore(dA) && this.isAfter(dB))
6+
return (this.isAfter(dA, u) && this.isBefore(dB, u))
7+
|| (this.isBefore(dA, u) && this.isAfter(dB, u))
88
}
99
}
10-

src/plugin/isSameOrAfter/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default (o, c) => {
2+
c.prototype.isSameOrAfter = function (that, units) {
3+
return this.isSame(that, units) || this.isAfter(that, units)
4+
}
5+
}

src/plugin/isSameOrBefore/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default (o, c) => {
2+
c.prototype.isSameOrBefore = function (that, units) {
3+
return this.isSame(that, units) || this.isBefore(that, units)
4+
}
5+
}

test/comparison.test.js

Lines changed: 530 additions & 0 deletions
Large diffs are not rendered by default.

test/index.d.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@ dayjs().isSame(dayjs())
7070

7171
dayjs().isAfter(dayjs())
7272

73+
dayjs().isBefore(dayjs(), 'minutes')
74+
75+
dayjs().isSame(dayjs(), 'hours')
76+
77+
dayjs().isAfter(dayjs(), 'year')
78+
7379
dayjs('2000-01-01').isLeapYear()

0 commit comments

Comments
 (0)