Skip to content

Commit 40a3431

Browse files
committed
fix: Move toObject, toArray API to separate plugin from core
1 parent 8d63d88 commit 40a3431

File tree

6 files changed

+60
-38
lines changed

6 files changed

+60
-38
lines changed

src/index.js

-24
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,6 @@ class Dayjs {
360360
return new Date(this.$d)
361361
}
362362

363-
toArray() {
364-
return [
365-
this.$y,
366-
this.$M,
367-
this.$D,
368-
this.$H,
369-
this.$m,
370-
this.$s,
371-
this.$ms
372-
]
373-
}
374-
375363
toJSON() {
376364
return this.toISOString()
377365
}
@@ -383,18 +371,6 @@ class Dayjs {
383371
return this.$d.toISOString()
384372
}
385373

386-
toObject() {
387-
return {
388-
years: this.$y,
389-
months: this.$M,
390-
date: this.$D,
391-
hours: this.$H,
392-
minutes: this.$m,
393-
seconds: this.$s,
394-
milliseconds: this.$ms
395-
}
396-
}
397-
398374
toString() {
399375
return this.$d.toUTCString()
400376
}

src/plugin/toArray/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default (o, c) => {
2+
const proto = c.prototype
3+
proto.toArray = function () {
4+
return [
5+
this.$y,
6+
this.$M,
7+
this.$D,
8+
this.$H,
9+
this.$m,
10+
this.$s,
11+
this.$ms
12+
]
13+
}
14+
}
15+

src/plugin/toObject/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default (o, c) => {
2+
const proto = c.prototype
3+
proto.toObject = function () {
4+
return {
5+
years: this.$y,
6+
months: this.$M,
7+
date: this.$D,
8+
hours: this.$H,
9+
minutes: this.$m,
10+
seconds: this.$s,
11+
milliseconds: this.$ms
12+
}
13+
}
14+
}
15+

types/index.d.ts

-14
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ declare namespace dayjs {
1212
type OpUnitTypeShort = 'w'
1313
export type OpUnitType = UnitType | "week" | OpUnitTypeShort;
1414

15-
interface DayjsObject {
16-
years: number
17-
months: number
18-
date: number
19-
hours: number
20-
minutes: number
21-
seconds: number
22-
milliseconds: number
23-
}
24-
2515
class Dayjs {
2616
constructor (config?: ConfigType)
2717

@@ -67,14 +57,10 @@ declare namespace dayjs {
6757

6858
toDate(): Date
6959

70-
toArray(): number[]
71-
7260
toJSON(): string
7361

7462
toISOString(): string
7563

76-
toObject(): DayjsObject
77-
7864
toString(): string
7965

8066
utcOffset(): number

types/plugin/toArray.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
declare module 'dayjs' {
7+
interface Dayjs {
8+
toArray(): number[]
9+
}
10+
}

types/plugin/toObject.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin
5+
6+
interface DayjsObject {
7+
years: number
8+
months: number
9+
date: number
10+
hours: number
11+
minutes: number
12+
seconds: number
13+
milliseconds: number
14+
}
15+
16+
declare module 'dayjs' {
17+
interface Dayjs {
18+
toObject(): DayjsObject
19+
}
20+
}

0 commit comments

Comments
 (0)