@nsnanocat/util 2.1.3 → 2.1.5

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 CHANGED
@@ -315,10 +315,11 @@ await runScript("$done({})", { timeout: 20 });
315
315
  - `database: object`(默认数据库)
316
316
  - 返回:`{ Settings, Configs, Caches }`
317
317
 
318
- 合并顺序由 `$argument.Storage` 控制(持久化读取统一使用 `PersistentStore = Storage.getItem(key, {})`):
319
- 1. 默认(`undefined`):`database[name]` -> `$argument` -> `PersistentStore[name]`
320
- 2. `$argument`:`database[name]` -> `PersistentStore[name]` -> `$argument`
321
- 3. `PersistentStore` / `BoxJs`:`database[name]` -> `PersistentStore[name]`
318
+ 合并顺序由 `$argument.Storage` 控制(持久化读取统一使用 `PersistentStore = Storage.getItem(key, {})`;支持别名):
319
+ - 可用值(大小写敏感):`undefined` | `Argument` | `$argument` | `PersistentStore` | `BoxJs` | `boxjs` | `$persistentStore` | `database`
320
+ 1. `undefined`:`database[name]` -> `$argument` -> `PersistentStore[name]`
321
+ 2. `Argument` / `$argument`:`database[name]` -> `PersistentStore[name]` -> `$argument`
322
+ 3. `PersistentStore` / `BoxJs` / `$persistentStore`(默认):`database[name]` -> `PersistentStore[name]`
322
323
  4. `database`:仅 `database[name]`
323
324
 
324
325
  注意:`Configs` 与 `Caches` 始终按每个 `name` 合并(与 `$argument.Storage` 无关)。
@@ -330,7 +331,7 @@ await runScript("$done({})", { timeout: 20 });
330
331
 
331
332
  示例:
332
333
  ```js
333
- import { getStorage } from "@nsnanocat/util/getStorage.mjs";
334
+ import getStorage from "@nsnanocat/util/getStorage.mjs";
334
335
 
335
336
  const store = getStorage("@my_box", ["YouTube", "Global"], database);
336
337
  ```
package/getStorage.mjs CHANGED
@@ -17,25 +17,30 @@ import { Storage } from "./polyfill/Storage.mjs";
17
17
  * 读取并合并默认配置、持久化配置与 `$argument`。
18
18
  * Read and merge default config, persisted config and `$argument`.
19
19
  *
20
- * 注意:`Configs` 与 `Caches` 始终按每个 profile(`names`)合并;`Settings` 的合并顺序由 `$argument.Storage` 控制。
21
- * Note: `Configs` and `Caches` are always merged per-profile (`names`); the merge order for `Settings` is controlled by `$argument.Storage`.
20
+ * 注意:`Configs` 与 `Caches` 始终按每个 profile(`names`)合并;`Settings` 的合并顺序由 `$argument.Storage` 控制(支持别名)。
21
+ * Note: `Configs` and `Caches` are always merged per-profile (`names`); the merge order for `Settings` is controlled by `$argument.Storage` (aliases supported).
22
22
  *
23
- * 合并来源与顺序由 `$argument.Storage` 控制:
24
- * Merge source order is controlled by `$argument.Storage`:
25
- * - `undefined`(默认): `database[name]` -> `$argument` -> `PersistentStore[name]`
26
- * - `$argument`: `database[name]` -> `PersistentStore[name]` -> `$argument`
27
- * - `PersistentStore` / `BoxJs`: `database[name]` -> `PersistentStore[name]`
23
+ * 合并来源与顺序由 `$argument.Storage` 控制(支持以下值 / 别名):
24
+ * Merge source order is controlled by `$argument.Storage` (accepted values / aliases):
25
+ * - `undefined`: `database[name]` -> `$argument` -> `PersistentStore[name]`
26
+ * - `Argument` / `$argument`: `database[name]` -> `PersistentStore[name]` -> `$argument`
27
+ * - `PersistentStore` / `BoxJs` / `boxjs` / `$persistentStore`(默认):`database[name]` -> `PersistentStore[name]`
28
28
  * - `database`: 仅 `database[name]`
29
29
  *
30
- * @since 2.1.2
30
+ * 注意:字符串比较为精确匹配(区分大小写)。
31
+ *
32
+ * @since 2.1.3
31
33
  * @link https://github.com/NanoCat-Me/utils/blob/main/getStorage.mjs
32
34
  * @author VirgilClyne
33
35
  * @param {string} key 持久化主键 / Persistent store key.
34
36
  * @param {string|string[]|Array<string|string[]>} names 目标配置名 / Target profile names.
35
37
  * @param {Record<string, any>} database 默认数据库 / Default database object.
36
38
  * @returns {StorageProfile}
39
+ *
40
+ * @module getStorage
41
+ * @default
37
42
  */
38
- export function getStorage(key, names, database) {
43
+ export default function getStorage(key, names, database) {
39
44
  if (database?.Default?.Settings?.LogLevel) Console.logLevel = database.Default.Settings.LogLevel;
40
45
  Console.debug("☑️ getStorage");
41
46
  names = [names].flat(Number.POSITIVE_INFINITY);
@@ -65,14 +70,18 @@ export function getStorage(key, names, database) {
65
70
  _.merge(Root.Caches, PersistentStore?.[name]?.Caches);
66
71
  });
67
72
  switch ($argument.Storage) {
73
+ case "Argument":
68
74
  case "$argument":
69
75
  names.forEach(name => {
70
76
  _.merge(Root.Settings, database?.[name]?.Settings, PersistentStore?.[name]?.Settings);
71
77
  });
72
78
  _.merge(Root.Settings, $argument);
73
79
  break;
80
+ default:
74
81
  case "BoxJs":
82
+ case "boxjs":
75
83
  case "PersistentStore":
84
+ case "$persistentStore":
76
85
  names.forEach(name => {
77
86
  _.merge(Root.Settings, database?.[name]?.Settings, PersistentStore?.[name]?.Settings);
78
87
  });
@@ -82,7 +91,6 @@ export function getStorage(key, names, database) {
82
91
  _.merge(Root.Settings, database?.[name]?.Settings);
83
92
  });
84
93
  break;
85
- default:
86
94
  case undefined:
87
95
  names.forEach(name => {
88
96
  _.merge(Root.Settings, database?.[name]?.Settings);
package/package.json CHANGED
@@ -14,6 +14,7 @@
14
14
  "license": "Apache-2.0",
15
15
  "bugs": "https://github.com/NSNanoCat/util/issues",
16
16
  "main": "index.js",
17
+ "types": "types/nsnanocat-util.d.ts",
17
18
  "type": "module",
18
19
  "scripts": {
19
20
  "tsc:build": "npx tsc",
@@ -30,7 +31,8 @@
30
31
  "index.js",
31
32
  "lib",
32
33
  "polyfill",
33
- "getStorage.mjs"
34
+ "getStorage.mjs",
35
+ "types"
34
36
  ],
35
37
  "devDependencies": {
36
38
  "typescript": "^5.6.3"
@@ -39,5 +41,5 @@
39
41
  "registry": "https://registry.npmjs.org/",
40
42
  "access": "public"
41
43
  },
42
- "version": "2.1.3"
44
+ "version": "2.1.5"
43
45
  }
@@ -0,0 +1,134 @@
1
+ declare module "@nsnanocat/util" {
2
+ export type AppName = "Quantumult X" | "Loon" | "Shadowrocket" | "Node.js" | "Egern" | "Surge" | "Stash";
3
+
4
+ export const $app: AppName | undefined;
5
+ export const $argument: Record<string, unknown>;
6
+ export const argument: typeof $argument;
7
+
8
+ export interface DonePayload {
9
+ status?: number | string;
10
+ url?: string;
11
+ headers?: Record<string, unknown>;
12
+ body?: string | ArrayBuffer | ArrayBufferView;
13
+ bodyBytes?: ArrayBuffer;
14
+ policy?: string;
15
+ }
16
+
17
+ export function done(object?: DonePayload): void;
18
+
19
+ export interface NotificationContentObject {
20
+ open?: string;
21
+ "open-url"?: string;
22
+ url?: string;
23
+ openUrl?: string;
24
+ copy?: string;
25
+ "update-pasteboard"?: string;
26
+ updatePasteboard?: string;
27
+ media?: string;
28
+ "media-url"?: string;
29
+ mediaUrl?: string;
30
+ mime?: string;
31
+ "auto-dismiss"?: number;
32
+ sound?: string;
33
+ }
34
+
35
+ export type NotificationContent = NotificationContentObject | string | number | boolean;
36
+
37
+ export function notification(
38
+ title?: string,
39
+ subtitle?: string,
40
+ body?: string,
41
+ content?: NotificationContent,
42
+ ): void;
43
+
44
+ export function time(format: string, ts?: number): string;
45
+ export function wait(delay?: number): Promise<void>;
46
+
47
+ export interface FetchRequest {
48
+ url: string;
49
+ method?: string;
50
+ headers?: Record<string, unknown>;
51
+ body?: string | ArrayBuffer | ArrayBufferView | object;
52
+ bodyBytes?: ArrayBuffer;
53
+ timeout?: number | string;
54
+ policy?: string;
55
+ redirection?: boolean;
56
+ "auto-redirect"?: boolean;
57
+ opts?: Record<string, unknown>;
58
+ [key: string]: unknown;
59
+ }
60
+
61
+ export interface FetchResponse {
62
+ ok: boolean;
63
+ status: number;
64
+ statusCode?: number;
65
+ statusText?: string;
66
+ headers?: Record<string, unknown>;
67
+ body?: string | ArrayBuffer;
68
+ bodyBytes?: ArrayBuffer;
69
+ [key: string]: unknown;
70
+ }
71
+
72
+ export function fetch(resource: FetchRequest | string, options?: Partial<FetchRequest>): Promise<FetchResponse>;
73
+
74
+ export class Console {
75
+ static clear(): void;
76
+ static count(label?: string): void;
77
+ static countReset(label?: string): void;
78
+ static debug(...msg: unknown[]): void;
79
+ static error(...msg: unknown[]): void;
80
+ static exception(...msg: unknown[]): void;
81
+ static group(label: string): number;
82
+ static groupEnd(): string | undefined;
83
+ static info(...msg: unknown[]): void;
84
+ static get logLevel(): "OFF" | "ERROR" | "WARN" | "INFO" | "DEBUG" | "ALL";
85
+ static set logLevel(level: number | string);
86
+ static log(...msg: unknown[]): void;
87
+ static time(label?: string): Map<string, number>;
88
+ static timeEnd(label?: string): boolean;
89
+ static timeLog(label?: string): void;
90
+ static warn(...msg: unknown[]): void;
91
+ }
92
+
93
+ export class Lodash {
94
+ static escape(string: string): string;
95
+ static get<T = unknown, D = undefined>(object?: Record<string, unknown>, path?: string | string[], defaultValue?: D): T | D;
96
+ static merge<T extends Record<string, unknown>>(object: T, ...sources: Array<Record<string, unknown> | null | undefined>): T;
97
+ static omit<T extends Record<string, unknown>>(object?: T, paths?: string | string[]): T;
98
+ static pick<T extends Record<string, unknown>, K extends keyof T>(object?: T, paths?: K | K[]): Pick<T, K>;
99
+ static set<T extends Record<string, unknown>>(object: T, path: string | string[], value: unknown): T;
100
+ static toPath(value: string): string[];
101
+ static unescape(string: string): string;
102
+ static unset(object?: Record<string, unknown>, path?: string | string[]): boolean;
103
+ }
104
+
105
+ export const StatusTexts: Record<number, string>;
106
+
107
+ export class Storage {
108
+ static data: Record<string, unknown> | null;
109
+ static dataFile: string;
110
+ static getItem<T = unknown>(keyName: string, defaultValue?: T): T;
111
+ static setItem(keyName: string, keyValue: unknown): boolean;
112
+ static removeItem(keyName: string): boolean;
113
+ static clear(): boolean;
114
+ }
115
+ }
116
+
117
+ declare module "@nsnanocat/util/getStorage.mjs" {
118
+ export interface StorageProfile {
119
+ Settings: Record<string, unknown>;
120
+ Configs: Record<string, unknown>;
121
+ Caches: Record<string, unknown>;
122
+ }
123
+
124
+ export default function getStorage(
125
+ key: string,
126
+ names: string | string[] | Array<string | string[]>,
127
+ database: Record<string, unknown>,
128
+ ): StorageProfile;
129
+ }
130
+
131
+ declare module "@nsnanocat/util/lib/environment.mjs" {
132
+ export const $environment: Record<string, unknown>;
133
+ export function environment(): Record<string, unknown>;
134
+ }