import { array } from 'vega' ; const d3 = require ( 'd3' ) ; const dl = require ( 'datalib' ) ; const MIME = { json : 'application/json' , html : 'text/html' } ; param { Object | string } data param { string } type returns { void } export function toBlob ( data , type ) { const blob = new Blob ( [ data ] , { type : MIME [ type ] } ) ; const url = window . URL . createObjectURL ( blob ) ; return download ( url , type ) ; } param { string } url param { string } ext returns { void } export function download ( url , ext ) { const el = d3 . select ( document . createElement ( 'a' ) ) . attr ( 'href' , url ) . attr ( 'target' , '_blank' ) . attr ( 'download' , 'lyra.' + ext ) . node ( ) ; const evt = document . createEvent ( 'MouseEvents' ) ; evt . initMouseEvent ( 'click' , true , true , document . defaultView , 1 , 0 , 0 , 0 , 0 , false , false , false , false , 0 , null ) ; el . dispatchEvent ( evt ) ; } export function get ( url , cb ) { dl . load ( { url } , ( err , data ) => cb ( data , err ) ) ; } export function fopen ( accept , cb ) { const el = d3 . select ( document . createElement ( 'input' ) ) . attr ( 'type' , 'file' ) . attr ( 'accept' , array ( accept ) . map ( ext => `.${ ext }` ) . join ( ',' ) ) . on ( 'change' , ( ) => { return read ( d3 . event . target . files , cb ) ; } ) . node ( ) . click ( ) ; } export function read ( entries , cb ) { for ( const entry of entries ) { const reader = new FileReader ( ) ; reader . onloadend = ( ( file ) => { return ( e ) => { const res = e . target . result ; cb ( typeof res === 'string' ? res : Buffer . from ( res ) . toString ( 'utf-8' ) , file ) ; } ; } ) ( entry ) ; reader . readAsText ( entry ) ; } }