@whitesev/utils 2.8.1 → 2.9.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 (74) hide show
  1. package/README.md +176 -176
  2. package/dist/index.amd.js +271 -777
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.amd.min.js +2 -0
  5. package/dist/index.amd.min.js.map +1 -0
  6. package/dist/index.cjs.js +271 -777
  7. package/dist/index.cjs.js.map +1 -1
  8. package/dist/index.cjs.min.js +2 -0
  9. package/dist/index.cjs.min.js.map +1 -0
  10. package/dist/index.esm.js +271 -777
  11. package/dist/index.esm.js.map +1 -1
  12. package/dist/index.esm.min.js +2 -0
  13. package/dist/index.esm.min.js.map +1 -0
  14. package/dist/index.iife.js +271 -777
  15. package/dist/index.iife.js.map +1 -1
  16. package/dist/index.iife.min.js +2 -0
  17. package/dist/index.iife.min.js.map +1 -0
  18. package/dist/index.system.js +271 -777
  19. package/dist/index.system.js.map +1 -1
  20. package/dist/index.system.min.js +2 -0
  21. package/dist/index.system.min.js.map +1 -0
  22. package/dist/index.umd.js +271 -777
  23. package/dist/index.umd.js.map +1 -1
  24. package/dist/index.umd.min.js +2 -0
  25. package/dist/index.umd.min.js.map +1 -0
  26. package/dist/types/src/Utils.d.ts +103 -450
  27. package/dist/types/src/UtilsGMCookie.d.ts +4 -0
  28. package/dist/types/src/UtilsGMMenu.d.ts +3 -6
  29. package/dist/types/src/types/Httpx.d.ts +1344 -1344
  30. package/dist/types/src/types/Log.d.ts +19 -19
  31. package/dist/types/src/types/Progress.d.ts +20 -20
  32. package/dist/types/src/types/React.d.ts +119 -119
  33. package/dist/types/src/types/TryCatch.d.ts +9 -9
  34. package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
  35. package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
  36. package/dist/types/src/types/Vue2.d.ts +166 -166
  37. package/dist/types/src/types/WindowApi.d.ts +14 -14
  38. package/dist/types/src/types/ajaxHooker.d.ts +151 -151
  39. package/dist/types/src/types/env.d.ts +7 -7
  40. package/dist/types/src/types/global.d.ts +31 -31
  41. package/index.ts +3 -0
  42. package/package.json +16 -10
  43. package/src/ColorConversion.ts +105 -105
  44. package/src/CommonUtil.ts +280 -280
  45. package/src/DOMUtils.ts +251 -251
  46. package/src/Dictionary.ts +153 -153
  47. package/src/GBKEncoder.ts +108 -108
  48. package/src/Hooks.ts +73 -73
  49. package/src/Httpx.ts +1457 -1457
  50. package/src/LockFunction.ts +62 -62
  51. package/src/Log.ts +258 -258
  52. package/src/Progress.ts +108 -108
  53. package/src/TryCatch.ts +86 -86
  54. package/src/Utils.ts +3778 -4772
  55. package/src/UtilsCommon.ts +14 -14
  56. package/src/UtilsGMCookie.ts +272 -254
  57. package/src/UtilsGMMenu.ts +441 -445
  58. package/src/Vue.ts +233 -233
  59. package/src/WindowApi.ts +59 -59
  60. package/src/indexedDB.ts +497 -497
  61. package/src/types/Httpx.d.ts +1344 -1344
  62. package/src/types/Log.d.ts +19 -19
  63. package/src/types/Progress.d.ts +20 -20
  64. package/src/types/React.d.ts +119 -119
  65. package/src/types/TryCatch.d.ts +9 -9
  66. package/src/types/UtilsGMCookie.d.ts +93 -93
  67. package/src/types/UtilsGMMenu.d.ts +77 -77
  68. package/src/types/Vue2.d.ts +166 -166
  69. package/src/types/WindowApi.d.ts +14 -14
  70. package/src/types/ajaxHooker.d.ts +151 -151
  71. package/src/types/env.d.ts +7 -7
  72. package/src/types/global.d.ts +31 -31
  73. package/dist/types/src/types/Event.d.ts +0 -188
  74. package/src/types/Event.d.ts +0 -188
@@ -1,14 +1,14 @@
1
- /**
2
- * 生成uuid
3
- */
4
- export const GenerateUUID = function () {
5
- if (typeof window?.crypto?.randomUUID === "function") {
6
- return window.crypto.randomUUID();
7
- } else {
8
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
9
- const randomValue = (Math.random() * 16) | 0,
10
- randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
11
- return randomCharValue.toString(16);
12
- });
13
- }
14
- };
1
+ /**
2
+ * 生成uuid
3
+ */
4
+ export const GenerateUUID = function () {
5
+ if (typeof window?.crypto?.randomUUID === "function") {
6
+ return window.crypto.randomUUID();
7
+ } else {
8
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
9
+ const randomValue = (Math.random() * 16) | 0,
10
+ randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
11
+ return randomCharValue.toString(16);
12
+ });
13
+ }
14
+ };
@@ -1,254 +1,272 @@
1
- import { CommonUtil } from "./CommonUtil";
2
- import type {
3
- UtilsGMCookieDeleteOptions,
4
- UtilsGMCookieListOptions,
5
- UtilsGMCookieResult,
6
- UtilsGMCookieSetOptions,
7
- WindowApiOption,
8
- } from "./types/UtilsGMCookie";
9
-
10
- export class UtilsGMCookie {
11
- private windowApi = {
12
- window: window,
13
- document: document,
14
- };
15
- constructor(windowApiOption?: WindowApiOption) {
16
- if (windowApiOption) {
17
- this.windowApi = Object.assign({}, windowApiOption);
18
- }
19
- }
20
- /**
21
- * 获取Cookie分组
22
- */
23
- private getCookiesList() {
24
- if (this.windowApi.document.cookie.trim() === "") {
25
- return [];
26
- }
27
- return this.windowApi.document.cookie.split(";");
28
- }
29
- /**
30
- * 获取单个cookie
31
- * @param cookieName cookie名
32
- */
33
- get(cookieName: string) {
34
- if (typeof cookieName !== "string") {
35
- throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
36
- }
37
- const cookies = this.getCookiesList();
38
- let findValue: UtilsGMCookieResult | undefined = void 0;
39
- for (const cookieItem of cookies) {
40
- const item = cookieItem.trim();
41
- const itemSplit = item.split("=");
42
- const itemName = itemSplit[0];
43
- itemSplit.splice(0, 1);
44
- const itemValue = decodeURIComponent(itemSplit.join(""));
45
- if (itemName === cookieName) {
46
- findValue = {
47
- domain: this.windowApi.window.location.hostname,
48
- expirationDate: null,
49
- hostOnly: true,
50
- httpOnly: false,
51
- name: cookieName,
52
- path: "/",
53
- sameSite: "unspecified",
54
- secure: true,
55
- session: false,
56
- value: itemValue,
57
- };
58
- break;
59
- }
60
- }
61
- return findValue;
62
- }
63
- /**
64
- * 获取多组Cookie
65
- * @param option 配置
66
- * @param callback 获取操作后的回调
67
- * + cookies object[]
68
- * + error string|undefined
69
- **/
70
- list(option: UtilsGMCookieListOptions | object, callback?: (data: UtilsGMCookieResult[], error?: Error) => void) {
71
- if (option == null) {
72
- throw new Error("Utils.GMCookie.list 参数不能为空");
73
- }
74
- const resultData: UtilsGMCookieResult[] = [];
75
- try {
76
- let defaultOption: Required<UtilsGMCookieListOptions> = {
77
- url: this.windowApi.window.location.href,
78
- domain: this.windowApi.window.location.hostname,
79
- name: "",
80
- path: "/",
81
- };
82
- defaultOption = CommonUtil.assign(defaultOption, option);
83
- const cookies = this.getCookiesList();
84
- cookies.forEach((item) => {
85
- item = item.trim();
86
- const itemSplit = item.split("=");
87
- const itemName = itemSplit[0];
88
- itemSplit.splice(0, 1);
89
- const itemValue = decodeURIComponent(itemSplit.join(""));
90
- const nameRegexp =
91
- (defaultOption.name as RegExp) instanceof RegExp
92
- ? defaultOption.name
93
- : new RegExp(`^${defaultOption.name}`, "g");
94
- if (itemName.match(nameRegexp as RegExp)) {
95
- resultData.push({
96
- domain: this.windowApi.window.location.hostname,
97
- expirationDate: null,
98
- hostOnly: true,
99
- httpOnly: false,
100
- name: itemName,
101
- path: "/",
102
- sameSite: "unspecified",
103
- secure: true,
104
- session: false,
105
- value: itemValue,
106
- });
107
- }
108
- });
109
- if (typeof callback === "function") {
110
- callback(resultData);
111
- }
112
- } catch (error) {
113
- if (typeof callback === "function") {
114
- callback(resultData, error as Error);
115
- }
116
- }
117
- }
118
- /**
119
- * 获取多组Cookie
120
- * @param option 配置
121
- **/
122
- getList(option: UtilsGMCookieListOptions | object): UtilsGMCookieResult[] {
123
- if (option == null) {
124
- throw new Error("Utils.GMCookie.list 参数不能为空");
125
- }
126
- const resultData: UtilsGMCookieResult[] = [];
127
- let defaultOption: Required<UtilsGMCookieListOptions> = {
128
- url: this.windowApi.window.location.href,
129
- domain: this.windowApi.window.location.hostname,
130
- name: "",
131
- path: "/",
132
- };
133
- defaultOption = CommonUtil.assign(defaultOption, option);
134
- const cookies = this.getCookiesList();
135
- cookies.forEach((item) => {
136
- item = item.trim();
137
- const itemSplit = item.split("=");
138
- const itemName = itemSplit[0];
139
- itemSplit.splice(0, 1);
140
- const itemValue = decodeURIComponent(itemSplit.join(""));
141
- const nameRegexp =
142
- (defaultOption.name as RegExp) instanceof RegExp
143
- ? defaultOption.name
144
- : new RegExp(`^${defaultOption.name}`, "g");
145
- if (itemName.match(nameRegexp as RegExp)) {
146
- resultData.push({
147
- domain: this.windowApi.window.location.hostname,
148
- expirationDate: null,
149
- hostOnly: true,
150
- httpOnly: false,
151
- name: itemName,
152
- path: "/",
153
- sameSite: "unspecified",
154
- secure: true,
155
- session: false,
156
- value: itemValue,
157
- });
158
- }
159
- });
160
- return resultData;
161
- }
162
- /**
163
- * 设置Cookie
164
- * @param option 配置
165
- * @param callback 设置操作后的回调(成功/失败)
166
- */
167
- set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
168
- let errorInfo;
169
- try {
170
- let defaultOption: Required<UtilsGMCookieSetOptions> = {
171
- url: this.windowApi.window.location.href,
172
- name: "",
173
- value: "",
174
- domain: "",
175
- path: "/",
176
- secure: true,
177
- httpOnly: false,
178
- /**
179
- * Expires in 30 days
180
- */
181
- expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
182
- };
183
- defaultOption = CommonUtil.assign(defaultOption, option);
184
- const life = defaultOption.expirationDate
185
- ? defaultOption.expirationDate
186
- : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
187
- let cookieStr = `${defaultOption.name}=${decodeURIComponent(defaultOption.value as string)};expires=${(
188
- new Date(life) as any
189
- ).toGMTString()}; path=/`;
190
- if (CommonUtil.isNull(defaultOption.domain)) {
191
- cookieStr += `; domain=${defaultOption.domain}`;
192
- }
193
- this.windowApi.document.cookie = cookieStr;
194
- } catch (error: any) {
195
- errorInfo = error;
196
- } finally {
197
- if (typeof callback === "function") {
198
- callback(errorInfo);
199
- }
200
- }
201
- }
202
- /**
203
- * 删除Cookie
204
- * @param option 配置
205
- * @param callback 删除操作后的回调(成功/失败)
206
- */
207
- delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void) {
208
- let errorInfo;
209
- try {
210
- let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
211
- url: this.windowApi.window.location.href,
212
- name: "",
213
- path: "/",
214
- firstPartyDomain: "",
215
- };
216
- defaultOption = CommonUtil.assign(defaultOption, option);
217
- let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
218
- if (CommonUtil.isNull(defaultOption.firstPartyDomain)) {
219
- cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
220
- }
221
- this.windowApi.document.cookie = cookieStr;
222
- } catch (error: any) {
223
- errorInfo = error;
224
- } finally {
225
- if (typeof callback === "function") {
226
- callback(errorInfo);
227
- }
228
- }
229
- }
230
- /**
231
- * 解析cookie字符串
232
- * 例如:document.cookie
233
- * @param cookieStr
234
- */
235
- parseCookie(cookieStr: string) {
236
- if (cookieStr.trim() === "") {
237
- return [];
238
- }
239
- const cookies = cookieStr.split(";");
240
- const result: { key: string; value: string }[] = [];
241
- for (const cookieItem of cookies) {
242
- const item = cookieItem.trim();
243
- const itemSplit = item.split("=");
244
- const itemName = itemSplit[0];
245
- itemSplit.splice(0, 1);
246
- const itemValue = decodeURIComponent(itemSplit.join(""));
247
- result.push({
248
- key: itemName,
249
- value: itemValue,
250
- });
251
- }
252
- return result;
253
- }
254
- }
1
+ import { CommonUtil } from "./CommonUtil";
2
+ import type {
3
+ UtilsGMCookieDeleteOptions,
4
+ UtilsGMCookieListOptions,
5
+ UtilsGMCookieResult,
6
+ UtilsGMCookieSetOptions,
7
+ WindowApiOption,
8
+ } from "./types/UtilsGMCookie";
9
+
10
+ export class UtilsGMCookie {
11
+ private windowApi = {
12
+ window: window,
13
+ document: document,
14
+ };
15
+ constructor(windowApiOption?: WindowApiOption) {
16
+ if (windowApiOption) {
17
+ this.windowApi = Object.assign({}, windowApiOption);
18
+ }
19
+ }
20
+ /**
21
+ * 获取Cookie分组
22
+ */
23
+ private getCookiesList() {
24
+ if (this.windowApi.document.cookie.trim() === "") {
25
+ return [];
26
+ }
27
+ return this.windowApi.document.cookie.split(";");
28
+ }
29
+ /**
30
+ * 获取单个cookie
31
+ * @param cookieName cookie名
32
+ */
33
+ get(cookieName: string) {
34
+ if (typeof cookieName !== "string") {
35
+ throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
36
+ }
37
+ const cookies = this.getCookiesList();
38
+ let findValue: UtilsGMCookieResult | undefined = void 0;
39
+ for (const cookieItem of cookies) {
40
+ const item = cookieItem.trim();
41
+ const itemSplit = item.split("=");
42
+ const itemName = itemSplit[0];
43
+ itemSplit.splice(0, 1);
44
+ const itemValue = decodeURIComponent(itemSplit.join(""));
45
+ if (itemName === cookieName) {
46
+ findValue = {
47
+ domain: this.windowApi.window.location.hostname,
48
+ expirationDate: null,
49
+ hostOnly: true,
50
+ httpOnly: false,
51
+ name: cookieName,
52
+ path: "/",
53
+ sameSite: "unspecified",
54
+ secure: true,
55
+ session: false,
56
+ value: itemValue,
57
+ };
58
+ break;
59
+ }
60
+ }
61
+ return findValue;
62
+ }
63
+ /**
64
+ * 获取多组Cookie
65
+ * @param option 配置
66
+ * @param callback 获取操作后的回调
67
+ * + cookies object[]
68
+ * + error string|undefined
69
+ **/
70
+ list(option: UtilsGMCookieListOptions | object): {
71
+ cookies: UtilsGMCookieResult[];
72
+ error?: Error;
73
+ };
74
+ list(
75
+ option: UtilsGMCookieListOptions | object,
76
+ callback?: (data: UtilsGMCookieResult[], error?: Error) => void
77
+ ): void;
78
+ list(
79
+ option: UtilsGMCookieListOptions | object,
80
+ callback?: (data: UtilsGMCookieResult[], error?: Error) => void
81
+ ): {
82
+ cookies: UtilsGMCookieResult[];
83
+ error?: Error;
84
+ } | void {
85
+ if (option == null) {
86
+ throw new Error("Utils.GMCookie.list 参数不能为空");
87
+ }
88
+ const resultData: UtilsGMCookieResult[] = [];
89
+ let error;
90
+ try {
91
+ let defaultOption: Required<UtilsGMCookieListOptions> = {
92
+ url: this.windowApi.window.location.href,
93
+ domain: this.windowApi.window.location.hostname,
94
+ name: "",
95
+ path: "/",
96
+ };
97
+ defaultOption = CommonUtil.assign(defaultOption, option);
98
+ const cookies = this.getCookiesList();
99
+ cookies.forEach((item) => {
100
+ item = item.trim();
101
+ const itemSplit = item.split("=");
102
+ const itemName = itemSplit[0];
103
+ itemSplit.splice(0, 1);
104
+ const itemValue = decodeURIComponent(itemSplit.join(""));
105
+ const nameRegexp =
106
+ (defaultOption.name as RegExp) instanceof RegExp
107
+ ? defaultOption.name
108
+ : new RegExp(`^${defaultOption.name}`, "g");
109
+ if (itemName.match(nameRegexp as RegExp)) {
110
+ resultData.push({
111
+ domain: this.windowApi.window.location.hostname,
112
+ expirationDate: null,
113
+ hostOnly: true,
114
+ httpOnly: false,
115
+ name: itemName,
116
+ path: "/",
117
+ sameSite: "unspecified",
118
+ secure: true,
119
+ session: false,
120
+ value: itemValue,
121
+ });
122
+ }
123
+ });
124
+ } catch (e) {
125
+ error = e;
126
+ }
127
+ if (typeof callback === "function") {
128
+ callback(resultData, error as Error);
129
+ } else {
130
+ return {
131
+ cookies: resultData,
132
+ error: error as Error,
133
+ };
134
+ }
135
+ }
136
+ /**
137
+ * 获取多组Cookie
138
+ * @param option 配置
139
+ **/
140
+ getList(option: UtilsGMCookieListOptions | object): UtilsGMCookieResult[] {
141
+ if (option == null) {
142
+ throw new Error("Utils.GMCookie.list 参数不能为空");
143
+ }
144
+ const resultData: UtilsGMCookieResult[] = [];
145
+ let defaultOption: Required<UtilsGMCookieListOptions> = {
146
+ url: this.windowApi.window.location.href,
147
+ domain: this.windowApi.window.location.hostname,
148
+ name: "",
149
+ path: "/",
150
+ };
151
+ defaultOption = CommonUtil.assign(defaultOption, option);
152
+ const cookies = this.getCookiesList();
153
+ cookies.forEach((item) => {
154
+ item = item.trim();
155
+ const itemSplit = item.split("=");
156
+ const itemName = itemSplit[0];
157
+ itemSplit.splice(0, 1);
158
+ const itemValue = decodeURIComponent(itemSplit.join(""));
159
+ const nameRegexp =
160
+ (defaultOption.name as RegExp) instanceof RegExp
161
+ ? defaultOption.name
162
+ : new RegExp(`^${defaultOption.name}`, "g");
163
+ if (itemName.match(nameRegexp as RegExp)) {
164
+ resultData.push({
165
+ domain: this.windowApi.window.location.hostname,
166
+ expirationDate: null,
167
+ hostOnly: true,
168
+ httpOnly: false,
169
+ name: itemName,
170
+ path: "/",
171
+ sameSite: "unspecified",
172
+ secure: true,
173
+ session: false,
174
+ value: itemValue,
175
+ });
176
+ }
177
+ });
178
+ return resultData;
179
+ }
180
+ /**
181
+ * 设置Cookie
182
+ * @param option 配置
183
+ * @param callback 设置操作后的回调(成功/失败)
184
+ */
185
+ set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
186
+ let errorInfo;
187
+ try {
188
+ let defaultOption: Required<UtilsGMCookieSetOptions> = {
189
+ url: this.windowApi.window.location.href,
190
+ name: "",
191
+ value: "",
192
+ domain: "",
193
+ path: "/",
194
+ secure: true,
195
+ httpOnly: false,
196
+ /**
197
+ * Expires in 30 days
198
+ */
199
+ expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
200
+ };
201
+ defaultOption = CommonUtil.assign(defaultOption, option);
202
+ const life = defaultOption.expirationDate
203
+ ? defaultOption.expirationDate
204
+ : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
205
+ let cookieStr = `${defaultOption.name}=${decodeURIComponent(defaultOption.value as string)};expires=${(
206
+ new Date(life) as any
207
+ ).toGMTString()}; path=/`;
208
+ if (CommonUtil.isNull(defaultOption.domain)) {
209
+ cookieStr += `; domain=${defaultOption.domain}`;
210
+ }
211
+ this.windowApi.document.cookie = cookieStr;
212
+ } catch (error: any) {
213
+ errorInfo = error;
214
+ } finally {
215
+ if (typeof callback === "function") {
216
+ callback(errorInfo);
217
+ }
218
+ }
219
+ }
220
+ /**
221
+ * 删除Cookie
222
+ * @param option 配置
223
+ * @param callback 删除操作后的回调(成功/失败)
224
+ */
225
+ delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void) {
226
+ let errorInfo;
227
+ try {
228
+ let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
229
+ url: this.windowApi.window.location.href,
230
+ name: "",
231
+ path: "/",
232
+ firstPartyDomain: "",
233
+ };
234
+ defaultOption = CommonUtil.assign(defaultOption, option);
235
+ let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
236
+ if (CommonUtil.isNull(defaultOption.firstPartyDomain)) {
237
+ cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
238
+ }
239
+ this.windowApi.document.cookie = cookieStr;
240
+ } catch (error: any) {
241
+ errorInfo = error;
242
+ } finally {
243
+ if (typeof callback === "function") {
244
+ callback(errorInfo);
245
+ }
246
+ }
247
+ }
248
+ /**
249
+ * 解析cookie字符串
250
+ * 例如:document.cookie
251
+ * @param cookieStr
252
+ */
253
+ parseCookie(cookieStr: string) {
254
+ if (cookieStr.trim() === "") {
255
+ return [];
256
+ }
257
+ const cookies = cookieStr.split(";");
258
+ const result: { key: string; value: string }[] = [];
259
+ for (const cookieItem of cookies) {
260
+ const item = cookieItem.trim();
261
+ const itemSplit = item.split("=");
262
+ const itemName = itemSplit[0];
263
+ itemSplit.splice(0, 1);
264
+ const itemValue = decodeURIComponent(itemSplit.join(""));
265
+ result.push({
266
+ key: itemName,
267
+ value: itemValue,
268
+ });
269
+ }
270
+ return result;
271
+ }
272
+ }