@leexi/shared 0.7.2 → 0.7.4
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.
package/dist/module.json
CHANGED
|
@@ -5,11 +5,11 @@ export const useLocalStorage = (key, defaultValue) => {
|
|
|
5
5
|
throw new Error(`[useLocalStorage]: prefer camelCase fro key \`${key}\``);
|
|
6
6
|
}
|
|
7
7
|
const _findLocalStorageValue = () => {
|
|
8
|
-
if (
|
|
8
|
+
if (_tryLocalStorage()?.getItem(key)) {
|
|
9
9
|
return localStorage.getItem(key);
|
|
10
10
|
}
|
|
11
11
|
for (const caseKey of [key, kebabcase(key), snakecase(key)]) {
|
|
12
|
-
if (
|
|
12
|
+
if (_tryLocalStorage()?.getItem(caseKey)) {
|
|
13
13
|
localStorage.setItem(key, localStorage.getItem(caseKey));
|
|
14
14
|
localStorage.removeItem(caseKey);
|
|
15
15
|
return localStorage.getItem(key);
|
|
@@ -26,12 +26,19 @@ export const useLocalStorage = (key, defaultValue) => {
|
|
|
26
26
|
return _parseLocalStorageValue();
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
|
+
const _tryLocalStorage = () => {
|
|
30
|
+
try {
|
|
31
|
+
return localStorage;
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
29
36
|
const data = useState(key, _parseLocalStorageValue);
|
|
30
37
|
watch(data, () => {
|
|
31
38
|
if (data.value) {
|
|
32
|
-
|
|
39
|
+
_tryLocalStorage()?.setItem(key, JSON.stringify(data.value));
|
|
33
40
|
} else {
|
|
34
|
-
|
|
41
|
+
_tryLocalStorage()?.removeItem(key);
|
|
35
42
|
}
|
|
36
43
|
}, { deep: true, immediate: true });
|
|
37
44
|
return data;
|