From ac532a0f0a003911bb22c395245bfe6433fd1c7b Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 7 Mar 2019 14:02:52 +0800 Subject: [PATCH] fix: Expand setters like .year(2000) .hour(12) --- src/index.js | 37 +++++++++++++++++++++---------------- test/get-set.test.js | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/index.js b/src/index.js index 55e5d647..112ae33b 100644 --- a/src/index.js +++ b/src/index.js @@ -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() { diff --git a/test/get-set.test.js b/test/get-set.test.js index af5676a3..7587915c 100644 --- a/test/get-set.test.js +++ b/test/get-set.test.js @@ -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', () => {