Skip to content

Instantly share code, notes, and snippets.

@kallewoof
Created June 14, 2016 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kallewoof/0fa22a0ab3550e22c50f1357c1d0a4ce to your computer and use it in GitHub Desktop.
Save kallewoof/0fa22a0ab3550e22c50f1357c1d0a4ce to your computer and use it in GitHub Desktop.
const storageHandler = {
get(obj, name) {
return obj.getItem(name) || obj[name];
},
set(obj, name, value) {
obj.setItem(name, value);
},
has(obj, name) {
return name in obj.d || name in obj;
},
deleteProperty(obj, name) {
obj.removeItem(name);
},
};
class StorageClass {
constructor() {
this.d = {};
this.k = [];
}
get length() {
return this.k.length;
}
getItem(name) {
return this.d[name];
}
setItem(name, value) {
if (!this.k.indexOf(name)) {
this.k.push(name);
}
this.d[name] = value;
}
removeItem(name) {
this.k.pop(this.k.indexOf(name));
delete this.d[name];
}
clear() {
this.d = {};
this.k = [];
}
}
window.localStorage = new Proxy(new StorageClass(), storageHandler);
window.sessionStorage = new Proxy(new StorageClass(), storageHandler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment