nhanh-pure-function 1.4.0 → 1.4.1

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.
@@ -0,0 +1,249 @@
1
+ export const EXTENSION_TO_MIME = {
2
+ /* 常见扩展名映射 */
3
+ ".mp3": "audio/mpeg",
4
+ ".mp4": "video/mp4",
5
+ ".m4a": "audio/mp4",
6
+ ".aac": "audio/aac",
7
+ ".ogg": "audio/ogg",
8
+ ".wav": "audio/wav",
9
+ ".flac": "audio/flac",
10
+ ".opus": "audio/opus",
11
+ ".webm": "video/webm",
12
+ ".avi": "video/x-msvideo",
13
+ ".mov": "video/quicktime",
14
+ ".wmv": "video/x-ms-wmv",
15
+ ".png": "image/png",
16
+ ".jpg": "image/jpeg",
17
+ ".jpeg": "image/jpeg",
18
+ ".gif": "image/gif",
19
+ ".bmp": "image/bmp",
20
+ ".tiff": "image/tiff",
21
+ ".ico": "image/vnd.microsoft.icon",
22
+ ".svg": "image/svg+xml",
23
+ ".webp": "image/webp",
24
+ ".heif": "image/heif",
25
+ ".heic": "image/heic",
26
+ ".json": "application/json",
27
+ ".xml": "application/xml",
28
+ ".html": "text/html",
29
+ ".htm": "text/html",
30
+ ".css": "text/css",
31
+ ".js": "application/javascript",
32
+ ".ts": "application/typescript",
33
+ ".csv": "text/csv",
34
+ ".tsv": "text/tab-separated-values",
35
+ ".txt": "text/plain",
36
+ ".md": "text/markdown",
37
+ ".rtf": "application/rtf",
38
+ ".pdf": "application/pdf",
39
+ ".zip": "application/zip",
40
+ ".rar": "application/x-rar-compressed",
41
+ ".tar": "application/x-tar",
42
+ ".gz": "application/gzip",
43
+ ".7z": "application/x-7z-compressed",
44
+ ".exe": "application/x-msdownload",
45
+ ".apk": "application/vnd.android.package-archive",
46
+ ".doc": "application/msword",
47
+ ".docx":
48
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
49
+ ".xls": "application/vnd.ms-excel",
50
+ ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
51
+ ".ppt": "application/vnd.ms-powerpoint",
52
+ ".pptx":
53
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation",
54
+ ".odt": "application/vnd.oasis.opendocument.text",
55
+ ".ods": "application/vnd.oasis.opendocument.spreadsheet",
56
+ ".odp": "application/vnd.oasis.opendocument.presentation",
57
+ ".jsonld": "application/ld+json",
58
+ ".yaml": "application/x-yaml",
59
+ ".yml": "application/x-yaml",
60
+ ".woff": "font/woff",
61
+ ".woff2": "font/woff2",
62
+ ".ttf": "font/ttf",
63
+ ".otf": "font/otf",
64
+ ".eot": "application/vnd.ms-fontobject",
65
+ ".map": "application/json",
66
+ // 可根据需要继续扩展
67
+ };
68
+
69
+ export const FILE_EXTENSIONS = {
70
+ image: [
71
+ ".jpg",
72
+ ".jpeg",
73
+ ".png",
74
+ ".gif",
75
+ ".bmp",
76
+ ".webp",
77
+ ".tiff",
78
+ ".svg",
79
+ ".heif",
80
+ ".heic",
81
+ ".ico",
82
+ ".raw",
83
+ ".jfif",
84
+ ".avif",
85
+ ".png8",
86
+ ".indd",
87
+ ".eps",
88
+ ".ai",
89
+ ],
90
+ ppt: [".ppt", ".pptx", ".odp"],
91
+ word: [".doc", ".docx", ".odt", ".rtf"],
92
+ excel: [".xls", ".xlsx", ".ods", ".csv", ".tsv"],
93
+ pdf: [".pdf"],
94
+ text: [
95
+ ".txt",
96
+ ".csv",
97
+ ".md",
98
+ ".json",
99
+ ".yaml",
100
+ ".yml",
101
+ ".log",
102
+ ".ini",
103
+ ".rtf",
104
+ ],
105
+ audio: [
106
+ ".mp3",
107
+ ".wav",
108
+ ".ogg",
109
+ ".flac",
110
+ ".aac",
111
+ ".wma",
112
+ ".m4a",
113
+ ".alac",
114
+ ".ape",
115
+ ".opus",
116
+ ".amr",
117
+ ".ra",
118
+ ".mid",
119
+ ".midi",
120
+ ".aiff",
121
+ ".pcm",
122
+ ".au",
123
+ ".wavpack",
124
+ ".spx",
125
+ ],
126
+ video: [
127
+ ".mp4",
128
+ ".avi",
129
+ ".mkv",
130
+ ".mov",
131
+ ".wmv",
132
+ ".flv",
133
+ ".webm",
134
+ ".mpg",
135
+ ".mpeg",
136
+ ".3gp",
137
+ ".vob",
138
+ ".ogv",
139
+ ".m4v",
140
+ ".ts",
141
+ ".rm",
142
+ ".rmvb",
143
+ ".m2ts",
144
+ ".divx",
145
+ ".xvid",
146
+ ".swf",
147
+ ".f4v",
148
+ ],
149
+ archive: [
150
+ ".zip",
151
+ ".rar",
152
+ ".tar",
153
+ ".gz",
154
+ ".bz2",
155
+ ".xz",
156
+ ".7z",
157
+ ".tar.gz",
158
+ ".tar.bz2",
159
+ ".tar.xz",
160
+ ".tar.lz",
161
+ ".tar.lzma",
162
+ ".cab",
163
+ ".iso",
164
+ ".dmg",
165
+ ".tgz",
166
+ ".apk",
167
+ ".gz2",
168
+ ".tar.zst",
169
+ ],
170
+ code: [
171
+ ".js",
172
+ ".ts",
173
+ ".py",
174
+ ".java",
175
+ ".cpp",
176
+ ".c",
177
+ ".html",
178
+ ".css",
179
+ ".scss",
180
+ ".less",
181
+ ".sass",
182
+ ".php",
183
+ ".rb",
184
+ ".go",
185
+ ".swift",
186
+ ".rs",
187
+ ".kt",
188
+ ".scala",
189
+ ".lua",
190
+ ".pl",
191
+ ".m",
192
+ ".h",
193
+ ".xml",
194
+ ".json",
195
+ ".yaml",
196
+ ".yml",
197
+ ".toml",
198
+ ".vue",
199
+ ".ejs",
200
+ ".handlebars",
201
+ ".jinja",
202
+ ".dart",
203
+ ],
204
+ font: [
205
+ ".woff",
206
+ ".woff2",
207
+ ".ttf",
208
+ ".otf",
209
+ ".eot",
210
+ ".svg",
211
+ ".ttc",
212
+ ".fnt",
213
+ ".fon",
214
+ ".otc",
215
+ ],
216
+ database: [
217
+ ".sql",
218
+ ".sqlite",
219
+ ".db",
220
+ ".mdb",
221
+ ".accdb",
222
+ ".jsonld",
223
+ ".xml",
224
+ ".csv",
225
+ ],
226
+ markup: [".html", ".htm", ".xhtml", ".xml", ".json", ".yaml", ".yml"],
227
+ configuration: [
228
+ ".ini",
229
+ ".conf",
230
+ ".cfg",
231
+ ".env",
232
+ ".properties",
233
+ ".json",
234
+ ".toml",
235
+ ],
236
+ logs: [".log", ".err", ".trace", ".out"],
237
+ script: [
238
+ ".bash",
239
+ ".sh",
240
+ ".zsh",
241
+ ".bat",
242
+ ".ps1",
243
+ ".vbs",
244
+ ".cmd",
245
+ ".sed",
246
+ ".awk",
247
+ ".php",
248
+ ],
249
+ };
@@ -317,6 +317,16 @@ export class _FileTypeChecker {
317
317
  */
318
318
  static matchesMimeType(type: string, accept?: string): boolean;
319
319
 
320
+ /**
321
+ * 类型标准化函数
322
+ * 该函数旨在将文件类型或MIME类型字符串转换为标准格式
323
+ * 主要处理三种情况:带扩展名的字符串、简写格式的类型以及已标准格式的类型
324
+ *
325
+ * @param {string} type - 文件类型或MIME类型字符串
326
+ * @returns {string} 标准化的MIME类型字符串,如果无法识别则返回原始输入
327
+ */
328
+ static _normalizeType(type: string): string;
329
+
320
330
  /**
321
331
  * 检查URL是否具有任何指定的文件扩展名
322
332
  * @param {string} url - 文件的URL
@@ -1,3 +1,5 @@
1
+ import { EXTENSION_TO_MIME, FILE_EXTENSIONS } from "../Constant";
2
+
1
3
  /**
2
4
  * 非null | undefined判断
3
5
  * @param value any
@@ -141,39 +143,27 @@ export function _TimeTransition(
141
143
  template = "YYYY-MM-DD hh:mm:ss",
142
144
  pad = true
143
145
  ) {
144
- try {
145
- time = new Date(time);
146
- } catch (error) {
147
- console.error(error);
146
+ const date = new Date(time);
147
+
148
+ if (isNaN(date.getTime())) {
149
+ console.error("Invalid date");
148
150
  return "";
149
151
  }
152
+
150
153
  const dictionary = {
151
- YYYY: "getFullYear",
152
- MM: "getMonth",
153
- DD: "getDate",
154
- hh: "getHours",
155
- mm: "getMinutes",
156
- ss: "getSeconds",
157
- ms: (num) => +num % 1000,
154
+ YYYY: (date) => date.getFullYear(),
155
+ MM: (date) => date.getMonth() + 1, // Adjust for 0-based month
156
+ DD: (date) => date.getDate(),
157
+ hh: (date) => date.getHours(),
158
+ mm: (date) => date.getMinutes(),
159
+ ss: (date) => date.getSeconds(),
160
+ ms: (date) => date.getMilliseconds(),
158
161
  };
159
- for (const key in dictionary) {
160
- if (Object.hasOwnProperty.call(dictionary, key)) {
161
- if (new RegExp(key).test(template)) {
162
- let value,
163
- fun = dictionary[key];
164
-
165
- if (typeof fun == "function") value = fun(time);
166
- else value = time[fun]();
167
162
 
168
- if (key == "MM") value++;
169
-
170
- if (pad) value = String(value).padStart(2, "0");
171
-
172
- template = template.replace(key, value);
173
- }
174
- }
175
- }
176
- return template;
163
+ return template.replace(/YYYY|MM|DD|hh|mm|ss|ms/g, (match) => {
164
+ const value = dictionary[match](date);
165
+ return pad ? String(value).padStart(2, "0") : String(value);
166
+ });
177
167
  }
178
168
 
179
169
  /**
@@ -667,78 +657,8 @@ export function _IsSecureContext(url) {
667
657
  * 用于检查文件URL的类型
668
658
  */
669
659
  export class _FileTypeChecker {
670
- // 定义各种文件类型的文件扩展名
671
- static fileExtensions = {
672
- image: [
673
- ".jpg",
674
- ".jpeg",
675
- ".png",
676
- ".gif",
677
- ".bmp",
678
- ".webp",
679
- ".tiff",
680
- ".svg",
681
- ".heif",
682
- ".heic",
683
- ".ico",
684
- ".raw",
685
- ".jfif",
686
- ],
687
- ppt: [".ppt", ".pptx"],
688
- word: [".doc", ".docx"],
689
- excel: [".xls", ".xlsx"],
690
- pdf: [".pdf"],
691
- text: [".txt", ".csv"],
692
- audio: [
693
- ".mp3",
694
- ".wav",
695
- ".ogg",
696
- ".flac",
697
- ".aac",
698
- ".wma",
699
- ".m4a",
700
- ".alac",
701
- ".ape",
702
- ".opus",
703
- ".amr",
704
- ".ra",
705
- ],
706
- video: [
707
- ".mp4",
708
- ".avi",
709
- ".mkv",
710
- ".mov",
711
- ".wmv",
712
- ".flv",
713
- ".webm",
714
- ".mpg",
715
- ".mpeg",
716
- ".3gp",
717
- ".vob",
718
- ".ogv",
719
- ".m4v",
720
- ".ts",
721
- ".rm",
722
- ".rmvb",
723
- ],
724
- archive: [
725
- ".zip",
726
- ".rar",
727
- ".tar",
728
- ".gz",
729
- ".bz2",
730
- ".xz",
731
- ".7z",
732
- ".tar.gz",
733
- ".tar.bz2",
734
- ".tar.xz",
735
- ],
736
- code: [".js", ".ts", ".py", ".java", ".cpp", ".c"],
737
- font: [".woff", ".woff2", ".ttf", ".otf"],
738
- };
739
-
740
660
  // 缓存文件扩展名的条目,以提高性能
741
- static cachedEntries = Object.entries(_FileTypeChecker.fileExtensions);
661
+ static cachedEntries = Object.entries(FILE_EXTENSIONS);
742
662
 
743
663
  /**
744
664
  * 检查给定URL的文件类型
@@ -759,11 +679,11 @@ export class _FileTypeChecker {
759
679
  // 如果指定了文件类型,则检查URL是否具有该类型的任何文件扩展名
760
680
  if (type) {
761
681
  // 确保指定的文件类型是已知的
762
- if (!_FileTypeChecker.fileExtensions.hasOwnProperty(type)) {
682
+ if (!FILE_EXTENSIONS.hasOwnProperty(type)) {
763
683
  console.error(`Unknown file type: ${type}`);
764
684
  return "unknown";
765
685
  }
766
- const extensions = _FileTypeChecker.fileExtensions[type];
686
+ const extensions = FILE_EXTENSIONS[type];
767
687
  return _FileTypeChecker._checkExtension(lowerCaseUrl, extensions);
768
688
  }
769
689
 
@@ -804,38 +724,63 @@ export class _FileTypeChecker {
804
724
  * @returns {boolean} - 如果类型匹配,则返回 true,否则返回 false
805
725
  */
806
726
  static matchesMimeType(type, accept) {
727
+ // 参数有效性检查
807
728
  if (
808
729
  !accept ||
809
730
  !type ||
810
731
  typeof accept !== "string" ||
811
732
  typeof type !== "string"
812
- )
733
+ ) {
813
734
  return true;
735
+ }
814
736
 
815
- const [typeMain, typeSub] = type.split("/");
816
- const mimePatterns = accept.split(",");
737
+ // 标准化类型和接受模式
738
+ const normalizedType = _FileTypeChecker._normalizeType(type);
739
+ const mimePatterns = accept
740
+ .split(",")
741
+ .map((pattern) => _FileTypeChecker._normalizeType(pattern.trim()));
817
742
 
818
- return mimePatterns.some((pattern) => {
819
- const [patternMain, patternSub] = pattern.split("/");
743
+ // 拆分主/子类型
744
+ const [typeMain, typeSub = "*"] = normalizedType.split("/");
820
745
 
821
- // 处理简写格式(如 "image" 等价于 "image/*")
822
- const effectivePatternSub = patternSub || "*";
823
- const effectiveTypeSub = typeSub || "*";
746
+ return mimePatterns.some((pattern) => {
747
+ const [patternMain, patternSub = "*"] = pattern.split("/");
824
748
 
825
- // 主类型匹配:模式包含通配符 或 类型包含通配符 或 精确匹配
749
+ // 主类型匹配逻辑
826
750
  const mainMatch =
827
751
  patternMain === "*" || typeMain === "*" || patternMain === typeMain;
828
752
 
829
- // 子类型匹配:同上逻辑
753
+ // 子类型匹配逻辑
830
754
  const subMatch =
831
- effectivePatternSub === "*" ||
832
- effectiveTypeSub === "*" ||
833
- effectivePatternSub === effectiveTypeSub;
755
+ patternSub === "*" || typeSub === "*" || patternSub === typeSub;
834
756
 
835
757
  return mainMatch && subMatch;
836
758
  });
837
759
  }
838
760
 
761
+ /**
762
+ * 类型标准化函数
763
+ * 该函数旨在将文件类型或MIME类型字符串转换为标准格式
764
+ * 主要处理三种情况:带扩展名的字符串、简写格式的类型以及已标准格式的类型
765
+ *
766
+ * @param {string} type - 文件类型或MIME类型字符串
767
+ * @returns {string} 标准化的MIME类型字符串,如果无法识别则返回原始输入
768
+ */
769
+ static _normalizeType(type) {
770
+ // 处理扩展名(如 .mp3)
771
+ if (type.startsWith(".") && !type.includes("/")) {
772
+ return EXTENSION_TO_MIME[type.toLowerCase()] || type;
773
+ }
774
+
775
+ // 处理简写格式(如 "image" 转换为 "image/*")
776
+ if (!type.includes("/")) {
777
+ return `${type}/*`;
778
+ }
779
+
780
+ // 返回原始输入,因为它已经是标准格式
781
+ return type;
782
+ }
783
+
839
784
  /**
840
785
  * 检查URL是否具有任何指定的文件扩展名
841
786
  * @param {string} url - 文件的URL
package/lib/test.ts CHANGED
@@ -1,12 +1 @@
1
1
  import { _FileTypeChecker, _GetHrefName } from "./Utility/Utility";
2
-
3
- let value: any = _GetHrefName(
4
- "https://fastly.picsum.photos/id/841/805/453.jpg?hmac=RMVB2BBB6T34wI08nwWz1urBgdzSQxNR6L48hkOsTNo"
5
- );
6
-
7
- console.log(value);
8
-
9
- value = _FileTypeChecker.parseAddresses(
10
- "https://emergency.wjtsc.com:1128/resources/life-cycle/test/1-110000251006481.pcm,https://emergency.wjtsc.com:1128/resources/life-cycle/test/car-828972667665784.mp3,https://emergency.wjtsc.com:1128/resources/life-cycle/test/317361004975528.jpg"
11
- );
12
- console.log(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhanh-pure-function",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "纯函数工具",
5
5
  "main": "lib/Index.js",
6
6
  "scripts": {