import { Driver } from './Driver' ; import { serializeDataToString , convertFromJSON , isExpired } from './helpers' ; import { IStorageSetConfig , ICustomStorage , IStorage } from './metadata' ; export class PollyfillDriver extends Driver { constructor ( public storage ) { super ( storage ) ; } public set ( key , data , config ? ) { this . storage . setItem ( key , serializeDataToString ( { data , config } ) ) ; } public get ( key ) { this . _checkExpiry ( key ) ; const data = convertFromJSON ( this . storage . getItem ( key ) ) ; return ( data && data . hasOwnProperty ( 'data' ) ) ? data . data : data ; } public has ( key ) { this . _checkExpiry ( key ) ; const storage = < ICustomStorage > this . storage ; return storage . hasItem ? storage . hasItem ( key ) : this . storage . hasOwnProperty ( key ) ; } public key ( index = 0 ) { const key = this . storage . key ( index ) ; this . _checkExpiry ( key ) ; return this . storage . key ( index ) ; } private _checkExpiry ( key ) { const data = convertFromJSON ( this . storage . getItem ( key ) ) ; if ( data && isExpired ( data ) ) this . remove ( key ) ; } }