import { CSSObject } from 'styled-components' ; import { ConditionalStyles , TableColumn , Format , TableRow , Selector , SortOrder , SortFunction } from './types' ; export function prop < T , K extends keyof T > ( obj , key ) { return obj [ key ] ; } export function isEmpty ( field = '' ) { if ( typeof field === 'number' ) { return false ; } return ! field || field . length === 0 ; } export function sort < T > ( rows , selector , direction , sortFn ? ) { if ( ! selector ) { return rows ; } if ( sortFn && typeof sortFn === 'function' ) { return sortFn ( rows . slice ( 0 ) , selector as Selector < T > , direction ) ; } return rows . slice ( 0 ) . sort ( ( a , b ) => { let aValue ; let bValue ; if ( typeof selector === 'string' ) { aValue = parseSelector ( a , selector ) ; bValue = parseSelector ( b , selector ) ; } else { aValue = selector ( a ) ; bValue = selector ( b ) ; } if ( direction === 'asc' ) { if ( aValue < bValue ) { return - 1 ; } if ( aValue > bValue ) { return 1 ; } } if ( direction === 'desc' ) { if ( aValue > bValue ) { return - 1 ; } if ( aValue < bValue ) { return 1 ; } } return 0 ; } ) ; } export function parseSelector < T extends Record < string , any > > ( row , selector ) { return selector . split ( '.' ) . reduce ( ( acc , part ) => { const arr = part . match ( /[^\]\\[.]+/g ) ; if ( arr && arr . length > 1 ) { for ( let i = 0 ; i < arr . length ; i ++ ) { return acc [ arr [ i ] ] [ arr [ i + 1 ] ] ; } } return acc [ part ] ; } , row ) ; } export function getProperty < T > ( row , selector , format , rowIndex ) { if ( ! selector ) { return null ; } if ( typeof selector !== 'string' && typeof selector !== 'function' ) { throw new Error ( 'selector must be a . delimited string eg (my.property) or function (e.g. row => row.field' ) ; } if ( format && typeof format === 'function' ) { return format ( row , rowIndex ) ; } if ( selector && typeof selector === 'function' ) { return selector ( row , rowIndex ) ; } return parseSelector ( row , selector ) ; } export function insertItem < T > ( array = [ ] , item , index = 0 ) { return [ ... array . slice ( 0 , index ) , item , ... array . slice ( index ) ] ; } export function removeItem < T > ( array = [ ] , item , keyField = 'id' ) { const newArray = array . slice ( ) ; const outerField = prop ( item as TableRow , keyField ) ; if ( outerField ) { newArray . splice ( newArray . findIndex ( ( a ) => { const innerField = prop ( a as TableRow , keyField ) ; return innerField === outerField ; } ) , 1 ) ; } else { newArray . splice ( newArray . findIndex ( a => a === item ) , 1 ) ; } return newArray ; } export function decorateColumns < T > ( columns ) { return columns . map ( ( column , index ) => { const decoratedColumn = { ... column , sortable : column . sortable || ! ! column . sortFunction || undefined , } ; if ( ! column . id ) { decoratedColumn . id = index + 1 ; return decoratedColumn ; } return decoratedColumn ; } ) ; } export function getSortDirection ( ascDirection = false ) { return ascDirection ? SortOrder . ASC : SortOrder . DESC ; } export function handleFunctionProps ( object , ... args ) { let newObject ; Object . keys ( object ) . map ( o => object [ o ] ) . forEach ( ( value , index ) => { const oldObject = object ; if ( typeof value === 'function' ) { newObject = { ... oldObject , [ Object . keys ( object ) [ index ] ] : value ( ... args ) } ; } } ) ; return newObject || object ; } export function getNumberOfPages ( rowCount , rowsPerPage ) { return Math . ceil ( rowCount / rowsPerPage ) ; } export function recalculatePage ( prevPage , nextPage ) { return Math . min ( prevPage , nextPage ) ; } export const noop = ( ) : null => null ; export function getConditionalStyle < T > ( row , conditionalRowStyles = [ ] , baseClassNames = [ ] ) { let rowStyle = { } ; let classNames = [ ... baseClassNames ] ; if ( conditionalRowStyles . length ) { conditionalRowStyles . forEach ( crs => { if ( ! crs . when || typeof crs . when !== 'function' ) { throw new Error ( '"when" must be defined in the conditional style object and must be function' ) ; } if ( crs . when ( row ) ) { rowStyle = crs . style || { } ; if ( crs . classNames ) { classNames = [ ... classNames , ... crs . classNames ] ; } if ( typeof crs . style === 'function' ) { rowStyle = crs . style ( row ) || { } ; } } } ) ; } return { style : rowStyle , classNames : classNames . join ( ' ' ) } ; } export function isRowSelected < T > ( row , selectedRows = [ ] , keyField = 'id' ) { const outerField = prop ( row as TableRow , keyField ) ; if ( outerField ) { return selectedRows . some ( r => { const innerField = prop ( r as TableRow , keyField ) ; return innerField === outerField ; } ) ; } return selectedRows . some ( r => r === row ) ; } export function isOdd ( num ) { return num % 2 === 0 ; } export function findColumnIndexById < T > ( columns , id ) { if ( ! id ) { return - 1 ; } return columns . findIndex ( c => { return equalizeId ( c . id , id ) ; } ) ; } export function equalizeId ( a , b ) { return a == b ; }