Skip to content

Commit bb2b7ad

Browse files
committed
fix(add): toArray toJSON toObject
1 parent d0e5f87 commit bb2b7ad

3 files changed

Lines changed: 71 additions & 7 deletions

File tree

.eslintrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
"jest/globals": true
88
},
99
"rules": {
10-
"semi": [2, "never"]
10+
"semi": [
11+
2,
12+
"never"
13+
],
14+
"comma-dangle": [
15+
"error",
16+
"never"
17+
]
1118
}
1219
}

src/index.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Dayjs {
3030
this.$hour = this.$date.getHours()
3131
this.$minute = this.$date.getMinutes()
3232
this.$second = this.$date.getSeconds()
33+
this.$milliseconds = this.$date.getMilliseconds()
3334
}
3435

3536
year() {
@@ -44,6 +45,22 @@ class Dayjs {
4445
return this.$day
4546
}
4647

48+
hour() {
49+
return this.$hour
50+
}
51+
52+
minute() {
53+
return this.$minute
54+
}
55+
56+
second() {
57+
return this.$second
58+
}
59+
60+
millisecond() {
61+
return this.$milliseconds
62+
}
63+
4764
unix() {
4865
return Math.floor(this.valueOf() / 1000)
4966
}
@@ -53,10 +70,6 @@ class Dayjs {
5370
return this.$date.getTime()
5471
}
5572

56-
toString() {
57-
return this.$date.toUTCString()
58-
}
59-
6073
startOf(arg, isStartOf = true) {
6174
switch (arg) {
6275
case 'year':
@@ -197,9 +210,41 @@ class Dayjs {
197210
return new Date(this.$date)
198211
}
199212

213+
toArray() {
214+
return [
215+
this.year(),
216+
this.month(),
217+
this.date(),
218+
this.hour(),
219+
this.minute(),
220+
this.second(),
221+
this.millisecond()
222+
]
223+
}
224+
225+
toJSON() {
226+
return this.toISOString()
227+
}
228+
200229
toISOString() {
201230
return this.toDate().toISOString()
202231
}
232+
233+
toObject() {
234+
return {
235+
years: this.year(),
236+
months: this.month(),
237+
date: this.date(),
238+
hours: this.hour(),
239+
minutes: this.minute(),
240+
seconds: this.second(),
241+
milliseconds: this.millisecond()
242+
}
243+
}
244+
245+
toString() {
246+
return this.$date.toUTCString()
247+
}
203248
}
204249

205250
export default config => (new Dayjs(config))

test/display.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ it('Days in Month', () => {
8383
expect(dayjs('20140201').daysInMonth()).toBe(moment('20140201').daysInMonth())
8484
})
8585

86-
it('As Javascript Date', () => {
86+
it('As Javascript Date -> toDate', () => {
8787
const base = dayjs()
8888
const momentBase = moment()
8989
const jsDate = base.toDate()
@@ -94,6 +94,18 @@ it('As Javascript Date', () => {
9494
expect(jsDate.toUTCString()).not.toBe(base.toString())
9595
})
9696

97-
it('As ISO 8601 String e.g. 2013-02-04T22:44:30.652Z', () => {
97+
it('As Array -> toArray', () => {
98+
expect(dayjs().toArray()).toEqual(moment().toArray())
99+
})
100+
101+
it('As JSON -> toJSON', () => {
102+
expect(dayjs().toJSON()).toBe(moment().toJSON())
103+
})
104+
105+
it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
98106
expect(dayjs().toISOString()).toBe(moment().toISOString())
99107
})
108+
109+
it('As Object -> toObject', () => {
110+
expect(dayjs().toObject()).toEqual(moment().toObject())
111+
})

0 commit comments

Comments
 (0)