MCPcopy Create free account
hub / github.com/adobe/react-spectrum / cycleDate

Function cycleDate

packages/@internationalized/date/src/manipulation.ts:296–364  ·  view source on GitHub ↗
(
  value: CalendarDate | CalendarDateTime,
  field: DateField,
  amount: number,
  options?: CycleOptions
)

Source from the content-addressed store, hash-verified

294 options?: CycleOptions
295): CalendarDate;
296export function cycleDate(
297 value: CalendarDate | CalendarDateTime,
298 field: DateField,
299 amount: number,
300 options?: CycleOptions
301): Mutable<CalendarDate | CalendarDateTime> {
302 let mutable: Mutable<CalendarDate | CalendarDateTime> = value.copy();
303
304 switch (field) {
305 case 'era': {
306 let eras = value.calendar.getEras();
307 let eraIndex = eras.indexOf(value.era);
308 if (eraIndex < 0) {
309 throw new Error('Invalid era: ' + value.era);
310 }
311 eraIndex = cycleValue(eraIndex, amount, 0, eras.length - 1, options?.round);
312 mutable.era = eras[eraIndex];
313
314 // Constrain the year and other fields within the era, so the era doesn't change when we balance below.
315 constrain(mutable);
316 break;
317 }
318 case 'year': {
319 if (mutable.calendar.isInverseEra?.(mutable)) {
320 amount = -amount;
321 }
322
323 // The year field should not cycle within the era as that can cause weird behavior affecting other fields.
324 // We need to also allow values < 1 so that decrementing goes to the previous era. If we get -Infinity back
325 // we know we wrapped around after reaching 9999 (the maximum), so set the year back to 1.
326 mutable.year = cycleValue(value.year, amount, -Infinity, 9999, options?.round);
327 if (mutable.year === -Infinity) {
328 mutable.year = 1;
329 }
330
331 if (mutable.calendar.balanceYearMonth) {
332 mutable.calendar.balanceYearMonth(mutable, value);
333 }
334 break;
335 }
336 case 'month':
337 mutable.month = cycleValue(
338 value.month,
339 amount,
340 1,
341 value.calendar.getMonthsInYear(value),
342 options?.round
343 );
344 break;
345 case 'day':
346 mutable.day = cycleValue(
347 value.day,
348 amount,
349 1,
350 value.calendar.getDaysInMonth(value),
351 options?.round
352 );
353 break;

Callers 3

cycleMethod · 0.90
cycleMethod · 0.90
cycleZonedFunction · 0.85

Calls 9

constrainFunction · 0.85
cycleValueFunction · 0.70
copyMethod · 0.65
getErasMethod · 0.65
isInverseEraMethod · 0.65
balanceYearMonthMethod · 0.65
getMonthsInYearMethod · 0.65
getDaysInMonthMethod · 0.65
balanceDateMethod · 0.65

Tested by

no test coverage detected