upsetjs import type { ISets , ISet } from '../model' ; import { PostprocessSetOptions , postprocessSets } from './asSets' ; param elements param acc param options export default function extractSets < T > ( elements , acc , options ? ) ; param elements param options export default function extractSets < T extends { sets : string [ ] ; } > ( elements , options ? ) ; export default function extractSets < T > ( elements , accOrOptions ? , o = { } ) { const acc = typeof accOrOptions === 'function' ? accOrOptions : ( e ) => ( e as unknown as { sets : string [ ] ; } ) . sets ; const options = ( typeof accOrOptions !== 'function' ? accOrOptions : o ) ?? { } ; const sets = Object . create ( null ) ; elements . forEach ( ( elem ) => { acc ( elem ) . forEach ( ( set ) => { const s = typeof set === 'string' ? set : String ( set ) ; const r = sets [ s ] ; if ( r == null ) { sets [ s ] = [ elem ] ; } else { r . push ( elem ) ; } } ) ; } ) ; return postprocessSets ( Object . entries ( sets ) . map ( ( [ set , elems ] ) => { const r = { type : 'set' , elems , name : String ( set ) , cardinality : elems . length , } ; return r ; } ) , options ) ; }