Skip to content

Commit

Permalink
fix: Expand setters like .year(2000) .hour(12)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Mar 7, 2019
1 parent 785405f commit ac532a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/index.js
Expand Up @@ -109,36 +109,41 @@ class Dayjs {
return this.endOf(units) < dayjs(that)
}

year() {
return this.$y
$g(input, get, set) {
if (Utils.u(input)) return this[get]
return this.set(set, input)
}

month() {
return this.$M
year(input) {
return this.$g(input, '$y', C.Y)
}

day() {
return this.$W
month(input) {
return this.$g(input, '$M', C.M)
}

date() {
return this.$D
day(input) {
return this.$g(input, '$W', C.D)
}

hour() {
return this.$H
date(input) {
return this.$g(input, '$D', C.DATE)
}

minute() {
return this.$m
hour(input) {
return this.$g(input, '$H', C.H)
}

second() {
return this.$s
minute(input) {
return this.$g(input, '$m', C.MIN)
}

millisecond() {
return this.$ms
second(input) {
return this.$g(input, '$s', C.S)
}

millisecond(input) {
return this.$g(input, '$ms', C.MS)
}

unix() {
Expand Down
16 changes: 16 additions & 0 deletions test/get-set.test.js
Expand Up @@ -12,34 +12,50 @@ afterEach(() => {

it('Year', () => {
expect(dayjs().year()).toBe(moment().year())
expect(dayjs().year(0).valueOf()).toBe(moment().year(0).valueOf())
expect(dayjs().year(2000).valueOf()).toBe(moment().year(2000).valueOf())
})

it('Month', () => {
expect(dayjs().month()).toBe(moment().month())
expect(dayjs().month(0).valueOf()).toBe(moment().month(0).valueOf())
expect(dayjs().month(1).valueOf()).toBe(moment().month(1).valueOf())
})

it('Day of Week', () => {
expect(dayjs().day()).toBe(moment().day())
expect(dayjs().day(0).format()).toBe(moment().day(0).format())
expect(dayjs().day(1).format()).toBe(moment().day(1).format())
})

it('Date', () => {
expect(dayjs().date()).toBe(moment().date())
expect(dayjs().date(0).valueOf()).toBe(moment().date(0).valueOf())
expect(dayjs().date(1).valueOf()).toBe(moment().date(1).valueOf())
})

it('Hour', () => {
expect(dayjs().hour()).toBe(moment().hour())
expect(dayjs().hour(0).valueOf()).toBe(moment().hour(0).valueOf())
expect(dayjs().hour(1).valueOf()).toBe(moment().hour(1).valueOf())
})

it('Minute', () => {
expect(dayjs().minute()).toBe(moment().minute())
expect(dayjs().minute(0).valueOf()).toBe(moment().minute(0).valueOf())
expect(dayjs().minute(1).valueOf()).toBe(moment().minute(1).valueOf())
})

it('Second', () => {
expect(dayjs().second()).toBe(moment().second())
expect(dayjs().second(0).valueOf()).toBe(moment().second(0).valueOf())
expect(dayjs().second(1).valueOf()).toBe(moment().second(1).valueOf())
})

it('Millisecond', () => {
expect(dayjs().millisecond()).toBe(moment().millisecond())
expect(dayjs().millisecond(0).valueOf()).toBe(moment().millisecond(0).valueOf())
expect(dayjs().millisecond(1).valueOf()).toBe(moment().millisecond(1).valueOf())
})

it('Set Day', () => {
Expand Down

0 comments on commit ac532a0

Please sign in to comment.