Created
October 13, 2019 10:13
-
-
Save charliewilco/6ed68b17dc5e789a945324fa74359a42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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