import { ContextFunc , ConstOrPromise } from '../generic-types' ; import { BasicOptions } from './basic' ; export type SetPageFunction < Context > = ( context : Context , page : number ) => ConstOrPromise < void > ; export type GetCurrentPageFunction < Context > = ContextFunc < Context , number | undefined > ; export type GetTotalPagesFunction < Context > = ContextFunc < Context , number > ; export interface GenericPaginationOptions < Context > { example readonly setPage : SetPageFunction < Context > ; example readonly getCurrentPage : GetCurrentPageFunction < Context > ; } export interface PaginationOptions < Context > extends BasicOptions < Context > , GenericPaginationOptions < Context > { example readonly getTotalPages : GetTotalPagesFunction < Context > ; } param totalPages param currentPage return export function createPaginationChoices ( totalPages , currentPage ) { const totalPagesFixed = Math . ceil ( totalPages ) ; const currentPageFinite = ( typeof currentPage === 'number' && Number . isFinite ( currentPage ) ) ? currentPage : 1 ; const currentPageFixed = Math . max ( 1 , Math . min ( totalPagesFixed , Math . floor ( currentPageFinite ) ) ) ; const buttons = { } ; if ( ! Number . isFinite ( totalPagesFixed ) || ! Number . isFinite ( currentPageFixed ) || totalPagesFixed < 2 ) { return buttons ; } const before = currentPageFixed - 1 ; const after = currentPageFixed + 1 ; if ( currentPageFixed > 1 ) { if ( before > 1 ) { buttons [ 1 ] = '1 ⏪' ; } buttons [ before ] = `${ before } ◀️` ; } buttons [ currentPageFixed ] = String ( currentPageFixed ) ; if ( currentPageFixed < totalPagesFixed ) { buttons [ after ] = `▶️ ${ after }` ; if ( after < totalPagesFixed ) { buttons [ totalPagesFixed ] = `⏩ ${ totalPagesFixed }` ; } } return buttons ; }