Skip to content

Commit

Permalink
Merge pull request #55 from xx45/dev
Browse files Browse the repository at this point in the history
fix parse && add startOf endOf week
  • Loading branch information
iamkun authored Apr 25, 2018
2 parents b3a678d + a922807 commit 4abede4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
42 changes: 22 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import * as C from './constant'
import * as Utils from './utils'

const parseConfig = (config) => {
let reg
if (!config) return new Date()
if (config instanceof Date) return config
const configStr = String(config)
if (/^(\d){8}$/.test(configStr)) {
const y = configStr.substr(0, 4)
const m = configStr.substr(4, 2)
const d = configStr.substr(6, 2)
return new Date(y, m - 1, d)
}
return new Date(config) // e.g. timestamp
// eslint-disable-next-line no-cond-assign
if (reg = String(config).match(/^(\d{4})-?(\d{2})-?(\d{1,2})$/)) {
// 2018-08-08 or 20180808
return new Date(reg[1], reg[2] - 1, reg[3])
}
return new Date(config) // timestamp
}

class Dayjs {
Expand All @@ -21,8 +20,8 @@ class Dayjs {
}

init() {
this.timeZone = this.$d.getTimezoneOffset() / 60
this.timeZoneString = Utils.padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+')
this.$zone = this.$d.getTimezoneOffset() / 60
this.$zoneStr = Utils.padStart(String(this.$zone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+')
this.$y = this.$d.getFullYear()
this.$M = this.$d.getMonth()
this.$D = this.$d.getDate()
Expand Down Expand Up @@ -92,17 +91,20 @@ class Dayjs {

startOf(units, isStartOf = true) { // isStartOf -> endOf
const unit = Utils.prettyUnit(units)
const instanceFactory = (d, m, y = this.$y, isEnd = false) => {
const ins = new Dayjs(new Date(y, m, d))
return isEnd ? ins.endOf(C.D) : ins
}
switch (unit) {
case C.Y:
if (isStartOf) {
return new Dayjs(new Date(this.$y, 0, 1))
}
return new Dayjs(new Date(this.$y, 11, 31)).endOf('day')
return isStartOf ? instanceFactory(1, 0) :
instanceFactory(31, 11, this.$y, true)
case C.M:
if (isStartOf) {
return new Dayjs(new Date(this.$y, this.$M, 1))
}
return new Dayjs(new Date(this.$y, this.$M + 1, 0)).endOf('day')
return isStartOf ? instanceFactory(1, this.$M) :
instanceFactory(0, this.$M + 1, this.$y, true)
case C.W:
return isStartOf ? instanceFactory(this.$D - this.$W, this.$M) :
instanceFactory(this.$D + (6 - this.$W), this.$M, this.$y, true)
case C.D:
if (isStartOf) {
return new Dayjs(this.toDate().setHours(0, 0, 0, 0))
Expand Down Expand Up @@ -219,9 +221,9 @@ class Dayjs {
case 'ss':
return Utils.padStart(String(this.$s), 2, '0')
case 'Z':
return `${this.timeZoneString.slice(0, -2)}:00`
return `${this.$zoneStr.slice(0, -2)}:00`
default: // 'ZZ'
return this.timeZoneString
return this.$zoneStr
}
})
}
Expand Down
28 changes: 12 additions & 16 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@ afterEach(() => {
MockDate.reset()
})

it('StartOf EndOf Year with s and upper case', () => {
expect(dayjs().startOf('YearS').unix()).toBe(moment().startOf('year').unix())
expect(dayjs().endOf('year').unix()).toBe(moment().endOf('year').unix())
})

it('StartOf EndOf Month', () => {
expect(dayjs().startOf('month').unix()).toBe(moment().startOf('month').unix())
expect(dayjs().endOf('month').unix()).toBe(moment().endOf('month').unix())
})
describe('StartOf EndOf', () => {
it('StartOf EndOf Year ... with s and upper case', () => {
const testArr = ['Year', 'year', 'YearS', 'month', 'day', 'week']
testArr.forEach((d) => {
expect(dayjs().startOf(d).unix()).toBe(moment().startOf(d).unix())
expect(dayjs().endOf(d).unix()).toBe(moment().endOf(d).unix())
})
})

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('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', () => {
expect(dayjs().add(1, 's').unix()).toBe(moment().add(1, 's').unix())
Expand Down
6 changes: 5 additions & 1 deletion test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ beforeEach(() => {
afterEach(() => {
MockDate.reset()
})

describe('Parse', () => {
it('Now', () => {
expect(dayjs().unix()).toBe(moment().unix())
})

it('String 20130208', () => {
expect(dayjs('20130208').unix()).toBe(moment('20130208').unix())
const timeArr = ['20130108', '2018-04-24']
timeArr.forEach((t) => {
expect(dayjs(t).unix()).toBe(moment(t).unix())
})
})

it('String ISO 8601 date, time and zone', () => {
Expand Down

0 comments on commit 4abede4

Please sign in to comment.