File

src/Driver.ts

Index

Properties
Methods

Constructor

constructor(storage: IWebStorage | ICustomStorage)
Defined in src/Driver.ts:6
Parameters :
Name Type Optional Description
storage IWebStorage | ICustomStorage

Methods

Public clear
clear()
Defined in src/Driver.ts:27
Returns : void
Public get
get(key: string)
Defined in src/Driver.ts:13
Parameters :
Name Type Optional Description
key string
Returns : any
Public has
has(key: string)
Defined in src/Driver.ts:17
Parameters :
Name Type Optional Description
key string
Returns : boolean
Public isSupported
isSupported()
Defined in src/Driver.ts:35
Returns : boolean
Public key
key(index: )
Defined in src/Driver.ts:31
Parameters :
Name Type Optional Description
index
Returns : string
Public remove
remove(key: string)
Defined in src/Driver.ts:23
Parameters :
Name Type Optional Description
key string
Returns : void
Public set
set(key: string, data: any, config?: IStorageSetConfig)
Defined in src/Driver.ts:9
Parameters :
Name Type Optional Description
key string
data any
config IStorageSetConfig true
Returns : void

Properties

Public storage
storage: IWebStorage | ICustomStorage
Type : IWebStorage | ICustomStorage
Defined in src/Driver.ts:7
import {IStorageSetConfig, IWebStorage, ICustomStorage} from './metadata'
import {convertFromJSON, serializeDataToString} from './helpers'

const LOCKER_TEST_KEY = 'LOCKER_TEST_KEY'

export class Driver {
  constructor(public storage: IWebStorage | ICustomStorage) { }

  public set(key: string, data: any, config?: IStorageSetConfig): void {
    this.storage.setItem(key, serializeDataToString(data), config)
  }

  public get(key: string): any {
    return convertFromJSON(this.storage.getItem(key))
  }

  public has(key: string): boolean {
    const storage = <ICustomStorage>this.storage

    return storage.hasItem ? storage.hasItem(key) : this.storage.hasOwnProperty(key)
  }

  public remove(key: string): void {
    this.storage.removeItem(key)
  }

  public clear(): void {
    this.storage.clear()
  }

  public key(index = 0): string {
    return this.storage.key(index)
  }

  public isSupported(): boolean {
    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
  }
}

results matching ""

    No results matching ""