upsetjs import type { ISetCombination , SetCombinationType , ISets } from '../model' ; import { byCardinality , byComposite , byDegree , byGroup , byName , negate } from './utils' ; import type { SortSetOrder } from './asSets' ; param sets param symbol export function fromSetName < T > ( sets , symbol = /[∩∪&|]/ ) { const byName = new Map ( sets . map ( ( s ) => [ s . name , s ] ) ) ; return ( s ) => { return s . name . split ( symbol ) . map ( ( setName ) => byName . get ( setName . trim ( ) ) ! ) ; } ; } /**
 * sort orders for set combinations
 */ export type SortCombinationOrder = SortSetOrder | 'group' | 'degree' | 'group:asc' | 'group:desc' | 'degree:asc' | 'degree:desc' ; export type SortCombinationOrders = readonly SortCombinationOrder [ ] ; export interface PostprocessCombinationsOptions { /**
     * order the sets combinations by the given criteria
     */ order ? : SortCombinationOrder | SortCombinationOrders ; /**
     * limit to the top N after sorting
     */ limit ? : number ; } function toOrder < T , S extends ISetCombination < T > > ( sets , order ? ) { if ( ! order ) { return byName ; } const arr = ( Array . isArray ( order ) ? order : [ order ] ) as SortCombinationOrders ; if ( arr . length === 0 ) { return byName ; } return byComposite < S > ( arr . map ( ( o ) => { switch ( o ) { case 'cardinality' : case 'cardinality:desc' : return byCardinality ; case 'cardinality:asc' : return negate ( byCardinality ) ; case 'name:desc' : return negate ( byName ) ; case 'degree' : case 'degree:asc' : return byDegree ; case 'degree:desc' : return negate ( byDegree ) ; case 'group' : case 'group:asc' : return byGroup ( sets ) ; case 'group:desc' : return negate ( byGroup ( sets ) ) ; default : return byName ; } } ) ) ; } internal export function postprocessCombinations < T , S extends ISetCombination < T > > ( sets , combinations , options = { } ) { let r = combinations as S [ ] ; if ( options . order ) { r = r . sort ( toOrder ( sets , options . order ) ) ; } if ( options . limit != null ) { return r . slice ( 0 , options . limit ) ; } return r ; } param sets export function asCombination < T , S extends { name : string ; elems : readonly T [ ] ; } > ( set , type , toSets ) { const sets = toSets ( set ) ; return Object . assign ( { type , cardinality : set . elems . length , sets : new Set ( sets ) , degree : sets . length , } , set ) ; } param sets param type param toSets export default function asCombinations < T , S extends { name : string ; elems : readonly T [ ] ; } > ( sets , type , toSets ) { return sets . map ( ( set ) => asCombination ( set , type , toSets ) ) ; }