( value: CalendarDate | CalendarDateTime, field: DateField, amount: number, options?: CycleOptions )
| 294 | options?: CycleOptions |
| 295 | ): CalendarDate; |
| 296 | export 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; |
no test coverage detected