@vvi/browser 1.0.0 → 1.0.1
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/CHANGELOG.md +7 -1
- package/README.md +1 -1
- package/cjs/index.js +2 -0
- package/cjs/storage.js +196 -0
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/storage.d.ts +33 -11
- package/es/storage.js +194 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# browser
|
|
2
2
|
|
|
3
|
-
[&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/@vvi/browser) [?logo=github>)](https://github.com/
|
|
3
|
+
[&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/@vvi/browser) [?logo=github>)](https://github.com/gleanings/browser/issues)
|
|
4
4
|
|
|
5
5
|
## 安装
|
|
6
6
|
|
package/cjs/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var cookie = require('./cookie.js');
|
|
|
4
4
|
var copyText = require('./copy-text.js');
|
|
5
5
|
var isOrientation = require('./is-orientation.js');
|
|
6
6
|
var isDark = require('./is-dark.js');
|
|
7
|
+
var storage = require('./storage.js');
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
|
|
@@ -21,3 +22,4 @@ exports.matchMediaEveryState = isDark.matchMediaEveryState;
|
|
|
21
22
|
exports.matchMediaSome = isDark.matchMediaSome;
|
|
22
23
|
exports.matchMediaSomeState = isDark.matchMediaSomeState;
|
|
23
24
|
exports.matchMediaState = isDark.matchMediaState;
|
|
25
|
+
exports.Storage = storage.Storage;
|
package/cjs/storage.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var is = require('@vvi/is');
|
|
4
|
+
var utils = require('@vvi/utils');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module @vvi/browser/storage
|
|
8
|
+
* @file storage.ts
|
|
9
|
+
* @description 本地储存
|
|
10
|
+
* @author Mr.MudBean <Mr.MudBean@outlook.com>
|
|
11
|
+
* @copyright 2026 ©️ Mr.MudBean
|
|
12
|
+
* @since 2026-08-06 23:31
|
|
13
|
+
* @version 1.0.0
|
|
14
|
+
* @lastModified 2026-08-26 01:18
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* # storage 储存类
|
|
18
|
+
*
|
|
19
|
+
* 通过 `new Storage` 构建一个储存对象,实现简单的 storage 操纵。
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const storage = new Storage();
|
|
24
|
+
*
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
class Storage {
|
|
28
|
+
/** 默认使用 localStorage ,也可以使用 sessionStorage */
|
|
29
|
+
#t = window.localStorage;
|
|
30
|
+
/**
|
|
31
|
+
* # 前缀
|
|
32
|
+
* 设置可防止多项目部署的时候 key 冲突 (例如 `myApp_` )
|
|
33
|
+
*
|
|
34
|
+
* *该值设定后不建议随意更改*
|
|
35
|
+
*/
|
|
36
|
+
prefix = '';
|
|
37
|
+
/**
|
|
38
|
+
* # Storage 构造方法
|
|
39
|
+
*
|
|
40
|
+
* @param prefix - 前缀,设置前缀可防止多项目部署时 key 冲突 (例如: `myApp_` )
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const storage = new Storage('');
|
|
44
|
+
*
|
|
45
|
+
*
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
constructor(prefix) {
|
|
49
|
+
if (prefix && is.typeOf(prefix) === 'string') {
|
|
50
|
+
this.prefix = prefix;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* # 切换储存类型
|
|
55
|
+
* @param type - 要切换的类型,除了传入 `session` 外,其余都将视为更改为 `localStorage`
|
|
56
|
+
*/
|
|
57
|
+
use(type) {
|
|
58
|
+
this.#t = type === 'session' ? window.sessionStorage : window.localStorage;
|
|
59
|
+
}
|
|
60
|
+
/** 拼接方法(内部) */
|
|
61
|
+
#k(key) {
|
|
62
|
+
if (this.prefix) {
|
|
63
|
+
return this.prefix
|
|
64
|
+
.concat(this.prefix.endsWith('_') ? '' : '_')
|
|
65
|
+
.concat(key);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return key;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* # 设置键与值
|
|
73
|
+
*/
|
|
74
|
+
set(key, value) {
|
|
75
|
+
if (!key || is.typeOf(key) !== 'string')
|
|
76
|
+
return false;
|
|
77
|
+
try {
|
|
78
|
+
const finalKey = this.#k(key);
|
|
79
|
+
const finalValue = typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
80
|
+
this.#t.setItem(finalKey, finalValue);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
if (e?.name === 'QuotaExceededError') {
|
|
85
|
+
// 处理空间不足的情况(QuotaExceededError)
|
|
86
|
+
console.warn('[Storage] 储存空间已满,请清理数据');
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
console.error(`[Storage] 设置项 "${key}" 失败:`, e);
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* # 读取键的值
|
|
96
|
+
*/
|
|
97
|
+
get(key) {
|
|
98
|
+
if (!key || is.typeOf(key) !== 'string')
|
|
99
|
+
return null;
|
|
100
|
+
try {
|
|
101
|
+
// 1. 处理键
|
|
102
|
+
const finalKey = this.#k(key);
|
|
103
|
+
// 2. 获取原始
|
|
104
|
+
const value = this.#t.getItem(finalKey);
|
|
105
|
+
if (value === null) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
// 3. 尝试解析 JSON ,失败则返回原字符串
|
|
109
|
+
return utils.tryJSONParse(value);
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
console.error(`[Storage] 获取键 "${key}" 失败:`, e);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* # 移除某键与值
|
|
118
|
+
*/
|
|
119
|
+
remove(key) {
|
|
120
|
+
if (!key || is.typeOf(key) !== 'string')
|
|
121
|
+
return this;
|
|
122
|
+
try {
|
|
123
|
+
const finalKey = this.#k(key);
|
|
124
|
+
this.#t.removeItem(finalKey);
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
console.error(`[Storage] 移除键 "${key}" 失败:`, e);
|
|
128
|
+
}
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* # 清理所有(该前缀下)的键与值
|
|
133
|
+
*/
|
|
134
|
+
clean() {
|
|
135
|
+
try {
|
|
136
|
+
if (this.prefix) {
|
|
137
|
+
// 1. 有前缀的,只删除该前缀的 key
|
|
138
|
+
const keysToRemove = [];
|
|
139
|
+
/** 前缀 */
|
|
140
|
+
const prefix = (this.prefix && this.prefix.concat('_')) || '';
|
|
141
|
+
// 2. 遍历本地储存键并比对前缀
|
|
142
|
+
for (let i = 0; i < this.#t.length; i++) {
|
|
143
|
+
const _key = this.#t.key(i);
|
|
144
|
+
if (!_key)
|
|
145
|
+
continue;
|
|
146
|
+
if (_key.startsWith(prefix))
|
|
147
|
+
keysToRemove.push(_key);
|
|
148
|
+
}
|
|
149
|
+
keysToRemove.forEach(k => this.#t.removeItem(k));
|
|
150
|
+
}
|
|
151
|
+
else
|
|
152
|
+
this.#t.clear();
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
console.error('[Storage] 请求失败:', e);
|
|
156
|
+
}
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* # 判定是否有某键值存在
|
|
161
|
+
*/
|
|
162
|
+
has(key) {
|
|
163
|
+
return this.get(key) !== null;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* # 获取所有(设定前缀下)的本地储存值
|
|
167
|
+
* @returns 获取的所有本地储存
|
|
168
|
+
*/
|
|
169
|
+
getAll() {
|
|
170
|
+
const result = {};
|
|
171
|
+
try {
|
|
172
|
+
/** 有前缀 */
|
|
173
|
+
const hasPrefix = Boolean(this.prefix);
|
|
174
|
+
/** 真实前缀 */
|
|
175
|
+
const prefix = hasPrefix ? this.prefix.concat('_') : '';
|
|
176
|
+
/** 没有前缀 */
|
|
177
|
+
const notPrefix = !hasPrefix;
|
|
178
|
+
/** 遍历并提取相应的值 */
|
|
179
|
+
for (let i = 0, j = this.#t.length; i < j; i++) {
|
|
180
|
+
const k = this.#t.key(i);
|
|
181
|
+
if (k && (notPrefix || k.startsWith(prefix))) {
|
|
182
|
+
/** 原始键值 */
|
|
183
|
+
const rawKey = hasPrefix ? k.replace(this.prefix, '') : k;
|
|
184
|
+
const val = this.#t.getItem(k); // k 是遍历来的键,值一定存在
|
|
185
|
+
result[rawKey] = utils.tryJSONParse(val);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
console.error('[Storage] 获取所有储存失败:', e);
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
exports.Storage = Storage;
|
package/es/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export type { CookieOption, CookieSameSite } from './cookie';
|
|
|
3
3
|
export { copyText } from './copy-text';
|
|
4
4
|
export { isOrientation } from './is-orientation';
|
|
5
5
|
export { isDarkTheme, matchMediaByString, matchMediaState, matchMediaEvery, matchMediaEveryState, matchMediaSome, matchMediaSomeState, } from './is-dark';
|
|
6
|
+
export { Storage } from './storage';
|
package/es/index.js
CHANGED
|
@@ -2,3 +2,4 @@ export { getAllCookie, getCookie, hasCookie, removeCookie, setCookie } from './c
|
|
|
2
2
|
export { copyText } from './copy-text.js';
|
|
3
3
|
export { isOrientation } from './is-orientation.js';
|
|
4
4
|
export { isDarkTheme, matchMediaByString, matchMediaEvery, matchMediaEveryState, matchMediaSome, matchMediaSomeState, matchMediaState } from './is-dark.js';
|
|
5
|
+
export { Storage } from './storage.js';
|
package/es/storage.d.ts
CHANGED
|
@@ -6,46 +6,68 @@
|
|
|
6
6
|
* @copyright 2026 ©️ Mr.MudBean
|
|
7
7
|
* @since 2026-08-06 23:31
|
|
8
8
|
* @version 1.0.0
|
|
9
|
-
* @lastModified 2026-08-
|
|
9
|
+
* @lastModified 2026-08-26 01:18
|
|
10
10
|
*/
|
|
11
11
|
/**
|
|
12
|
+
* # storage 储存类
|
|
12
13
|
*
|
|
14
|
+
* 通过 `new Storage` 构建一个储存对象,实现简单的 storage 操纵。
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const storage = new Storage();
|
|
19
|
+
*
|
|
20
|
+
* ```
|
|
13
21
|
*/
|
|
14
22
|
export declare class Storage {
|
|
15
23
|
#private;
|
|
16
24
|
/**
|
|
17
25
|
* # 前缀
|
|
18
26
|
* 设置可防止多项目部署的时候 key 冲突 (例如 `myApp_` )
|
|
27
|
+
*
|
|
28
|
+
* *该值设定后不建议随意更改*
|
|
19
29
|
*/
|
|
20
30
|
prefix: string;
|
|
31
|
+
/**
|
|
32
|
+
* # Storage 构造方法
|
|
33
|
+
*
|
|
34
|
+
* @param prefix - 前缀,设置前缀可防止多项目部署时 key 冲突 (例如: `myApp_` )
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const storage = new Storage('');
|
|
38
|
+
*
|
|
39
|
+
*
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
constructor(prefix?: string);
|
|
21
43
|
/**
|
|
22
44
|
* # 切换储存类型
|
|
23
45
|
* @param type - 要切换的类型,除了传入 `session` 外,其余都将视为更改为 `localStorage`
|
|
24
46
|
*/
|
|
25
47
|
use(type?: 'session'): void;
|
|
26
48
|
/**
|
|
27
|
-
*
|
|
49
|
+
* # 设置键与值
|
|
28
50
|
*/
|
|
29
|
-
set(key: string, value:
|
|
51
|
+
set<T extends any>(key: string, value: T): boolean;
|
|
30
52
|
/**
|
|
31
|
-
*
|
|
53
|
+
* # 读取键的值
|
|
32
54
|
*/
|
|
33
|
-
get(key: string):
|
|
55
|
+
get<T extends any = any>(key: string): T | null;
|
|
34
56
|
/**
|
|
35
|
-
*
|
|
57
|
+
* # 移除某键与值
|
|
36
58
|
*/
|
|
37
59
|
remove(key: string): Storage;
|
|
38
60
|
/**
|
|
39
|
-
*
|
|
61
|
+
* # 清理所有(该前缀下)的键与值
|
|
40
62
|
*/
|
|
41
|
-
|
|
63
|
+
clean(): Storage;
|
|
42
64
|
/**
|
|
43
|
-
*
|
|
65
|
+
* # 判定是否有某键值存在
|
|
44
66
|
*/
|
|
45
67
|
has(key: string): boolean;
|
|
46
68
|
/**
|
|
47
|
-
* #
|
|
69
|
+
* # 获取所有(设定前缀下)的本地储存值
|
|
48
70
|
* @returns 获取的所有本地储存
|
|
49
71
|
*/
|
|
50
|
-
getAll
|
|
72
|
+
getAll<T extends Record<string, any> = Record<string, any>>(): Partial<T>;
|
|
51
73
|
}
|
package/es/storage.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { typeOf } from '@vvi/is';
|
|
2
|
+
import { tryJSONParse } from '@vvi/utils';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @module @vvi/browser/storage
|
|
6
|
+
* @file storage.ts
|
|
7
|
+
* @description 本地储存
|
|
8
|
+
* @author Mr.MudBean <Mr.MudBean@outlook.com>
|
|
9
|
+
* @copyright 2026 ©️ Mr.MudBean
|
|
10
|
+
* @since 2026-08-06 23:31
|
|
11
|
+
* @version 1.0.0
|
|
12
|
+
* @lastModified 2026-08-26 01:18
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* # storage 储存类
|
|
16
|
+
*
|
|
17
|
+
* 通过 `new Storage` 构建一个储存对象,实现简单的 storage 操纵。
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const storage = new Storage();
|
|
22
|
+
*
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
class Storage {
|
|
26
|
+
/** 默认使用 localStorage ,也可以使用 sessionStorage */
|
|
27
|
+
#t = window.localStorage;
|
|
28
|
+
/**
|
|
29
|
+
* # 前缀
|
|
30
|
+
* 设置可防止多项目部署的时候 key 冲突 (例如 `myApp_` )
|
|
31
|
+
*
|
|
32
|
+
* *该值设定后不建议随意更改*
|
|
33
|
+
*/
|
|
34
|
+
prefix = '';
|
|
35
|
+
/**
|
|
36
|
+
* # Storage 构造方法
|
|
37
|
+
*
|
|
38
|
+
* @param prefix - 前缀,设置前缀可防止多项目部署时 key 冲突 (例如: `myApp_` )
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const storage = new Storage('');
|
|
42
|
+
*
|
|
43
|
+
*
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
constructor(prefix) {
|
|
47
|
+
if (prefix && typeOf(prefix) === 'string') {
|
|
48
|
+
this.prefix = prefix;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* # 切换储存类型
|
|
53
|
+
* @param type - 要切换的类型,除了传入 `session` 外,其余都将视为更改为 `localStorage`
|
|
54
|
+
*/
|
|
55
|
+
use(type) {
|
|
56
|
+
this.#t = type === 'session' ? window.sessionStorage : window.localStorage;
|
|
57
|
+
}
|
|
58
|
+
/** 拼接方法(内部) */
|
|
59
|
+
#k(key) {
|
|
60
|
+
if (this.prefix) {
|
|
61
|
+
return this.prefix
|
|
62
|
+
.concat(this.prefix.endsWith('_') ? '' : '_')
|
|
63
|
+
.concat(key);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return key;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* # 设置键与值
|
|
71
|
+
*/
|
|
72
|
+
set(key, value) {
|
|
73
|
+
if (!key || typeOf(key) !== 'string')
|
|
74
|
+
return false;
|
|
75
|
+
try {
|
|
76
|
+
const finalKey = this.#k(key);
|
|
77
|
+
const finalValue = typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
78
|
+
this.#t.setItem(finalKey, finalValue);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
if (e?.name === 'QuotaExceededError') {
|
|
83
|
+
// 处理空间不足的情况(QuotaExceededError)
|
|
84
|
+
console.warn('[Storage] 储存空间已满,请清理数据');
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
console.error(`[Storage] 设置项 "${key}" 失败:`, e);
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* # 读取键的值
|
|
94
|
+
*/
|
|
95
|
+
get(key) {
|
|
96
|
+
if (!key || typeOf(key) !== 'string')
|
|
97
|
+
return null;
|
|
98
|
+
try {
|
|
99
|
+
// 1. 处理键
|
|
100
|
+
const finalKey = this.#k(key);
|
|
101
|
+
// 2. 获取原始
|
|
102
|
+
const value = this.#t.getItem(finalKey);
|
|
103
|
+
if (value === null) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
// 3. 尝试解析 JSON ,失败则返回原字符串
|
|
107
|
+
return tryJSONParse(value);
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
console.error(`[Storage] 获取键 "${key}" 失败:`, e);
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* # 移除某键与值
|
|
116
|
+
*/
|
|
117
|
+
remove(key) {
|
|
118
|
+
if (!key || typeOf(key) !== 'string')
|
|
119
|
+
return this;
|
|
120
|
+
try {
|
|
121
|
+
const finalKey = this.#k(key);
|
|
122
|
+
this.#t.removeItem(finalKey);
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
console.error(`[Storage] 移除键 "${key}" 失败:`, e);
|
|
126
|
+
}
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* # 清理所有(该前缀下)的键与值
|
|
131
|
+
*/
|
|
132
|
+
clean() {
|
|
133
|
+
try {
|
|
134
|
+
if (this.prefix) {
|
|
135
|
+
// 1. 有前缀的,只删除该前缀的 key
|
|
136
|
+
const keysToRemove = [];
|
|
137
|
+
/** 前缀 */
|
|
138
|
+
const prefix = (this.prefix && this.prefix.concat('_')) || '';
|
|
139
|
+
// 2. 遍历本地储存键并比对前缀
|
|
140
|
+
for (let i = 0; i < this.#t.length; i++) {
|
|
141
|
+
const _key = this.#t.key(i);
|
|
142
|
+
if (!_key)
|
|
143
|
+
continue;
|
|
144
|
+
if (_key.startsWith(prefix))
|
|
145
|
+
keysToRemove.push(_key);
|
|
146
|
+
}
|
|
147
|
+
keysToRemove.forEach(k => this.#t.removeItem(k));
|
|
148
|
+
}
|
|
149
|
+
else
|
|
150
|
+
this.#t.clear();
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
console.error('[Storage] 请求失败:', e);
|
|
154
|
+
}
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* # 判定是否有某键值存在
|
|
159
|
+
*/
|
|
160
|
+
has(key) {
|
|
161
|
+
return this.get(key) !== null;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* # 获取所有(设定前缀下)的本地储存值
|
|
165
|
+
* @returns 获取的所有本地储存
|
|
166
|
+
*/
|
|
167
|
+
getAll() {
|
|
168
|
+
const result = {};
|
|
169
|
+
try {
|
|
170
|
+
/** 有前缀 */
|
|
171
|
+
const hasPrefix = Boolean(this.prefix);
|
|
172
|
+
/** 真实前缀 */
|
|
173
|
+
const prefix = hasPrefix ? this.prefix.concat('_') : '';
|
|
174
|
+
/** 没有前缀 */
|
|
175
|
+
const notPrefix = !hasPrefix;
|
|
176
|
+
/** 遍历并提取相应的值 */
|
|
177
|
+
for (let i = 0, j = this.#t.length; i < j; i++) {
|
|
178
|
+
const k = this.#t.key(i);
|
|
179
|
+
if (k && (notPrefix || k.startsWith(prefix))) {
|
|
180
|
+
/** 原始键值 */
|
|
181
|
+
const rawKey = hasPrefix ? k.replace(this.prefix, '') : k;
|
|
182
|
+
const val = this.#t.getItem(k); // k 是遍历来的键,值一定存在
|
|
183
|
+
result[rawKey] = tryJSONParse(val);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
catch (e) {
|
|
188
|
+
console.error('[Storage] 获取所有储存失败:', e);
|
|
189
|
+
}
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export { Storage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvi/browser",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"main": "cjs/index.js",
|
|
5
5
|
"types": "es/index.d.ts",
|
|
6
6
|
"author": {
|
|
@@ -91,12 +91,12 @@
|
|
|
91
91
|
"@vvi/utils": "^2.1.0"
|
|
92
92
|
},
|
|
93
93
|
"bugs": {
|
|
94
|
-
"url": "https://github.com/
|
|
94
|
+
"url": "https://github.com/gleanings/browser/issues",
|
|
95
95
|
"email": "Mr.MudBean@outlook.com"
|
|
96
96
|
},
|
|
97
97
|
"repository": {
|
|
98
98
|
"type": "git",
|
|
99
|
-
"url": "git+https://github.com/
|
|
99
|
+
"url": "git+https://github.com/gleanings/browser.git"
|
|
100
100
|
},
|
|
101
101
|
"publishConfig": {
|
|
102
102
|
"access": "public",
|