-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-storage-service.ts
More file actions
116 lines (101 loc) · 3.1 KB
/
Copy pathlocal-storage-service.ts
File metadata and controls
116 lines (101 loc) · 3.1 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
export class LocalStorageService
{
private storageType: string = "localStorage";
/**
* Custom built local storage service.
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
*/
constructor()
{
this.IsAvailable = this.IsStorageAvailable(this.storageType);
}
/**
* Let's you know if localStorage is available to use in this browser.
* */
public IsAvailable: boolean = false;
/**
* Clears out all localStorage objects.
* WARNING: THIS WILL ALSO CLEAR ALL 3RD PARTY STORAGE ITEMS.
* */
public ClearAll(): void
{
window.localStorage.clear();
}
/**
* Gets a localStorage item.
* NOTE: This WILL work on a regular string types but it will not deserialize for you.
* @param key - The key used to set the item originally.
*/
public GetSerializedItem(key: string): string | null
{
return window.localStorage.getItem(key);
}
/**
* Gets a localStorage item.
* There is no need to deserialize your value. It's handled in this method.
* Simply tell it your type when you call the method like this: "...GetItem<string[]>("KeyName")".
* NOTE: This WILL NOT work on a regular string or int types. It must be an object or array value!
* @param key - The key used to set the item originally.
*/
public GetDeserializedItem<TObject>(key: string): TObject | null
{
let deserializedValue: TObject | null = null;
let storedValue: TObject = JSON.parse(window.localStorage.getItem(key) ?? "null");
if (storedValue != null)
{
deserializedValue = storedValue;
}
return deserializedValue;
}
/**
* Removes an item from localStorage.
* @param key - The key used to set the item originally.
*/
public RemoveItem(key: string): void
{
window.localStorage.removeItem(key);
}
/**
* Sets a localStorage item.
* There is no need to serialize your value ahead of time. It's handled in this method.
* Simply tell it your type when you call the method like this: "...SetItem<string, number[]>("KeyName", [1,2,3,4])".
* The key will always be a string so just tell it that.
* @param key - The key of the localStorage item.
* @param value - The value of the localStorage item.
*/
public SetItem<KType, TObject>(key: string, value: TObject): void
{
// Just set a blank string if this goes south.
let serializedValue: string = "";
// If it's not already a string let's serialize it to JSON.
if (typeof (value) !== "string")
{
serializedValue = JSON.stringify(value);
}
// Finally, save the item to localStorage.
window.localStorage.setItem(key, serializedValue);
}
/**
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#feature-detecting_localstorage
* @param type - A string representing the desired type. E.g. 'localStorage'
*/
private IsStorageAvailable(type: string): boolean
{
var isAvailable = false;
var storage;
try
{
//@ts-ignore - window takes an array index of type string, here
storage = window[type];
var x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
isAvailable = true;
}
catch (e)
{
isAvailable = false;
}
return isAvailable;
}
}