import { PaginationState } from './types' ; const buildRange = ( max , min ) : number [ ] => { return [ ... Array ( max ) ] . map ( ( _ , index ) => min + index ) ; } ; type BuildPageRangeConfig = Pick < PaginationState , 'currentPage' | 'lastPage' | 'rangeSize' > ; export const buildPageRange = ( { currentPage , lastPage , rangeSize } ) => { const adjustedRangeSize = lastPage < rangeSize ? lastPage : rangeSize ; if ( currentPage <= Math . floor ( rangeSize / 2 ) ) { const rangeMin = 1 ; return buildRange ( adjustedRangeSize , rangeMin ) ; } if ( currentPage + Math . floor ( adjustedRangeSize / 2 ) > lastPage ) { const rangeMin = lastPage - adjustedRangeSize + 1 ; return buildRange ( adjustedRangeSize , rangeMin ) ; } const rangeMin = currentPage - Math . floor ( adjustedRangeSize / 2 ) ; return buildRange ( adjustedRangeSize , rangeMin ) ; } ;