mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-14 01:50:39 +09:00
33 lines
974 B
TypeScript
33 lines
974 B
TypeScript
|
|
const storageEntries = new Map<string, string>();
|
||
|
|
|
||
|
|
function installStorageMock(target: Record<string, unknown>) {
|
||
|
|
Object.defineProperty(target, "localStorage", {
|
||
|
|
configurable: true,
|
||
|
|
value: {
|
||
|
|
getItem: (key: string) => storageEntries.get(key) ?? null,
|
||
|
|
setItem: (key: string, value: string) => {
|
||
|
|
storageEntries.set(key, String(value));
|
||
|
|
},
|
||
|
|
removeItem: (key: string) => {
|
||
|
|
storageEntries.delete(key);
|
||
|
|
},
|
||
|
|
clear: () => {
|
||
|
|
storageEntries.clear();
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (
|
||
|
|
typeof globalThis.localStorage?.getItem !== "function"
|
||
|
|
|| typeof globalThis.localStorage?.setItem !== "function"
|
||
|
|
|| typeof globalThis.localStorage?.removeItem !== "function"
|
||
|
|
|| typeof globalThis.localStorage?.clear !== "function"
|
||
|
|
) {
|
||
|
|
installStorageMock(globalThis);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof window !== "undefined" && window.localStorage !== globalThis.localStorage) {
|
||
|
|
installStorageMock(window as unknown as Record<string, unknown>);
|
||
|
|
}
|