2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-24 09:32:02 +00:00
iconify/packages/core/src/browser-storage/global.ts

41 lines
810 B
TypeScript
Raw Normal View History

2022-06-28 19:11:15 +00:00
import { browserStorageConfig } from './data';
import type { BrowserStorageConfig } from './types';
/**
* Fake window for unit testing
*/
type FakeWindow = Record<string, typeof localStorage>;
let _window: FakeWindow =
typeof window === 'undefined' ? {} : (window as unknown as FakeWindow);
/**
* Get browser storage
*/
export function getBrowserStorage(
key: keyof BrowserStorageConfig
): typeof localStorage | undefined {
const attr = key + 'Storage';
try {
if (
_window &&
_window[attr] &&
typeof _window[attr].length === 'number'
) {
return _window[attr];
}
} catch (err) {
//
}
// Failed - mark as disabled
browserStorageConfig[key] = false;
}
/**
* Mock window for unit testing
*/
export function mockWindow(fakeWindow: FakeWindow): void {
_window = fakeWindow;
}