@vvi/browser 1.0.0 → 1.1.0

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 CHANGED
@@ -1,3 +1,14 @@
1
1
  # 更新日志 📔
2
2
 
3
- ## v0.0.0 (2026-7-29)
3
+ ## 1.1.0 (2026-10-12)
4
+
5
+ - 维护信息
6
+ - 将 inline-style 迁移到该包
7
+
8
+ ## 1.0.1 (2026-8-25)
9
+
10
+ - 维护注释及导出
11
+
12
+ ## v1.0.0 (2026-7-29)
13
+
14
+ - 构建项目
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  # MIT License
2
2
 
3
- Copyright ©️ <2026> <MrMudBean>
3
+ Copyright ©️ <2026> <Mr.MudBean>
4
4
 
5
5
  Permission to use, copy, modify, and/or distribute this software for any
6
6
  purpose with or without fee is hereby granted, provided that the above
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # browser
2
2
 
3
- [![version](<https://img.shields.io/npm/v/@vvi/browser.svg?logo=npm&logoColor=rgb(0,0,0)&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/@vvi/browser) [![issues 提交](<https://img.shields.io/badge/issues-提交-rgb(255,0,63)?logo=github>)](https://github.com/MrMudBean/browser/issues)
3
+ [![version](<https://img.shields.io/npm/v/@vvi/browser.svg?logo=npm&logoColor=rgb(0,0,0)&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/@vvi/browser) [![issues 提交](<https://img.shields.io/badge/issues-提交-rgb(255,0,63)?logo=github>)](https://github.com/gleanings/browser/issues)
4
4
 
5
5
  ## 安装
6
6
 
package/cjs/index.js CHANGED
@@ -4,6 +4,8 @@ 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');
8
+ var inlineStyle = require('./inline-style.js');
7
9
 
8
10
 
9
11
 
@@ -21,3 +23,6 @@ exports.matchMediaEveryState = isDark.matchMediaEveryState;
21
23
  exports.matchMediaSome = isDark.matchMediaSome;
22
24
  exports.matchMediaSomeState = isDark.matchMediaSomeState;
23
25
  exports.matchMediaState = isDark.matchMediaState;
26
+ exports.Storage = storage.Storage;
27
+ exports.removeStyle = inlineStyle.removeStyle;
28
+ exports.setStyle = inlineStyle.setStyle;
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ var is = require('@vvi/is');
4
+
5
+ // if (Object.keys(oldStyle).length !== 0) {
6
+ // for (let i = 0, k = Object.keys(oldStyle), j = k.length; i < j; i++) {
7
+ // if (k[i].startsWith('--')) {
8
+ // oldStyle[i] = window
9
+ // .getComputedStyle(node)
10
+ // .getPropertyValue(oldStyle[i]); /// 处理自定义属性
11
+ // } else {
12
+ // /** @ts-expect-error: 油盐不进 */
13
+ // templateStyle[oldStyle[i]] = oldStyle[oldStyle[i]];
14
+ // }
15
+ // }
16
+ // }
17
+ /**
18
+ * ## 为节点元素设置标准样式
19
+ * @param node 将设置样式的元素节点
20
+ * @param style 将设置的新样式
21
+ */
22
+ function setStyle(node, style) {
23
+ node.setAttribute('style', transformStyle({ ...getOldStyle(node), ...style }).replaceAll(/;;/gm, ';'));
24
+ }
25
+ /**
26
+ * ## 移除样式
27
+ * @param node 元素节点
28
+ * @param style 要移除的样式名称
29
+ */
30
+ function removeStyle(node, style) {
31
+ node.setAttribute('style', transformStyle(getOldStyle(node), style).replaceAll(/;;/gm, ';'));
32
+ }
33
+ /**
34
+ * ## 获取原有的行内样式
35
+ *
36
+ * @param node 节点元素
37
+ * @returns 元素的行内样式
38
+ */
39
+ function getOldStyle(node) {
40
+ const oldStyle = (node.getAttribute('style') ?? '')
41
+ .replace(/(;{2,})/gm, ';') // 去除多余的 `;`
42
+ .split(';') // 按 `;` 分割为数组
43
+ .reduce((previousValue, currentValue) => {
44
+ // 使用数组方法进行累计
45
+ if (!is.isBusinessEmptyString(currentValue)) {
46
+ /** 第一个被找到的下标 */
47
+ const firstIndex = currentValue.indexOf(':');
48
+ /** css Property 属性 */
49
+ let property = currentValue
50
+ .slice(0, firstIndex) // 从元组中切出所需的属性名
51
+ .trim(); /// 去除多余空格
52
+ property = hyphenToCapitalization(property); /// 连字符转大写
53
+ /** css property value 属性值 */
54
+ const propertyValue = node.style[property];
55
+ if (!is.isUndefined(propertyValue))
56
+ previousValue[property] = propertyValue; // 配置属性
57
+ }
58
+ return previousValue;
59
+ }, {});
60
+ return oldStyle;
61
+ }
62
+ /**
63
+ * ## 将对象的样式转化为 string
64
+ *
65
+ * 方便赋值
66
+ * @param style 待转化的样式,对象形式方便处理,字符串形式方便赋值
67
+ * @param removeProperty 待移除的属性,是一个字符串数组
68
+ * @returns 返回一个行内样式
69
+ *
70
+ */
71
+ function transformStyle(style, removeProperty) {
72
+ return Object.keys(style).reduce((previousValue, _c) => {
73
+ // 移除属性
74
+ if (removeProperty && removeProperty.includes(_c))
75
+ return previousValue;
76
+ return (previousValue + `${capitalizationToHyphen(_c)}: ${style[_c]};`);
77
+ }, '');
78
+ }
79
+ /**
80
+ * ## 连字符形式转大写
81
+ *
82
+ * @param property 待转化的属性字符串
83
+ * @returns
84
+ */
85
+ function hyphenToCapitalization(property) {
86
+ return property.startsWith('--')
87
+ ? property
88
+ : property.replace(/([a-z])-([a-z])/g, (_, m1, m2) => m1.concat(m2.toUpperCase()));
89
+ }
90
+ /**
91
+ * ## 大写转连字符
92
+ *
93
+ * @param property 待转化为连字符形式的字符串
94
+ * @returns 转换后de
95
+ */
96
+ function capitalizationToHyphen(property) {
97
+ return property.startsWith('--')
98
+ ? property
99
+ : property.replace(/[A-Z]/gm, letter => `-${letter.toLowerCase()}`);
100
+ }
101
+
102
+ exports.removeStyle = removeStyle;
103
+ exports.setStyle = setStyle;
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,5 @@ 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';
7
+ export { setStyle, removeStyle } from './inline-style';
package/es/index.js CHANGED
@@ -2,3 +2,5 @@ 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';
6
+ export { removeStyle, setStyle } from './inline-style.js';
@@ -0,0 +1,14 @@
1
+ /**
2
+ * ## 为节点元素设置标准样式
3
+ * @param node 将设置样式的元素节点
4
+ * @param style 将设置的新样式
5
+ */
6
+ export declare function setStyle(node: HTMLElement | HTMLInputElement, style: {
7
+ [x: string]: string | number;
8
+ }): void;
9
+ /**
10
+ * ## 移除样式
11
+ * @param node 元素节点
12
+ * @param style 要移除的样式名称
13
+ */
14
+ export declare function removeStyle(node: HTMLElement | HTMLInputElement, style: string[]): void;
@@ -0,0 +1,100 @@
1
+ import { isBusinessEmptyString, isUndefined } from '@vvi/is';
2
+
3
+ // if (Object.keys(oldStyle).length !== 0) {
4
+ // for (let i = 0, k = Object.keys(oldStyle), j = k.length; i < j; i++) {
5
+ // if (k[i].startsWith('--')) {
6
+ // oldStyle[i] = window
7
+ // .getComputedStyle(node)
8
+ // .getPropertyValue(oldStyle[i]); /// 处理自定义属性
9
+ // } else {
10
+ // /** @ts-expect-error: 油盐不进 */
11
+ // templateStyle[oldStyle[i]] = oldStyle[oldStyle[i]];
12
+ // }
13
+ // }
14
+ // }
15
+ /**
16
+ * ## 为节点元素设置标准样式
17
+ * @param node 将设置样式的元素节点
18
+ * @param style 将设置的新样式
19
+ */
20
+ function setStyle(node, style) {
21
+ node.setAttribute('style', transformStyle({ ...getOldStyle(node), ...style }).replaceAll(/;;/gm, ';'));
22
+ }
23
+ /**
24
+ * ## 移除样式
25
+ * @param node 元素节点
26
+ * @param style 要移除的样式名称
27
+ */
28
+ function removeStyle(node, style) {
29
+ node.setAttribute('style', transformStyle(getOldStyle(node), style).replaceAll(/;;/gm, ';'));
30
+ }
31
+ /**
32
+ * ## 获取原有的行内样式
33
+ *
34
+ * @param node 节点元素
35
+ * @returns 元素的行内样式
36
+ */
37
+ function getOldStyle(node) {
38
+ const oldStyle = (node.getAttribute('style') ?? '')
39
+ .replace(/(;{2,})/gm, ';') // 去除多余的 `;`
40
+ .split(';') // 按 `;` 分割为数组
41
+ .reduce((previousValue, currentValue) => {
42
+ // 使用数组方法进行累计
43
+ if (!isBusinessEmptyString(currentValue)) {
44
+ /** 第一个被找到的下标 */
45
+ const firstIndex = currentValue.indexOf(':');
46
+ /** css Property 属性 */
47
+ let property = currentValue
48
+ .slice(0, firstIndex) // 从元组中切出所需的属性名
49
+ .trim(); /// 去除多余空格
50
+ property = hyphenToCapitalization(property); /// 连字符转大写
51
+ /** css property value 属性值 */
52
+ const propertyValue = node.style[property];
53
+ if (!isUndefined(propertyValue))
54
+ previousValue[property] = propertyValue; // 配置属性
55
+ }
56
+ return previousValue;
57
+ }, {});
58
+ return oldStyle;
59
+ }
60
+ /**
61
+ * ## 将对象的样式转化为 string
62
+ *
63
+ * 方便赋值
64
+ * @param style 待转化的样式,对象形式方便处理,字符串形式方便赋值
65
+ * @param removeProperty 待移除的属性,是一个字符串数组
66
+ * @returns 返回一个行内样式
67
+ *
68
+ */
69
+ function transformStyle(style, removeProperty) {
70
+ return Object.keys(style).reduce((previousValue, _c) => {
71
+ // 移除属性
72
+ if (removeProperty && removeProperty.includes(_c))
73
+ return previousValue;
74
+ return (previousValue + `${capitalizationToHyphen(_c)}: ${style[_c]};`);
75
+ }, '');
76
+ }
77
+ /**
78
+ * ## 连字符形式转大写
79
+ *
80
+ * @param property 待转化的属性字符串
81
+ * @returns
82
+ */
83
+ function hyphenToCapitalization(property) {
84
+ return property.startsWith('--')
85
+ ? property
86
+ : property.replace(/([a-z])-([a-z])/g, (_, m1, m2) => m1.concat(m2.toUpperCase()));
87
+ }
88
+ /**
89
+ * ## 大写转连字符
90
+ *
91
+ * @param property 待转化为连字符形式的字符串
92
+ * @returns 转换后de
93
+ */
94
+ function capitalizationToHyphen(property) {
95
+ return property.startsWith('--')
96
+ ? property
97
+ : property.replace(/[A-Z]/gm, letter => `-${letter.toLowerCase()}`);
98
+ }
99
+
100
+ export { removeStyle, setStyle };
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-08 19:34
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: any): boolean;
51
+ set<T extends any>(key: string, value: T): boolean;
30
52
  /**
31
- *
53
+ * # 读取键的值
32
54
  */
33
- get(key: string): any;
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
- clear(): Storage;
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(): Record<string, any>;
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.0",
3
+ "version": "1.1.0",
4
4
  "main": "cjs/index.js",
5
5
  "types": "es/index.d.ts",
6
6
  "author": {
@@ -49,6 +49,16 @@
49
49
  "types": "./es/copy-text.d.ts"
50
50
  }
51
51
  },
52
+ "./inline-style": {
53
+ "import": {
54
+ "default": "./es/inline-style.js",
55
+ "types": "./es/inline-style.d.ts"
56
+ },
57
+ "require": {
58
+ "default": "./cjs/inline-style.js",
59
+ "types": "./es/inline-style.d.ts"
60
+ }
61
+ },
52
62
  "./is-dark": {
53
63
  "import": {
54
64
  "default": "./es/is-dark.js",
@@ -85,18 +95,18 @@
85
95
  "vvi",
86
96
  "mudbean"
87
97
  ],
88
- "homepage": "https://npm.lmssee.com/browser",
98
+ "homepage": "https://npms.gleanings.cn/browser",
89
99
  "dependencies": {
90
- "@vvi/is": "^2.0.6",
91
- "@vvi/utils": "^2.1.0"
100
+ "@vvi/is": "^2.0.8",
101
+ "@vvi/utils": "^2.1.2"
92
102
  },
93
103
  "bugs": {
94
- "url": "https://github.com/MrMudBean/browser/issues",
104
+ "url": "https://github.com/gleanings/browser/issues",
95
105
  "email": "Mr.MudBean@outlook.com"
96
106
  },
97
107
  "repository": {
98
108
  "type": "git",
99
- "url": "git+https://github.com/MrMudBean/browser.git"
109
+ "url": "git+https://github.com/gleanings/browser.git"
100
110
  },
101
111
  "publishConfig": {
102
112
  "access": "public",