nhanh-pure-function 1.3.21 → 1.4.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.
@@ -6,109 +6,6 @@ export function _ScrollEndListener(
6
6
  callback: (trigger: "vertical" | "horizontal") => void
7
7
  ): (payload: Event) => void;
8
8
 
9
- /**
10
- * 设置量词属性
11
- * @param {*} data 需修改对象
12
- * @param {*} options 配置
13
- * @returns data
14
- */
15
- export function _SetQuantifierAttribute<T>(
16
- data: T,
17
- options?: (
18
- | keyof T
19
- | [
20
- keyof T,
21
- {
22
- join?: boolean;
23
- suffix?: string;
24
- integer?: boolean;
25
- }
26
- ]
27
- )[]
28
- ): T;
29
-
30
- /**
31
- * 为属性值为null | undefined的属性设置默认值
32
- * @param data 需修改对象
33
- * @param options 配置
34
- * @returns
35
- */
36
- export function _SetDefaultValue<T>(
37
- data: T,
38
- options?: {
39
- defaultValue?: string;
40
- fieldsNotRequiringAction?: (string | number | symbol)[];
41
- }
42
- ): T;
43
-
44
- /**
45
- * 将字典value转为对应label
46
- * @param data 需修改对象
47
- * @param options 配置
48
- * @returns
49
- */
50
- export function _SetDictionary<T>(
51
- data: T,
52
- options: {
53
- dictionaryOptions?: { [key in keyof T]: { [key: string | number]: any } };
54
- dictionaryLabel?: (keyof T)[];
55
- dictionaryLabelJoin?: (keyof T)[];
56
- defaultValue?: string;
57
- }
58
- ): T;
59
-
60
- /**
61
- * 将字符串拼接的图片地址转为数组
62
- * @param data 需修改对象
63
- * @param options 配置
64
- * @returns
65
- */
66
- export function _SetPhoto<T>(
67
- data: T,
68
- options: {
69
- label?: (keyof T)[];
70
- defaultUrl?: {
71
- [key in keyof T]: string[];
72
- };
73
- }
74
- ): T;
75
-
76
- /**
77
- * 将接口返回的数据进行处理,得到展示数据
78
- * @param data object 类型的数据
79
- * @param options 配置
80
- * @returns exhibit_data
81
- */
82
- export function _Exhibit_details<T>(
83
- data: T,
84
- options?: {
85
- dictionaryOptions?: { [key in keyof T]: { [key: string | number]: any } };
86
- dictionaryLabel?: (keyof T)[];
87
- dictionaryLabelJoin?: (keyof T)[];
88
-
89
- photoLabel?: (keyof T)[];
90
- photoDefaultUrl?: {
91
- [key in keyof T]: string[];
92
- };
93
-
94
- quantifierLabel?: (
95
- | keyof T
96
- | [
97
- keyof T,
98
- {
99
- join?: boolean;
100
- suffix?: string;
101
- integer?: boolean;
102
- }
103
- ]
104
- )[];
105
-
106
- filterLabel?: (keyof T)[];
107
-
108
- defaultValue?: string;
109
- }
110
- ): T;
111
-
112
9
  type UiLibrary = "naiveUI" | "ElementPlus" | "Element";
113
10
  /**
114
11
  * 点击非指定dom(包含子级dom)时执行 callback
package/lib/User/User.js CHANGED
@@ -44,205 +44,6 @@ export function _ScrollEndListener(callback) {
44
44
  };
45
45
  }
46
46
 
47
- /**
48
- * 设置量词属性
49
- * @param {*} data 需修改对象
50
- * @param {*} options 配置
51
- * @returns data
52
- */
53
- export function _SetQuantifierAttribute(data, options = []) {
54
- if (!_IsObject(data)) {
55
- console.error("异常输入:", data);
56
- return data;
57
- }
58
-
59
- options.forEach((item) => {
60
- if (typeof item === "string") {
61
- data[item] = _FormatNumberWithUnit(data[item]);
62
- } else if (Array.isArray(item)) {
63
- const [label, config] = data[item];
64
- if (_NotNull(label) && _IsObject(config))
65
- data[label] = _FormatNumberWithUnit(label, config);
66
- }
67
- });
68
- return data;
69
- }
70
-
71
- /**
72
- * 为属性值为null | undefined的属性设置默认值
73
- * @param data 需修改对象
74
- * @param options 配置
75
- * @returns
76
- */
77
- export function _SetDefaultValue(data, options = {}) {
78
- if (!_IsObject(data)) {
79
- console.error("异常输入:", data);
80
- return data;
81
- }
82
-
83
- const { defaultValue = "--", fieldsNotRequiringAction } = options;
84
-
85
- for (const key in data) {
86
- if (Object.prototype.hasOwnProperty.call(data, key)) {
87
- const element = data[key];
88
- if (fieldsNotRequiringAction) {
89
- if (!fieldsNotRequiringAction.includes(key) && !_NotNull(element)) {
90
- data[key] = defaultValue;
91
- }
92
- } else if (!_NotNull(element)) {
93
- data[key] = defaultValue;
94
- }
95
- }
96
- }
97
-
98
- return data;
99
- }
100
-
101
- /**
102
- * 将字典value转为对应label
103
- * @param data 需修改对象
104
- * @param options 配置
105
- * @returns
106
- */
107
- export function _SetDictionary(data, options = {}) {
108
- if (!_IsObject(data)) {
109
- console.error("异常输入:", data);
110
- return data;
111
- }
112
-
113
- const {
114
- dictionaryLabel = [],
115
- dictionaryLabelJoin = [],
116
- dictionaryOptions,
117
- defaultValue = "--",
118
- } = options;
119
-
120
- if (dictionaryOptions) {
121
- dictionaryLabel.forEach((label) => {
122
- if (_NotNull(data[label])) {
123
- const options = dictionaryOptions[label];
124
-
125
- if (options) {
126
- data[label] = options[data[label]];
127
- } else {
128
- data[label] = defaultValue;
129
- }
130
- } else {
131
- data[label] = defaultValue;
132
- }
133
- });
134
- dictionaryLabelJoin.forEach((label) => {
135
- if (_NotNull(data[label]) && data[label] != "") {
136
- const options = dictionaryOptions[label];
137
- if (options) {
138
- const oldvalue = data[label].split(",");
139
- data[label] = "";
140
- oldvalue.forEach((_label) => {
141
- data[label] += options[_label];
142
- });
143
- } else {
144
- data[label] = defaultValue;
145
- }
146
- } else {
147
- data[label] = defaultValue;
148
- }
149
- });
150
- }
151
-
152
- return data;
153
- }
154
-
155
- /**
156
- * 将字符串拼接的图片地址转为数组
157
- * @param data 需修改对象
158
- * @param options 配置
159
- * @returns
160
- */
161
- export function _SetPhoto(data, options = {}) {
162
- if (!_IsObject(data)) {
163
- console.error("异常输入:", data);
164
- return data;
165
- }
166
-
167
- const { label, defaultUrl } = options;
168
-
169
- if (label) {
170
- label.forEach((label) => {
171
- const defaultValue = (defaultUrl && defaultUrl[label]) || [];
172
- const value = data[label];
173
- if (typeof value === "string") {
174
- data[label] = value.split(",").filter(Boolean);
175
- } else {
176
- data[label] = defaultValue;
177
- }
178
- });
179
- }
180
-
181
- return data;
182
- }
183
-
184
- /**
185
- * 将接口返回的数据进行处理,得到展示数据
186
- * @param data object 类型的数据
187
- * @param options 配置
188
- * @returns exhibit_data
189
- */
190
- export function _Exhibit_details(data, options = {}) {
191
- if (!_IsObject(data)) {
192
- console.error("异常输入:", data);
193
- return {};
194
- }
195
-
196
- data = JSON.parse(JSON.stringify(data));
197
-
198
- const {
199
- dictionaryLabel = [],
200
- dictionaryLabelJoin = [],
201
- dictionaryOptions,
202
-
203
- photoLabel = [],
204
- photoDefaultUrl,
205
-
206
- quantifierLabel = [],
207
-
208
- filterLabel = [],
209
-
210
- defaultValue = "--",
211
- } = options;
212
-
213
- _SetDictionary(data, {
214
- dictionaryLabel,
215
- dictionaryLabelJoin,
216
- dictionaryOptions,
217
- defaultValue,
218
- });
219
-
220
- _SetPhoto(data, {
221
- label: photoLabel,
222
- defaultUrl: photoDefaultUrl,
223
- });
224
-
225
- _SetQuantifierAttribute(data, quantifierLabel);
226
-
227
- _SetDefaultValue(data, {
228
- defaultValue,
229
- fieldsNotRequiringAction: dictionaryLabel
230
- .concat(dictionaryLabelJoin)
231
- .concat(photoLabel)
232
- .concat(
233
- quantifierLabel
234
- .map((item) => {
235
- if (typeof item == "string") return item;
236
- if (Array.isArray(item)) return item[0];
237
- })
238
- .filter(Boolean)
239
- )
240
- .concat(filterLabel),
241
- });
242
-
243
- return data;
244
- }
245
-
246
47
  /**
247
48
  * 点击非指定dom(包含子级dom)时执行 callback
248
49
  * @param querySelector 允许点击的 dom 顶层祖先元素选择器
@@ -287,7 +287,6 @@ export class _FileTypeChecker {
287
287
  * 检查给定URL的文件类型
288
288
  * @param {string} url - 文件的URL
289
289
  * @returns {keyof _FileTypeChecker['fileExtensions'] | 'unknown'} - 返回文件类型或 "unknown"
290
- * @throws {Error} - 如果URL无效或指定的文件类型未知,则抛出错误
291
290
  */
292
291
  static check(url: string): keyof FileExtensions | "unknown";
293
292
  /**
@@ -295,7 +294,6 @@ export class _FileTypeChecker {
295
294
  * @param {string} url - 文件的URL
296
295
  * @param {keyof _FileTypeChecker['fileExtensions']} [type] - 可选参数,指定要检查的文件类型
297
296
  * @returns {boolean} - 返回布尔值
298
- * @throws {Error} - 如果URL无效或指定的文件类型未知,则抛出错误
299
297
  */
300
298
  static check(url: string, type: keyof FileExtensions): boolean;
301
299
 
@@ -306,11 +304,18 @@ export class _FileTypeChecker {
306
304
  *
307
305
  * @param {string} url - 以逗号分隔的URL字符串,每个URL代表一个资源的位置
308
306
  * @returns {Array} - 包含每个URL及其相关信息(名称和类型)的对象数组
309
- * @throws {Error} - 如果提供的URL为空或不是字符串,则抛出错误
310
307
  */
311
308
  static parseAddresses(
312
309
  url: string
313
- ): { url: string; name: string; type: string }[];
310
+ ): { url: string; name: string; type: keyof FileExtensions | "unknown" }[];
311
+
312
+ /**
313
+ * 检查 MIME 类型是否与指定的模式匹配
314
+ * @param {string} type - 要检查的 MIME 类型(如 "image/png")
315
+ * @param {string} [accept] - 可接受的 MIME 类型模式(如 "image/*, text/plain")
316
+ * @returns {boolean} - 如果类型匹配,则返回 true,否则返回 false
317
+ */
318
+ static matchesMimeType(type: string, accept?: string): boolean;
314
319
 
315
320
  /**
316
321
  * 检查URL是否具有任何指定的文件扩展名
@@ -269,48 +269,6 @@ export function _DownloadFile(href, fileName) {
269
269
  }
270
270
  })
271
271
  .catch(reject);
272
-
273
- // try {
274
- // // 验证 href 和 fileName 是否为合法字符串
275
- // if (typeof href !== "string" || !href.trim()) {
276
- // throw new Error("无效的 href 参数");
277
- // }
278
- // if (typeof fileName !== "string") {
279
- // fileName = _GetHrefName(href, "downloaded_file");
280
- // }
281
-
282
- // fetch(href)
283
- // .then((response) => {
284
- // if (!response.ok) {
285
- // throw new Error(`文件下载失败,状态码: ${response.status}`);
286
- // }
287
- // return response.blob();
288
- // })
289
- // .then((blob) => {
290
- // const url = URL.createObjectURL(blob); // 创建文件 URL
291
-
292
- // const a = document.createElement("a");
293
- // a.href = url;
294
- // a.download = decodeURIComponent(fileName);
295
-
296
- // // 临时将 a 标签添加到 DOM,然后触发点击事件,最后移除
297
- // document.body.appendChild(a);
298
- // a.click();
299
- // document.body.removeChild(a);
300
-
301
- // // 释放 URL 对象
302
- // URL.revokeObjectURL(url);
303
-
304
- // resolve();
305
- // })
306
- // .catch((error) => {
307
- // console.error("下载文件时发生错误:", error.message);
308
- // reject(error);
309
- // });
310
- // } catch (error) {
311
- // console.error("下载文件时发生错误:", error.message);
312
- // reject(error);
313
- // }
314
272
  });
315
273
  }
316
274
 
@@ -448,13 +406,17 @@ export function _Debounce(fn, delay) {
448
406
  * @returns {Function}
449
407
  */
450
408
  export function _Throttle(fn, delay) {
451
- let timer;
409
+ let lastCallTime = -Infinity;
410
+
452
411
  return function (...args) {
453
- if (!timer) {
454
- timer = setTimeout(() => {
455
- fn.apply(this, args);
456
- timer = null;
457
- }, delay);
412
+ const now = performance.now();
413
+ if (now - lastCallTime > delay) {
414
+ lastCallTime = now;
415
+ try {
416
+ fn(...args);
417
+ } catch (error) {
418
+ console.error("Throttled function execution failed:", error);
419
+ }
458
420
  }
459
421
  };
460
422
  }
@@ -783,12 +745,12 @@ export class _FileTypeChecker {
783
745
  * @param {string} url - 文件的URL
784
746
  * @param {string} [type] - 可选参数,指定要检查的文件类型
785
747
  * @returns {string} - 如果URL与指定类型或任何已知类型匹配,则返回文件类型,否则返回"unknown"
786
- * @throws {Error} - 如果URL无效或指定的文件类型未知,则抛出错误
787
748
  */
788
749
  static check(url, type) {
789
750
  // 确保提供的URL是字符串且非空
790
751
  if (!url || typeof url !== "string") {
791
- throw new Error("Invalid URL provided");
752
+ console.error("Invalid URL provided");
753
+ return type ? false : "unknown";
792
754
  }
793
755
 
794
756
  // 将URL转换为小写,以确保文件扩展名匹配不区分大小写
@@ -798,7 +760,8 @@ export class _FileTypeChecker {
798
760
  if (type) {
799
761
  // 确保指定的文件类型是已知的
800
762
  if (!_FileTypeChecker.fileExtensions.hasOwnProperty(type)) {
801
- throw new Error(`Unknown file type: ${type}`);
763
+ console.error(`Unknown file type: ${type}`);
764
+ return "unknown";
802
765
  }
803
766
  const extensions = _FileTypeChecker.fileExtensions[type];
804
767
  return _FileTypeChecker._checkExtension(lowerCaseUrl, extensions);
@@ -815,12 +778,12 @@ export class _FileTypeChecker {
815
778
  *
816
779
  * @param {string} url - 以逗号分隔的URL字符串,每个URL代表一个资源的位置
817
780
  * @returns {Array} - 包含每个URL及其相关信息(名称和类型)的对象数组
818
- * @throws {Error} - 如果提供的URL为空或不是字符串,则抛出错误
819
781
  */
820
782
  static parseAddresses(url) {
821
783
  // 确保提供的URL是字符串且非空
822
784
  if (!url || typeof url !== "string") {
823
- throw new Error("Invalid URL provided");
785
+ console.error("Invalid URL provided");
786
+ return [];
824
787
  }
825
788
 
826
789
  // 分割URL字符串并映射每个URL到包含其详细信息的对象
@@ -834,6 +797,45 @@ export class _FileTypeChecker {
834
797
  });
835
798
  }
836
799
 
800
+ /**
801
+ * 检查 MIME 类型是否与指定的模式匹配
802
+ * @param {string} type - 要检查的 MIME 类型(如 "image/png")
803
+ * @param {string} [accept] - 可接受的 MIME 类型模式(如 "image/*, text/plain")
804
+ * @returns {boolean} - 如果类型匹配,则返回 true,否则返回 false
805
+ */
806
+ static matchesMimeType(type, accept) {
807
+ if (
808
+ !accept ||
809
+ !type ||
810
+ typeof accept !== "string" ||
811
+ typeof type !== "string"
812
+ )
813
+ return true;
814
+
815
+ const [typeMain, typeSub] = type.split("/");
816
+ const mimePatterns = accept.split(",");
817
+
818
+ return mimePatterns.some((pattern) => {
819
+ const [patternMain, patternSub] = pattern.split("/");
820
+
821
+ // 处理简写格式(如 "image" 等价于 "image/*")
822
+ const effectivePatternSub = patternSub || "*";
823
+ const effectiveTypeSub = typeSub || "*";
824
+
825
+ // 主类型匹配:模式包含通配符 或 类型包含通配符 或 精确匹配
826
+ const mainMatch =
827
+ patternMain === "*" || typeMain === "*" || patternMain === typeMain;
828
+
829
+ // 子类型匹配:同上逻辑
830
+ const subMatch =
831
+ effectivePatternSub === "*" ||
832
+ effectiveTypeSub === "*" ||
833
+ effectivePatternSub === effectiveTypeSub;
834
+
835
+ return mainMatch && subMatch;
836
+ });
837
+ }
838
+
837
839
  /**
838
840
  * 检查URL是否具有任何指定的文件扩展名
839
841
  * @param {string} url - 文件的URL
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhanh-pure-function",
3
- "version": "1.3.21",
3
+ "version": "1.4.0",
4
4
  "description": "纯函数工具",
5
5
  "main": "lib/Index.js",
6
6
  "scripts": {