File size: 1,113 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * LocalStorage polyfill from https://gist.github.com/juliocesar/926500
 * length and key methods were added to allow for implementing removeExpiredExperimentAssignments
 * Exported only for testing purposes
 */
export const polyfilledLocalStorage: Storage = {
	_data: {} as Record< string, string >,
	setItem: function ( id: string, val: string ): void {
		this._data[ id ] = val;
	},
	getItem: function ( id: string ): string | null {
		return this._data.hasOwnProperty( id ) ? this._data[ id ] : null;
	},
	removeItem: function ( id: string ): void {
		delete this._data[ id ];
	},
	clear: function (): void {
		this._data = {};
	},
	get length(): number {
		return Object.keys( this._data ).length;
	},
	key: function ( index: number ): string | null {
		const keys = Object.keys( this._data );
		return index in keys ? keys[ index ] : null;
	},
};

/**
 * A polyfilled LocalStorage.
 * The polyfill is required at least for testing.
 */
let localStorage = polyfilledLocalStorage;
try {
	if ( window.localStorage ) {
		localStorage = window.localStorage;
	}
} catch ( e ) {}

export default localStorage;