Skip to content

Commit 7318797

Browse files
committed
fix: Add .get API
1 parent e925de6 commit 7318797

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ class Dayjs {
234234
return this.clone().$set(string, int)
235235
}
236236

237+
get(unit) {
238+
return this[Utils.p(unit)]()
239+
}
240+
237241
add(number, units) {
238242
number = Number(number) // eslint-disable-line no-param-reassign
239243
const unit = Utils.p(units)

test/get-set.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,56 @@ afterEach(() => {
1111
})
1212

1313
it('Year', () => {
14+
expect(dayjs().get('year')).toBe(moment().get('year'))
1415
expect(dayjs().year()).toBe(moment().year())
1516
expect(dayjs().year(0).valueOf()).toBe(moment().year(0).valueOf())
1617
expect(dayjs().year(2000).valueOf()).toBe(moment().year(2000).valueOf())
1718
})
1819

1920
it('Month', () => {
21+
expect(dayjs().get('month')).toBe(moment().get('month'))
2022
expect(dayjs().month()).toBe(moment().month())
2123
expect(dayjs().month(0).valueOf()).toBe(moment().month(0).valueOf())
2224
expect(dayjs().month(1).valueOf()).toBe(moment().month(1).valueOf())
2325
})
2426

2527
it('Day of Week', () => {
28+
expect(dayjs().get('day')).toBe(moment().get('day'))
2629
expect(dayjs().day()).toBe(moment().day())
2730
expect(dayjs().day(0).format()).toBe(moment().day(0).format())
2831
expect(dayjs().day(1).format()).toBe(moment().day(1).format())
2932
})
3033

3134
it('Date', () => {
35+
expect(dayjs().get('date')).toBe(moment().get('date'))
3236
expect(dayjs().date()).toBe(moment().date())
3337
expect(dayjs().date(0).valueOf()).toBe(moment().date(0).valueOf())
3438
expect(dayjs().date(1).valueOf()).toBe(moment().date(1).valueOf())
3539
})
3640

3741
it('Hour', () => {
42+
expect(dayjs().get('hour')).toBe(moment().get('hour'))
3843
expect(dayjs().hour()).toBe(moment().hour())
3944
expect(dayjs().hour(0).valueOf()).toBe(moment().hour(0).valueOf())
4045
expect(dayjs().hour(1).valueOf()).toBe(moment().hour(1).valueOf())
4146
})
4247

4348
it('Minute', () => {
49+
expect(dayjs().get('minute')).toBe(moment().get('minute'))
4450
expect(dayjs().minute()).toBe(moment().minute())
4551
expect(dayjs().minute(0).valueOf()).toBe(moment().minute(0).valueOf())
4652
expect(dayjs().minute(1).valueOf()).toBe(moment().minute(1).valueOf())
4753
})
4854

4955
it('Second', () => {
56+
expect(dayjs().get('second')).toBe(moment().get('second'))
5057
expect(dayjs().second()).toBe(moment().second())
5158
expect(dayjs().second(0).valueOf()).toBe(moment().second(0).valueOf())
5259
expect(dayjs().second(1).valueOf()).toBe(moment().second(1).valueOf())
5360
})
5461

5562
it('Millisecond', () => {
63+
expect(dayjs().get('millisecond')).toBe(moment().get('millisecond'))
5664
expect(dayjs().millisecond()).toBe(moment().millisecond())
5765
expect(dayjs().millisecond(0).valueOf()).toBe(moment().millisecond(0).valueOf())
5866
expect(dayjs().millisecond(1).valueOf()).toBe(moment().millisecond(1).valueOf())

types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ declare namespace dayjs {
5353

5454
set(unit: UnitType, value: number): Dayjs
5555

56+
get(unit: UnitType): number
57+
5658
add(value: number, unit: OpUnitType): Dayjs
5759

5860
subtract(value: number, unit: OpUnitType): Dayjs

0 commit comments

Comments
 (0)