@cardsjd/storage 0.0.1 → 0.0.2

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/lib/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import Storage from './storage';
2
-
3
- export { Storage };
1
+ import Storage from './storage';
2
+
3
+ export { Storage };
package/lib/storage.js CHANGED
@@ -1,45 +1,52 @@
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
- }
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
+
10
+ let dataRaw = null;
11
+ try {
12
+ if (localStorage && localStorage.getItem) {
13
+ dataRaw = localStorage.getItem(key);
14
+ }
15
+ } catch (e) {
16
+ console.log('Error in load: ' + e);
17
+ }
18
+ if (dataRaw === null) {
19
+ const { value } = await Preferences.get({
20
+ key,
21
+ });
22
+ dataRaw = value;
23
+ }
24
+
25
+ if (dataRaw !== null && dataRaw != '{}') {
26
+ try {
27
+ data = JSON.parse(dataRaw);
28
+ } catch (e) {
29
+ data = null; //error in the above string(in this case,yes)!
30
+ }
31
+
32
+ }
33
+
34
+ return data;
35
+ }
36
+
37
+ async save(key, data) {
38
+ const jsonData = JSON.stringify(data);
39
+
40
+ try {
41
+ localStorage[key] = jsonData;
42
+ } catch (e) {
43
+ console.warn('save: ' + e);
44
+ }
45
+
46
+ //save to local file
47
+ await Preferences.set({
48
+ key,
49
+ value: jsonData,
50
+ });
51
+ }
52
+ }
package/package.json CHANGED
@@ -1,17 +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
- }
1
+ {
2
+ "name": "@cardsjd/storage",
3
+ "private": false,
4
+ "version": "0.0.2",
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
+ }