@esportsplus/web-storage 0.0.1 → 0.0.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/build/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import local from './local';
2
2
  declare const _default: {
3
3
  local: {
4
- clear: (callback?: ((err: any) => void) | undefined) => Promise<void>;
4
+ clear: () => void;
5
5
  delete: (key: string) => void;
6
- get: (key: string) => Promise<any>;
6
+ get: (key: string, fallback?: any) => Promise<any>;
7
+ has: (key: string) => Promise<boolean>;
7
8
  set: (key: string, value: any) => void;
8
9
  useIndexedDB: (options?: import("./local/types").Options) => void;
9
10
  useLocalStorage: (options?: import("./local/types").Options) => void;
@@ -1,8 +1,9 @@
1
1
  import type { Options } from './types';
2
2
  declare const _default: {
3
- clear: (callback?: ((err: any) => void) | undefined) => Promise<void>;
3
+ clear: () => void;
4
4
  delete: (key: string) => void;
5
- get: (key: string) => Promise<any>;
5
+ get: (key: string, fallback?: any) => Promise<any>;
6
+ has: (key: string) => Promise<boolean>;
6
7
  set: (key: string, value: any) => void;
7
8
  useIndexedDB: (options?: Options) => void;
8
9
  useLocalStorage: (options?: Options) => void;
@@ -1,17 +1,51 @@
1
+ import dot from '@esportsplus/dot';
1
2
  import localforage from 'localforage';
2
- let driver = localforage.LOCALSTORAGE;
3
+ let cache = {}, driver = localforage.LOCALSTORAGE;
3
4
  function init(options = {}) {
4
5
  localforage.config(Object.assign({ name: 'store' }, options, { driver }));
5
6
  }
6
- const clear = localforage.clear;
7
+ function sync(key) {
8
+ localforage.setItem((key.split('.')[0] || ''), dot.get(cache, key));
9
+ }
10
+ const clear = () => {
11
+ cache = {};
12
+ localforage.clear();
13
+ };
7
14
  const del = (key) => {
8
- localforage.removeItem(key);
15
+ dot.set(cache, key, undefined);
16
+ if (key.includes('.')) {
17
+ sync(key);
18
+ }
19
+ else {
20
+ localforage.removeItem(key);
21
+ }
22
+ };
23
+ const get = async (key, fallback = null) => {
24
+ if (await has(key)) {
25
+ return dot.get(cache, key);
26
+ }
27
+ if (typeof fallback === 'function') {
28
+ set(key, await fallback());
29
+ }
30
+ let value = dot.get(cache, key);
31
+ if (value === null) {
32
+ throw new Error(`'${key}' has not been set in storage`);
33
+ }
34
+ return value;
9
35
  };
10
- const get = (key) => {
11
- return localforage.getItem(key);
36
+ const has = async (key) => {
37
+ if (dot.has(cache, key)) {
38
+ return true;
39
+ }
40
+ let value = await localforage.getItem(key.split('.')[0] || '');
41
+ if (value !== null) {
42
+ set(key, value);
43
+ }
44
+ return value !== null;
12
45
  };
13
46
  const set = (key, value) => {
14
- localforage.setItem(key, value);
47
+ dot.set(cache, key, value);
48
+ sync(key);
15
49
  };
16
50
  const useIndexedDB = (options = {}) => {
17
51
  driver = localforage.INDEXEDDB;
@@ -25,4 +59,4 @@ const useOptions = (options = {}) => {
25
59
  init(options);
26
60
  };
27
61
  init();
28
- export default { clear, delete: del, get, set, useIndexedDB, useLocalStorage, useOptions };
62
+ export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
package/package.json CHANGED
@@ -26,5 +26,5 @@
26
26
  "prepublishOnly": "npm run build"
27
27
  },
28
28
  "types": "./build/index.d.ts",
29
- "version": "0.0.1"
29
+ "version": "0.0.4"
30
30
  }
@@ -1,46 +1,91 @@
1
- import type { Options } from './types';
2
- import localforage from 'localforage';
3
-
4
-
5
- let driver: any = localforage.LOCALSTORAGE;
6
-
7
-
8
- function init(options: Options = {}): void {
9
- localforage.config(Object.assign({ name: 'store' }, options, { driver }));
10
- }
11
-
12
-
13
- const clear = localforage.clear;
14
-
15
- const del = (key: string): void => {
16
- localforage.removeItem(key);
17
- }
18
-
19
- const get = (key: string): Promise<any> => {
20
- return localforage.getItem(key);
21
- };
22
-
23
- const set = (key: string, value: any): void => {
24
- localforage.setItem(key, value);
25
- };
26
-
27
- const useIndexedDB = (options: Options = {}): void => {
28
- driver = localforage.INDEXEDDB;
29
- init(options);
30
- };
31
-
32
- const useLocalStorage = (options: Options = {}): void => {
33
- driver = localforage.LOCALSTORAGE;
34
- init(options);
35
- };
36
-
37
- const useOptions = (options: Options = {}): void => {
38
- init(options);
39
- };
40
-
41
-
42
- // Initialize using localstorage as default storage
43
- init();
44
-
45
-
46
- export default { clear, delete: del, get, set, useIndexedDB, useLocalStorage, useOptions };
1
+ import type { Options } from './types';
2
+ import dot from '@esportsplus/dot';
3
+ import localforage from 'localforage';
4
+
5
+
6
+ let cache: any = {},
7
+ driver: any = localforage.LOCALSTORAGE;
8
+
9
+
10
+ function init(options: Options = {}): void {
11
+ localforage.config(Object.assign({ name: 'store' }, options, { driver }));
12
+ }
13
+
14
+ function sync(key: string) {
15
+ localforage.setItem((key.split('.')[0] || ''), dot.get(cache, key));
16
+ }
17
+
18
+
19
+ const clear = () => {
20
+ cache = {};
21
+ localforage.clear();
22
+ };
23
+
24
+ const del = (key: string): void => {
25
+ dot.set(cache, key, undefined);
26
+
27
+ if (key.includes('.')) {
28
+ sync(key);
29
+ }
30
+ else {
31
+ localforage.removeItem(key);
32
+ }
33
+ }
34
+
35
+ const get = async (key: string, fallback: any = null): Promise<any> => {
36
+ if (await has(key)) {
37
+ return dot.get(cache, key);
38
+ }
39
+
40
+ if (typeof fallback === 'function') {
41
+ set(key, await fallback());
42
+ }
43
+
44
+ let value = dot.get(cache, key);
45
+
46
+ if (value === null) {
47
+ throw new Error(`'${key}' has not been set in storage`);
48
+ }
49
+
50
+ return value;
51
+ };
52
+
53
+ const has = async (key: string): Promise<boolean> => {
54
+ if (dot.has(cache, key)) {
55
+ return true;
56
+ }
57
+
58
+ let value: any = await localforage.getItem(key.split('.')[0] || '');
59
+
60
+ if (value !== null) {
61
+ set(key, value);
62
+ }
63
+
64
+ return value !== null;
65
+ };
66
+
67
+ const set = (key: string, value: any): void => {
68
+ dot.set(cache, key, value);
69
+ sync(key);
70
+ };
71
+
72
+ const useIndexedDB = (options: Options = {}): void => {
73
+ driver = localforage.INDEXEDDB;
74
+ init(options);
75
+ };
76
+
77
+ const useLocalStorage = (options: Options = {}): void => {
78
+ driver = localforage.LOCALSTORAGE;
79
+ init(options);
80
+ };
81
+
82
+ const useOptions = (options: Options = {}): void => {
83
+ init(options);
84
+ };
85
+
86
+
87
+ // Initialize using localstorage as default storage
88
+ init();
89
+
90
+
91
+ export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };