export function saveTextToFile ( textstr , filename ) { const blobToSave = new Blob ( [ textstr ] , { type : "text/plain" , } ) ; let blobURL = "" ; if ( typeof window . webkitURL !== "undefined" ) { blobURL = window . webkitURL . createObjectURL ( blobToSave ) ; } else { blobURL = window . URL . createObjectURL ( blobToSave ) ; } const link = document . createElement ( "a" ) ; link . id = "jspsych-download-as-text-link" ; link . style . display = "none" ; link . download = filename ; link . href = blobURL ; link . click ( ) ; } export function JSON2CSV ( objArray ) { const array = typeof objArray != "object" ? JSON . parse ( objArray ) : objArray ; let line = "" ; let result = "" ; const columns = [ ] ; for ( const row of array ) { for ( const key in row ) { let keyString = key + "" ; keyString = '"' + keyString . replace ( /"/g , '""' ) + '",' ; if ( ! columns . includes ( key ) ) { columns . push ( key ) ; line += keyString ; } } } line = line . slice ( 0 , - 1 ) ; result += line + "\r\n" ; for ( const row of array ) { line = "" ; for ( const col of columns ) { let value = typeof row [ col ] === "undefined" ? "" : row [ col ] ; if ( typeof value == "object" ) { value = JSON . stringify ( value ) ; } const valueString = value + "" ; line += '"' + valueString . replace ( /"/g , '""' ) + '",' ; } line = line . slice ( 0 , - 1 ) ; result += line + "\r\n" ; } return result ; } export function getQueryString ( ) { const a = window . location . search . substr ( 1 ) . split ( "&" ) ; const b = { } ; for ( let i = 0 ; i < a . length ; ++ i ) { const p = a [ i ] . split ( "=" , 2 ) ; if ( p . length == 1 ) b [ p [ 0 ] ] = "" ; else b [ p [ 0 ] ] = decodeURIComponent ( p [ 1 ] . replace ( /\+/g , " " ) ) ; } return b ; }