@fangzhongya/utils 0.0.38 → 0.0.40

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 (77) hide show
  1. package/dist/basic/index.cjs +17 -17
  2. package/dist/basic/index.js +16 -16
  3. package/dist/basic/object/index.cjs +5 -5
  4. package/dist/basic/object/index.js +5 -5
  5. package/dist/basic/string/index.cjs +14 -14
  6. package/dist/basic/string/index.js +18 -18
  7. package/dist/basic/string/toJsons.cjs +3 -3
  8. package/dist/basic/string/toJsons.js +2 -2
  9. package/dist/{chunk-RLJWS3GU.js → chunk-3RROPV2V.js} +5 -1
  10. package/dist/{chunk-WCOLWI35.cjs → chunk-5UXVYCNI.cjs} +5 -1
  11. package/dist/{chunk-SZ3EQ45X.js → chunk-5WQXANBO.js} +5 -1
  12. package/dist/{chunk-77ZAHOKN.cjs → chunk-7JDXGM4H.cjs} +13 -13
  13. package/dist/{chunk-TTIQUN2J.cjs → chunk-E74ZD4NG.cjs} +11 -7
  14. package/dist/{chunk-FA4AVVIV.cjs → chunk-OWIGBBKX.cjs} +4 -4
  15. package/dist/{chunk-HKUOF43F.js → chunk-PKKBS6BE.js} +16 -16
  16. package/dist/chunk-PW76Z25D.cjs +249 -0
  17. package/dist/chunk-UCF3YRN2.js +31 -0
  18. package/dist/{chunk-OZWQIUPS.cjs → chunk-UHKL2RG3.cjs} +5 -5
  19. package/dist/{chunk-L5HSTRIG.js → chunk-YKNGDK4I.js} +4 -4
  20. package/dist/{chunk-UZMD5VAA.cjs → chunk-Z25KMFRX.cjs} +15 -15
  21. package/dist/chunk-Z2EAQZ47.cjs +31 -0
  22. package/dist/chunk-ZLEU2YQ2.js +249 -0
  23. package/dist/{chunk-XOTRYNO4.js → chunk-ZNABWVK2.js} +4 -4
  24. package/dist/{chunk-BWA6OSIZ.js → chunk-ZVTCCLFN.js} +16 -16
  25. package/dist/css/index.cjs +13 -13
  26. package/dist/css/index.js +13 -13
  27. package/dist/date/hourFormat.cjs +7 -0
  28. package/dist/date/hourFormat.d.cts +8 -0
  29. package/dist/date/hourFormat.d.ts +8 -0
  30. package/dist/date/hourFormat.js +7 -0
  31. package/dist/date/index.cjs +6 -2
  32. package/dist/date/index.d.cts +1 -0
  33. package/dist/date/index.d.ts +1 -0
  34. package/dist/date/index.js +6 -2
  35. package/dist/html/index.cjs +6 -6
  36. package/dist/html/index.js +8 -8
  37. package/dist/{index-DQTqufH0.d.ts → index-9-f0oXM3.d.ts} +4 -2
  38. package/dist/index-CKervg3c.d.ts +10 -0
  39. package/dist/index-R9zb6ij8.d.cts +10 -0
  40. package/dist/{index-CTT_peAX.d.cts → index-_GH04n6g.d.cts} +4 -2
  41. package/dist/index.cjs +61 -60
  42. package/dist/index.d.cts +3 -2
  43. package/dist/index.d.ts +3 -2
  44. package/dist/index.js +62 -61
  45. package/dist/judge/index.cjs +6 -6
  46. package/dist/judge/index.js +5 -5
  47. package/dist/judge/matchs.cjs +3 -3
  48. package/dist/judge/matchs.js +2 -2
  49. package/dist/urls/getReplaceUrls.cjs +3 -3
  50. package/dist/urls/getReplaceUrls.js +2 -2
  51. package/dist/urls/index.cjs +16 -16
  52. package/dist/urls/index.js +19 -19
  53. package/dist/window/download.cjs +6 -2
  54. package/dist/window/download.d.cts +41 -19
  55. package/dist/window/download.d.ts +41 -19
  56. package/dist/window/download.js +5 -1
  57. package/dist/window/index.cjs +7 -3
  58. package/dist/window/index.d.cts +1 -1
  59. package/dist/window/index.d.ts +1 -1
  60. package/dist/window/index.js +6 -2
  61. package/package.json +6 -1
  62. package/dist/chunk-LHMAKOVB.cjs +0 -123
  63. package/dist/chunk-UQM7IS4W.js +0 -123
  64. package/dist/index-C1rfQdS-.d.ts +0 -8
  65. package/dist/index-CsYVH5m1.d.cts +0 -8
  66. package/dist/{chunk-EJZIISBA.js → chunk-6K32ZUJG.js} +6 -6
  67. package/dist/{chunk-YP4WA3PQ.js → chunk-GHJWTV6H.js} +3 -3
  68. package/dist/{chunk-UJ6W4KCR.cjs → chunk-HMAAC5QA.cjs} +3 -3
  69. package/dist/{chunk-5HC662VK.js → chunk-I5D6RCLU.js} +3 -3
  70. package/dist/{chunk-IEQHKLEL.js → chunk-IPNGPIGK.js} +4 -4
  71. package/dist/{chunk-ETMYYVQJ.cjs → chunk-L6JTKQ65.cjs} +2 -2
  72. package/dist/{chunk-JQPUT54I.js → chunk-M7CA6X7T.js} +12 -12
  73. package/dist/{chunk-QN6KZWKM.cjs → chunk-SGYFZFWD.cjs} +4 -4
  74. package/dist/{chunk-NGQKVTCR.cjs → chunk-T22I7TJQ.cjs} +3 -3
  75. package/dist/{chunk-FRGDQOX4.js → chunk-T2QKEABG.js} +3 -3
  76. package/dist/{chunk-DI5JC25H.cjs → chunk-XDTLZL44.cjs} +11 -11
  77. package/dist/{chunk-UGT4QBTM.cjs → chunk-XRQIE2K5.cjs} +4 -4
@@ -0,0 +1,249 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// packages/window/download.ts
2
+ function downloadText(text, filename = "text.txt", mimeType = "text/plain") {
3
+ downloadFile(text, { filename, mimeType });
4
+ }
5
+ function downloadJSON(jsonData, filename = "data.json") {
6
+ downloadFile(jsonData, {
7
+ filename,
8
+ mimeType: "application/json"
9
+ });
10
+ }
11
+ function downloadCSV(data, filename = "data.csv", headers) {
12
+ let csvContent = "";
13
+ if (Array.isArray(data) && data.length > 0) {
14
+ if (Array.isArray(data[0])) {
15
+ const rows = data;
16
+ csvContent = rows.map(
17
+ (row) => row.map((cell) => `"${String(cell).replace(/"/g, '""')}"`).join(",")
18
+ ).join("\n");
19
+ } else if (typeof data[0] === "object") {
20
+ const objects = data;
21
+ const objectHeaders = headers || Object.keys(objects[0]);
22
+ csvContent = objectHeaders.map((header) => `"${String(header).replace(/"/g, '""')}"`).join(",") + "\n";
23
+ csvContent += objects.map(
24
+ (obj) => objectHeaders.map(
25
+ (header) => `"${String(obj[header] || "").replace(
26
+ /"/g,
27
+ '""'
28
+ )}"`
29
+ ).join(",")
30
+ ).join("\n");
31
+ }
32
+ }
33
+ downloadFile(csvContent, {
34
+ filename,
35
+ mimeType: "text/csv"
36
+ });
37
+ }
38
+ async function downloadViaAJAX(url, filename, options = {}) {
39
+ try {
40
+ const response = await fetch(url, {
41
+ method: "GET",
42
+ ...options
43
+ });
44
+ if (!response.ok) {
45
+ throw new Error(`HTTP error! status: ${response.status}`);
46
+ }
47
+ const blob = await response.blob();
48
+ let finalFilename = filename;
49
+ if (!finalFilename) {
50
+ const contentDisposition = response.headers.get(
51
+ "content-disposition"
52
+ );
53
+ if (contentDisposition) {
54
+ const filenameMatch = contentDisposition.match(/filename="?(.+?)"?$/);
55
+ if (filenameMatch) {
56
+ finalFilename = filenameMatch[1];
57
+ }
58
+ }
59
+ }
60
+ downloadFile(blob, { filename: finalFilename });
61
+ } catch (error) {
62
+ console.error("AJAX \u4E0B\u8F7D\u5931\u8D25:", error);
63
+ throw new Error(
64
+ `\u4E0B\u8F7D\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`
65
+ );
66
+ }
67
+ }
68
+ function downloadFile(data, options = {}) {
69
+ const {
70
+ filename = "download",
71
+ mimeType = "application/octet-stream",
72
+ charset = "utf-8",
73
+ bom = ""
74
+ } = options;
75
+ try {
76
+ let blobData;
77
+ if (data instanceof Blob) {
78
+ blobData = data;
79
+ } else if (typeof data === "object") {
80
+ const jsonString = JSON.stringify(data, null, 2);
81
+ blobData = bom + jsonString;
82
+ } else if (typeof data === "string") {
83
+ blobData = bom + data;
84
+ } else {
85
+ blobData = data;
86
+ }
87
+ const blob = new Blob([blobData], {
88
+ type: `${mimeType};charset=${charset}`
89
+ });
90
+ const blobUrl = URL.createObjectURL(blob);
91
+ const link = document.createElement("a");
92
+ link.href = blobUrl;
93
+ link.download = filename;
94
+ link.style.display = "none";
95
+ document.body.appendChild(link);
96
+ link.click();
97
+ setTimeout(() => {
98
+ document.body.removeChild(link);
99
+ URL.revokeObjectURL(blobUrl);
100
+ }, 100);
101
+ } catch (error) {
102
+ console.error("\u6587\u4EF6\u4E0B\u8F7D\u5931\u8D25:", error);
103
+ throw new Error(
104
+ `\u4E0B\u8F7D\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`
105
+ );
106
+ }
107
+ }
108
+ async function downloadFromURL(url, filename, options = {}) {
109
+ const {
110
+ useBlob = true,
111
+ // 默认使用 Blob 方式,确保能触发下载
112
+ headers = {},
113
+ timeout = 3e4
114
+ } = options;
115
+ try {
116
+ if (!useBlob) {
117
+ const link = document.createElement("a");
118
+ link.href = url;
119
+ link.download = filename || getFilenameFromURL(url);
120
+ link.style.display = "none";
121
+ link.target = "_blank";
122
+ document.body.appendChild(link);
123
+ link.click();
124
+ setTimeout(() => {
125
+ document.body.removeChild(link);
126
+ }, 100);
127
+ return;
128
+ }
129
+ const controller = new AbortController();
130
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
131
+ const response = await fetch(url, {
132
+ method: "GET",
133
+ headers,
134
+ signal: controller.signal
135
+ });
136
+ clearTimeout(timeoutId);
137
+ if (!response.ok) {
138
+ throw new Error(`HTTP error! status: ${response.status}`);
139
+ }
140
+ const blob = await response.blob();
141
+ let finalFilename = filename;
142
+ if (!finalFilename) {
143
+ const contentDisposition = response.headers.get(
144
+ "content-disposition"
145
+ );
146
+ if (contentDisposition) {
147
+ const filenameMatch = contentDisposition.match(
148
+ /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
149
+ );
150
+ if (filenameMatch && filenameMatch[1]) {
151
+ finalFilename = filenameMatch[1].replace(/['"]/g, "");
152
+ }
153
+ }
154
+ if (!finalFilename) {
155
+ finalFilename = getFilenameFromURL(url);
156
+ }
157
+ }
158
+ const mimeType = getMimeTypeFromBlob(blob, url);
159
+ downloadFile(blob, {
160
+ filename: finalFilename,
161
+ mimeType
162
+ });
163
+ } catch (error) {
164
+ console.error("URL \u4E0B\u8F7D\u5931\u8D25:", error);
165
+ if (error instanceof Error && error.name === "AbortError") {
166
+ throw new Error("\u4E0B\u8F7D\u8D85\u65F6\uFF0C\u8BF7\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\u6216\u6587\u4EF6\u5927\u5C0F");
167
+ }
168
+ if (useBlob) {
169
+ console.log("Blob \u65B9\u5F0F\u5931\u8D25\uFF0C\u5C1D\u8BD5\u76F4\u63A5\u4E0B\u8F7D...");
170
+ await downloadFromURL(url, filename, {
171
+ ...options,
172
+ useBlob: false
173
+ });
174
+ } else {
175
+ throw new Error(
176
+ `\u4E0B\u8F7D\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`
177
+ );
178
+ }
179
+ }
180
+ }
181
+ async function downloadImage(imageUrl, filename) {
182
+ await downloadFromURL(imageUrl, filename, {
183
+ useBlob: true,
184
+ headers: {
185
+ Accept: "image/*"
186
+ }
187
+ });
188
+ }
189
+ function getFilenameFromURL(url) {
190
+ try {
191
+ const urlObj = new URL(url);
192
+ const pathname = urlObj.pathname;
193
+ const filename = pathname.substring(pathname.lastIndexOf("/") + 1);
194
+ if (!filename || filename === "/" || !filename.includes(".")) {
195
+ const extension = getFileExtensionFromURL(url) || "bin";
196
+ return `download_${Date.now()}.${extension}`;
197
+ }
198
+ return decodeURIComponent(filename);
199
+ } catch (e) {
200
+ const extension = getFileExtensionFromURL(url) || "bin";
201
+ return `download_${Date.now()}.${extension}`;
202
+ }
203
+ }
204
+ function getFileExtensionFromURL(url) {
205
+ const match = url.match(/\.([a-zA-Z0-9]+)(?:[?#]|$)/);
206
+ return match ? match[1].toLowerCase() : null;
207
+ }
208
+ function getMimeTypeFromBlob(blob, url) {
209
+ if (blob.type && blob.type !== "application/octet-stream") {
210
+ return blob.type;
211
+ }
212
+ const extension = getFileExtensionFromURL(url);
213
+ const mimeTypes = {
214
+ jpg: "image/jpeg",
215
+ jpeg: "image/jpeg",
216
+ png: "image/png",
217
+ gif: "image/gif",
218
+ webp: "image/webp",
219
+ svg: "image/svg+xml",
220
+ pdf: "application/pdf",
221
+ txt: "text/plain",
222
+ json: "application/json",
223
+ csv: "text/csv",
224
+ xml: "application/xml",
225
+ zip: "application/zip",
226
+ mp3: "audio/mpeg",
227
+ mp4: "video/mp4",
228
+ avi: "video/x-msvideo"
229
+ };
230
+ return extension && mimeTypes[extension] ? mimeTypes[extension] : "application/octet-stream";
231
+ }
232
+ async function downloadMultipleFiles(downloads) {
233
+ return Promise.all(
234
+ downloads.map(
235
+ (download) => downloadFromURL(download.url, download.filename)
236
+ )
237
+ );
238
+ }
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+ exports.downloadText = downloadText; exports.downloadJSON = downloadJSON; exports.downloadCSV = downloadCSV; exports.downloadViaAJAX = downloadViaAJAX; exports.downloadFile = downloadFile; exports.downloadFromURL = downloadFromURL; exports.downloadImage = downloadImage; exports.downloadMultipleFiles = downloadMultipleFiles;
@@ -0,0 +1,31 @@
1
+ // packages/date/hourFormat.ts
2
+ var hourFormat = (milliseconds, filled, milli) => {
3
+ let totalSeconds = milliseconds;
4
+ if (milli) {
5
+ totalSeconds = milliseconds / 1e3;
6
+ }
7
+ let hours = Math.floor(totalSeconds / 3600);
8
+ totalSeconds %= 3600;
9
+ let minutes = Math.floor(totalSeconds / 60);
10
+ totalSeconds %= 60;
11
+ let seconds = Math.floor(totalSeconds);
12
+ if (hours == 0) {
13
+ return `${getFilled(minutes, filled)}:${getFilled(seconds, true)}`;
14
+ } else {
15
+ return `${getFilled(hours, filled)}:${getFilled(
16
+ minutes,
17
+ true
18
+ )}:${getFilled(seconds, true)}`;
19
+ }
20
+ };
21
+ function getFilled(hours, is) {
22
+ if (is) {
23
+ return hours.toString().padStart(2, "0");
24
+ } else {
25
+ return hours;
26
+ }
27
+ }
28
+
29
+ export {
30
+ hourFormat
31
+ };
@@ -27,16 +27,16 @@
27
27
  var _chunkV2DXOH6Bcjs = require('./chunk-V2DXOH6B.cjs');
28
28
 
29
29
 
30
- var _chunkNGQKVTCRcjs = require('./chunk-NGQKVTCR.cjs');
30
+ var _chunkT22I7TJQcjs = require('./chunk-T22I7TJQ.cjs');
31
31
 
32
32
 
33
- var _chunkZZEFL2TEcjs = require('./chunk-ZZEFL2TE.cjs');
33
+ var _chunkYGJOBIEOcjs = require('./chunk-YGJOBIEO.cjs');
34
34
 
35
35
 
36
- var _chunk2BY5RQHUcjs = require('./chunk-2BY5RQHU.cjs');
36
+ var _chunkZZEFL2TEcjs = require('./chunk-ZZEFL2TE.cjs');
37
37
 
38
38
 
39
- var _chunkYGJOBIEOcjs = require('./chunk-YGJOBIEO.cjs');
39
+ var _chunk2BY5RQHUcjs = require('./chunk-2BY5RQHU.cjs');
40
40
 
41
41
 
42
42
  var _chunk75ZPJI57cjs = require('./chunk-75ZPJI57.cjs');
@@ -69,7 +69,7 @@ _chunk75ZPJI57cjs.__export.call(void 0, judge_exports, {
69
69
  isTel: () => _chunkV2DXOH6Bcjs.isTel,
70
70
  isURL: () => _chunkV2DXOH6Bcjs.isURL,
71
71
  isWeChat: () => _chunkV2DXOH6Bcjs.isWeChat,
72
- matchs: () => _chunkNGQKVTCRcjs.matchs,
72
+ matchs: () => _chunkT22I7TJQcjs.matchs,
73
73
  matchsEnd: () => _chunkYGJOBIEOcjs.matchsEnd,
74
74
  matchsStart: () => _chunkZZEFL2TEcjs.matchsStart,
75
75
  matchsWhole: () => _chunk2BY5RQHUcjs.matchsWhole
@@ -27,16 +27,16 @@ import {
27
27
  } from "./chunk-PLNZCEDI.js";
28
28
  import {
29
29
  matchs
30
- } from "./chunk-FRGDQOX4.js";
30
+ } from "./chunk-T2QKEABG.js";
31
+ import {
32
+ matchsEnd
33
+ } from "./chunk-YNOFNHEK.js";
31
34
  import {
32
35
  matchsStart
33
36
  } from "./chunk-S6JRKYPY.js";
34
37
  import {
35
38
  matchsWhole
36
39
  } from "./chunk-IRGCP7KH.js";
37
- import {
38
- matchsEnd
39
- } from "./chunk-YNOFNHEK.js";
40
40
  import {
41
41
  __export
42
42
  } from "./chunk-MLKGABMK.js";
@@ -1,19 +1,22 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkL7FSHU27cjs = require('./chunk-L7FSHU27.cjs');
4
3
 
5
4
 
6
5
 
7
- var _chunkJHV27P2Qcjs = require('./chunk-JHV27P2Q.cjs');
6
+ var _chunkL6JTKQ65cjs = require('./chunk-L6JTKQ65.cjs');
7
+
8
+
9
+ var _chunkXG44HG5Ncjs = require('./chunk-XG44HG5N.cjs');
8
10
 
9
11
 
12
+ var _chunkJ7S3KBHLcjs = require('./chunk-J7S3KBHL.cjs');
10
13
 
11
14
 
15
+ var _chunkL7FSHU27cjs = require('./chunk-L7FSHU27.cjs');
12
16
 
13
- var _chunkETMYYVQJcjs = require('./chunk-ETMYYVQJ.cjs');
14
17
 
15
18
 
16
- var _chunkXG44HG5Ncjs = require('./chunk-XG44HG5N.cjs');
19
+ var _chunkJHV27P2Qcjs = require('./chunk-JHV27P2Q.cjs');
17
20
 
18
21
 
19
22
 
@@ -27,19 +30,16 @@ var _chunk3GVTDRAEcjs = require('./chunk-3GVTDRAE.cjs');
27
30
  var _chunkXWCRGY54cjs = require('./chunk-XWCRGY54.cjs');
28
31
 
29
32
 
30
- var _chunkJ7S3KBHLcjs = require('./chunk-J7S3KBHL.cjs');
31
-
32
-
33
- var _chunkOJBZ7UUCcjs = require('./chunk-OJBZ7UUC.cjs');
33
+ var _chunk2H3KVSFAcjs = require('./chunk-2H3KVSFA.cjs');
34
34
 
35
35
 
36
- var _chunkJR6ZN6QDcjs = require('./chunk-JR6ZN6QD.cjs');
36
+ var _chunkILJLXJ5Ocjs = require('./chunk-ILJLXJ5O.cjs');
37
37
 
38
38
 
39
- var _chunk2H3KVSFAcjs = require('./chunk-2H3KVSFA.cjs');
39
+ var _chunkOJBZ7UUCcjs = require('./chunk-OJBZ7UUC.cjs');
40
40
 
41
41
 
42
- var _chunkILJLXJ5Ocjs = require('./chunk-ILJLXJ5O.cjs');
42
+ var _chunkJR6ZN6QDcjs = require('./chunk-JR6ZN6QD.cjs');
43
43
 
44
44
 
45
45
  var _chunkPW7RP73Jcjs = require('./chunk-PW7RP73J.cjs');
@@ -67,14 +67,14 @@ _chunk75ZPJI57cjs.__export.call(void 0, string_exports, {
67
67
  getStartSame: () => _chunkJ7S3KBHLcjs.getStartSame,
68
68
  getStartSames: () => _chunkPW7RP73Jcjs.getStartSames,
69
69
  getStrNunPosit: () => _chunk2H3KVSFAcjs.getStrNunPosit,
70
- serializeCodeBlock: () => _chunkETMYYVQJcjs.serializeCodeBlock,
71
- serializeFunctionRef: () => _chunkETMYYVQJcjs.serializeFunctionRef,
70
+ serializeCodeBlock: () => _chunkL6JTKQ65cjs.serializeCodeBlock,
71
+ serializeFunctionRef: () => _chunkL6JTKQ65cjs.serializeFunctionRef,
72
72
  splitUpper: () => _chunkL7FSHU27cjs.splitUpper,
73
73
  startsNum: () => _chunkILJLXJ5Ocjs.startsNum,
74
74
  toFunction: () => _chunkQXK4IUBIcjs.toFunction,
75
75
  toJSONParse: () => _chunkJHV27P2Qcjs.toJSONParse,
76
- toJSONSParse: () => _chunkETMYYVQJcjs.toJSONSParse,
77
- toJSONSStringify: () => _chunkETMYYVQJcjs.toJSONSStringify,
76
+ toJSONSParse: () => _chunkL6JTKQ65cjs.toJSONSParse,
77
+ toJSONSStringify: () => _chunkL6JTKQ65cjs.toJSONSStringify,
78
78
  toJSONStringify: () => _chunkJHV27P2Qcjs.toJSONStringify,
79
79
  toStringParse: () => _chunkXG44HG5Ncjs.toStringParse
80
80
  });
@@ -0,0 +1,31 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// packages/date/hourFormat.ts
2
+ var hourFormat = (milliseconds, filled, milli) => {
3
+ let totalSeconds = milliseconds;
4
+ if (milli) {
5
+ totalSeconds = milliseconds / 1e3;
6
+ }
7
+ let hours = Math.floor(totalSeconds / 3600);
8
+ totalSeconds %= 3600;
9
+ let minutes = Math.floor(totalSeconds / 60);
10
+ totalSeconds %= 60;
11
+ let seconds = Math.floor(totalSeconds);
12
+ if (hours == 0) {
13
+ return `${getFilled(minutes, filled)}:${getFilled(seconds, true)}`;
14
+ } else {
15
+ return `${getFilled(hours, filled)}:${getFilled(
16
+ minutes,
17
+ true
18
+ )}:${getFilled(seconds, true)}`;
19
+ }
20
+ };
21
+ function getFilled(hours, is) {
22
+ if (is) {
23
+ return hours.toString().padStart(2, "0");
24
+ } else {
25
+ return hours;
26
+ }
27
+ }
28
+
29
+
30
+
31
+ exports.hourFormat = hourFormat;
@@ -0,0 +1,249 @@
1
+ // packages/window/download.ts
2
+ function downloadText(text, filename = "text.txt", mimeType = "text/plain") {
3
+ downloadFile(text, { filename, mimeType });
4
+ }
5
+ function downloadJSON(jsonData, filename = "data.json") {
6
+ downloadFile(jsonData, {
7
+ filename,
8
+ mimeType: "application/json"
9
+ });
10
+ }
11
+ function downloadCSV(data, filename = "data.csv", headers) {
12
+ let csvContent = "";
13
+ if (Array.isArray(data) && data.length > 0) {
14
+ if (Array.isArray(data[0])) {
15
+ const rows = data;
16
+ csvContent = rows.map(
17
+ (row) => row.map((cell) => `"${String(cell).replace(/"/g, '""')}"`).join(",")
18
+ ).join("\n");
19
+ } else if (typeof data[0] === "object") {
20
+ const objects = data;
21
+ const objectHeaders = headers || Object.keys(objects[0]);
22
+ csvContent = objectHeaders.map((header) => `"${String(header).replace(/"/g, '""')}"`).join(",") + "\n";
23
+ csvContent += objects.map(
24
+ (obj) => objectHeaders.map(
25
+ (header) => `"${String(obj[header] || "").replace(
26
+ /"/g,
27
+ '""'
28
+ )}"`
29
+ ).join(",")
30
+ ).join("\n");
31
+ }
32
+ }
33
+ downloadFile(csvContent, {
34
+ filename,
35
+ mimeType: "text/csv"
36
+ });
37
+ }
38
+ async function downloadViaAJAX(url, filename, options = {}) {
39
+ try {
40
+ const response = await fetch(url, {
41
+ method: "GET",
42
+ ...options
43
+ });
44
+ if (!response.ok) {
45
+ throw new Error(`HTTP error! status: ${response.status}`);
46
+ }
47
+ const blob = await response.blob();
48
+ let finalFilename = filename;
49
+ if (!finalFilename) {
50
+ const contentDisposition = response.headers.get(
51
+ "content-disposition"
52
+ );
53
+ if (contentDisposition) {
54
+ const filenameMatch = contentDisposition.match(/filename="?(.+?)"?$/);
55
+ if (filenameMatch) {
56
+ finalFilename = filenameMatch[1];
57
+ }
58
+ }
59
+ }
60
+ downloadFile(blob, { filename: finalFilename });
61
+ } catch (error) {
62
+ console.error("AJAX \u4E0B\u8F7D\u5931\u8D25:", error);
63
+ throw new Error(
64
+ `\u4E0B\u8F7D\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`
65
+ );
66
+ }
67
+ }
68
+ function downloadFile(data, options = {}) {
69
+ const {
70
+ filename = "download",
71
+ mimeType = "application/octet-stream",
72
+ charset = "utf-8",
73
+ bom = ""
74
+ } = options;
75
+ try {
76
+ let blobData;
77
+ if (data instanceof Blob) {
78
+ blobData = data;
79
+ } else if (typeof data === "object") {
80
+ const jsonString = JSON.stringify(data, null, 2);
81
+ blobData = bom + jsonString;
82
+ } else if (typeof data === "string") {
83
+ blobData = bom + data;
84
+ } else {
85
+ blobData = data;
86
+ }
87
+ const blob = new Blob([blobData], {
88
+ type: `${mimeType};charset=${charset}`
89
+ });
90
+ const blobUrl = URL.createObjectURL(blob);
91
+ const link = document.createElement("a");
92
+ link.href = blobUrl;
93
+ link.download = filename;
94
+ link.style.display = "none";
95
+ document.body.appendChild(link);
96
+ link.click();
97
+ setTimeout(() => {
98
+ document.body.removeChild(link);
99
+ URL.revokeObjectURL(blobUrl);
100
+ }, 100);
101
+ } catch (error) {
102
+ console.error("\u6587\u4EF6\u4E0B\u8F7D\u5931\u8D25:", error);
103
+ throw new Error(
104
+ `\u4E0B\u8F7D\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`
105
+ );
106
+ }
107
+ }
108
+ async function downloadFromURL(url, filename, options = {}) {
109
+ const {
110
+ useBlob = true,
111
+ // 默认使用 Blob 方式,确保能触发下载
112
+ headers = {},
113
+ timeout = 3e4
114
+ } = options;
115
+ try {
116
+ if (!useBlob) {
117
+ const link = document.createElement("a");
118
+ link.href = url;
119
+ link.download = filename || getFilenameFromURL(url);
120
+ link.style.display = "none";
121
+ link.target = "_blank";
122
+ document.body.appendChild(link);
123
+ link.click();
124
+ setTimeout(() => {
125
+ document.body.removeChild(link);
126
+ }, 100);
127
+ return;
128
+ }
129
+ const controller = new AbortController();
130
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
131
+ const response = await fetch(url, {
132
+ method: "GET",
133
+ headers,
134
+ signal: controller.signal
135
+ });
136
+ clearTimeout(timeoutId);
137
+ if (!response.ok) {
138
+ throw new Error(`HTTP error! status: ${response.status}`);
139
+ }
140
+ const blob = await response.blob();
141
+ let finalFilename = filename;
142
+ if (!finalFilename) {
143
+ const contentDisposition = response.headers.get(
144
+ "content-disposition"
145
+ );
146
+ if (contentDisposition) {
147
+ const filenameMatch = contentDisposition.match(
148
+ /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
149
+ );
150
+ if (filenameMatch && filenameMatch[1]) {
151
+ finalFilename = filenameMatch[1].replace(/['"]/g, "");
152
+ }
153
+ }
154
+ if (!finalFilename) {
155
+ finalFilename = getFilenameFromURL(url);
156
+ }
157
+ }
158
+ const mimeType = getMimeTypeFromBlob(blob, url);
159
+ downloadFile(blob, {
160
+ filename: finalFilename,
161
+ mimeType
162
+ });
163
+ } catch (error) {
164
+ console.error("URL \u4E0B\u8F7D\u5931\u8D25:", error);
165
+ if (error instanceof Error && error.name === "AbortError") {
166
+ throw new Error("\u4E0B\u8F7D\u8D85\u65F6\uFF0C\u8BF7\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\u6216\u6587\u4EF6\u5927\u5C0F");
167
+ }
168
+ if (useBlob) {
169
+ console.log("Blob \u65B9\u5F0F\u5931\u8D25\uFF0C\u5C1D\u8BD5\u76F4\u63A5\u4E0B\u8F7D...");
170
+ await downloadFromURL(url, filename, {
171
+ ...options,
172
+ useBlob: false
173
+ });
174
+ } else {
175
+ throw new Error(
176
+ `\u4E0B\u8F7D\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`
177
+ );
178
+ }
179
+ }
180
+ }
181
+ async function downloadImage(imageUrl, filename) {
182
+ await downloadFromURL(imageUrl, filename, {
183
+ useBlob: true,
184
+ headers: {
185
+ Accept: "image/*"
186
+ }
187
+ });
188
+ }
189
+ function getFilenameFromURL(url) {
190
+ try {
191
+ const urlObj = new URL(url);
192
+ const pathname = urlObj.pathname;
193
+ const filename = pathname.substring(pathname.lastIndexOf("/") + 1);
194
+ if (!filename || filename === "/" || !filename.includes(".")) {
195
+ const extension = getFileExtensionFromURL(url) || "bin";
196
+ return `download_${Date.now()}.${extension}`;
197
+ }
198
+ return decodeURIComponent(filename);
199
+ } catch {
200
+ const extension = getFileExtensionFromURL(url) || "bin";
201
+ return `download_${Date.now()}.${extension}`;
202
+ }
203
+ }
204
+ function getFileExtensionFromURL(url) {
205
+ const match = url.match(/\.([a-zA-Z0-9]+)(?:[?#]|$)/);
206
+ return match ? match[1].toLowerCase() : null;
207
+ }
208
+ function getMimeTypeFromBlob(blob, url) {
209
+ if (blob.type && blob.type !== "application/octet-stream") {
210
+ return blob.type;
211
+ }
212
+ const extension = getFileExtensionFromURL(url);
213
+ const mimeTypes = {
214
+ jpg: "image/jpeg",
215
+ jpeg: "image/jpeg",
216
+ png: "image/png",
217
+ gif: "image/gif",
218
+ webp: "image/webp",
219
+ svg: "image/svg+xml",
220
+ pdf: "application/pdf",
221
+ txt: "text/plain",
222
+ json: "application/json",
223
+ csv: "text/csv",
224
+ xml: "application/xml",
225
+ zip: "application/zip",
226
+ mp3: "audio/mpeg",
227
+ mp4: "video/mp4",
228
+ avi: "video/x-msvideo"
229
+ };
230
+ return extension && mimeTypes[extension] ? mimeTypes[extension] : "application/octet-stream";
231
+ }
232
+ async function downloadMultipleFiles(downloads) {
233
+ return Promise.all(
234
+ downloads.map(
235
+ (download) => downloadFromURL(download.url, download.filename)
236
+ )
237
+ );
238
+ }
239
+
240
+ export {
241
+ downloadText,
242
+ downloadJSON,
243
+ downloadCSV,
244
+ downloadViaAJAX,
245
+ downloadFile,
246
+ downloadFromURL,
247
+ downloadImage,
248
+ downloadMultipleFiles
249
+ };
@@ -1,9 +1,9 @@
1
- import {
2
- string_exports
3
- } from "./chunk-HKUOF43F.js";
4
1
  import {
5
2
  object_exports
6
- } from "./chunk-IEQHKLEL.js";
3
+ } from "./chunk-IPNGPIGK.js";
4
+ import {
5
+ string_exports
6
+ } from "./chunk-PKKBS6BE.js";
7
7
  import {
8
8
  array_exports
9
9
  } from "./chunk-HAXXG6HP.js";