mirror of
https://github.com/iconify/iconify.git
synced 2024-10-24 09:32:02 +00:00
41 lines
810 B
TypeScript
41 lines
810 B
TypeScript
|
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;
|
||
|
}
|