From 73187972c7265785c93d4d4c9e8d079143d777fd Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 1 Apr 2019 16:17:09 +0800 Subject: [PATCH] fix: Add .get API --- src/index.js | 4 ++++ test/get-set.test.js | 8 ++++++++ types/index.d.ts | 2 ++ 3 files changed, 14 insertions(+) diff --git a/src/index.js b/src/index.js index d0213dedb..6ad4e5a1e 100644 --- a/src/index.js +++ b/src/index.js @@ -234,6 +234,10 @@ class Dayjs { return this.clone().$set(string, int) } + get(unit) { + return this[Utils.p(unit)]() + } + add(number, units) { number = Number(number) // eslint-disable-line no-param-reassign const unit = Utils.p(units) diff --git a/test/get-set.test.js b/test/get-set.test.js index cf604df77..dae4d476c 100644 --- a/test/get-set.test.js +++ b/test/get-set.test.js @@ -11,48 +11,56 @@ afterEach(() => { }) it('Year', () => { + expect(dayjs().get('year')).toBe(moment().get('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().get('month')).toBe(moment().get('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().get('day')).toBe(moment().get('day')) 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().get('date')).toBe(moment().get('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().get('hour')).toBe(moment().get('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().get('minute')).toBe(moment().get('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().get('second')).toBe(moment().get('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().get('millisecond')).toBe(moment().get('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()) diff --git a/types/index.d.ts b/types/index.d.ts index f32c4c003..6473865c6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -53,6 +53,8 @@ declare namespace dayjs { set(unit: UnitType, value: number): Dayjs + get(unit: UnitType): number + add(value: number, unit: OpUnitType): Dayjs subtract(value: number, unit: OpUnitType): Dayjs