@esportsplus/web-storage 0.0.4 → 0.0.5

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
@@ -3,8 +3,13 @@ declare const _default: {
3
3
  local: {
4
4
  clear: () => void;
5
5
  delete: (key: string) => void;
6
- get: (key: string, fallback?: any) => Promise<any>;
6
+ get: (key: string, value?: any) => Promise<any>;
7
7
  has: (key: string) => Promise<boolean>;
8
+ prepend: (key: string, value: any) => Promise<void>;
9
+ push: (key: string, value: any) => Promise<void>;
10
+ replace: (values: {
11
+ [key: string]: any;
12
+ }) => void;
8
13
  set: (key: string, value: any) => void;
9
14
  useIndexedDB: (options?: import("./local/types").Options) => void;
10
15
  useLocalStorage: (options?: import("./local/types").Options) => void;
@@ -2,8 +2,13 @@ import type { Options } from './types';
2
2
  declare const _default: {
3
3
  clear: () => void;
4
4
  delete: (key: string) => void;
5
- get: (key: string, fallback?: any) => Promise<any>;
5
+ get: (key: string, value?: any) => Promise<any>;
6
6
  has: (key: string) => Promise<boolean>;
7
+ prepend: (key: string, value: any) => Promise<void>;
8
+ push: (key: string, value: any) => Promise<void>;
9
+ replace: (values: {
10
+ [key: string]: any;
11
+ }) => void;
7
12
  set: (key: string, value: any) => void;
8
13
  useIndexedDB: (options?: Options) => void;
9
14
  useLocalStorage: (options?: Options) => void;
@@ -5,7 +5,8 @@ function init(options = {}) {
5
5
  localforage.config(Object.assign({ name: 'store' }, options, { driver }));
6
6
  }
7
7
  function sync(key) {
8
- localforage.setItem((key.split('.')[0] || ''), dot.get(cache, key));
8
+ let root = (key.split('.')[0] || '');
9
+ localforage.setItem(root, dot.get(cache, root));
9
10
  }
10
11
  const clear = () => {
11
12
  cache = {};
@@ -20,14 +21,15 @@ const del = (key) => {
20
21
  localforage.removeItem(key);
21
22
  }
22
23
  };
23
- const get = async (key, fallback = null) => {
24
+ const get = async (key, value = null) => {
24
25
  if (await has(key)) {
25
26
  return dot.get(cache, key);
26
27
  }
27
- if (typeof fallback === 'function') {
28
- set(key, await fallback());
28
+ if (typeof value === 'function') {
29
+ value = await value();
29
30
  }
30
- let value = dot.get(cache, key);
31
+ set(key, value);
32
+ value = dot.get(cache, key);
31
33
  if (value === null) {
32
34
  throw new Error(`'${key}' has not been set in storage`);
33
35
  }
@@ -43,6 +45,27 @@ const has = async (key) => {
43
45
  }
44
46
  return value !== null;
45
47
  };
48
+ const prepend = async (key, value) => {
49
+ let values = await get(key, []);
50
+ if (!Array.isArray(values)) {
51
+ values = [values];
52
+ }
53
+ values.unshift(value);
54
+ set(key, values);
55
+ };
56
+ const push = async (key, value) => {
57
+ let values = await get(key, []);
58
+ if (!Array.isArray(values)) {
59
+ values = [values];
60
+ }
61
+ values.push(value);
62
+ set(key, values);
63
+ };
64
+ const replace = (values) => {
65
+ for (let key in values) {
66
+ set(key, values[key]);
67
+ }
68
+ };
46
69
  const set = (key, value) => {
47
70
  dot.set(cache, key, value);
48
71
  sync(key);
@@ -59,4 +82,4 @@ const useOptions = (options = {}) => {
59
82
  init(options);
60
83
  };
61
84
  init();
62
- export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
85
+ export default { clear, delete: del, get, has, prepend, push, replace, 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.4"
29
+ "version": "0.0.5"
30
30
  }
@@ -12,7 +12,9 @@ function init(options: Options = {}): void {
12
12
  }
13
13
 
14
14
  function sync(key: string) {
15
- localforage.setItem((key.split('.')[0] || ''), dot.get(cache, key));
15
+ let root = (key.split('.')[0] || '');
16
+
17
+ localforage.setItem(root, dot.get(cache, root));
16
18
  }
17
19
 
18
20
 
@@ -32,16 +34,18 @@ const del = (key: string): void => {
32
34
  }
33
35
  }
34
36
 
35
- const get = async (key: string, fallback: any = null): Promise<any> => {
37
+ const get = async (key: string, value: any = null): Promise<any> => {
36
38
  if (await has(key)) {
37
39
  return dot.get(cache, key);
38
40
  }
39
41
 
40
- if (typeof fallback === 'function') {
41
- set(key, await fallback());
42
+ if (typeof value === 'function') {
43
+ value = await value();
42
44
  }
43
45
 
44
- let value = dot.get(cache, key);
46
+ set(key, value);
47
+
48
+ value = dot.get(cache, key);
45
49
 
46
50
  if (value === null) {
47
51
  throw new Error(`'${key}' has not been set in storage`);
@@ -64,6 +68,36 @@ const has = async (key: string): Promise<boolean> => {
64
68
  return value !== null;
65
69
  };
66
70
 
71
+ const prepend = async (key: string, value: any): Promise<void> => {
72
+ let values = await get(key, []);
73
+
74
+ if (!Array.isArray(values)) {
75
+ values = [values];
76
+ }
77
+
78
+ values.unshift(value);
79
+
80
+ set(key, values);
81
+ };
82
+
83
+ const push = async (key: string, value: any): Promise<void> => {
84
+ let values = await get(key, []);
85
+
86
+ if (!Array.isArray(values)) {
87
+ values = [values];
88
+ }
89
+
90
+ values.push(value);
91
+
92
+ set(key, values);
93
+ };
94
+
95
+ const replace = (values: { [key: string]: any }): void => {
96
+ for (let key in values) {
97
+ set(key, values[key]);
98
+ }
99
+ };
100
+
67
101
  const set = (key: string, value: any): void => {
68
102
  dot.set(cache, key, value);
69
103
  sync(key);
@@ -88,4 +122,4 @@ const useOptions = (options: Options = {}): void => {
88
122
  init();
89
123
 
90
124
 
91
- export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
125
+ export default { clear, delete: del, get, has, prepend, push, replace, set, useIndexedDB, useLocalStorage, useOptions };