export function generateGUID ( ) { if ( typeof window . crypto !== 'undefined' && typeof window . crypto . getRandomValues !== 'undefined' ) { const buf = new Uint16Array ( 8 ) ; window . crypto . getRandomValues ( buf ) ; const S4 = function ( num ) { let ret = num . toString ( 16 ) ; while ( ret . length < 4 ) { ret = '0' + ret ; } return ret ; } ; return S4 ( buf [ 0 ] ) + S4 ( buf [ 1 ] ) + '-' + S4 ( buf [ 2 ] ) + '-' + S4 ( buf [ 3 ] ) + '-' + S4 ( buf [ 4 ] ) + '-' + S4 ( buf [ 5 ] ) + S4 ( buf [ 6 ] ) + S4 ( buf [ 7 ] ) ; } else { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( /[xy]/g , function ( c ) { const r = ( Math . random ( ) * 16 ) | 0 ; const v = c === 'x' ? r : ( r & 0x3 ) | 0x8 ; return v . toString ( 16 ) ; } ) ; } } export function to < T , U = Error > ( promise , errorExt ? ) { return promise . then < [ null , T ] > ( ( data ) => [ null , data ] ) . catch < [ U , undefined ] > ( ( err ) => { if ( errorExt ) { Object . assign ( err , errorExt ) ; } return [ err , undefined ] ; } ) ; } export function partition < T > ( array , isValid ) { const pass = [ ] ; const fail = [ ] ; array . forEach ( ( element ) => { if ( isValid ( element ) ) { pass . push ( element ) ; } else { fail . push ( element ) ; } } ) ; return [ pass , fail ] ; } function readParameterFromUrl ( url , parameter ) { try { const parsedUrl = new URL ( url ) ; return parsedUrl . searchParams . get ( parameter ) || '' ; } catch ( error ) { return url ; } } export function parseIACUrl ( url , parameter ) { let result ; if ( Array . isArray ( url ) ) { result = url . map ( ( chunk ) => readParameterFromUrl ( chunk , parameter ) ) ; } else { try { result = readParameterFromUrl ( url , parameter ) . split ( ',' ) ; } catch ( error ) { result = url . split ( ',' ) ; } } result = result . reduce ( ( pv , cv ) => [ ... pv , ... cv . split ( ',' ) ] , [ ] ) ; return result . filter ( ( el ) => el !== '' ) ; } export function serializedDataToUrlString ( data , host = 'airgap-wallet://' , parameter = 'd' ) { return `${ host }?${ parameter }=${ Array . isArray ( data ) ? data . join ( ',' ) : data }` ; } export function toBoolean ( value ) { return ! ! value ; } export function assertNever ( name , arg ) { throw new Error ( `${ name } ${ arg }` ) ; } export const toHexString = ( bytes ) => bytes . reduce ( ( str , byte ) => str + byte . toString ( 16 ) . padStart ( 2 , '0' ) , '' ) ;