Skip to content

Instantly share code, notes, and snippets.

@charliewilco
Created October 13, 2019 10:13
Show Gist options
  • Save charliewilco/6ed68b17dc5e789a945324fa74359a42 to your computer and use it in GitHub Desktop.
Save charliewilco/6ed68b17dc5e789a945324fa74359a42 to your computer and use it in GitHub Desktop.
interface StringTMap<T> {
[key: string]: T;
}
export function storageMock() {
let storage: StringTMap<string> = {};
return {
setItem(key: string, value: any) {
storage[key] = value || "";
},
getItem(key: string) {
return key in storage ? storage[key] : null;
},
removeItem(key: string) {
delete storage[key];
},
get length() {
return Object.keys(storage).length;
},
key(i: number) {
const keys = Object.keys(storage);
return keys[i] || null;
}
};
}
if (!(global as any).localStorage) {
(global as any).localStorage = storageMock();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment