import { IStorageSetConfig , IWebStorage , ICustomStorage } from './metadata' ; import { convertFromJSON , serializeDataToString } from './helpers' ; const LOCKER_TEST_KEY = 'LOCKER_TEST_KEY' ; export class Driver { constructor ( public storage ) { } public set ( key , data , config ? ) { this . storage . setItem ( key , serializeDataToString ( data ) , config ) ; } public get ( key ) { return convertFromJSON ( this . storage . getItem ( key ) ) ; } public has ( key ) { const storage = < ICustomStorage > this . storage ; return storage . hasItem ? storage . hasItem ( key ) : this . storage . hasOwnProperty ( key ) ; } public remove ( key ) { this . storage . removeItem ( key ) ; } public clear ( ) { this . storage . clear ( ) ; } public key ( index = 0 ) { return this . storage . key ( index ) ; } public isSupported ( ) { try { this . storage . setItem ( LOCKER_TEST_KEY , LOCKER_TEST_KEY ) ; this . storage . getItem ( LOCKER_TEST_KEY ) ; this . storage . removeItem ( LOCKER_TEST_KEY ) ; } catch ( e ) { return false ; } return true ; } }