@esportsplus/web-storage 0.0.3 → 0.0.6
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 +6 -1
- package/build/local/index.d.ts +6 -1
- package/build/local/index.js +49 -10
- package/package.json +30 -30
- package/src/index.ts +5 -5
- package/src/local/index.ts +67 -12
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,
|
|
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;
|
package/build/local/index.d.ts
CHANGED
|
@@ -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,
|
|
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;
|
package/build/local/index.js
CHANGED
|
@@ -4,23 +4,31 @@ let cache = {}, driver = localforage.LOCALSTORAGE;
|
|
|
4
4
|
function init(options = {}) {
|
|
5
5
|
localforage.config(Object.assign({ name: 'store' }, options, { driver }));
|
|
6
6
|
}
|
|
7
|
+
function sync(key) {
|
|
8
|
+
let root = (key.split('.')[0] || '');
|
|
9
|
+
localforage.setItem(root, dot.get(cache, root));
|
|
10
|
+
}
|
|
7
11
|
const clear = () => {
|
|
8
12
|
cache = {};
|
|
9
13
|
localforage.clear();
|
|
10
14
|
};
|
|
11
15
|
const del = (key) => {
|
|
12
16
|
dot.set(cache, key, undefined);
|
|
13
|
-
|
|
17
|
+
if (key.includes('.')) {
|
|
18
|
+
sync(key);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
localforage.removeItem(key);
|
|
22
|
+
}
|
|
14
23
|
};
|
|
15
|
-
const get = async (key,
|
|
16
|
-
if (
|
|
24
|
+
const get = async (key, value = null) => {
|
|
25
|
+
if (await has(key)) {
|
|
17
26
|
return dot.get(cache, key);
|
|
18
27
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
value = await fallback();
|
|
28
|
+
if (typeof value === 'function') {
|
|
29
|
+
value = await value();
|
|
22
30
|
}
|
|
23
|
-
set(
|
|
31
|
+
set(key, value);
|
|
24
32
|
value = dot.get(cache, key);
|
|
25
33
|
if (value === null) {
|
|
26
34
|
throw new Error(`'${key}' has not been set in storage`);
|
|
@@ -28,11 +36,42 @@ const get = async (key, fallback = null) => {
|
|
|
28
36
|
return value;
|
|
29
37
|
};
|
|
30
38
|
const has = async (key) => {
|
|
31
|
-
|
|
39
|
+
if (dot.has(cache, key)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
let k = key.split('.'), f = k.shift() || '', value = await localforage.getItem(f);
|
|
43
|
+
if (value !== null) {
|
|
44
|
+
set(f, value);
|
|
45
|
+
if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
|
|
46
|
+
return dot.has(value, k.join('.'));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
};
|
|
51
|
+
const prepend = async (key, value) => {
|
|
52
|
+
let values = await get(key, []);
|
|
53
|
+
if (!Array.isArray(values)) {
|
|
54
|
+
values = [values];
|
|
55
|
+
}
|
|
56
|
+
values.unshift(value);
|
|
57
|
+
set(key, values);
|
|
58
|
+
};
|
|
59
|
+
const push = async (key, value) => {
|
|
60
|
+
let values = await get(key, []);
|
|
61
|
+
if (!Array.isArray(values)) {
|
|
62
|
+
values = [values];
|
|
63
|
+
}
|
|
64
|
+
values.push(value);
|
|
65
|
+
set(key, values);
|
|
66
|
+
};
|
|
67
|
+
const replace = (values) => {
|
|
68
|
+
for (let key in values) {
|
|
69
|
+
set(key, values[key]);
|
|
70
|
+
}
|
|
32
71
|
};
|
|
33
72
|
const set = (key, value) => {
|
|
34
73
|
dot.set(cache, key, value);
|
|
35
|
-
|
|
74
|
+
sync(key);
|
|
36
75
|
};
|
|
37
76
|
const useIndexedDB = (options = {}) => {
|
|
38
77
|
driver = localforage.INDEXEDDB;
|
|
@@ -46,4 +85,4 @@ const useOptions = (options = {}) => {
|
|
|
46
85
|
init(options);
|
|
47
86
|
};
|
|
48
87
|
init();
|
|
49
|
-
export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
|
|
88
|
+
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.
|
|
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.6"
|
|
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 };
|
package/src/local/index.ts
CHANGED
|
@@ -11,6 +11,12 @@ function init(options: Options = {}): void {
|
|
|
11
11
|
localforage.config(Object.assign({ name: 'store' }, options, { driver }));
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
function sync(key: string) {
|
|
15
|
+
let root = (key.split('.')[0] || '');
|
|
16
|
+
|
|
17
|
+
localforage.setItem(root, dot.get(cache, root));
|
|
18
|
+
}
|
|
19
|
+
|
|
14
20
|
|
|
15
21
|
const clear = () => {
|
|
16
22
|
cache = {};
|
|
@@ -19,22 +25,25 @@ const clear = () => {
|
|
|
19
25
|
|
|
20
26
|
const del = (key: string): void => {
|
|
21
27
|
dot.set(cache, key, undefined);
|
|
22
|
-
|
|
28
|
+
|
|
29
|
+
if (key.includes('.')) {
|
|
30
|
+
sync(key);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
localforage.removeItem(key);
|
|
34
|
+
}
|
|
23
35
|
}
|
|
24
36
|
|
|
25
|
-
const get = async (key: string,
|
|
26
|
-
if (
|
|
37
|
+
const get = async (key: string, value: any = null): Promise<any> => {
|
|
38
|
+
if (await has(key)) {
|
|
27
39
|
return dot.get(cache, key);
|
|
28
40
|
}
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
value
|
|
32
|
-
|
|
33
|
-
if (value === null && typeof fallback === 'function' ) {
|
|
34
|
-
value = await fallback();
|
|
42
|
+
if (typeof value === 'function') {
|
|
43
|
+
value = await value();
|
|
35
44
|
}
|
|
36
45
|
|
|
37
|
-
set(
|
|
46
|
+
set(key, value);
|
|
38
47
|
|
|
39
48
|
value = dot.get(cache, key);
|
|
40
49
|
|
|
@@ -46,12 +55,58 @@ const get = async (key: string, fallback: any = null): Promise<any> => {
|
|
|
46
55
|
};
|
|
47
56
|
|
|
48
57
|
const has = async (key: string): Promise<boolean> => {
|
|
49
|
-
|
|
58
|
+
if (dot.has(cache, key)) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let k = key.split('.'),
|
|
63
|
+
f = k.shift() || '',
|
|
64
|
+
value = await localforage.getItem(f);
|
|
65
|
+
|
|
66
|
+
if (value !== null) {
|
|
67
|
+
set(f, value);
|
|
68
|
+
|
|
69
|
+
if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
|
|
70
|
+
return dot.has(value, k.join('.'));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return false;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const prepend = async (key: string, value: any): Promise<void> => {
|
|
78
|
+
let values = await get(key, []);
|
|
79
|
+
|
|
80
|
+
if (!Array.isArray(values)) {
|
|
81
|
+
values = [values];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
values.unshift(value);
|
|
85
|
+
|
|
86
|
+
set(key, values);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const push = async (key: string, value: any): Promise<void> => {
|
|
90
|
+
let values = await get(key, []);
|
|
91
|
+
|
|
92
|
+
if (!Array.isArray(values)) {
|
|
93
|
+
values = [values];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
values.push(value);
|
|
97
|
+
|
|
98
|
+
set(key, values);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const replace = (values: { [key: string]: any }): void => {
|
|
102
|
+
for (let key in values) {
|
|
103
|
+
set(key, values[key]);
|
|
104
|
+
}
|
|
50
105
|
};
|
|
51
106
|
|
|
52
107
|
const set = (key: string, value: any): void => {
|
|
53
108
|
dot.set(cache, key, value);
|
|
54
|
-
|
|
109
|
+
sync(key);
|
|
55
110
|
};
|
|
56
111
|
|
|
57
112
|
const useIndexedDB = (options: Options = {}): void => {
|
|
@@ -73,4 +128,4 @@ const useOptions = (options: Options = {}): void => {
|
|
|
73
128
|
init();
|
|
74
129
|
|
|
75
130
|
|
|
76
|
-
export default { clear, delete: del, get, has, set, useIndexedDB, useLocalStorage, useOptions };
|
|
131
|
+
export default { clear, delete: del, get, has, prepend, push, replace, set, useIndexedDB, useLocalStorage, useOptions };
|