Skip to content

Instantly share code, notes, and snippets.

@whisher
Created December 7, 2018 20:36
Show Gist options
  • Save whisher/2c972695355572138de593f0e7b7ff86 to your computer and use it in GitHub Desktop.
Save whisher/2c972695355572138de593f0e7b7ff86 to your computer and use it in GitHub Desktop.
export const customStorage: Storage = {
length: 0,
clear: function(): void {
if (window && window.localStorage) {
window.localStorage.clear();
this.length = window.localStorage.length;
}
},
getItem: function(key: string): string | null {
try {
return window.localStorage.getItem(key);
} catch (err) {
return null;
}
},
key: function(index: number): string | null {
try {
return window.localStorage.key(index);
} catch (err) {
return null;
}
},
removeItem: function(key: string): void {
try {
window.localStorage.removeItem(key);
this.length = window.localStorage.length;
} catch (err) {
return;
}
},
setItem: function(key: string, data: string): void {
try {
window.localStorage.setItem(key, data);
this.length = window.localStorage.length;
} catch (err) {
return;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment