Skip to content

Commit

Permalink
feat(startOf): add startOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Apr 11, 2018
1 parent db66598 commit 6443b7f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Dayjs {

parseConfig(config) {
if (!config) return new Date()
if (config instanceof Date) return config
if (/^(\d){8}$/.test(config)) {
this.utc = true
const y = config.substr(0, 4)
Expand All @@ -33,6 +34,17 @@ class Dayjs {
toString() {
return this.date.toUTCString()
}

startOf(arg) {
switch (arg) {
case 'year':
return new Dayjs(new Date(this.year(), 0, 1))
case 'month':
return new Dayjs(new Date(this.year(), this.month(), 1))
default:
return this
}
}
}

export default config => (
Expand Down
15 changes: 15 additions & 0 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import moment from 'moment'
import dayjs from '../src'

test('StartOfYear', () => {
expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix())
})

test('StartOfMonth', () => {
expect(dayjs().startOf('month').unix()).toBe(moment().startOf('month').unix())
})

test('StartOfOther', () => {
expect(dayjs().startOf('otherString').unix()).toBe(moment().startOf('otherString').unix())
})

0 comments on commit 6443b7f

Please sign in to comment.