Skip to content

Commit

Permalink
Merge pull request #23 from xx45/dev
Browse files Browse the repository at this point in the history
fix(add endof): add endOf && add startOf day
  • Loading branch information
iamkun authored Apr 19, 2018
2 parents cfb0c03 + 3a569ce commit c1beb5f
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 48 deletions.
9 changes: 8 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
"jest/globals": true
},
"rules": {
"semi": [2, "never"]
"semi": [
2,
"never"
],
"comma-dangle": [
"error",
"never"
]
}
}
6 changes: 6 additions & 0 deletions src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ export const SECONDS_A_MINUTE = 60
export const SECONDS_A_HOUR = SECONDS_A_MINUTE * 60
export const SECONDS_A_DAY = SECONDS_A_HOUR * 24
export const SECONDS_A_WEEK = SECONDS_A_DAY * 7

export const MILLISECONDS_A_SECOND = 1000
export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND
export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND
export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND
export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND
119 changes: 101 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Dayjs {
this.$hour = this.$date.getHours()
this.$minute = this.$date.getMinutes()
this.$second = this.$date.getSeconds()
this.$milliseconds = this.$date.getMilliseconds()
}

year() {
Expand All @@ -44,6 +45,22 @@ class Dayjs {
return this.$day
}

hour() {
return this.$hour
}

minute() {
return this.$minute
}

second() {
return this.$second
}

millisecond() {
return this.$milliseconds
}

unix() {
return Math.floor(this.valueOf() / 1000)
}
Expand All @@ -53,21 +70,32 @@ class Dayjs {
return this.$date.getTime()
}

toString() {
return this.$date.toUTCString()
}

startOf(arg) {
startOf(arg, isStartOf = true) {
switch (arg) {
case 'year':
return new Dayjs(new Date(this.year(), 0, 1))
if (isStartOf) {
return new Dayjs(new Date(this.year(), 0, 1))
}
return new Dayjs(new Date(this.year(), 11, 31)).endOf('day')
case 'month':
return new Dayjs(new Date(this.year(), this.month(), 1))
if (isStartOf) {
return new Dayjs(new Date(this.year(), this.month(), 1))
}
return new Dayjs(new Date(this.year(), this.month() + 1, 0)).endOf('day')
case 'day':
if (isStartOf) {
return new Dayjs(this.toDate().setHours(0, 0, 0, 0))
}
return new Dayjs(this.toDate().setHours(23, 59, 59, 999))
default:
return this
return this.clone()
}
}

endOf(arg) {
return this.startOf(arg, false)
}

set(string, int) {
if (!Utils.isNumber(int)) return this
switch (string) {
Expand Down Expand Up @@ -99,25 +127,25 @@ class Dayjs {
switch (string) {
case 'm':
case 'minutes':
step = Constant.SECONDS_A_MINUTE
step = Constant.MILLISECONDS_A_MINUTE
break
case 'h':
case 'hours':
step = Constant.SECONDS_A_HOUR
step = Constant.MILLISECONDS_A_HOUR
break
case 'd':
case 'days':
step = Constant.SECONDS_A_DAY
step = Constant.MILLISECONDS_A_DAY
break
case 'w':
case 'weeks':
step = Constant.SECONDS_A_WEEK
step = Constant.MILLISECONDS_A_WEEK
break
default: // s seconds
step = 1
step = Constant.MILLISECONDS_A_SECOND
}
const nextTimeStamp = this.unix() + (number * step)
return new Dayjs(nextTimeStamp * 1000)
const nextTimeStamp = this.valueOf() + (number * step)
return new Dayjs(nextTimeStamp)
}

subtract(number, string) {
Expand Down Expand Up @@ -165,13 +193,36 @@ class Dayjs {
})
}

diff(otherDate) {
diff(otherDate, unit, float = false) {
const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate)
return this.valueOf() - other.valueOf()
const diff = this - other
let result = Utils.monthDiff(this, other)
switch (unit) {
case 'years':
result /= 12
break
case 'months':
break
case 'quarters':
result /= 3
break
case 'weeks':
result = diff / Constant.MILLISECONDS_A_WEEK
break
case 'days':
result = diff / Constant.MILLISECONDS_A_DAY
break
case 'seconds':
result = diff / Constant.MILLISECONDS_A_SECOND
break
default: // milliseconds
result = diff
}
return float ? result : Utils.absFloor(result)
}

daysInMonth() {
return new Dayjs(new Date(this.year(), this.month() + 1, 0)).date()
return this.endOf('month').date()
}

clone() {
Expand All @@ -182,9 +233,41 @@ class Dayjs {
return new Date(this.$date)
}

toArray() {
return [
this.year(),
this.month(),
this.date(),
this.hour(),
this.minute(),
this.second(),
this.millisecond()
]
}

toJSON() {
return this.toISOString()
}

toISOString() {
return this.toDate().toISOString()
}

toObject() {
return {
years: this.year(),
months: this.month(),
date: this.date(),
hours: this.hour(),
minutes: this.minute(),
seconds: this.second(),
milliseconds: this.millisecond()
}
}

toString() {
return this.$date.toUTCString()
}
}

export default config => (new Dayjs(config))
18 changes: 18 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,21 @@ export const padStart = (string, length, pad) => {

export const isNumber = n => (!Number.isNaN(parseFloat(n)) && Number.isFinite(n))

export const monthDiff = (a, b) => {
// function from moment.js monthDiff
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
const anchor = a.clone().add(wholeMonthDiff, 'months')
let anchor2
let adjust
if (b - anchor < 0) {
anchor2 = a.clone().add(wholeMonthDiff - 1, 'months')
adjust = (b - anchor) / (anchor - anchor2)
} else {
anchor2 = a.clone().add(wholeMonthDiff + 1, 'months')
adjust = (b - anchor) / (anchor2 - anchor)
}
return Number(-(wholeMonthDiff + adjust)) || 0
}

export const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))

75 changes: 64 additions & 11 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,56 @@ it('Format Complex with other string - : / ', () => {
expect(dayjs().format(string)).toBe(moment().format(string))
})

it('Difference', () => {
const dateString = '20110101'

const dayjsA = dayjs()
const dayjsB = dayjs(dateString)

const momentA = moment()
const momentB = moment(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
describe('Difference', () => {
it('empty -> default milliseconds', () => {
const dateString = '20110101'
const dayjsA = dayjs()
const dayjsB = dayjs(dateString)
const momentA = moment()
const momentB = moment(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
})

it('diff -> none dayjs object', () => {
const dateString = '2013-02-08'
const dayjsA = dayjs()
const dayjsB = new Date(dateString)
const momentA = moment()
const momentB = new Date(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
})

it('diff -> in seconds, days, weeks, months, quarters, years ', () => {
const dayjsA = dayjs()
const dayjsB = dayjs().add(1000, 'days')
const dayjsC = dayjs().subtract(1000, 'days')
const momentA = moment()
const momentB = moment().add(1000, 'days')
const momentC = moment().subtract(1000, 'days')
const units = ['seconds', 'days', 'weeks', 'months', 'quarters', 'years']
units.forEach((unit) => {
expect(dayjsA.diff(dayjsB, unit)).toBe(momentA.diff(momentB, unit))
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
expect(dayjsA.diff(dayjsC, unit)).toBe(momentA.diff(momentC, unit))
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true))
})
})

it('Special diff in month according to moment.js', () => {
const dayjsA = dayjs('20160115')
const dayjsB = dayjs('20160215')
const dayjsC = dayjs('20170115')
const momentA = moment('20160115')
const momentB = moment('20160215')
const momentC = moment('20170115')
const units = ['months', 'quarters', 'years']
units.forEach((unit) => {
expect(dayjsA.diff(dayjsB, unit)).toBe(momentA.diff(momentB, unit))
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
expect(dayjsA.diff(dayjsC, unit)).toBe(momentA.diff(momentC, unit))
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true))
})
})
})

it('Unix Timestamp (milliseconds)', () => {
Expand All @@ -80,9 +121,10 @@ it('Unix Timestamp (seconds)', () => {

it('Days in Month', () => {
expect(dayjs().daysInMonth()).toBe(moment().daysInMonth())
expect(dayjs('20140201').daysInMonth()).toBe(moment('20140201').daysInMonth())
})

it('As Javascript Date', () => {
it('As Javascript Date -> toDate', () => {
const base = dayjs()
const momentBase = moment()
const jsDate = base.toDate()
Expand All @@ -93,7 +135,18 @@ it('As Javascript Date', () => {
expect(jsDate.toUTCString()).not.toBe(base.toString())
})

it('As ISO 8601 String e.g. 2013-02-04T22:44:30.652Z', () => {
it('As Array -> toArray', () => {
expect(dayjs().toArray()).toEqual(moment().toArray())
})

it('As JSON -> toJSON', () => {
expect(dayjs().toJSON()).toBe(moment().toJSON())
})

it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
expect(dayjs().toISOString()).toBe(moment().toISOString())
})

it('As Object -> toObject', () => {
expect(dayjs().toObject()).toEqual(moment().toObject())
})
14 changes: 11 additions & 3 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ afterEach(() => {
MockDate.reset()
})

it('StartOf Year', () => {
it('StartOf EndOf Year', () => {
expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix())
expect(dayjs().endOf('year').unix()).toBe(moment().endOf('year').unix())
})

it('StartOf Month', () => {
it('StartOf EndOf Month', () => {
expect(dayjs().startOf('month').unix()).toBe(moment().startOf('month').unix())
expect(dayjs().endOf('month').unix()).toBe(moment().endOf('month').unix())
})

it('StartOf Other', () => {
it('StartOf EndOf Day', () => {
expect(dayjs().startOf('day').unix()).toBe(moment().startOf('day').unix())
expect(dayjs().endOf('day').unix()).toBe(moment().endOf('day').unix())
})

it('StartOf EndOf Other -> no change', () => {
expect(dayjs().startOf('otherString').unix()).toBe(moment().startOf('otherString').unix())
expect(dayjs().endOf('otherString').unix()).toBe(moment().endOf('otherString').unix())
})

it('Add Time days', () => {
Expand Down
Loading

0 comments on commit c1beb5f

Please sign in to comment.