import { TopLevelUnitSpec } from 'vega-lite/build/src/spec/unit' ; const REGEX = /(\w+)\(([\w\.\/]+)(,([\w\.]+))?\)/ ; /**
 * Convert from ASP to Vega-Lite.
 */ export default function asp2vl ( facts ) { let mark = '' ; let url = 'data/cars.json' ; const encodings = { } ; for ( const value of facts ) { const cleanedValue = value . replace ( /\"/g , '' ) ; const negSymbol = value . trim ( ) . startsWith ( ':-' ) ; const [ _ , predicate , first , __ , second ] = REGEX . exec ( cleanedValue ) as any ; if ( predicate === 'mark' ) { mark = first ; } else if ( predicate === 'data' ) { url = first ; } else if ( predicate !== 'soft' ) { if ( ! encodings [ first ] ) { encodings [ first ] = { } ; } encodings [ first ] [ predicate ] = second || ! negSymbol ; } } const encoding = { } ; for ( const e of Object . keys ( encodings ) ) { const enc = encodings [ e ] ; if ( enc . type === 'quantitative' && enc . zero === undefined && enc . bin === undefined ) { enc . zero = false ; } const scale = { ... ( enc . log ? { type : 'log' } : { } ) , ... ( enc . zero === undefined ? { } : enc . zero ? { zero : true } : { zero : false } ) , } ; encoding [ enc . channel ] = { type : enc . type , ... ( enc . aggregate ? { aggregate : enc . aggregate } : { } ) , ... ( enc . field ? { field : enc . field } : { } ) , ... ( enc . stack ? { stack : enc . stack } : { } ) , ... ( enc . bin !== undefined ? ( + enc . bin === 10 ? { bin : true } : { bin : { maxbins : + enc . bin } } ) : { } ) , ... ( Object . keys ( scale ) . length ? { scale } : { } ) , } ; } return { $schema : 'https://vega.github.io/schema/vega-lite/v4.json' , data : { url : `${ url }` } , mark , encoding , } as TopLevelUnitSpec ; }