-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
82 lines (66 loc) · 2.09 KB
/
index.ts
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
export class Override {
protected _overrideNamespace: string;
protected _overrideRegex: RegExp;
constructor(overrideNamespace: string) {
this._overrideNamespace = overrideNamespace;
this._overrideRegex = new RegExp(`^${overrideNamespace}_override_(.*?)$`);
}
/**
* Try to obtain overrides from sessionStorage
*
* @returns {object} overrides
*/
getAll(): object {
if (!('sessionStorage' in window))
return {};
const overrides = {};
Object.keys(sessionStorage)
.forEach(item => {
if (!this._overrideRegex.test(item)) {
return;
}
try {
const itemName = this._overrideRegex.exec(item)[1];
overrides[itemName] = JSON.parse(
sessionStorage[item],
);
} catch (err) {
void err;
}
});
return overrides;
}
get<T>(overrideName: string): T {
try {
return JSON.parse(
sessionStorage[
`${this._overrideNamespace}_override_${overrideName}`
],
);
} catch {}
return void 0;
}
set<T>(
overrideName: string,
overrideValue: T,
): void {
if (!('sessionStorage' in window))
return;
console.assert(overrideName !== null && overrideName !== undefined, 'Override name is mandatory');
try {
sessionStorage[`${this._overrideNamespace}_override_${overrideName}`] = JSON.stringify(overrideValue);
} catch (err) {
void 0;
}
}
delete(overrideName: string): void {
if (!('sessionStorage' in window))
return;
console.assert(overrideName !== null && overrideName !== undefined, 'Override name is mandatory');
try {
sessionStorage.removeItem(`${this._overrideNamespace}_override_${overrideName}`);
} catch (err) {
void 0;
}
}
};