All files / src/date-range-picker/utils build-year-dropdown.js

100% Statements 5/5
100% Branches 0/0
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19                      15x 1x 1x 22x 22x      
import { addYears, differenceInCalendarYears } from "date-fns"
import { localeFormat } from "./locale-format"
 
/**
 *
 * @param {Date} min - Earliest allowed date
 * @param {Date} max - Latest allowed date
 * @param {number} pageNum - Each page starts a month later than the last
 *
 * @returns {object[]} - { value: Date, text: string }
 */
export const buildYearDropdown = (min, max, pageNum) => {
  const numYrs = differenceInCalendarYears(max, min) + pageNum + 1
  return [...Array(numYrs)].map((_, i) => {
    const value = addYears(min, i)
    return { value, text: localeFormat(value, "yyyy") }
  })
}