@nsnanocat/util 2.1.5 → 2.1.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/README.md +24 -4
- package/getStorage.mjs +35 -8
- package/package.json +1 -1
- package/polyfill/Storage.mjs +10 -2
- package/types/nsnanocat-util.d.ts +9 -0
package/README.md
CHANGED
|
@@ -87,7 +87,7 @@ import {
|
|
|
87
87
|
### 仓库中存在但未从主入口导出
|
|
88
88
|
- `lib/environment.mjs`
|
|
89
89
|
- `lib/runScript.mjs`
|
|
90
|
-
- `getStorage.mjs
|
|
90
|
+
- `getStorage.mjs`(薯条项目自用,仅当你的存储结构与薯条项目一致时再使用;请通过子路径 `@nsnanocat/util/getStorage.mjs` 导入)
|
|
91
91
|
|
|
92
92
|
## 模块依赖关系
|
|
93
93
|
|
|
@@ -336,6 +336,24 @@ import getStorage from "@nsnanocat/util/getStorage.mjs";
|
|
|
336
336
|
const store = getStorage("@my_box", ["YouTube", "Global"], database);
|
|
337
337
|
```
|
|
338
338
|
|
|
339
|
+
#### 命名导出(辅助函数)
|
|
340
|
+
|
|
341
|
+
`getStorage.mjs` 同时导出以下辅助函数:
|
|
342
|
+
- `traverseObject(o, c)`:深度遍历对象并替换叶子值
|
|
343
|
+
- `string2number(string)`:将纯数字字符串转换为数字
|
|
344
|
+
- `value2array(value)`:字符串按逗号拆分;数字/布尔值会被包装为单元素数组
|
|
345
|
+
|
|
346
|
+
示例:
|
|
347
|
+
```js
|
|
348
|
+
import getStorage, {
|
|
349
|
+
traverseObject,
|
|
350
|
+
string2number,
|
|
351
|
+
value2array,
|
|
352
|
+
} from "@nsnanocat/util/getStorage.mjs";
|
|
353
|
+
|
|
354
|
+
const store = getStorage("@my_box", ["YouTube", "Global"], database);
|
|
355
|
+
```
|
|
356
|
+
|
|
339
357
|
### `polyfill/fetch.mjs`
|
|
340
358
|
|
|
341
359
|
`fetch` 是仿照 Web API `Window.fetch` 设计的跨平台适配实现:
|
|
@@ -412,10 +430,12 @@ const store = getStorage("@my_box", ["YouTube", "Global"], database);
|
|
|
412
430
|
#### `Storage.removeItem(keyName)`
|
|
413
431
|
- Quantumult X:可用(`$prefs.removeValueForKey`)。
|
|
414
432
|
- Surge:通过 `$persistentStore.write(null, keyName)` 删除。
|
|
415
|
-
-
|
|
433
|
+
- Node.js:可用(删除 `box.dat` 中对应 key 并落盘)。
|
|
434
|
+
- Loon / Stash / Egern / Shadowrocket:返回 `false`。
|
|
416
435
|
|
|
417
436
|
#### `Storage.clear()`
|
|
418
437
|
- Quantumult X:可用(`$prefs.removeAllValues`)。
|
|
438
|
+
- Node.js:可用(清空 `box.dat` 并落盘)。
|
|
419
439
|
- 其他平台:返回 `false`。
|
|
420
440
|
|
|
421
441
|
#### Node.js 特性
|
|
@@ -424,7 +444,7 @@ const store = getStorage("@my_box", ["YouTube", "Global"], database);
|
|
|
424
444
|
|
|
425
445
|
与 Web Storage 的行为差异:
|
|
426
446
|
- 支持 `@key.path` 深路径读写(Web Storage 原生不支持)。
|
|
427
|
-
- `removeItem/clear` 仅部分平台可用(目前为 Quantumult X,以及 Surge 的 `removeItem`)。
|
|
447
|
+
- `removeItem/clear` 仅部分平台可用(目前为 Quantumult X、Node.js,以及 Surge 的 `removeItem`)。
|
|
428
448
|
- `getItem` 会尝试 `JSON.parse`,`setItem` 写入对象会 `JSON.stringify`。
|
|
429
449
|
|
|
430
450
|
平台后端映射:
|
|
@@ -604,7 +624,7 @@ console.log(value); // 1
|
|
|
604
624
|
| 通知 | `$notify` | `$notification.post` | `$notification.post` | `$notification.post` | `$notification.post` | `$notification.post` | 无 |
|
|
605
625
|
| 持久化 | `$prefs` | `$persistentStore` | `$persistentStore` | `$persistentStore` | `$persistentStore` | `$persistentStore` | `box.dat` |
|
|
606
626
|
| 结束脚本 | `$done` | `$done` | `$done` | `$done` | `$done` | `$done` | `process.exit(1)` |
|
|
607
|
-
| `removeItem/clear` | 可用 | 不可用 | `removeItem` 可用 / `clear` 不可用 | 不可用 | 不可用 | 不可用 |
|
|
627
|
+
| `removeItem/clear` | 可用 | 不可用 | `removeItem` 可用 / `clear` 不可用 | 不可用 | 不可用 | 不可用 | 可用 |
|
|
608
628
|
| `policy` 注入(`fetch/done`) | `opts.policy` | `node` | `X-Surge-Policy`(done) | `X-Stash-Selected-Proxy` | 无专门映射 | `X-Surge-Proxy`(fetch) | 无 |
|
|
609
629
|
|
|
610
630
|
## 已知限制与注意事项
|
package/getStorage.mjs
CHANGED
|
@@ -106,12 +106,20 @@ export default function getStorage(key, names, database) {
|
|
|
106
106
|
/***************** traverseObject *****************/
|
|
107
107
|
traverseObject(Root.Settings, (key, value) => {
|
|
108
108
|
Console.debug("☑️ traverseObject", `${key}: ${typeof value}`, `${key}: ${JSON.stringify(value)}`);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
109
|
+
switch (typeof value) {
|
|
110
|
+
case "string":
|
|
111
|
+
switch (value) {
|
|
112
|
+
case "true":
|
|
113
|
+
case "false":
|
|
114
|
+
case "[]":
|
|
115
|
+
value = JSON.parse(value); // 字符串转Boolean/空数组
|
|
116
|
+
break;
|
|
117
|
+
default:
|
|
118
|
+
if (value.includes(","))
|
|
119
|
+
value = value2array(value).map(item => string2number(item)); // 字符串转数组转数字
|
|
120
|
+
else value = string2number(value); // 字符串转数字
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
115
123
|
}
|
|
116
124
|
return value;
|
|
117
125
|
});
|
|
@@ -128,7 +136,7 @@ export default function getStorage(key, names, database) {
|
|
|
128
136
|
* @param {(key: string, value: any) => any} c 处理回调 / Transformer callback.
|
|
129
137
|
* @returns {Record<string, any>}
|
|
130
138
|
*/
|
|
131
|
-
function traverseObject(o, c) {
|
|
139
|
+
export function traverseObject(o, c) {
|
|
132
140
|
for (const t in o) {
|
|
133
141
|
const n = o[t];
|
|
134
142
|
o[t] = "object" === typeof n && null !== n ? traverseObject(n, c) : c(t, n);
|
|
@@ -143,7 +151,26 @@ function traverseObject(o, c) {
|
|
|
143
151
|
* @param {string} string 输入字符串 / Input string.
|
|
144
152
|
* @returns {string|number}
|
|
145
153
|
*/
|
|
146
|
-
function string2number(string) {
|
|
154
|
+
export function string2number(string) {
|
|
147
155
|
if (/^\d+$/.test(string)) string = Number.parseInt(string, 10);
|
|
148
156
|
return string;
|
|
149
157
|
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 将值包装为数组。
|
|
161
|
+
* Split value into array.
|
|
162
|
+
*
|
|
163
|
+
* @param {string|number|boolean|string[]|null|undefined} value 输入值 / Input value.
|
|
164
|
+
* @returns {(string|number|boolean)[]}
|
|
165
|
+
*/
|
|
166
|
+
export function value2array(value) {
|
|
167
|
+
switch (typeof value) {
|
|
168
|
+
case "string":
|
|
169
|
+
return value.split(",");
|
|
170
|
+
case "number":
|
|
171
|
+
case "boolean":
|
|
172
|
+
return [value];
|
|
173
|
+
default:
|
|
174
|
+
return value || [];
|
|
175
|
+
}
|
|
176
|
+
}
|
package/package.json
CHANGED
package/polyfill/Storage.mjs
CHANGED
|
@@ -208,7 +208,11 @@ export class Storage {
|
|
|
208
208
|
result = $prefs.removeValueForKey(keyName);
|
|
209
209
|
break;
|
|
210
210
|
case "Node.js":
|
|
211
|
-
result = false;
|
|
211
|
+
// result = false;
|
|
212
|
+
Storage.data = Storage.#loaddata(Storage.dataFile);
|
|
213
|
+
delete Storage.data[keyName];
|
|
214
|
+
Storage.#writedata(Storage.dataFile);
|
|
215
|
+
result = true;
|
|
212
216
|
break;
|
|
213
217
|
default:
|
|
214
218
|
result = false;
|
|
@@ -239,7 +243,11 @@ export class Storage {
|
|
|
239
243
|
result = $prefs.removeAllValues();
|
|
240
244
|
break;
|
|
241
245
|
case "Node.js":
|
|
242
|
-
result = false;
|
|
246
|
+
// result = false;
|
|
247
|
+
Storage.data = Storage.#loaddata(Storage.dataFile);
|
|
248
|
+
Storage.data = {};
|
|
249
|
+
Storage.#writedata(Storage.dataFile);
|
|
250
|
+
result = true;
|
|
243
251
|
break;
|
|
244
252
|
default:
|
|
245
253
|
result = false;
|
|
@@ -121,6 +121,15 @@ declare module "@nsnanocat/util/getStorage.mjs" {
|
|
|
121
121
|
Caches: Record<string, unknown>;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
export function traverseObject(
|
|
125
|
+
o: Record<string, unknown>,
|
|
126
|
+
c: (key: string, value: unknown) => unknown,
|
|
127
|
+
): Record<string, unknown>;
|
|
128
|
+
|
|
129
|
+
export function string2number(string: string): string | number;
|
|
130
|
+
|
|
131
|
+
export function value2array(value: string | number | boolean | string[] | null | undefined): Array<string | number | boolean>;
|
|
132
|
+
|
|
124
133
|
export default function getStorage(
|
|
125
134
|
key: string,
|
|
126
135
|
names: string | string[] | Array<string | string[]>,
|