Skip to content

Instantly share code, notes, and snippets.

@T4rk1n
Created May 5, 2017 18:33
Show Gist options
  • Save T4rk1n/85c9e012bda97657a90565ad0bf70a11 to your computer and use it in GitHub Desktop.
Save T4rk1n/85c9e012bda97657a90565ad0bf70a11 to your computer and use it in GitHub Desktop.
const defaultStorageOptions = {
onSuccess: (value) => {},
onError: (error) => {}
}
/**
* Base class with `abstract` methods.
*/
export class BaseStorage {
/**
* Get a persistent storage item.
*
* @param {string} key
* @param {any} [options=defaultStorageOptions]
*
* @memberOf BaseStorage
*/
getStorageItem(key, options=defaultStorageOptions) {
throw new Error("Abstract method called")
}
/**
* Set a persistent storage item.
*
* @param {string} key
* @param {any} value
* @param {} [options=defaultStorageOptions]
*
* @memberOf BaseStorage
*/
setStorageItem(key, value, options=defaultStorageOptions) {
throw new Error("Abstract method called")
}
removeStorageItem(key, options=defaultStorageOptions) {
throw new Error("Abstract method called")
}
}
export class BaseBrowserStorage extends BaseStorage {
/**
* Base class for the browser storage options (localStorage, sessionStorage).
*/
constructor() {
super()
//if (new.target === BaseBrowserStorage) throw TypeError("abstract class can't be created!")
if (typeof window === 'undefined') throw TypeError("Not in browser but using browser storage ?")
this._storage = null
}
getStorageItem(key, options={isJson: false}) {
const { isJson } = options
let j = this._storage.getItem(key)
if (j && isJson) {
j = JSON.parse(j)
}
return j
}
setStorageItem(key, value, option={}) {
this._storage.setItem(key, typeof(value) !== 'string' ? JSON.stringify(value) : value)
}
removeStorageItem(key) {
this._storage.removeItem(key)
}
}
export class BrowserSessionStorage extends BaseBrowserStorage {
/**
* Container for window.sessionStorage
*/
constructor() {
super()
//noinspection Eslint
this._storage = window.sessionStorage
}
}
export class BrowserLocalStorage extends BaseBrowserStorage {
/**
* Container for window.localStorage
*/
constructor() {
super()
//noinspection Eslint
this._storage = window.localStorage
}
}
/**
* Storage using cookies
*/
export class BrowserCookieStorage extends BaseStorage {
getStorageItem(key, options={}) {
// found on http://stackoverflow.com/questions/5639346/what-is-the-shortest-function-for-reading-a-cookie-by-name-in-javascript
//noinspection Eslint
const cook = document.cookie.match('(^|;)\\s*' + key + '\\s*=\\s*([^;]+)')
return cook ? cook.pop() : ''
}
setStorageItem(key, value, options={expiration:null, cookiepath:null, ...defaultStorageOptions}) {
//noinspection Eslint
const { expiration, cookiepath } = options
document.cookie = `${key}=${value};path=${cookiepath || ''} ;expires=${expiration || ""}} }`
}
removeStorageItem(key) {
//noinspection Eslint
document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC;`
}
}
/**
* Will return a base storage with the wanted methods.
* @param {function} getMethod
* @param {function} setMethod
* @param {function} delMethod
* @returns {BaseStorage}
* @constructor
*/
export const CustomStorage = (getMethod, setMethod, delMethod) => {
let storage = new BaseStorage()
storage.prototype.getStorageItem = getMethod
storage.prototype.setStorageItem = setMethod
storage.prototype.removeStorageItem = delMethod
return storage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment