@whitesev/utils 1.0.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.
Files changed (55) hide show
  1. package/README.md +172 -0
  2. package/dist/index.cjs.js +6017 -0
  3. package/dist/index.cjs.js.map +1 -0
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.esm.js +6015 -0
  6. package/dist/index.esm.js.map +1 -0
  7. package/dist/index.umd.js +6023 -0
  8. package/dist/index.umd.js.map +1 -0
  9. package/dist/src/ColorConversion.d.ts +45 -0
  10. package/dist/src/Dictionary.d.ts +87 -0
  11. package/dist/src/GBKEncoder.d.ts +17 -0
  12. package/dist/src/Hooks.d.ts +5 -0
  13. package/dist/src/Httpx.d.ts +50 -0
  14. package/dist/src/LockFunction.d.ts +16 -0
  15. package/dist/src/Log.d.ts +66 -0
  16. package/dist/src/Progress.d.ts +6 -0
  17. package/dist/src/Utils.d.ts +1560 -0
  18. package/dist/src/UtilsCore.d.ts +9 -0
  19. package/dist/src/UtilsGMCookie.d.ts +36 -0
  20. package/dist/src/UtilsGMMenu.d.ts +119 -0
  21. package/dist/src/ajaxHooker.d.ts +6 -0
  22. package/dist/src/indexedDB.d.ts +165 -0
  23. package/dist/src/tryCatch.d.ts +31 -0
  24. package/index.ts +3 -0
  25. package/package.json +34 -0
  26. package/rollup.config.js +28 -0
  27. package/src/ColorConversion.ts +124 -0
  28. package/src/Dictionary.ts +158 -0
  29. package/src/GBKEncoder.js +111 -0
  30. package/src/GBKEncoder.ts +116 -0
  31. package/src/Hooks.js +73 -0
  32. package/src/Httpx.js +747 -0
  33. package/src/LockFunction.js +35 -0
  34. package/src/Log.js +256 -0
  35. package/src/Progress.js +98 -0
  36. package/src/Utils.ts +4495 -0
  37. package/src/UtilsCore.ts +39 -0
  38. package/src/UtilsGMCookie.ts +167 -0
  39. package/src/UtilsGMMenu.js +464 -0
  40. package/src/ajaxHooker.js +560 -0
  41. package/src/indexedDB.js +355 -0
  42. package/src/tryCatch.js +100 -0
  43. package/src/types/AjaxHooker.d.ts +153 -0
  44. package/src/types/DOMUtils.d.ts +188 -0
  45. package/src/types/Hook.d.ts +16 -0
  46. package/src/types/Httpx.d.ts +1308 -0
  47. package/src/types/Indexdb.d.ts +128 -0
  48. package/src/types/LockFunction.d.ts +47 -0
  49. package/src/types/Log.d.ts +91 -0
  50. package/src/types/Progress.d.ts +30 -0
  51. package/src/types/TryCatch.d.ts +6 -0
  52. package/src/types/UtilsCore.d.ts +7 -0
  53. package/src/types/UtilsGMMenu.d.ts +224 -0
  54. package/src/types/global.d.ts +58 -0
  55. package/tsconfig.json +32 -0
@@ -0,0 +1,9 @@
1
+ declare const UtilsCore: {
2
+ init(option?: UtilsCoreOption): void;
3
+ readonly document: Document;
4
+ readonly window: Window;
5
+ readonly globalThis: typeof globalThis;
6
+ readonly self: typeof globalThis;
7
+ readonly top: Window;
8
+ };
9
+ export { UtilsCore };
@@ -0,0 +1,36 @@
1
+ declare interface UtilsGMCookieListResult {
2
+ /** 为 window.location.hostname */
3
+ domain: string;
4
+ expirationDate: null;
5
+ hostOnly: true;
6
+ httpOnly: false;
7
+ name: string;
8
+ path: "/";
9
+ sameSite: "unspecified";
10
+ secure: true;
11
+ session: false;
12
+ value: string;
13
+ }
14
+ declare class UtilsGMCookie {
15
+ /**
16
+ * 获取Cookie
17
+ * @param paramDetails
18
+ * @param callback
19
+ * + cookies object[]
20
+ * + error string|undefined
21
+ **/
22
+ list(paramDetails?: Partial<UtilsGMCookieListResult>, callback?: (resultData: UtilsGMCookieListResult[], error?: Error) => void): void;
23
+ /**
24
+ * 设置Cookie
25
+ * @param paramDetails
26
+ * @param callback
27
+ */
28
+ set(paramDetails?: {}, callback?: (error?: Error) => void): void;
29
+ /**
30
+ * 删除Cookie
31
+ * @param paramDetails
32
+ * @param callback
33
+ */
34
+ delete(paramDetails?: {}, callback?: (error?: Error) => void): void;
35
+ }
36
+ export { UtilsGMCookie };
@@ -0,0 +1,119 @@
1
+ /**
2
+ *
3
+ * @param {UtilsGMMenuConstructorOptions} details
4
+ */
5
+ export function GMMenu(details: UtilsGMMenuConstructorOptions): void;
6
+ export class GMMenu {
7
+ constructor(details: UtilsGMMenuConstructorOptions);
8
+ /**
9
+ * 新增菜单数据
10
+ * @param {UtilsGMMenuOption[]|UtilsGMMenuOption} paramData
11
+ */
12
+ add: (paramData: UtilsGMMenuOption[] | UtilsGMMenuOption) => void;
13
+ /**
14
+ * 更新菜单数据
15
+ * @param { ?UtilsGMMenuOption[]|UtilsGMMenuOption } options 数据
16
+ */
17
+ update: (options: (UtilsGMMenuOption[] | UtilsGMMenuOption) | null) => void;
18
+ /**
19
+ * 卸载菜单
20
+ * @param {number} menuId 已注册的菜单id
21
+ */
22
+ delete: (menuId: number) => void;
23
+ /**
24
+ * 根据键值获取enable值
25
+ * @param {string} menuKey 菜单-键key
26
+ * @returns {boolean}
27
+ */
28
+ get: (menuKey: string) => boolean;
29
+ /**
30
+ * 根据键值获取enable值
31
+ * @param {string} menuKey 菜单-键key
32
+ * @returns {boolean}
33
+ */
34
+ getEnable: (menuKey: string) => boolean;
35
+ /**
36
+ * 根据键值获取text值
37
+ * @param {string} menuKey 菜单-键key
38
+ * @returns {string}
39
+ */
40
+ getText: (menuKey: string) => string;
41
+ /**
42
+ * 根据键值获取showText函数的值
43
+ * @param {string} menuKey 菜单-键key
44
+ * @returns {string}
45
+ */
46
+ getShowTextValue: (menuKey: string) => string;
47
+ /**
48
+ * 获取当前已注册菜单的id
49
+ * @param {string} menuKey
50
+ * @returns {?number}
51
+ */
52
+ getMenuId: (menuKey: string) => number | null;
53
+ /**
54
+ * 根据键值获取accessKey值
55
+ * @param {string} menuKey 菜单-键key
56
+ * @returns {?string}
57
+ */
58
+ getAccessKey: (menuKey: string) => string | null;
59
+ /**
60
+ * 根据键值获取autoClose值
61
+ * @param {string} menuKey 菜单-键key
62
+ * @returns {?boolean}
63
+ */
64
+ getAutoClose: (menuKey: string) => boolean | null;
65
+ /**
66
+ * 根据键值获取autoReload值
67
+ * @param {string} menuKey 菜单-键key
68
+ * @returns {boolean}
69
+ */
70
+ getAutoReload: (menuKey: string) => boolean;
71
+ /**
72
+ * 根据键值获取callback函数
73
+ * @param {string} menuKey 菜单-键key
74
+ * @returns {?Function}
75
+ */
76
+ getCallBack: (menuKey: string) => Function | null;
77
+ /**
78
+ * 获取当enable为true时默认显示在菜单中前面的emoji图标
79
+ * @returns {string}
80
+ */
81
+ getEnableTrueEmoji: () => string;
82
+ /**
83
+ * 获取当enable为false时默认显示在菜单中前面的emoji图标
84
+ * @returns {string}
85
+ */
86
+ getEnableFalseEmoji: () => string;
87
+ /**
88
+ * 获取本地存储的菜单外部的键名
89
+ * @param {string} keyName
90
+ */
91
+ getLocalStorageKeyName: () => string;
92
+ /**
93
+ * 设置菜单的值
94
+ * @param {string} menuKey 菜单-键key
95
+ * @param {any} value 需要设置的值
96
+ */
97
+ setValue: (menuKey: string, value: any) => void;
98
+ /**
99
+ * 设置菜单的值
100
+ * @param {string} menuKey 菜单-键key
101
+ * @param {boolean} value 需要设置的值
102
+ */
103
+ setEnable: (menuKey: string, value: boolean) => void;
104
+ /**
105
+ * 设置当enable为true时默认显示在菜单中前面的emoji图标
106
+ * @param {string} emojiString
107
+ */
108
+ setEnableTrueEmoji: (emojiString: string) => void;
109
+ /**
110
+ * 设置当enable为false时默认显示在菜单中前面的emoji图标
111
+ * @param {string} emojiString
112
+ */
113
+ setEnableFalseEmoji: (emojiString: string) => void;
114
+ /**
115
+ * 设置本地存储的菜单外部的键名
116
+ * @param {string} keyName
117
+ */
118
+ setLocalStorageKeyName: (keyName: string) => void;
119
+ }
@@ -0,0 +1,6 @@
1
+ export namespace ajaxHooker {
2
+ function hook(fn: any): number;
3
+ function filter(arr: any): void;
4
+ function protect(): void;
5
+ function unhook(): void;
6
+ }
@@ -0,0 +1,165 @@
1
+ export function indexedDB(dbName?: string, storeName?: string, dbVersion?: number): void;
2
+ export class indexedDB {
3
+ constructor(dbName?: string, storeName?: string, dbVersion?: number);
4
+ dbName: string;
5
+ slqVersion: string;
6
+ dbVersion: number;
7
+ storeName: string;
8
+ indexedDB: IDBFactory;
9
+ db: {};
10
+ store: any;
11
+ errorCode: {
12
+ success: {
13
+ code: number;
14
+ msg: string;
15
+ };
16
+ error: {
17
+ code: number;
18
+ msg: string;
19
+ };
20
+ open: {
21
+ code: number;
22
+ msg: string;
23
+ };
24
+ save: {
25
+ code: number;
26
+ msg: string;
27
+ };
28
+ get: {
29
+ code: number;
30
+ msg: string;
31
+ };
32
+ delete: {
33
+ code: number;
34
+ msg: string;
35
+ };
36
+ deleteAll: {
37
+ code: number;
38
+ msg: string;
39
+ };
40
+ };
41
+ /**
42
+ * 创建 “表”
43
+ * @param {string} dbName 表名
44
+ * @returns
45
+ */
46
+ createStore: (dbName: string) => any;
47
+ /**
48
+ * 打开数据库
49
+ * @param {function} callback 回调
50
+ * @param {string} dbName 数据库名
51
+ */
52
+ open: (callback: Function, dbName: string) => void;
53
+ /**
54
+ * 保存数据到数据库
55
+ * @param {string} key 数据key
56
+ * @param {any} value 数据值
57
+ * @returns {Promise< {
58
+ * code: number,
59
+ * msg: string,
60
+ * success: boolean
61
+ * }>}
62
+ */
63
+ save: (key: string, value: any) => Promise<{
64
+ code: number;
65
+ msg: string;
66
+ success: boolean;
67
+ }>;
68
+ /**
69
+ * 根据key获取值
70
+ * @param {string} key 数据key
71
+ * @returns {Promise< {
72
+ * code: number,
73
+ * msg: string,
74
+ * data: [...any],
75
+ * success: true
76
+ * }| {
77
+ * code: number,
78
+ * msg: string,
79
+ * error: Error,
80
+ * result: any,
81
+ * } >}
82
+ */
83
+ get: (key: string) => Promise<{
84
+ code: number;
85
+ msg: string;
86
+ data: [...any];
87
+ success: true;
88
+ } | {
89
+ code: number;
90
+ msg: string;
91
+ error: Error;
92
+ result: any;
93
+ }>;
94
+ /**
95
+ * 正则获取数据
96
+ * @param {string} key 数据键
97
+ * @returns { Promise<{
98
+ * code: number,
99
+ * msg: string,
100
+ * data: [...any],
101
+ * success: true
102
+ * }|{
103
+ * code: number,
104
+ * msg: string,
105
+ * error: Error,
106
+ * result: any,
107
+ * }> }
108
+ */
109
+ regexpGet: (key: string) => Promise<{
110
+ code: number;
111
+ msg: string;
112
+ data: [...any];
113
+ success: true;
114
+ } | {
115
+ code: number;
116
+ msg: string;
117
+ error: Error;
118
+ result: any;
119
+ }>;
120
+ /**
121
+ * 删除数据
122
+ * @param {string} key 数据键
123
+ * @returns {Promise<{
124
+ * code: number,
125
+ * msg: string,
126
+ * success: true,
127
+ * }|{
128
+ * code: number,
129
+ * msg: string,
130
+ * error: Error,
131
+ * }>}
132
+ */
133
+ delete: (key: string) => Promise<{
134
+ code: number;
135
+ msg: string;
136
+ success: true;
137
+ } | {
138
+ code: number;
139
+ msg: string;
140
+ error: Error;
141
+ }>;
142
+ /**
143
+ * 删除所有数据
144
+ * @returns {Promise<{
145
+ * code: number,
146
+ * msg: string,
147
+ * error: Error,
148
+ * result: any,
149
+ * }|{
150
+ * code: number,
151
+ * msg: string,
152
+ * success: true,
153
+ * }>}
154
+ */
155
+ deleteAll: () => Promise<{
156
+ code: number;
157
+ msg: string;
158
+ error: Error;
159
+ result: any;
160
+ } | {
161
+ code: number;
162
+ msg: string;
163
+ success: true;
164
+ }>;
165
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ *
3
+ * @param {...any} args
4
+ * @returns
5
+ */
6
+ export function TryCatch(...args: any[]): {
7
+ (): void;
8
+ /**
9
+ * 配置
10
+ * @param {{
11
+ * log: boolean
12
+ * }} paramDetails
13
+ */
14
+ config(paramDetails: {
15
+ log: boolean;
16
+ }): any;
17
+ /**
18
+ * 设置错误处理函数。
19
+ * @param {function|string} handler 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
20
+ * @returns 返回 tryCatchObj 函数。
21
+ */
22
+ error(handler: Function | string): any;
23
+ /**
24
+ * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
25
+ * @param {function|string} callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
26
+ * @param {object|null} [__context__] 待执行函数的作用域,用于apply指定
27
+ * @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
28
+ * @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
29
+ */
30
+ run(callback: Function | string, __context__?: object | null | undefined): any;
31
+ };
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Utils } from "./src/Utils";
2
+
3
+ export default Utils;
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@whitesev/utils",
3
+ "version": "1.0.0",
4
+ "description": "一个常用的工具库",
5
+ "main": "dist/index.cjs.js",
6
+ "module": "dist/node/index.esm.js",
7
+ "types": "dist/index.d.ts",
8
+ "jsdelivr": "dist/index.umd.js",
9
+ "exports": {
10
+ "./package.json": "./package.json",
11
+ ".": {
12
+ "import": "./dist/index.esm.js",
13
+ "require": "./dist/index.cjs.js",
14
+ "types": "./dist/index.d.ts"
15
+ }
16
+ },
17
+ "scripts": {
18
+ "build": "rollup --config"
19
+ },
20
+ "keywords": [
21
+ "typescript"
22
+ ],
23
+ "author": "WhiteSev",
24
+ "license": "MIT",
25
+ "dependencies": {
26
+ "@rollup/plugin-commonjs": "^25.0.8",
27
+ "@rollup/plugin-node-resolve": "^15.2.3",
28
+ "@rollup/plugin-typescript": "^11.1.6",
29
+ "tslib": "^2.6.2"
30
+ },
31
+ "devDependencies": {
32
+ "typescript": "^5.4.5"
33
+ }
34
+ }
@@ -0,0 +1,28 @@
1
+ // 允许使用 node 或 umd 包
2
+ const commonjs = require("@rollup/plugin-commonjs");
3
+ const { nodeResolve } = require("@rollup/plugin-node-resolve");
4
+ // 编译 TS 代码
5
+ const typescript = require("@rollup/plugin-typescript");
6
+
7
+ module.exports = {
8
+ plugins: [nodeResolve(), commonjs(), typescript()],
9
+ input: "./index.ts", // 源文件入口
10
+ output: [
11
+ {
12
+ file: "dist/index.esm.js", // package.json 中 "module": "dist/index.esm.js"
13
+ format: "esm", // es module 形式的包, 用来import 导入, 可以tree shaking
14
+ sourcemap: true,
15
+ },
16
+ {
17
+ file: "dist/index.cjs.js", // package.json 中 "main": "dist/index.cjs.js",
18
+ format: "cjs", // commonjs 形式的包, require 导入
19
+ sourcemap: true,
20
+ },
21
+ {
22
+ file: "dist/index.umd.js",
23
+ name: "Utils", // 模块名
24
+ format: "umd", // umd 兼容形式的包, 可以直接应用于网页 script
25
+ sourcemap: true,
26
+ },
27
+ ],
28
+ };
@@ -0,0 +1,124 @@
1
+ class ColorConversion {
2
+ constructor() {}
3
+ /**
4
+ * 判断是否是16进制颜色
5
+ * @param str
6
+ */
7
+ isHex(str: string) {
8
+ if (typeof str !== "string") {
9
+ return false;
10
+ }
11
+ if (!str.match(/^(\#|)[0-9a-fA-F]{6}$/)) {
12
+ return false;
13
+ }
14
+ return true;
15
+ }
16
+ /**
17
+ * 16进制颜色转rgba
18
+ *
19
+ * #ff0000 转 rgba(123,123,123, 0.4)
20
+ * @param hex
21
+ * @param opacity
22
+ */
23
+ hexToRgba(hex: string, opacity: number) {
24
+ if (!this.isHex(hex)) {
25
+ throw new TypeError("输入错误的hex" + hex);
26
+ }
27
+ return hex && hex.replace(/\s+/g, "").length === 7
28
+ ? "rgba(" +
29
+ parseInt("0x" + hex.slice(1, 3)) +
30
+ "," +
31
+ parseInt("0x" + hex.slice(3, 5)) +
32
+ "," +
33
+ parseInt("0x" + hex.slice(5, 7)) +
34
+ "," +
35
+ opacity +
36
+ ")"
37
+ : "";
38
+ }
39
+ /**
40
+ * hex转rgb
41
+ * @param str
42
+ * @returns
43
+ */
44
+ hexToRgb(str: string) {
45
+ if (!this.isHex(str)) {
46
+ throw new TypeError("输入错误的hex" + str);
47
+ }
48
+ /* replace替换查找的到的字符串 */
49
+ str = str.replace("#", "");
50
+ /* match得到查询数组 */
51
+ let hxs = str.match(/../g);
52
+ if (hxs == null) {
53
+ throw new TypeError("输入错误的hex" + str);
54
+ }
55
+ for (let index = 0; index < 3; index++)
56
+ hxs[index] = parseInt(hxs[index], 16).toString();
57
+ return hxs;
58
+ }
59
+ /**
60
+ * rgb转hex
61
+ * @param redValue
62
+ * @param greenValue
63
+ * @param blueValue
64
+ * @returns
65
+ */
66
+ rgbToHex(
67
+ redValue: string | number,
68
+ greenValue: string | number,
69
+ blueValue: string | number
70
+ ) {
71
+ /* 验证输入的rgb值是否合法 */
72
+ let validPattern = /^\d{1,3}$/;
73
+ if (
74
+ !validPattern.test(redValue.toString()) ||
75
+ !validPattern.test(greenValue.toString()) ||
76
+ !validPattern.test(blueValue.toString())
77
+ )
78
+ throw new TypeError("输入错误的rgb颜色值");
79
+ let hexs = [
80
+ redValue.toString(16),
81
+ greenValue.toString(16),
82
+ blueValue.toString(16),
83
+ ];
84
+ for (let index = 0; index < 3; index++)
85
+ if (hexs[index].length == 1) hexs[index] = "0" + hexs[index];
86
+ return "#" + hexs.join("");
87
+ }
88
+ /**
89
+ * 获取颜色变暗或亮
90
+ * @param color 颜色
91
+ * @param level 0~1.0
92
+ * @returns
93
+ */
94
+ getDarkColor(color: string, level: number) {
95
+ if (!this.isHex(color)) {
96
+ throw new TypeError("输入错误的hex" + color);
97
+ }
98
+ let rgbc = this.hexToRgb(color);
99
+ for (let index = 0; index < 3; index++)
100
+ // @ts-ignore
101
+ rgbc[index] = Math.floor(rgbc[index] * (1 - level)).toString();
102
+ return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
103
+ }
104
+ /**
105
+ * 获取颜色变亮
106
+ * @param color 颜色
107
+ * @param level 0~1.0
108
+ * @returns
109
+ */
110
+ getLightColor(color: string, level: number) {
111
+ if (!this.isHex(color)) {
112
+ throw new TypeError("输入错误的hex" + color);
113
+ }
114
+ let rgbc = this.hexToRgb(color);
115
+ for (let index = 0; index < 3; index++)
116
+ rgbc[index] = Math.floor(
117
+ // @ts-ignore
118
+ (255 - rgbc[index]) * level + rgbc[index]
119
+ ).toString();
120
+ return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
121
+ }
122
+ }
123
+
124
+ export { ColorConversion };