UNPKG

@rjsf/utils

Version:
29 lines 1.39 kB
import pad from './pad.js'; /** Returns a list of options for a date range between `start` and `stop`. If the start date is greater than the end * date, then the date range is reversed. If `start` and `stop` are negative numbers (or zero), then they will be * treated as relative to the current year. * * @param start - The starting point of the date range * @param stop - The ending point of the date range * @returns - The list of EnumOptionsType for the date range between `start` and `stop` * @throws - Error when `start` and `stop` aren't both <= 0 or > 0 */ export default function dateRangeOptions(start, stop) { const bothRelative = start <= 0 && stop <= 0; const eitherNegative = start < 0 || stop < 0; if (!bothRelative && eitherNegative) { throw new Error(`Both start (${start}) and stop (${stop}) must both be <= 0 or > 0, got one of each`); } const currentYear = new Date().getFullYear(); const resolvedStart = bothRelative ? currentYear + start : start; const resolvedStop = bothRelative ? currentYear + stop : stop; if (resolvedStart > resolvedStop) { return dateRangeOptions(stop, start).reverse(); } const options = []; for (let i = resolvedStart; i <= resolvedStop; i += 1) { options.push({ value: i, label: pad(i, 2) }); } return options; } //# sourceMappingURL=dateRangeOptions.js.map