Skip to content

Commit

Permalink
fix: Add .get API
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Apr 1, 2019
1 parent e925de6 commit 7318797
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/get-set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7318797

Please sign in to comment.