File

src/PolyfillDriver.ts

Extends

Driver

Index

Properties
Methods

Constructor

constructor(storage: IStorage)
Parameters :
Name Type Optional Description
storage IStorage

Methods

Private _checkExpiry
_checkExpiry(key: string)
Parameters :
Name Type Optional Description
key string
Returns : void
Public get
get(key: string)
Parameters :
Name Type Optional Description
key string
Returns : any
Public has
has(key: string)
Parameters :
Name Type Optional Description
key string
Returns : boolean
Public key
key(index: )
Parameters :
Name Type Optional Description
index
Returns : string
Public set
set(key: string, data: any, config?: IStorageSetConfig)
Parameters :
Name Type Optional Description
key string
data any
config IStorageSetConfig true
Returns : void

Properties

Public storage
storage: IStorage
Type : IStorage
import {Driver} from './Driver'
import {serializeDataToString, convertFromJSON, isExpired} from './helpers'
import {IStorageSetConfig, ICustomStorage, IStorage} from './metadata'

// This driver is a special driver to handle expiry and other options (similar to cookies)
// stores data as {data: myData, config: {expiry: new Date()}}
export class PollyfillDriver extends Driver {
  constructor(public storage: IStorage) {
    super(storage)
  }

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

  public get(key: string): any {
    this._checkExpiry(key)

    const data = convertFromJSON(this.storage.getItem(key))

    return (data && data.hasOwnProperty('data')) ? data.data : data
  }

  public has(key: string): boolean {
    this._checkExpiry(key)

    const storage = <ICustomStorage>this.storage

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

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

    this._checkExpiry(key)

    return this.storage.key(index)
  }

  private _checkExpiry(key: string) {
    const data = convertFromJSON(this.storage.getItem(key))

    if (data && isExpired(data))
      this.remove(key)
  }
}

results matching ""

    No results matching ""