Skip to content

Commit

Permalink
Merge pull request #12 from xx45/dev
Browse files Browse the repository at this point in the history
feat(api): add year(), month()
  • Loading branch information
iamkun authored Apr 11, 2018
2 parents 78c4fbf + 4665d0a commit 9fc5a8d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ 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)
const m = config.substr(4, 2)
const d = config.substr(6, 2)
return `${y}-${m}-${d}`
}
return false
return config
}

year() {
return this.date.getFullYear()
}

month() {
return this.date.getMonth()
}

unix() {
Expand All @@ -25,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
11 changes: 11 additions & 0 deletions test/get-set.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import moment from 'moment'
import dayjs from '../src'

test('Year', () => {
expect(dayjs().year()).toBe(moment().year())
})

test('Month', () => {
expect(dayjs().month()).toBe(moment().month())
})

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('StartOf Year', () => {
expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix())
})

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

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

5 changes: 5 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ test('Now', () => {
test('String 20130208', () => {
expect(dayjs('20130208').unix()).toBe(moment('20130208').unix())
})

test('String Other', () => {
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
})

0 comments on commit 9fc5a8d

Please sign in to comment.