license import { Cost , Dataset , MimeType_ , URL_ } from './datasets' ; /**
 * Function that can possibly convert between data type T to
 * data type V.
 *
 * It determines if it is able to convert T, based on it's mimetype
 * and returns a mapping of possible resulting mimetypes and
 * resulting data.
 */ export type Converter < T , U > = ( dataset : { url : URL_ ; mimeType : MimeType_ ; cost : Cost ; data : T ; } ) => Array < { mimeType : MimeType_ ; cost : Cost ; data : U ; } > ; /**
 * Applies the converter to a dataset iteratively until all mimetypes are filled out.
 *
 * Uses an implementation of Dijkstra's algorithm.
 */ export function applyConverterDataset < T > ( url , dataset , converter ) { let toProcess = [ ... dataset , ] . map ( ( [ mimeType , [ cost , data ] ] ) => ( { mimeType , cost , data } ) ) ; const processed = new Map ( ) ; while ( toProcess . length !== 0 ) { const { mimeType , cost , data } = toProcess . pop ( ) ! ; if ( processed . has ( mimeType ) && processed . get ( mimeType ) ! [ 0 ] <= cost ) { continue ; } processed . set ( mimeType , [ cost , data ] ) ; toProcess = toProcess . concat ( converter ( { cost , data , mimeType , url } ) ) ; } return processed ; } export function combineManyConverters < T , U > ( ... converters ) { return ( dataset ) => converters . map ( ( c ) => c ( dataset ) ) . reduce ( ( l , r ) => l . concat ( r ) , [ ] ) ; }