Skip to content

Commit ac532a0

Browse files
committedMar 7, 2019
fix: Expand setters like .year(2000) .hour(12)
1 parent 785405f commit ac532a0

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed
 

‎src/index.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,41 @@ class Dayjs {
109109
return this.endOf(units) < dayjs(that)
110110
}
111111

112-
year() {
113-
return this.$y
112+
$g(input, get, set) {
113+
if (Utils.u(input)) return this[get]
114+
return this.set(set, input)
114115
}
115116

116-
month() {
117-
return this.$M
117+
year(input) {
118+
return this.$g(input, '$y', C.Y)
118119
}
119120

120-
day() {
121-
return this.$W
121+
month(input) {
122+
return this.$g(input, '$M', C.M)
122123
}
123124

124-
date() {
125-
return this.$D
125+
day(input) {
126+
return this.$g(input, '$W', C.D)
126127
}
127128

128-
hour() {
129-
return this.$H
129+
date(input) {
130+
return this.$g(input, '$D', C.DATE)
130131
}
131132

132-
minute() {
133-
return this.$m
133+
hour(input) {
134+
return this.$g(input, '$H', C.H)
134135
}
135136

136-
second() {
137-
return this.$s
137+
minute(input) {
138+
return this.$g(input, '$m', C.MIN)
138139
}
139140

140-
millisecond() {
141-
return this.$ms
141+
second(input) {
142+
return this.$g(input, '$s', C.S)
143+
}
144+
145+
millisecond(input) {
146+
return this.$g(input, '$ms', C.MS)
142147
}
143148

144149
unix() {

‎test/get-set.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,50 @@ afterEach(() => {
1212

1313
it('Year', () => {
1414
expect(dayjs().year()).toBe(moment().year())
15+
expect(dayjs().year(0).valueOf()).toBe(moment().year(0).valueOf())
16+
expect(dayjs().year(2000).valueOf()).toBe(moment().year(2000).valueOf())
1517
})
1618

1719
it('Month', () => {
1820
expect(dayjs().month()).toBe(moment().month())
21+
expect(dayjs().month(0).valueOf()).toBe(moment().month(0).valueOf())
22+
expect(dayjs().month(1).valueOf()).toBe(moment().month(1).valueOf())
1923
})
2024

2125
it('Day of Week', () => {
2226
expect(dayjs().day()).toBe(moment().day())
27+
expect(dayjs().day(0).format()).toBe(moment().day(0).format())
28+
expect(dayjs().day(1).format()).toBe(moment().day(1).format())
2329
})
2430

2531
it('Date', () => {
2632
expect(dayjs().date()).toBe(moment().date())
33+
expect(dayjs().date(0).valueOf()).toBe(moment().date(0).valueOf())
34+
expect(dayjs().date(1).valueOf()).toBe(moment().date(1).valueOf())
2735
})
2836

2937
it('Hour', () => {
3038
expect(dayjs().hour()).toBe(moment().hour())
39+
expect(dayjs().hour(0).valueOf()).toBe(moment().hour(0).valueOf())
40+
expect(dayjs().hour(1).valueOf()).toBe(moment().hour(1).valueOf())
3141
})
3242

3343
it('Minute', () => {
3444
expect(dayjs().minute()).toBe(moment().minute())
45+
expect(dayjs().minute(0).valueOf()).toBe(moment().minute(0).valueOf())
46+
expect(dayjs().minute(1).valueOf()).toBe(moment().minute(1).valueOf())
3547
})
3648

3749
it('Second', () => {
3850
expect(dayjs().second()).toBe(moment().second())
51+
expect(dayjs().second(0).valueOf()).toBe(moment().second(0).valueOf())
52+
expect(dayjs().second(1).valueOf()).toBe(moment().second(1).valueOf())
3953
})
4054

4155
it('Millisecond', () => {
4256
expect(dayjs().millisecond()).toBe(moment().millisecond())
57+
expect(dayjs().millisecond(0).valueOf()).toBe(moment().millisecond(0).valueOf())
58+
expect(dayjs().millisecond(1).valueOf()).toBe(moment().millisecond(1).valueOf())
4359
})
4460

4561
it('Set Day', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.