import { flatten } from 'lodash' ; import { ASCENDING , DESCENDING } from './constants' ; import { Fields , SortOption , SortDirection } from './types' ; const fieldNameToSortOptions = ( fieldName ) : SortOption [ ] => [ 'asc' , 'desc' ] . map ( ( direction ) => ( { name : direction === 'asc' ? ASCENDING ( fieldName ) : DESCENDING ( fieldName ) , value : fieldName , direction : direction as SortDirection , } ) ) ; /**
 * Adds two sort options for a given field, a "desc" and an "asc" option.
 */ export const buildSortOptions = ( fields , defaultSortOptions ) : SortOption [ ] => { const sortFieldsOptions = flatten ( fields . sortFields . map ( fieldNameToSortOptions ) ) ; const sortingOptions = [ ... defaultSortOptions , ... sortFieldsOptions ] ; return sortingOptions ; } ;