import { Injectable } from '@angular/core' ; @ Injectable ( ) export class CookieService { param { string } name returns public check ( name ) { if ( typeof document === "undefined" ) return false ; name = encodeURIComponent ( name ) ; let regexp = new RegExp ( '(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)' , 'g' ) ; let exists = regexp . test ( document . cookie ) ; return exists ; } param { string } name returns public get ( name ) { if ( this . 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 '' ; } } returns public getAll ( ) { let cookies = { } ; if ( document . cookie && document . cookie != '' ) { let split = document . cookie . split ( ';' ) ; for ( let s of split ) { let currCookie = s . 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 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 * 1000 * 60 * 60 * 24 ) ; cookieStr += 'expires=' + dtExpires . toUTCString ( ) + ';' ; } else { cookieStr += 'expires=' + expires . toUTCString ( ) + ';' ; } } if ( ! path ) { 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 delete ( name , path ? , domain ? ) { this . set ( name , '' , - 1 , path , domain ) ; } /**
     * Delete all cookie avaiable
     */ public deleteAll ( path ? , domain ? ) { let cookies = this . getAll ( ) ; for ( let cookieName of Object . keys ( cookies ) ) { this . delete ( cookieName , path , domain ) ; } } } export const Cookie = new CookieService ( ) ;