upsetjs import type { ISet } from '../model' ; import { byCardinality , byName , byComposite , negate } from './utils' ; param set export function asSet < T , S extends { name : string ; elems : readonly T [ ] ; } > ( set ) { return Object . assign ( { type : 'set' as const , cardinality : set . elems . length , } , set ) ; } /**
 * possible set sort orders
 */ export type SortSetOrder = 'cardinality' | 'name' | 'cardinality:desc' | 'name:asc' | 'cardinality:asc' | 'name:desc' ; export interface PostprocessSetOptions { /**
     * order the set by the given criteria
     */ order ? : SortSetOrder ; /**
     * limit to the top N sets after sorting
     */ limit ? : number ; } function toOrder < T , S extends ISet < T > > ( order ? ) { if ( ! order ) { return byName ; } switch ( order ) { case 'cardinality' : case 'cardinality:desc' : return byComposite < S > ( [ byCardinality , byName ] ) ; case 'cardinality:asc' : return byComposite < S > ( [ negate ( byCardinality ) , byName ] ) ; case 'name:desc' : return negate ( byName ) ; default : return byName ; } } internal export function postprocessSets < T , S extends ISet < T > > ( sets , options = { } ) { let r = sets as S [ ] ; if ( options . order ) { const order = toOrder ( options . order ) ; r = r . slice ( ) . sort ( order ) ; } if ( options . limit != null ) { return r . slice ( 0 , options . limit ) ; } return r ; } param sets param options export default function asSets < T , S extends { name : string ; elems : readonly T [ ] ; } > ( sets , options = { } ) { return postprocessSets ( sets . map ( asSet ) , options ) ; }