all files / src/ lsstorage.ts

100% Statements 12/12
66.67% Branches 4/6
100% Functions 5/5
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                15× 15× 15×   16×     14×    
import { Option, lsStorage } from './interfaces'
 
/**
 * Create customize localStorage
 */
export default class LSStorage {
	private storage: Storage
	private stringify: (any) => string
	private parse: (string) => any
	constructor({ storage = window.localStorage, stringify = JSON.stringify, parse = JSON.parse }: Option) {
		this.storage = storage
		this.stringify = stringify
		this.parse = parse
	}
	set(key, value) {
		this.storage.setItem(key, this.stringify(value))
	}
	get(key) {
		return this.parse(this.storage.getItem(key))
	}
	has(key) {
		return this.storage.getItem(key) != null
	}
}