upsetjs import { ISetLike , ISetOverlapFunction , setElemOverlapFactory , setOverlapFactory } from '@upsetjs/model' ; import type { UpSetSelection } from './interfaces' ; export function clsx ( ... classNames ) { return classNames . filter ( Boolean ) . join ( ' ' ) ; } export function generateId ( _args ? ) { return `upset-${ Math . random ( ) . toString ( 36 ) . slice ( 4 ) }` ; } export function isSetLike < T > ( s ? ) { return s != null && ! Array . isArray ( s ) ; } function elemOverlapOf < T > ( query , toElemKey ? ) { const f = setOverlapFactory ( query , toElemKey ) ; return ( s ) => { return f ( s . elems ) . intersection ; } ; } export function noGuessPossible ( ) { return - 1 ; } export function generateSelectionOverlap < T > ( selection , overlapGuesser , toElemKey ? ) { if ( ! selection ) { return noOverlap ; } if ( typeof selection === 'function' ) { return selection ; } if ( Array . isArray ( selection ) ) { return elemOverlapOf ( selection , toElemKey ) ; } const ss = selection as ISetLike < T > ; if ( ss . overlap ) { return ss . overlap . bind ( ss ) ; } let f = null ; return ( s ) => { if ( s . overlap ) { return s . overlap ( ss ) ; } const guess = overlapGuesser ( s , ss ) ; if ( guess >= 0 ) { return guess ; } if ( ! f ) { f = elemOverlapOf ( ss . elems , toElemKey ) ; } return f ( s ) ; } ; } export function generateSelectionName < T > ( selection ? ) { return Array . isArray ( selection ) ? `Array(${ selection . length })` : typeof selection === 'function' ? '?' : ( selection as ISetLike < T > ) ?. name ; } export function elemElemOverlapOf < T > ( query , toElemKey ? ) { const f = setElemOverlapFactory ( query , toElemKey ) ; return ( s ) => { return f ( s . elems ) . intersection ; } ; } export function noOverlap ( ) { return 0 ; } export function parseFontSize ( v ? ) { if ( v == null ) { return 10 ; } if ( v . endsWith ( 'pt' ) ) { return Math . floor ( ( 4 / 3 ) * Number . parseInt ( v , 10 ) ) ; } return Number . parseInt ( v , 10 ) ; } export function toAnchor ( alignment ) { const alignments = { left : 'start' , center : 'middle' , right : 'end' , } ; return alignments [ alignment ] ?? 'middle' ; }