@esportsplus/web-storage 0.0.4 → 0.0.7

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
  }
@@ -37,11 +39,38 @@ const has = async (key) => {
37
39
  if (dot.has(cache, key)) {
38
40
  return true;
39
41
  }
40
- let value = await localforage.getItem(key.split('.')[0] || '');
42
+ let k = key.split('.'), f = k.shift() || '', value = await localforage.getItem(f);
41
43
  if (value !== null) {
42
- set(key, value);
44
+ set(f, value);
45
+ if (Array.isArray(value) && k.length == 0) {
46
+ return true;
47
+ }
48
+ if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
49
+ return dot.has(value, k.join('.'));
50
+ }
51
+ }
52
+ return false;
53
+ };
54
+ const prepend = async (key, value) => {
55
+ let values = await get(key, []);
56
+ if (!Array.isArray(values)) {
57
+ values = [values];
58
+ }
59
+ values.unshift(value);
60
+ set(key, values);
61
+ };
62
+ const push = async (key, value) => {
63
+ let values = await get(key, []);
64
+ if (!Array.isArray(values)) {
65
+ values = [values];
66
+ }
67
+ values.push(value);
68
+ set(key, values);
69
+ };
70
+ const replace = (values) => {
71
+ for (let key in values) {
72
+ set(key, values[key]);
43
73
  }
44
- return value !== null;
45
74
  };
46
75
  const set = (key, value) => {
47
76
  dot.set(cache, key, value);
@@ -59,4 +88,4 @@ const useOptions = (options = {}) => {
59
88
  init(options);
60
89
  };
61
90
  init();
62
- export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
91
+ export default { clear, delete: del, get, has, prepend, push, replace, set, useIndexedDB, useLocalStorage, useOptions };
package/package.json CHANGED
@@ -1,30 +1,30 @@
1
- {
2
- "author": "ICJR",
3
- "dependencies": {
4
- "@esportsplus/dot": "^0.0.3",
5
- "localforage": "^1.10.0"
6
- },
7
- "description": "Web storage utility",
8
- "devDependencies": {
9
- "glob": "^7.1.7",
10
- "node-polyfill-webpack-plugin": "^1.1.4",
11
- "npm-run-all": "^4.1.5",
12
- "ts-loader": "^9.2.6",
13
- "tsc-alias": "^1.5.0",
14
- "tsconfig-paths-webpack-plugin": "^3.5.2",
15
- "typescript": "^4.5.2",
16
- "webpack": "^5.64.4",
17
- "webpack-cli": "^4.9.1"
18
- },
19
- "main": "./build/index.js",
20
- "name": "@esportsplus/web-storage",
21
- "private": false,
22
- "scripts": {
23
- "build": "tsc",
24
- "-": "-",
25
- "prepare": "npm run build",
26
- "prepublishOnly": "npm run build"
27
- },
28
- "types": "./build/index.d.ts",
29
- "version": "0.0.4"
30
- }
1
+ {
2
+ "author": "ICJR",
3
+ "dependencies": {
4
+ "@esportsplus/dot": "^0.0.3",
5
+ "localforage": "^1.10.0"
6
+ },
7
+ "description": "Web storage utility",
8
+ "devDependencies": {
9
+ "glob": "^7.1.7",
10
+ "node-polyfill-webpack-plugin": "^1.1.4",
11
+ "npm-run-all": "^4.1.5",
12
+ "ts-loader": "^9.2.6",
13
+ "tsc-alias": "^1.5.0",
14
+ "tsconfig-paths-webpack-plugin": "^3.5.2",
15
+ "typescript": "^4.5.2",
16
+ "webpack": "^5.64.4",
17
+ "webpack-cli": "^4.9.1"
18
+ },
19
+ "main": "./build/index.js",
20
+ "name": "@esportsplus/web-storage",
21
+ "private": false,
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "-": "-",
25
+ "prepare": "npm run build",
26
+ "prepublishOnly": "npm run build"
27
+ },
28
+ "types": "./build/index.d.ts",
29
+ "version": "0.0.7"
30
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- import local from './local';
2
-
3
-
4
- export default { local };
5
- export { local };
1
+ import local from './local';
2
+
3
+
4
+ export default { local };
5
+ export { local };
@@ -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`);
@@ -55,13 +59,53 @@ const has = async (key: string): Promise<boolean> => {
55
59
  return true;
56
60
  }
57
61
 
58
- let value: any = await localforage.getItem(key.split('.')[0] || '');
62
+ let k = key.split('.'),
63
+ f = k.shift() || '',
64
+ value = await localforage.getItem(f);
59
65
 
60
66
  if (value !== null) {
61
- set(key, value);
67
+ set(f, value);
68
+
69
+ if (Array.isArray(value) && k.length == 0) {
70
+ return true;
71
+ }
72
+
73
+ if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
74
+ return dot.has(value, k.join('.'));
75
+ }
76
+ }
77
+
78
+ return false;
79
+ };
80
+
81
+ const prepend = async (key: string, value: any): Promise<void> => {
82
+ let values = await get(key, []);
83
+
84
+ if (!Array.isArray(values)) {
85
+ values = [values];
62
86
  }
63
87
 
64
- return value !== null;
88
+ values.unshift(value);
89
+
90
+ set(key, values);
91
+ };
92
+
93
+ const push = async (key: string, value: any): Promise<void> => {
94
+ let values = await get(key, []);
95
+
96
+ if (!Array.isArray(values)) {
97
+ values = [values];
98
+ }
99
+
100
+ values.push(value);
101
+
102
+ set(key, values);
103
+ };
104
+
105
+ const replace = (values: { [key: string]: any }): void => {
106
+ for (let key in values) {
107
+ set(key, values[key]);
108
+ }
65
109
  };
66
110
 
67
111
  const set = (key: string, value: any): void => {
@@ -88,4 +132,4 @@ const useOptions = (options: Options = {}): void => {
88
132
  init();
89
133
 
90
134
 
91
- export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
135
+ export default { clear, delete: del, get, has, prepend, push, replace, set, useIndexedDB, useLocalStorage, useOptions };