/**
 * Generates a set of pages for the provided total pages. Ensures
 * the active page is centered unless we can't fill the specified number
 * of visible pages.
 */ export default function generatePages ( totalPages , page , maxVisiblePages ) { if ( totalPages <= 1 ) { return [ ] ; } if ( totalPages <= maxVisiblePages ) { return fill ( 1 , totalPages ) ; } let startPage = Math . max ( page - Math . floor ( maxVisiblePages / 2 ) , 1 ) ; let endPage = startPage + ( maxVisiblePages - 1 ) ; if ( endPage > totalPages ) { startPage = Math . max ( startPage - ( endPage - totalPages ) , 1 ) ; endPage = startPage + ( maxVisiblePages - 1 ) ; } const pages = fill ( startPage , endPage ) ; pages [ 0 ] = 1 ; pages [ pages . length - 1 ] = totalPages ; if ( pages [ 1 ] !== pages [ 0 ] + 1 ) { pages [ 1 ] = pages [ 2 ] - 2 ; } if ( pages [ pages . length - 2 ] !== pages [ pages . length - 1 ] - 1 ) { pages [ pages . length - 2 ] = pages [ pages . length - 3 ] + 2 ; } return pages ; } function fill ( from , to ) { const array = [ ] ; for ( let i = from ; i <= to ; i += 1 ) { array . push ( i ) ; } return array ; }