@ibiz-template/core 0.0.1-alpha.1 → 0.0.1-alpha.13

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 (69) hide show
  1. package/out/constant/http-status-message/http-status-message.d.ts +5 -0
  2. package/out/constant/http-status-message/http-status-message.d.ts.map +1 -0
  3. package/out/constant/http-status-message/http-status-message.js +20 -0
  4. package/out/constant/index.d.ts +1 -0
  5. package/out/constant/index.d.ts.map +1 -1
  6. package/out/constant/index.js +1 -0
  7. package/out/context/index.d.ts +10 -2
  8. package/out/context/index.d.ts.map +1 -1
  9. package/out/context/index.js +20 -4
  10. package/out/environment/environment.d.ts.map +1 -1
  11. package/out/environment/environment.js +6 -1
  12. package/out/error/http-error/http-error.d.ts +18 -0
  13. package/out/error/http-error/http-error.d.ts.map +1 -0
  14. package/out/error/http-error/http-error.js +36 -0
  15. package/out/error/index.d.ts +3 -0
  16. package/out/error/index.d.ts.map +1 -0
  17. package/out/error/index.js +2 -0
  18. package/out/error/runtime-error/runtime-error.d.ts +15 -0
  19. package/out/error/runtime-error/runtime-error.d.ts.map +1 -0
  20. package/out/error/runtime-error/runtime-error.js +15 -0
  21. package/out/ibizsys.d.ts +0 -7
  22. package/out/ibizsys.d.ts.map +1 -1
  23. package/out/ibizsys.js +1 -9
  24. package/out/index.d.ts +1 -1
  25. package/out/index.d.ts.map +1 -1
  26. package/out/index.js +1 -1
  27. package/out/install.d.ts.map +1 -1
  28. package/out/install.js +3 -2
  29. package/out/interface/i-environment/i-environment.d.ts +48 -1
  30. package/out/interface/i-environment/i-environment.d.ts.map +1 -1
  31. package/out/types.d.ts +10 -0
  32. package/out/types.d.ts.map +1 -1
  33. package/out/utils/index.d.ts +5 -2
  34. package/out/utils/index.d.ts.map +1 -1
  35. package/out/utils/index.js +4 -1
  36. package/out/utils/namespace/namespace.d.ts +148 -0
  37. package/out/utils/namespace/namespace.d.ts.map +1 -0
  38. package/out/utils/namespace/namespace.js +216 -0
  39. package/out/utils/net/http-response.d.ts +94 -0
  40. package/out/utils/net/http-response.d.ts.map +1 -0
  41. package/out/utils/net/http-response.js +53 -0
  42. package/out/utils/net/net.d.ts +16 -34
  43. package/out/utils/net/net.d.ts.map +1 -1
  44. package/out/utils/net/net.js +57 -28
  45. package/out/utils/plural/plural.d.ts +21 -0
  46. package/out/utils/plural/plural.d.ts.map +1 -0
  47. package/out/utils/plural/plural.js +25 -0
  48. package/out/utils/util/util.d.ts +29 -0
  49. package/out/utils/util/util.d.ts.map +1 -1
  50. package/out/utils/util/util.js +50 -0
  51. package/package.json +19 -9
  52. package/src/constant/http-status-message/http-status-message.ts +20 -0
  53. package/src/constant/index.ts +1 -0
  54. package/src/context/index.ts +24 -5
  55. package/src/environment/environment.ts +6 -1
  56. package/src/error/http-error/http-error.ts +43 -0
  57. package/src/error/index.ts +2 -0
  58. package/src/error/runtime-error/runtime-error.ts +14 -0
  59. package/src/ibizsys.ts +1 -10
  60. package/src/index.ts +1 -2
  61. package/src/install.ts +3 -2
  62. package/src/interface/i-environment/i-environment.ts +54 -2
  63. package/src/types.ts +11 -0
  64. package/src/utils/index.ts +5 -2
  65. package/src/utils/namespace/namespace.ts +244 -0
  66. package/src/utils/net/http-response.ts +113 -0
  67. package/src/utils/net/net.ts +67 -60
  68. package/src/utils/plural/plural.ts +27 -0
  69. package/src/utils/util/util.ts +64 -0
@@ -0,0 +1,244 @@
1
+ export const defaultNamespace = 'ibiz';
2
+ const statePrefix = 'is-';
3
+
4
+ /**
5
+ * css bem 命名规则拼接
6
+ * _bem('ibiz', 'layout') => ibiz-layout
7
+ * _bem('ibiz', 'layout', '', 'title') => ibiz-layout__title
8
+ * _bem('ibiz', 'layout', '', '', 'right') => ibiz-layout--right
9
+ * _bem('ibiz', 'layout', '', 'title', 'right') => ibiz-layout__title--right
10
+ * _bem('ibiz', 'layout', 'header', 'title', 'right') => ibiz-layout-header__title--right
11
+ *
12
+ * @author chitanda
13
+ * @date 2022-09-06 11:09:42
14
+ * @param {string} namespace 命名空间
15
+ * @param {string} block 块
16
+ * @param {string} blockSuffix 块后缀
17
+ * @param {string} element 元素
18
+ * @param {string} modifier 修饰符
19
+ * @return {*} {string}
20
+ */
21
+ function _bem(
22
+ namespace: string,
23
+ block: string,
24
+ blockSuffix?: string,
25
+ element?: string,
26
+ modifier?: string,
27
+ ): string {
28
+ let cls = `${namespace}-${block}`;
29
+ if (blockSuffix) {
30
+ cls += `-${blockSuffix}`;
31
+ }
32
+ if (element) {
33
+ cls += `__${element}`;
34
+ }
35
+ if (modifier) {
36
+ cls += `--${modifier}`;
37
+ }
38
+ return cls;
39
+ }
40
+
41
+ /**
42
+ * 全局样式处理命名空间
43
+ *
44
+ * @author chitanda
45
+ * @date 2022-09-06 11:09:50
46
+ * @export
47
+ * @class Namespace
48
+ */
49
+ export class Namespace {
50
+ /**
51
+ * 命名空间
52
+ *
53
+ * @author chitanda
54
+ * @date 2022-09-06 12:09:01
55
+ * @type {string}
56
+ */
57
+ namespace: string;
58
+
59
+ /**
60
+ * Creates an instance of Namespace.
61
+ *
62
+ * @author chitanda
63
+ * @date 2022-09-06 12:09:12
64
+ * @param {string} block 当前命名空间的根模块,例如组件的名称
65
+ * @param {string} [namespace] 指定命名空间,未指定使用默认值 ibiz
66
+ */
67
+ constructor(protected block: string, namespace?: string) {
68
+ this.namespace = namespace || defaultNamespace;
69
+ }
70
+
71
+ /**
72
+ * namespace-block
73
+ * namespace-block-blockSuffix
74
+ *
75
+ * @author chitanda
76
+ * @date 2022-09-06 12:09:08
77
+ * @param {string} [blockSuffix='']
78
+ * @return {*} {string}
79
+ */
80
+ b(blockSuffix: string = ''): string {
81
+ return _bem(this.namespace, this.block, blockSuffix, '', '');
82
+ }
83
+
84
+ /**
85
+ * namespace-block__element
86
+ *
87
+ * @author chitanda
88
+ * @date 2022-09-06 12:09:48
89
+ * @param {string} [element]
90
+ * @return {*} {string}
91
+ */
92
+ e(element?: string): string {
93
+ return element ? _bem(this.namespace, this.block, '', element, '') : '';
94
+ }
95
+
96
+ /**
97
+ * namespace-block--modifier
98
+ *
99
+ * @author chitanda
100
+ * @date 2022-09-06 12:09:37
101
+ * @param {string} [modifier]
102
+ * @return {*} {string}
103
+ */
104
+ m(modifier?: string): string {
105
+ return modifier ? _bem(this.namespace, this.block, '', '', modifier) : '';
106
+ }
107
+
108
+ /**
109
+ * namespace-block-blockSuffix__element
110
+ *
111
+ * @author chitanda
112
+ * @date 2022-09-06 12:09:52
113
+ * @param {string} [blockSuffix]
114
+ * @param {string} [element]
115
+ * @return {*} {string}
116
+ */
117
+ be(blockSuffix?: string, element?: string): string {
118
+ return blockSuffix && element
119
+ ? _bem(this.namespace, this.block, blockSuffix, element, '')
120
+ : '';
121
+ }
122
+
123
+ /**
124
+ * namespace-block__element--modifier
125
+ *
126
+ * @author chitanda
127
+ * @date 2022-09-06 12:09:19
128
+ * @param {string} [element]
129
+ * @param {string} [modifier]
130
+ * @return {*} {string}
131
+ */
132
+ em(element?: string, modifier?: string): string {
133
+ return element && modifier
134
+ ? _bem(this.namespace, this.block, '', element, modifier)
135
+ : '';
136
+ }
137
+
138
+ /**
139
+ * namespace-block-blockSuffix--modifier
140
+ *
141
+ * @author chitanda
142
+ * @date 2022-09-06 12:09:59
143
+ * @param {string} [blockSuffix]
144
+ * @param {string} [modifier]
145
+ * @return {*} {string}
146
+ */
147
+ bm(blockSuffix?: string, modifier?: string): string {
148
+ return blockSuffix && modifier
149
+ ? _bem(this.namespace, this.block, blockSuffix, '', modifier)
150
+ : '';
151
+ }
152
+
153
+ /**
154
+ * namespace-block-blockSuffix__element--modifier
155
+ *
156
+ * @author chitanda
157
+ * @date 2022-09-06 12:09:37
158
+ * @param {string} [blockSuffix]
159
+ * @param {string} [element]
160
+ * @param {string} [modifier]
161
+ * @return {*} {string}
162
+ */
163
+ bem(blockSuffix?: string, element?: string, modifier?: string): string {
164
+ return blockSuffix && element && modifier
165
+ ? _bem(this.namespace, this.block, blockSuffix, element, modifier)
166
+ : '';
167
+ }
168
+
169
+ /**
170
+ * 返回状态 class
171
+ *
172
+ * is('loading', false) => '';
173
+ * is('loading', true) => 'is-loading';
174
+ *
175
+ * @author chitanda
176
+ * @date 2022-09-06 12:09:57
177
+ * @param {string} name
178
+ * @param {boolean} [state]
179
+ * @return {*} {string}
180
+ */
181
+ is(name: string, state?: boolean): string {
182
+ return name && state ? `${statePrefix}${name}` : '';
183
+ }
184
+
185
+ /**
186
+ * 生成使用到的 css 变量 style 对象
187
+ *
188
+ * @author chitanda
189
+ * @date 2022-09-06 15:09:41
190
+ * @param {Record<string, string>} object
191
+ * @return {*} {Record<string, string>}
192
+ */
193
+ cssVar(object: Record<string, string>): Record<string, string> {
194
+ const styles: Record<string, string> = {};
195
+ for (const key in object) {
196
+ if (object[key]) {
197
+ styles[this.cssVarName(key)] = object[key];
198
+ }
199
+ }
200
+ return styles;
201
+ }
202
+
203
+ /**
204
+ * 生成使用到的 css block 变量 style 对象
205
+ *
206
+ * @author chitanda
207
+ * @date 2022-09-06 15:09:03
208
+ * @param {Record<string, string>} object
209
+ * @return {*} {Record<string, string>}
210
+ */
211
+ cssVarBlock(object: Record<string, string>): Record<string, string> {
212
+ const styles: Record<string, string> = {};
213
+ for (const key in object) {
214
+ if (object[key]) {
215
+ styles[this.cssVarBlockName(key)] = object[key];
216
+ }
217
+ }
218
+ return styles;
219
+ }
220
+
221
+ /**
222
+ * 生成 css var 变量名称
223
+ *
224
+ * @author chitanda
225
+ * @date 2022-09-06 15:09:21
226
+ * @param {string} name
227
+ * @return {*} {string}
228
+ */
229
+ cssVarName(name: string): string {
230
+ return `--${this.namespace}-${name}`;
231
+ }
232
+
233
+ /**
234
+ * 生成块 css var 变量名称
235
+ *
236
+ * @author chitanda
237
+ * @date 2022-09-06 15:09:35
238
+ * @param {string} name
239
+ * @return {*} {string}
240
+ */
241
+ cssVarBlockName(name: string): string {
242
+ return `--${this.namespace}-${this.block}-${name}`;
243
+ }
244
+ }
@@ -0,0 +1,113 @@
1
+ import { AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders } from 'axios';
2
+
3
+ /**
4
+ * Http请求返回接口
5
+ *
6
+ * @author chitanda
7
+ * @date 2022-08-21 17:08:06
8
+ * @export
9
+ * @interface IHttpResponse
10
+ * @extends {AxiosResponse}
11
+ * @template T
12
+ */
13
+ export interface IHttpResponse<T = IData> extends AxiosResponse {
14
+ /**
15
+ * 是否请求成功
16
+ *
17
+ * @description 当状态码为 200-299 时认为成功
18
+ * @author chitanda
19
+ * @date 2022-07-14 16:07:59
20
+ * @type {boolean}
21
+ */
22
+ ok: boolean;
23
+ /**
24
+ * 是否为本地仿造响应
25
+ *
26
+ * @description 只有当值为 true 时, 才认为是本地仿造响应
27
+ * @author chitanda
28
+ * @date 2022-08-18 15:08:44
29
+ * @type {boolean}
30
+ */
31
+ local?: boolean;
32
+
33
+ data: T;
34
+
35
+ /**
36
+ * 当前页
37
+ */
38
+ page?: number;
39
+
40
+ /**
41
+ * 分页条数
42
+ */
43
+ size?: number;
44
+
45
+ /**
46
+ * 总条数
47
+ */
48
+ total?: number;
49
+ }
50
+
51
+ /**
52
+ * 本地请求仿造响应
53
+ *
54
+ * @author chitanda
55
+ * @date 2022-08-21 17:08:00
56
+ * @export
57
+ * @class HttpResponse
58
+ * @implements {IHttpResponse<T>}
59
+ * @template T
60
+ */
61
+ export class HttpResponse<T = IData> implements IHttpResponse<T> {
62
+ /**
63
+ * 本地仿造响应
64
+ *
65
+ * @author chitanda
66
+ * @date 2022-08-18 15:08:06
67
+ */
68
+ local = true;
69
+
70
+ ok = false;
71
+
72
+ data: T;
73
+
74
+ status: number;
75
+
76
+ statusText: string;
77
+
78
+ /**
79
+ * header 本地仿造时值为空
80
+ *
81
+ * @author chitanda
82
+ * @date 2022-08-18 15:08:46
83
+ * @type {AxiosResponseHeaders}
84
+ */
85
+ headers: AxiosResponseHeaders = {};
86
+
87
+ /**
88
+ * AxiosResponse 本地仿造时值为空
89
+ *
90
+ * @author chitanda
91
+ * @date 2022-08-18 15:08:22
92
+ * @type {AxiosRequestConfig<IData>}
93
+ */
94
+ config: AxiosRequestConfig<IData> = {};
95
+
96
+ /**
97
+ * Creates an instance of HttpResponse.
98
+ *
99
+ * @author chitanda
100
+ * @date 2022-08-18 15:08:11
101
+ * @param {unknown} [data] 返回的数据
102
+ * @param {number} [status] 状态码 (默认为 200)
103
+ * @param {string} [statusText] 状态描述 (默认为空字符)
104
+ */
105
+ constructor(data?: unknown, status?: number, statusText?: string) {
106
+ this.data = data as T;
107
+ this.status = status || 200;
108
+ this.statusText = statusText || '';
109
+ if (this.status >= 200 && this.status < 300) {
110
+ this.ok = true;
111
+ }
112
+ }
113
+ }
@@ -1,27 +1,8 @@
1
- import axios, { AxiosRequestHeaders, AxiosResponse } from 'axios';
1
+ import axios, { AxiosError, AxiosRequestHeaders, AxiosResponse } from 'axios';
2
2
  import { stringify } from 'qs';
3
3
  import { notNilEmpty } from 'qx-util';
4
-
5
- /**
6
- * 返回值扩展
7
- *
8
- * @author chitanda
9
- * @date 2022-07-14 16:07:50
10
- * @export
11
- * @interface NetResponse
12
- * @extends {AxiosResponse}
13
- */
14
- export interface NetResponse extends AxiosResponse {
15
- /**
16
- * 是否请求成功
17
- *
18
- * @description 当状态码为 200-299 时认为成功
19
- * @author chitanda
20
- * @date 2022-07-14 16:07:59
21
- * @type {boolean}
22
- */
23
- ok: boolean;
24
- }
4
+ import { HttpError } from '../../error';
5
+ import { IHttpResponse } from './http-response';
25
6
 
26
7
  /**
27
8
  * 全局请求工具类
@@ -40,7 +21,11 @@ export class Net {
40
21
  * @protected
41
22
  * @type {string}
42
23
  */
43
- protected prefix: string = '';
24
+ protected _prefix: string = '';
25
+
26
+ get prefix(): string {
27
+ return this._prefix || ibiz.env.baseUrl;
28
+ }
44
29
 
45
30
  /**
46
31
  * Creates an instance of Net.
@@ -51,7 +36,7 @@ export class Net {
51
36
  */
52
37
  constructor(prefix?: string) {
53
38
  if (prefix) {
54
- this.prefix = prefix;
39
+ this._prefix = prefix;
55
40
  }
56
41
  }
57
42
 
@@ -62,26 +47,30 @@ export class Net {
62
47
  * @date 2022-07-14 15:07:01
63
48
  * @param {string} url
64
49
  * @param {IParams} [params={}]
65
- * @return {*} {Promise<NetResponse>}
50
+ * @return {*} {Promise<IHttpResponse>}
66
51
  */
67
52
  async post(
68
53
  url: string,
69
54
  data: IData,
70
55
  params: IParams = {},
71
56
  headers: AxiosRequestHeaders = {},
72
- ): Promise<NetResponse> {
57
+ ): Promise<IHttpResponse> {
73
58
  url = this.handleAppPresetParam(url, params);
74
- const response = await axios({
75
- method: 'post',
76
- url,
77
- data,
78
- headers: {
79
- 'Content-Type': 'application/json;charset=UTF-8',
80
- Accept: 'application/json',
81
- ...headers,
82
- },
83
- });
84
- return this.doResponseResult(response);
59
+ try {
60
+ const response = await axios({
61
+ method: 'post',
62
+ url,
63
+ data,
64
+ headers: {
65
+ 'Content-Type': 'application/json;charset=UTF-8',
66
+ Accept: 'application/json',
67
+ ...headers,
68
+ },
69
+ });
70
+ return this.doResponseResult(response);
71
+ } catch (error) {
72
+ throw new HttpError(error as AxiosError);
73
+ }
85
74
  }
86
75
 
87
76
  /**
@@ -91,20 +80,25 @@ export class Net {
91
80
  * @date 2022-07-14 15:07:08
92
81
  * @param {string} url
93
82
  * @param {IParams} [params={}]
94
- * @return {*} {Promise<NetResponse>}
83
+ * @return {*} {Promise<IHttpResponse>}
95
84
  */
96
85
  async get(
97
86
  url: string,
98
87
  params: IParams = {},
99
88
  headers: AxiosRequestHeaders = {},
100
- ): Promise<NetResponse> {
89
+ option: IParams = {},
90
+ ): Promise<IHttpResponse> {
101
91
  // if (params.srfparentdata) {
102
92
  // Object.assign(params, params.srfparentdata);
103
93
  // delete params.srfparentdata;
104
94
  // }
105
95
  url = this.attachUrlParam(url, params);
106
- const response = await axios.get(url, { headers });
107
- return this.doResponseResult(response);
96
+ try {
97
+ const response = await axios.get(url, { headers, ...option });
98
+ return this.doResponseResult(response);
99
+ } catch (error) {
100
+ throw new HttpError(error as AxiosError);
101
+ }
108
102
  }
109
103
 
110
104
  /**
@@ -114,16 +108,20 @@ export class Net {
114
108
  * @date 2022-07-14 15:07:43
115
109
  * @param {string} url
116
110
  * @param {IParams} [params]
117
- * @return {*} {Promise<NetResponse>}
111
+ * @return {*} {Promise<IHttpResponse>}
118
112
  */
119
113
  async delete(
120
114
  url: string,
121
115
  params?: IParams,
122
116
  headers: AxiosRequestHeaders = {},
123
- ): Promise<NetResponse> {
117
+ ): Promise<IHttpResponse> {
124
118
  url = this.handleAppPresetParam(url, params);
125
- const response = await axios.delete(url, { headers });
126
- return this.doResponseResult(response);
119
+ try {
120
+ const response = await axios.delete(url, { headers });
121
+ return this.doResponseResult(response);
122
+ } catch (error) {
123
+ throw new HttpError(error as AxiosError);
124
+ }
127
125
  }
128
126
 
129
127
  /**
@@ -132,17 +130,22 @@ export class Net {
132
130
  * @author chitanda
133
131
  * @date 2022-07-14 15:07:49
134
132
  * @param {string} url
135
- * @param {IParams} params
136
- * @return {*} {Promise<NetResponse>}
133
+ * @param {IData} data
134
+ * @return {*} {Promise<IHttpResponse>}
137
135
  */
138
136
  async put(
139
137
  url: string,
140
- params: IParams,
138
+ data: IData,
139
+ params: IParams = {},
141
140
  headers: AxiosRequestHeaders = {},
142
- ): Promise<NetResponse> {
141
+ ): Promise<IHttpResponse> {
143
142
  url = this.handleAppPresetParam(url, params);
144
- const response = await axios.put(url, params, { headers });
145
- return this.doResponseResult(response);
143
+ try {
144
+ const response = await axios.put(url, data, { headers });
145
+ return this.doResponseResult(response);
146
+ } catch (error) {
147
+ throw new HttpError(error as AxiosError);
148
+ }
146
149
  }
147
150
 
148
151
  /**
@@ -152,16 +155,20 @@ export class Net {
152
155
  * @date 2022-07-14 15:07:15
153
156
  * @param {string} url
154
157
  * @param {AxiosRequestHeaders} [headers={}]
155
- * @return {*} {Promise<NetResponse>}
158
+ * @return {*} {Promise<IHttpResponse>}
156
159
  */
157
160
  async getModel(
158
161
  url: string,
159
162
  headers: AxiosRequestHeaders = {},
160
- ): Promise<NetResponse> {
161
- const response = await axios.get(url, {
162
- headers,
163
- });
164
- return this.doResponseResult(response);
163
+ ): Promise<IHttpResponse> {
164
+ try {
165
+ const response = await axios.get(url, {
166
+ headers,
167
+ });
168
+ return this.doResponseResult(response);
169
+ } catch (error) {
170
+ throw new HttpError(error as AxiosError);
171
+ }
165
172
  }
166
173
 
167
174
  /**
@@ -171,10 +178,10 @@ export class Net {
171
178
  * @date 2022-07-14 16:07:23
172
179
  * @private
173
180
  * @param {AxiosResponse} response
174
- * @return {*} {NetResponse}
181
+ * @return {*} {IHttpResponse}
175
182
  */
176
- private doResponseResult(response: AxiosResponse): NetResponse {
177
- const res = response as NetResponse;
183
+ private doResponseResult(response: AxiosResponse): IHttpResponse {
184
+ const res = response as IHttpResponse;
178
185
  if (res.status >= 200 && res.status <= 299) {
179
186
  res.ok = true;
180
187
  }
@@ -0,0 +1,27 @@
1
+ import pluralize from 'pluralize';
2
+
3
+ /**
4
+ * 英文转复数写法
5
+ *
6
+ * @author chitanda
7
+ * @date 2022-08-25 18:08:41
8
+ * @export
9
+ * @param {string} key
10
+ * @return {*} {string}
11
+ */
12
+ export function plural(key: string): string {
13
+ return pluralize(key);
14
+ }
15
+
16
+ /**
17
+ * 英文转复数写法并转全小写
18
+ *
19
+ * @author chitanda
20
+ * @date 2022-08-25 18:08:23
21
+ * @export
22
+ * @param {string} key
23
+ * @return {*} {string}
24
+ */
25
+ export function pluralLower(key: string): string {
26
+ return plural(key).toLowerCase();
27
+ }
@@ -1,3 +1,5 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { debounce, DebounceSettings } from 'lodash-es';
1
3
  import { getCookie } from 'qx-util';
2
4
  import { CoreConst } from '../../constant';
3
5
 
@@ -12,3 +14,65 @@ import { CoreConst } from '../../constant';
12
14
  export function getToken(): string | null {
13
15
  return getCookie(CoreConst.TOKEN);
14
16
  }
17
+
18
+ /**
19
+ * 判断两个数组是否有相同的元素
20
+ *
21
+ * @author lxm
22
+ * @date 2022-09-20 19:09:29
23
+ * @export
24
+ * @param {any[]} arr1
25
+ * @param {any[]} arr2
26
+ * @returns {*} {boolean}
27
+ */
28
+ export function isOverlap(arr1: any[], arr2: any[]): boolean {
29
+ const newArr = Array.from(new Set([...arr1, ...arr2]));
30
+ return newArr.length !== arr1.length + arr2.length;
31
+ }
32
+
33
+ /**
34
+ * 防抖并合并每次的参数
35
+ *
36
+ * @author lxm
37
+ * @date 2022-09-20 21:09:53
38
+ * @export
39
+ * @template T
40
+ * @param {T} func 要防抖的函数
41
+ * @param {(
42
+ * oldParams: Parameters<T>,
43
+ * newParams: Parameters<T>,
44
+ * ) => Parameters<T>} mergeFunc 合并的回调函数
45
+ * @param {number} [wait] 防抖的延迟毫秒数
46
+ * @param {DebounceSettings} [options] 防抖函数的额外参数
47
+ * @returns {*}
48
+ */
49
+ export function debounceAndMerge<T extends (...args: any[]) => any>(
50
+ func: T,
51
+ mergeFunc: (
52
+ oldParams: Parameters<T>,
53
+ newParams: Parameters<T>,
54
+ ) => Parameters<T>,
55
+ wait?: number,
56
+ options?: DebounceSettings,
57
+ ) {
58
+ // 缓存上一次的函数参数
59
+ let oldParams: Parameters<T> | undefined;
60
+ const debounceFunc = debounce(
61
+ (...params: Parameters<T>) => {
62
+ // 防抖函数执行的时候清空缓存参数
63
+ oldParams = undefined;
64
+ return func(...params);
65
+ },
66
+ wait,
67
+ options,
68
+ );
69
+ return (...args: Parameters<T>) => {
70
+ // 合并参数,并把参数缓存到oldParams里,用新参数调用防抖函数
71
+ let newParams = args;
72
+ if (oldParams) {
73
+ newParams = mergeFunc(oldParams, newParams);
74
+ }
75
+ oldParams = newParams;
76
+ return debounceFunc(...newParams);
77
+ };
78
+ }