diff --git a/src/index.js b/src/index.js index 413b5becc..55e5d647f 100644 --- a/src/index.js +++ b/src/index.js @@ -360,18 +360,6 @@ class Dayjs { return new Date(this.$d) } - toArray() { - return [ - this.$y, - this.$M, - this.$D, - this.$H, - this.$m, - this.$s, - this.$ms - ] - } - toJSON() { return this.toISOString() } @@ -383,18 +371,6 @@ class Dayjs { return this.$d.toISOString() } - toObject() { - return { - years: this.$y, - months: this.$M, - date: this.$D, - hours: this.$H, - minutes: this.$m, - seconds: this.$s, - milliseconds: this.$ms - } - } - toString() { return this.$d.toUTCString() } diff --git a/src/plugin/toArray/index.js b/src/plugin/toArray/index.js new file mode 100644 index 000000000..9ffc16040 --- /dev/null +++ b/src/plugin/toArray/index.js @@ -0,0 +1,15 @@ +export default (o, c) => { + const proto = c.prototype + proto.toArray = function () { + return [ + this.$y, + this.$M, + this.$D, + this.$H, + this.$m, + this.$s, + this.$ms + ] + } +} + diff --git a/src/plugin/toObject/index.js b/src/plugin/toObject/index.js new file mode 100644 index 000000000..08d22d844 --- /dev/null +++ b/src/plugin/toObject/index.js @@ -0,0 +1,15 @@ +export default (o, c) => { + const proto = c.prototype + proto.toObject = function () { + return { + years: this.$y, + months: this.$M, + date: this.$D, + hours: this.$H, + minutes: this.$m, + seconds: this.$s, + milliseconds: this.$ms + } + } +} + diff --git a/types/index.d.ts b/types/index.d.ts index d6f547d22..abadb4692 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -12,16 +12,6 @@ declare namespace dayjs { type OpUnitTypeShort = 'w' export type OpUnitType = UnitType | "week" | OpUnitTypeShort; - interface DayjsObject { - years: number - months: number - date: number - hours: number - minutes: number - seconds: number - milliseconds: number - } - class Dayjs { constructor (config?: ConfigType) @@ -67,14 +57,10 @@ declare namespace dayjs { toDate(): Date - toArray(): number[] - toJSON(): string toISOString(): string - toObject(): DayjsObject - toString(): string utcOffset(): number diff --git a/types/plugin/toArray.d.ts b/types/plugin/toArray.d.ts new file mode 100644 index 000000000..45f1f0c3c --- /dev/null +++ b/types/plugin/toArray.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + toArray(): number[] + } +} diff --git a/types/plugin/toObject.d.ts b/types/plugin/toObject.d.ts new file mode 100644 index 000000000..ca12aaf00 --- /dev/null +++ b/types/plugin/toObject.d.ts @@ -0,0 +1,20 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +interface DayjsObject { + years: number + months: number + date: number + hours: number + minutes: number + seconds: number + milliseconds: number +} + +declare module 'dayjs' { + interface Dayjs { + toObject(): DayjsObject + } +}