@whitesev/utils 2.9.10 → 2.9.12

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.
@@ -3,21 +3,23 @@ export declare class GMMenu {
3
3
  private GM_Api;
4
4
  private MenuHandle;
5
5
  /**
6
- * @param details 菜单配置
6
+ * @param constructOptions 菜单配置
7
7
  */
8
- constructor(details: UtilsGMMenuConstructorOptions);
8
+ constructor(constructOptions: UtilsGMMenuConstructorOptions);
9
9
  /**
10
10
  * 新增菜单数据
11
- * @param menuOption
11
+ * @param options
12
12
  */
13
13
  private __add;
14
14
  /**
15
15
  * 新增菜单数据
16
16
  *
17
17
  * 自动调用.update()
18
- * @param menuOption
18
+ * @param options
19
19
  */
20
- add(menuOption: UtilsGMMenuOption[] | UtilsGMMenuOption): void;
20
+ add(options: UtilsGMMenuOption[] | UtilsGMMenuOption): {
21
+ destory: () => void;
22
+ };
21
23
  /**
22
24
  * 更新菜单数据
23
25
  *
@@ -31,9 +33,9 @@ export declare class GMMenu {
31
33
  update(options?: UtilsGMMenuOption[] | UtilsGMMenuOption): void;
32
34
  /**
33
35
  * 卸载菜单
34
- * @param menuId 已注册的菜单id
36
+ * @param menuId 已注册的菜单id或者键名
35
37
  */
36
- delete(menuId: number): void;
38
+ delete(menuId: number | string): void;
37
39
  /**
38
40
  * 根据键值获取enable值
39
41
  * @param menuKey 菜单-键key
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.9.10",
4
+ "version": "2.9.12",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
@@ -64,7 +64,7 @@
64
64
  "lint": "eslint .",
65
65
  "lint:fix": "eslint . --fix",
66
66
  "format": "prettier . --write",
67
- "dev": "pnpm run format && pnpm run lint && rollup -c -w",
67
+ "dev": "vue-tsc --noEmit && vite --force",
68
68
  "build": "pnpm run format && pnpm run lint && rollup -c"
69
69
  }
70
70
  }
package/src/CommonUtil.ts CHANGED
@@ -125,38 +125,54 @@ class CommonUtil {
125
125
  isNull(...args: any[]): boolean {
126
126
  let result = true;
127
127
  const checkList = [...args];
128
- for (const objItem of checkList) {
129
- let itemResult = false;
130
- if (objItem === null || objItem === undefined) {
131
- itemResult = true;
128
+ for (const obj of checkList) {
129
+ let flag = false;
130
+ if (obj === null || obj === undefined) {
131
+ // null undefined
132
+ flag = true;
132
133
  } else {
133
- switch (typeof objItem) {
134
+ switch (typeof obj) {
134
135
  case "object":
135
- if (typeof objItem[Symbol.iterator] === "function") {
136
- /* 可迭代 */
137
- itemResult = objItem.length === 0;
136
+ if (typeof obj[Symbol.iterator] === "function") {
137
+ // 可迭代
138
+ // Map Array NodeList HTMLCollection
139
+ if (obj instanceof Map) {
140
+ flag = obj.size === 0;
141
+ } else {
142
+ const length = obj.length;
143
+ if (typeof length === "number") {
144
+ flag = length === 0;
145
+ } else {
146
+ // 其它的不处理
147
+ }
148
+ }
138
149
  } else {
139
- itemResult = Object.keys(objItem).length === 0;
150
+ if (obj?.toString() === "[object Object]") {
151
+ // {}
152
+ flag = Object.keys(obj).length === 0;
153
+ }
140
154
  }
141
155
  break;
142
156
  case "number":
143
- itemResult = objItem === 0;
157
+ flag = isNaN(obj) ? true : obj === 0;
144
158
  break;
145
- case "string":
146
- itemResult = objItem.trim() === "" || objItem === "null" || objItem === "undefined";
159
+ case "string": {
160
+ const trimStr = obj.trim();
161
+ flag = trimStr.trim() === "" || trimStr === "null" || trimStr === "undefined";
147
162
  break;
163
+ }
148
164
  case "boolean":
149
- itemResult = !objItem;
165
+ flag = !obj;
150
166
  break;
151
167
  case "function": {
152
- const funcStr = objItem.toString().replace(/\s/g, "");
168
+ const funcStr = obj.toString().replace(/\s/g, "");
153
169
  /* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
154
- itemResult = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
170
+ flag = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
155
171
  break;
156
172
  }
157
173
  }
158
174
  }
159
- result = result && itemResult;
175
+ result = result && flag;
160
176
  }
161
177
 
162
178
  return result;
@@ -223,29 +223,30 @@ export class GMMenu {
223
223
  },
224
224
  };
225
225
  /**
226
- * @param details 菜单配置
226
+ * @param constructOptions 菜单配置
227
227
  */
228
- constructor(details: UtilsGMMenuConstructorOptions) {
229
- this.GM_Api.getValue = details.GM_getValue;
230
- this.GM_Api.setValue = details.GM_setValue;
231
- this.GM_Api.registerMenuCommand = details.GM_registerMenuCommand;
232
- this.GM_Api.unregisterMenuCommand = details.GM_unregisterMenuCommand;
233
- this.MenuHandle.$default.autoReload = typeof details.autoReload === "boolean" ? details.autoReload : true;
228
+ constructor(constructOptions: UtilsGMMenuConstructorOptions) {
229
+ this.GM_Api.getValue = constructOptions.GM_getValue;
230
+ this.GM_Api.setValue = constructOptions.GM_setValue;
231
+ this.GM_Api.registerMenuCommand = constructOptions.GM_registerMenuCommand;
232
+ this.GM_Api.unregisterMenuCommand = constructOptions.GM_unregisterMenuCommand;
233
+ this.MenuHandle.$default.autoReload =
234
+ typeof constructOptions.autoReload === "boolean" ? constructOptions.autoReload : true;
234
235
  for (const keyName of Object.keys(this.GM_Api)) {
235
236
  if (typeof (this.GM_Api as any)[keyName] !== "function") {
236
237
  throw new Error(`Utils.GM_Menu 请在脚本开头加上 @grant ${keyName},且传入该对象`);
237
238
  }
238
239
  }
239
- this.add(details?.data || []);
240
+ this.add(constructOptions?.data || []);
240
241
  }
241
242
  /**
242
243
  * 新增菜单数据
243
- * @param menuOption
244
+ * @param options
244
245
  */
245
- private __add(menuOption: UtilsGMMenuOption[] | UtilsGMMenuOption) {
246
- if (Array.isArray(menuOption)) {
247
- for (let index = 0; index < menuOption.length; index++) {
248
- const option = menuOption[index];
246
+ private __add(options: UtilsGMMenuOption[] | UtilsGMMenuOption) {
247
+ if (Array.isArray(options)) {
248
+ for (let index = 0; index < options.length; index++) {
249
+ const option = options[index];
249
250
  this.MenuHandle.$data.data.push({
250
251
  data: option,
251
252
  id: void 0,
@@ -253,7 +254,7 @@ export class GMMenu {
253
254
  }
254
255
  } else {
255
256
  this.MenuHandle.$data.data.push({
256
- data: menuOption,
257
+ data: options,
257
258
  id: void 0,
258
259
  });
259
260
  }
@@ -262,11 +263,21 @@ export class GMMenu {
262
263
  * 新增菜单数据
263
264
  *
264
265
  * 自动调用.update()
265
- * @param menuOption
266
+ * @param options
266
267
  */
267
- add(menuOption: UtilsGMMenuOption[] | UtilsGMMenuOption) {
268
- this.__add(menuOption);
268
+ add(options: UtilsGMMenuOption[] | UtilsGMMenuOption) {
269
+ this.__add(options);
269
270
  this.update();
271
+ return {
272
+ destory: () => {
273
+ if (!Array.isArray(options)) {
274
+ options = [options];
275
+ }
276
+ options.forEach((option) => {
277
+ this.delete(option.key);
278
+ });
279
+ },
280
+ };
270
281
  }
271
282
  /**
272
283
  * 更新菜单数据
@@ -285,13 +296,13 @@ export class GMMenu {
285
296
  } else if (options != null) {
286
297
  menuOptionList = [...menuOptionList, options];
287
298
  }
288
- menuOptionList.forEach((menuOption) => {
289
- const oldMenuOption = this.MenuHandle.getMenuOption(menuOption.key);
299
+ menuOptionList.forEach((option) => {
300
+ const oldMenuOption = this.MenuHandle.getMenuOption(option.key);
290
301
  if (oldMenuOption) {
291
302
  // 覆盖
292
- Object.assign(oldMenuOption, menuOption);
303
+ Object.assign(oldMenuOption, option);
293
304
  } else {
294
- this.__add(menuOption);
305
+ this.__add(option);
295
306
  }
296
307
  });
297
308
  this.MenuHandle.$data.data.forEach((value) => {
@@ -304,9 +315,15 @@ export class GMMenu {
304
315
  }
305
316
  /**
306
317
  * 卸载菜单
307
- * @param menuId 已注册的菜单id
318
+ * @param menuId 已注册的菜单id或者键名
308
319
  */
309
- delete(menuId: number) {
320
+ delete(menuId: number | string) {
321
+ if (typeof menuId === "string") {
322
+ const __menuId = this.getMenuId(menuId);
323
+ if (__menuId != null) {
324
+ menuId = __menuId;
325
+ }
326
+ }
310
327
  this.GM_Api.unregisterMenuCommand(menuId);
311
328
  }
312
329
  /**
@@ -328,18 +345,20 @@ export class GMMenu {
328
345
  * @param menuKey 菜单-键key
329
346
  */
330
347
  getShowTextValue(menuKey: string): string {
331
- return this.MenuHandle.getMenuHandledOption(menuKey)!.showText(this.getText(menuKey), this.getEnable(menuKey));
348
+ const text = this.getText(menuKey);
349
+ const enable = this.getEnable(menuKey);
350
+ return this.MenuHandle.getMenuHandledOption(menuKey)!.showText(text, enable);
332
351
  }
333
352
  /**
334
353
  * 获取当前已注册菜单的id
335
354
  * @param menuKey
336
355
  */
337
356
  getMenuId(menuKey: string): number | undefined | null {
338
- let result = null;
357
+ let result: number | undefined | null = null;
339
358
  for (let index = 0; index < this.MenuHandle.$data.data.length; index++) {
340
- const optionData = this.MenuHandle.$data.data[index];
341
- if (optionData!.handleData!.key === menuKey) {
342
- result = optionData.id;
359
+ const option = this.MenuHandle.$data.data[index];
360
+ if (option.handleData?.key === menuKey) {
361
+ result = option.id;
343
362
  break;
344
363
  }
345
364
  }