import type { CountPrice , CountPriceObject } from 'types/Rides' ; const compareFn = ( a , b , pattern ) => { return pattern . indexOf ( a ) - pattern . indexOf ( b ) ; } ; export const getSortedPattern = < T extends CountPriceObject > ( obj , pattern , property ? ) : T => { return Object . entries ( obj ) . sort ( ( [ key1 , val1 ] , [ key2 , val2 ] ) => property ? val2 [ property ] - val1 [ property ] : compareFn ( key1 , key2 , pattern ) ) . reduce ( ( tmp , [ key , val ] ) => ( { ... tmp , [ key ] : val } ) , { } ) as T ; } ; const getBucket = ( number ) : Array < number > => { const size = Math . ceil ( number / 9 ) ; const fill = Math . ceil ( number / size ) ; const diffFromMax = fill * size - number ; const bucket = new Array ( size ) . fill ( fill ) ; for ( let i = 0 ; i < diffFromMax ; i ++ ) { bucket [ size - i - 1 ] -- ; } return bucket ; } ; export const getCarsChunks = < T extends CountPriceObject > ( obj ) => { const buckets = getBucket ( Object . keys ( obj ) . length ) ; const arr = Object . entries ( obj ) ; const size = buckets [ 0 ] ; let to = 0 ; return buckets . reduce ( ( tmp , bucket , index ) => { to += bucket ; const chunk = arr . slice ( index * size , to ) . reduce ( ( tmp , [ key , val ] ) => ( { ... tmp , [ key ] : val } ) , { } ) as T ; tmp . push ( getSortedPattern ( chunk , [ ] , 'count' ) ) ; return tmp ; } , [ ] ) ; } ;