import { StorageAdapter } from './storage-adapter' ; export class CookieAdapter implements StorageAdapter { getItem ( key ) { if ( document . cookie ) { const cookies = document . cookie . split ( ';' ) ; const match = cookies . map ( cookie => ( { key : cookie . split ( '=' ) [ 0 ] . trim ( ) , value : cookie . split ( '=' ) [ 1 ] . trim ( ) } ) ) . find ( cookie => cookie . key === key ) ; return match ? match . value : null ; } return null ; } setItem ( key , value ) { document . cookie = `${ key }=${ value }; path=/` ; } removeItem ( key ) { document . cookie . split ( ';' ) . forEach ( cookie => { const eqPos = cookie . indexOf ( '=' ) ; const name = eqPos > - 1 ? cookie . substr ( 0 , eqPos ) . trim ( ) : cookie ; if ( name === key ) { document . cookie = cookie . trim ( ) . replace ( /=.*/ , `=;expires=${ new Date ( ) . toUTCString ( ) };path=/` ) ; } } ) ; } clear ( ) { document . cookie . split ( ';' ) . map ( cookie => cookie . split ( '=' ) [ 0 ] . trim ( ) ) . forEach ( cookie => this . removeItem ( cookie ) ) ; } getSupported ( ) { return this ; } }