@cardsjd/storage 0.0.1
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/README.md +0 -0
- package/lib/index.js +3 -0
- package/lib/storage.js +45 -0
- package/package.json +17 -0
package/README.md
ADDED
|
Binary file
|
package/lib/index.js
ADDED
package/lib/storage.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Preferences } from '@capacitor/preferences';
|
|
2
|
+
|
|
3
|
+
export default class Storage {
|
|
4
|
+
constructor(_options) {
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
async load(key) {
|
|
8
|
+
let data = null;
|
|
9
|
+
let dataRaw = localStorage?.getItem?.(key);
|
|
10
|
+
|
|
11
|
+
if (dataRaw === null) {
|
|
12
|
+
const { value } = await Preferences.get({
|
|
13
|
+
key,
|
|
14
|
+
});
|
|
15
|
+
dataRaw = value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (dataRaw !== null && dataRaw != '{}') {
|
|
19
|
+
try {
|
|
20
|
+
data = JSON.parse(dataRaw);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
data = null; //error in the above string(in this case,yes)!
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async save(key, data) {
|
|
31
|
+
const jsonData = JSON.stringify(data);
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
localStorage[key] = jsonData;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.warn('save: ' + e);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//save to local file
|
|
40
|
+
await Preferences.set({
|
|
41
|
+
key,
|
|
42
|
+
value: jsonData,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cardsjd/storage",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"lint": "eslint lib --report-unused-disable-directives --max-warnings 0",
|
|
12
|
+
"lint:fix": "npm run lint -- --fix"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@capacitor/preferences": "^5.0.7"
|
|
16
|
+
}
|
|
17
|
+
}
|