import * as hexToRgb from 'hex-to-rgb' ; import { AccountID } from 'caip' ; internal export type Deferred < T > = Promise < T > & { resolve : ( value ? : T | PromiseLike < T > ) => void ; reject : ( reason ? : any ) => void ; } ; internal export function deferred < T > ( ) { let methods ; const promise = new Promise < T > ( ( resolve , reject ) : void => { methods = { resolve , reject } ; } ) ; return Object . assign ( promise , methods ) as Deferred < T > ; } export const hexToRGBA = ( hex , opacity ? ) : string => `rgba(${ hexToRgb ( hex ) as string }, ${ ( opacity || 30 ) / 100 })` ; export const didShorten = ( did ) : string => `${ did . substring ( 0 , 10 ) }...${ did . substring ( did . length - 5 , did . length ) }` ; export function formatCAIP10 ( caip ) { const account = new AccountID ( caip ) ; return `${ account . address . slice ( 0 , 8 ) }…${ account . address . slice ( - 8 ) }` ; } export const ipfsToImg = ( url ) => { let formattedUrl = url . split ( 'ipfs://' ) [ 1 ] ; formattedUrl = `https://ipfs.infura.io/ipfs/${ formattedUrl }` ; return formattedUrl ; } ; export const urlToHost = ( url ) => { const parsed = new URL ( url ) ; return parsed . hostname ; } ; export const urlToIcon = ( url ) => { const host = urlToHost ( url ) ; return host . slice ( 0 , 1 ) . toUpperCase ( ) ; } ; param color export const detectContrast = ( color ) => { const background = hexToRgb ( color ) ; const contrast = Math . round ( ( parseInt ( background [ 0 ] ) * 299 + parseInt ( background [ 1 ] ) * 587 + parseInt ( background [ 2 ] ) * 114 ) / 1000 ) ; return contrast ; } ;