export const getCookieDomain = ( ) => { return location . hostname . replace ( 'www.' , '' ) ; } ; let cookieParsingCache ; export const getCookies = ( useCache = false ) : Record < string , string > => { if ( useCache && cookieParsingCache ) { return cookieParsingCache ; } let res = { } ; let cookies = document . cookie . split ( ';' ) ; for ( let i = 0 ; i < cookies . length ; i ++ ) { let cookie = cookies [ i ] ; let idx = cookie . indexOf ( '=' ) ; if ( idx > 0 ) { res [ cookie . substr ( i > 0 ? 1 : 0 , i > 0 ? idx - 1 : idx ) ] = cookie . substr ( idx + 1 ) ; } } cookieParsingCache = res ; return res ; } ; export const getCookie = ( name ) => { if ( ! name ) { return null ; } return decodeURIComponent ( document . cookie . replace ( new RegExp ( "(?:(?:^|.*;)\\s*" + encodeURIComponent ( name ) . replace ( /[\-\.\+\*]/g , "\\$&" ) + "\\s*\\=\\s*([^;]*).*$)|^.*$" ) , "$1" ) ) || null ; } ; export const setCookie = ( name , value , expire , domain , secure ) => { const expireString = expire === Infinity ? " expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + expire ; document . cookie = encodeURIComponent ( name ) + "=" + value + "; path=/;" + expireString + ( domain ? "; domain=" + domain : "" ) + ( secure ? "; secure" : "" ) ; } ; export const deleteCookie = ( name ) => { document . cookie = name + '= ; expires = Thu, 01 Jan 1970 00:00:00 GMT' ; } ; export const generateId = ( ) => Math . random ( ) . toString ( 36 ) . substring ( 2 , 12 ) ; export const generateRandom = ( ) => Math . random ( ) . toString ( 36 ) . substring ( 2 , 7 ) ; export const parseQuery = ( qs ? ) => { let queryString = qs || window . location . search . substring ( 1 ) ; let query = { } ; let pairs = ( queryString [ 0 ] === '?' ? queryString . substr ( 1 ) : queryString ) . split ( '&' ) ; for ( let i = 0 ; i < pairs . length ; i ++ ) { let pair = pairs [ i ] . split ( '=' ) ; query [ decodeURIComponent ( pair [ 0 ] ) ] = decodeURIComponent ( pair [ 1 ] || '' ) ; } return query ; } ; const UTM_TYPES = { utm_source : "source" , utm_medium : "medium" , utm_campaign : "campaign" , utm_term : "term" , utm_content : "content" } ; const CLICK_IDS = { gclid : true , fbclid : true , dclid : true } ; export const getDataFromParams = ( params ) => { const result = { utm : { } as Record < string , string > , click_id : { } as Record < string , any > } ; for ( let name in params ) { if ( ! params . hasOwnProperty ( name ) ) { continue ; } const val = params [ name ] ; const utm = UTM_TYPES [ name ] ; if ( utm ) { result . utm [ utm ] = val ; } else if ( CLICK_IDS [ name ] ) { result . click_id [ name ] = val ; } } return result ; } ; export const reformatDate = ( strDate ) => { const end = strDate . split ( '.' ) [ 1 ] ; if ( ! end ) { return strDate ; } if ( end . length >= 7 ) { return strDate ; } return strDate . slice ( 0 , - 1 ) + '0' . repeat ( 7 - end . length ) + 'Z' ; } ; function endsWith ( str , suffix ) { return str . indexOf ( suffix , str . length - suffix . length ) !== - 1 ; } export const getHostWithProtocol = ( host ) => { while ( endsWith ( host , "/" ) ) { host = host . substr ( 0 , host . length - 1 ) ; } if ( host . indexOf ( "https://" ) === 0 || host . indexOf ( "http://" ) === 0 ) { return host ; } else { return "//" + host ; } } ; export function awaitCondition < T > ( condition , factory , timeout = 500 , retries = 4 ) { return new Promise < T > ( ( resolve , reject ) => { if ( condition ( ) ) { resolve ( factory ( ) ) ; return ; } if ( retries === 0 ) { reject ( 'condition rejected' ) ; return ; } setTimeout ( ( ) => { awaitCondition ( condition , factory , timeout , retries - 1 ) . then ( resolve ) . catch ( reject ) ; } , timeout ) ; } ) ; }