/**
 * Class Cookie - Holds static functions to deal with Cookies
 */ import { Constants } from '../util/constants' ; export class Cookie { /**
     * Checks the existence of a single cookie by it's name
     */ public static check ( name ) { name = encodeURIComponent ( name ) ; let regexp = new RegExp ( '(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)' , 'g' ) ; let exists = regexp . test ( document . cookie ) ; return exists ; } /**
     * Retrieves a single cookie by it's name
     */ public static get ( name ) { if ( Cookie . check ( name ) ) { name = encodeURIComponent ( name ) ; let regexp = new RegExp ( '(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)' , 'g' ) ; let result = regexp . exec ( document . cookie ) ; return decodeURIComponent ( result [ 1 ] ) ; } else { return null ; } } /**
     * Retrieves all available cookies
     */ public static getAll ( ) { let cookies = { } ; if ( document . cookie && document . cookie != '' ) { let split = document . cookie . split ( ';' ) ; for ( let i = 0 ; i < split . length ; i ++ ) { let currCookie = split [ i ] . split ( '=' ) ; currCookie [ 0 ] = currCookie [ 0 ] . replace ( /^ / , '' ) ; cookies [ decodeURIComponent ( currCookie [ 0 ] ) ] = decodeURIComponent ( currCookie [ 1 ] ) ; } } return cookies ; } param { string } name param { string } value param { number } expires param { string } path param { string } domain param { boolean } secure public static set ( name , value , expires ? , path ? , domain ? , secure ? ) { let cookieStr = encodeURIComponent ( name ) + '=' + encodeURIComponent ( value ) + ';' ; if ( expires ) { if ( typeof expires === 'number' ) { let dtExpires = new Date ( new Date ( ) . getTime ( ) + expires * 60 * 1000 ) ; cookieStr += 'expires=' + dtExpires . toUTCString ( ) + ';' ; } else { cookieStr += 'expires=' + expires . toUTCString ( ) + ';' ; } } if ( path ) { cookieStr += 'path=' + path + ';' ; } if ( domain ) { cookieStr += 'domain=' + domain + ';' ; } if ( secure ) { cookieStr += 'secure;' ; } document . cookie = cookieStr ; } param { string } name param { string } path param { string } domain public static delete ( name , path ? , domain ? ) { if ( Cookie . get ( name ) ) { Cookie . set ( name , '' , - 1 , path , domain ) ; } } /**
     * Delete all cookies
     */ public static deleteAll ( path ? , domain ? ) { let cookies = Cookie . getAll ( ) ; for ( let cookieName in cookies ) { Cookie . delete ( cookieName , path , domain ) ; } } }