@vuetkit/shared 0.0.4 → 0.0.5

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.
package/README.md CHANGED
@@ -13,3 +13,10 @@ Collection of business development utilities for Vue3 projects.
13
13
  - [realObj](/shared/src/dataType/realObj/)
14
14
  - [isFunc](/shared/src/dataType/isFunc/)
15
15
  - [isArr](/shared/src/dataType/isArr/)
16
+
17
+ ### File
18
+
19
+ - [downloadFile](/shared/src/file/downloadFile/)
20
+ - [getFileExt](/shared/src/file/getFileExt/)
21
+ - [getFileMediaTypeByExt](/shared/src/file/getFileMediaTypeByExt/)
22
+ - [getFileMediaTypes](/shared/src/file/getFileMediaTypes/)
package/dist/index.d.ts CHANGED
@@ -13,4 +13,29 @@ declare function isObj<T>(obj: T): boolean;
13
13
  //#region src/dataType/realObj/index.d.ts
14
14
  declare function realObj<T>(obj: T): boolean;
15
15
  //#endregion
16
- export { getDataType, isArr, isFunc, isObj, realObj };
16
+ //#region src/file/downloadFile/index.d.ts
17
+ declare function downloadFile(blob: Blob, fileName: string): void;
18
+ //#endregion
19
+ //#region src/file/getFileExt/index.d.ts
20
+ declare function getFileExt(fileName: string): string;
21
+ //#endregion
22
+ //#region src/file/getFileMediaTypeByExt/index.d.ts
23
+ declare function getFileMediaTypeByExt(ext: string): string;
24
+ //#endregion
25
+ //#region src/file/getFileMediaTypes/index.d.ts
26
+ declare function getFileMediaTypes(): {
27
+ doc: string;
28
+ docx: string;
29
+ ppt: string;
30
+ pptx: string;
31
+ xls: string;
32
+ xlsx: string;
33
+ pdf: string;
34
+ png: string;
35
+ jpg: string;
36
+ jpeg: string;
37
+ gif: string;
38
+ webp: string;
39
+ };
40
+ //#endregion
41
+ export { downloadFile, getDataType, getFileExt, getFileMediaTypeByExt, getFileMediaTypes, isArr, isFunc, isObj, realObj };
package/dist/index.js CHANGED
@@ -26,4 +26,57 @@ function realObj(obj) {
26
26
  return getDataType(obj) === "[object Object]";
27
27
  }
28
28
  //#endregion
29
- export { getDataType, isArr, isFunc, isObj, realObj };
29
+ //#region src/file/getFileExt/index.ts
30
+ function getFileExt(fileName) {
31
+ var _fileName$split;
32
+ return ((_fileName$split = fileName.split(".")) === null || _fileName$split === void 0 || (_fileName$split = _fileName$split.pop()) === null || _fileName$split === void 0 ? void 0 : _fileName$split.toLowerCase()) || "";
33
+ }
34
+ //#endregion
35
+ //#region src/file/getFileMediaTypes/index.ts
36
+ const mediaTypes = {
37
+ doc: "application/msword",
38
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
39
+ ppt: "application/vnd.ms-powerpoint",
40
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
41
+ xls: "application/vnd.ms-excel",
42
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
43
+ pdf: "application/pdf",
44
+ png: "image/png",
45
+ jpg: "image/jpeg",
46
+ jpeg: "image/jpeg",
47
+ gif: "image/gif",
48
+ webp: "image/webp"
49
+ };
50
+ function getFileMediaTypes() {
51
+ return mediaTypes;
52
+ }
53
+ //#endregion
54
+ //#region src/file/getFileMediaTypeByExt/index.ts
55
+ function getFileMediaTypeByExt(ext) {
56
+ return getFileMediaTypes()[ext] || "";
57
+ }
58
+ //#endregion
59
+ //#region src/file/downloadFile/index.ts
60
+ function downloadFile(blob, fileName) {
61
+ if (!blob || !fileName || blob.size === 0) return;
62
+ let elink = document.createElement("a");
63
+ elink.download = fileName;
64
+ let curMimeType = blob.type;
65
+ if (!curMimeType) curMimeType = getFileMediaTypeByExt(getFileExt(fileName).slice(1));
66
+ const blobToDownload = curMimeType && curMimeType !== blob.type ? new Blob([blob], { type: curMimeType }) : blob;
67
+ const url = URL.createObjectURL(blobToDownload);
68
+ try {
69
+ elink.href = url;
70
+ elink.click();
71
+ } catch (error) {
72
+ console.error("Download file failed:", error);
73
+ } finally {
74
+ URL.revokeObjectURL(url);
75
+ if (elink) {
76
+ document.body.removeChild(elink);
77
+ elink = null;
78
+ }
79
+ }
80
+ }
81
+ //#endregion
82
+ export { downloadFile, getDataType, getFileExt, getFileMediaTypeByExt, getFileMediaTypes, isArr, isFunc, isObj, realObj };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vuetkit/shared",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.5",
5
5
  "description": "Collection of business development utilities for Vue3 projects",
6
6
  "author": "Kalu5",
7
7
  "license": "MIT",