@lark-apaas/client-toolkit 1.2.11 → 1.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { scopedStorage } from '../../utils/scopedStorage';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getAppId } from "./getAppId.js";
|
|
2
|
+
function getPrefix() {
|
|
3
|
+
const appId = getAppId(window.location.pathname);
|
|
4
|
+
const namespace = appId || '__global__';
|
|
5
|
+
return `__miaoda_${namespace}__:`;
|
|
6
|
+
}
|
|
7
|
+
function getScopedKeys() {
|
|
8
|
+
const prefix = getPrefix();
|
|
9
|
+
const keys = [];
|
|
10
|
+
for(let i = 0; i < localStorage.length; i++){
|
|
11
|
+
const key = localStorage.key(i);
|
|
12
|
+
if (key && key.startsWith(prefix)) keys.push(key.substring(prefix.length));
|
|
13
|
+
}
|
|
14
|
+
return keys;
|
|
15
|
+
}
|
|
16
|
+
const scopedStorage = {
|
|
17
|
+
getItem (key) {
|
|
18
|
+
const prefixedKey = getPrefix() + key;
|
|
19
|
+
return localStorage.getItem(prefixedKey);
|
|
20
|
+
},
|
|
21
|
+
setItem (key, value) {
|
|
22
|
+
const prefixedKey = getPrefix() + key;
|
|
23
|
+
localStorage.setItem(prefixedKey, value);
|
|
24
|
+
},
|
|
25
|
+
removeItem (key) {
|
|
26
|
+
const prefixedKey = getPrefix() + key;
|
|
27
|
+
localStorage.removeItem(prefixedKey);
|
|
28
|
+
},
|
|
29
|
+
clear () {
|
|
30
|
+
const prefix = getPrefix();
|
|
31
|
+
const keysToRemove = [];
|
|
32
|
+
for(let i = 0; i < localStorage.length; i++){
|
|
33
|
+
const key = localStorage.key(i);
|
|
34
|
+
if (key && key.startsWith(prefix)) keysToRemove.push(key);
|
|
35
|
+
}
|
|
36
|
+
keysToRemove.forEach((key)=>localStorage.removeItem(key));
|
|
37
|
+
},
|
|
38
|
+
key (index) {
|
|
39
|
+
const keys = getScopedKeys();
|
|
40
|
+
return keys[index] || null;
|
|
41
|
+
},
|
|
42
|
+
get length () {
|
|
43
|
+
return getScopedKeys().length;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
export { scopedStorage };
|