Skip to content

Commit c23b049

Browse files
committed
fix: update method to accepts all units
1 parent c9e0ae0 commit c23b049

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

Diff for: src/index.js

+55-28
Original file line numberDiff line numberDiff line change
@@ -183,41 +183,63 @@ export default (options, dayjsClass, dayjsFactory) => {
183183
const old$Set = dayjsClass.prototype.$set;
184184
// Override the default $set method to convert the date to the specified calendar system
185185
dayjsClass.prototype.$set = function (units, int) {
186-
if (("$C" in this && this.$C === "gregory") || !("$C" in this)) {
187-
return old$Set.bind(this)(units, int);
188-
}
189-
const instanceFactory = (d, m, y = this.$y) => {
190-
const convertedDate = calendarSystems[this.$C].convertToGregorian(
186+
const isGregory = !("$C" in this) || this.$C === "gregory";
187+
188+
if (isGregory) return old$Set.call(this, units, int);
189+
190+
const { $d, $u, $C, $y, $M, $D, $H, $m, $s, $ms } = this;
191+
const utcPrefix = $u ? "UTC" : "";
192+
const setFn = (fn, value) => $d[`set${utcPrefix}${fn}`](value);
193+
194+
const instanceFactory = (
195+
d,
196+
M,
197+
y = $y,
198+
h = $H,
199+
m = $m,
200+
s = $s,
201+
ms = $ms
202+
) => {
203+
const { year, month, day } = calendarSystems[$C].convertToGregorian(
191204
y,
192-
m,
205+
M,
193206
d
194207
);
195-
this.$d.setFullYear(convertedDate.year);
196-
this.$d.setMonth(convertedDate.month);
197-
this.$d.setDate(convertedDate.day);
208+
209+
setFn("FullYear", year);
210+
setFn("Month", month);
211+
setFn("Date", day);
212+
setFn("Hours", h);
213+
setFn("Minutes", m);
214+
setFn("Seconds", s);
215+
setFn("Milliseconds", ms);
216+
198217
return this;
199218
};
200-
switch (units) {
201-
case "date":
202-
case "day":
203-
instanceFactory(int, this.$M);
204-
break;
205-
case "month":
206-
instanceFactory(this.$D, int);
207-
break;
208-
case "year":
209-
instanceFactory(this.$D, this.$M, int);
210-
break;
211-
default:
212-
return old$Set.bind(this)(units, int);
219+
220+
const unitSetters = {
221+
date: () => instanceFactory(int, $M),
222+
day: () => instanceFactory(int, $M),
223+
month: () => instanceFactory($D, int),
224+
year: () => instanceFactory($D, $M, int),
225+
hour: () => instanceFactory($D, $M, $y, int),
226+
minute: () => instanceFactory($D, $M, $y, $H, int),
227+
second: () => instanceFactory($D, $M, $y, $H, $m, int),
228+
millisecond: () => instanceFactory($D, $M, $y, $H, $m, $s, int),
229+
};
230+
231+
if (units in unitSetters) {
232+
unitSetters[units]();
233+
} else {
234+
return old$Set.call(this, units, int);
213235
}
236+
214237
this.init();
215-
if ("$C" in this && this.$C !== "gregory") {
216-
return this.toCalendarSystem(this.$C);
217-
}
218-
return this;
238+
239+
return $C !== "gregory" ? this.toCalendarSystem($C) : this;
219240
};
220241

242+
// Override the default add method to convert the date to the specified calendar system
221243
const oldAdd = dayjsClass.prototype.add;
222244
dayjsClass.prototype.add = function (number, units) {
223245
number = Number(number); // eslint-disable-line no-param-reassign
@@ -289,7 +311,9 @@ export default (options, dayjsClass, dayjsFactory) => {
289311
};
290312

291313
// Convert a Day.js instance to a specific calendar system
292-
dayjsClass.prototype.toCalendarSystem = function (calendar) {
314+
dayjsClass.prototype.toCalendarSystem = function (
315+
calendar = defaultCalendarSystem
316+
) {
293317
if (!calendarSystems[calendar]) {
294318
throw new Error(`Calendar system '${calendar}' is not registered.`);
295319
}
@@ -333,7 +357,10 @@ export default (options, dayjsClass, dayjsFactory) => {
333357
}
334358
}
335359
// Update the locale to reflect the new calendar system
336-
dayjsFactory.updateLocale(newInstance.$L, calendarSystems[calendar].localeOverride(newInstance.$L));
360+
dayjsFactory.updateLocale(
361+
newInstance.$L,
362+
calendarSystems[calendar].localeOverride(newInstance.$L)
363+
);
337364
// dayjsFactory.locale(
338365
// newInstance.$L,
339366
// {

0 commit comments

Comments
 (0)