Skip to content

Commit

Permalink
Add plugin advancedCompare (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
suspectpart authored and iamkun committed Dec 11, 2018
1 parent 24ae06b commit 616bf1a
Show file tree
Hide file tree
Showing 10 changed files with 1,347 additions and 55 deletions.
70 changes: 35 additions & 35 deletions index.d.ts
Expand Up @@ -8,7 +8,7 @@ declare namespace dayjs {
export type OptionType = { locale: string }

export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date'

interface DayjsObject {
years: number
months: number
Expand All @@ -18,68 +18,68 @@ declare namespace dayjs {
seconds: number
milliseconds: number
}

class Dayjs {
constructor (config?: ConfigType)

clone(): Dayjs

isValid(): boolean

year(): number

month(): number

date(): number

day(): number

hour(): number

minute(): number

second(): number

millisecond(): number

set(unit: UnitType, value: number): Dayjs

add(value: number, unit: UnitType): Dayjs

subtract(value: number, unit: UnitType): Dayjs

startOf(unit: UnitType): Dayjs

endOf(unit: UnitType): Dayjs

format(template?: string): string

diff(dayjs: Dayjs, unit: UnitType, float?: boolean): number

valueOf(): number

unix(): number

daysInMonth(): number

toDate(): Date

toArray(): number[]

toJSON(): string

toISOString(): string

toObject(): DayjsObject

toString(): string
isBefore(dayjs: Dayjs): boolean
isSame(dayjs: Dayjs): boolean
isAfter(dayjs: Dayjs): boolean

isBefore(dayjs: Dayjs, unit?: UnitType): boolean

isSame(dayjs: Dayjs, unit?: UnitType): boolean

isAfter(dayjs: Dayjs, unit?: UnitType): boolean

isLeapYear(): boolean

locale(arg1: any, arg2?: any): Dayjs
Expand Down
17 changes: 7 additions & 10 deletions src/index.js
Expand Up @@ -92,20 +92,17 @@ class Dayjs {
return !(this.$d.toString() === 'Invalid Date')
}

$compare(that) {
return this.valueOf() - dayjs(that).valueOf()
isSame(that, units) {
const other = dayjs(that)
return this.startOf(units) <= other && other <= this.endOf(units)
}

isSame(that) {
return this.$compare(that) === 0
isAfter(that, units) {
return dayjs(that) < this.startOf(units)
}

isBefore(that) {
return this.$compare(that) < 0
}

isAfter(that) {
return this.$compare(that) > 0
isBefore(that, units) {
return this.endOf(units) < dayjs(that)
}

year() {
Expand Down
7 changes: 3 additions & 4 deletions src/plugin/isBetween/index.js
@@ -1,10 +1,9 @@
export default (o, c, d) => {
const proto = c.prototype
proto.isBetween = function (a, b) {
c.prototype.isBetween = function (a, b, u) {
const dA = d(a)
const dB = d(b)

return (this.isAfter(dA) && this.isBefore(dB)) || (this.isBefore(dA) && this.isAfter(dB))
return (this.isAfter(dA, u) && this.isBefore(dB, u))
|| (this.isBefore(dA, u) && this.isAfter(dB, u))
}
}

5 changes: 5 additions & 0 deletions src/plugin/isSameOrAfter/index.js
@@ -0,0 +1,5 @@
export default (o, c) => {
c.prototype.isSameOrAfter = function (that, units) {
return this.isSame(that, units) || this.isAfter(that, units)
}
}
5 changes: 5 additions & 0 deletions src/plugin/isSameOrBefore/index.js
@@ -0,0 +1,5 @@
export default (o, c) => {
c.prototype.isSameOrBefore = function (that, units) {
return this.isSame(that, units) || this.isBefore(that, units)
}
}
530 changes: 530 additions & 0 deletions test/comparison.test.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions test/index.d.test.ts
Expand Up @@ -70,4 +70,10 @@ dayjs().isSame(dayjs())

dayjs().isAfter(dayjs())

dayjs().isBefore(dayjs(), 'minutes')

dayjs().isSame(dayjs(), 'hours')

dayjs().isAfter(dayjs(), 'year')

dayjs('2000-01-01').isLeapYear()

0 comments on commit 616bf1a

Please sign in to comment.