@d-matrix/utils 1.4.0 → 1.5.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.
package/dist/file.d.ts CHANGED
@@ -27,4 +27,13 @@ export declare const validateImageSize: (file: BlobPart | FileURL, limitSize: {
27
27
  * @returns Promise<boolean>
28
28
  */
29
29
  export declare function isImageExists(src: string, img?: HTMLImageElement): Promise<boolean>;
30
+ /**
31
+ * 从Content-Disposition中获取文件名
32
+ * @param header 包含Content-Disposition, 值为'attachment;filename=%E5%A4%A7%E8%A1%8C%E6%8C%87%E5%AF%BC2024-06-27-2024-06-28.xlsx'
33
+ * @returns string
34
+ *
35
+ */
36
+ export declare function getFilenameFromContentDispositionHeader(header: {
37
+ ['content-disposition']: string;
38
+ }): string;
30
39
  export {};
package/dist/file.js CHANGED
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.isImageExists = exports.validateImageSize = exports.toImage = void 0;
39
+ exports.getFilenameFromContentDispositionHeader = exports.isImageExists = exports.validateImageSize = exports.toImage = void 0;
40
40
  /**
41
41
  * 转换BlobPart或者文件地址为图片对象
42
42
  * @param file 传入文件 或者 url
@@ -108,3 +108,17 @@ function isImageExists(src, img) {
108
108
  });
109
109
  }
110
110
  exports.isImageExists = isImageExists;
111
+ /**
112
+ * 从Content-Disposition中获取文件名
113
+ * @param header 包含Content-Disposition, 值为'attachment;filename=%E5%A4%A7%E8%A1%8C%E6%8C%87%E5%AF%BC2024-06-27-2024-06-28.xlsx'
114
+ * @returns string
115
+ *
116
+ */
117
+ function getFilenameFromContentDispositionHeader(header) {
118
+ var contentDisposition = header['content-disposition'];
119
+ if (!contentDisposition)
120
+ return '';
121
+ var filename = contentDisposition.split('filename=')[1].split(';')[0];
122
+ return decodeURIComponent(filename);
123
+ }
124
+ exports.getFilenameFromContentDispositionHeader = getFilenameFromContentDispositionHeader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "A dozen of utils for Front-End Development",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -9,7 +9,8 @@
9
9
  "clean": "rimraf dist",
10
10
  "cy:open": "cypress open",
11
11
  "cy:run": "cypress run",
12
- "test:tsd": "tsd"
12
+ "test:tsd": "tsd",
13
+ "postversion": "git push && git push --tags"
13
14
  },
14
15
  "engines": {
15
16
  "node": ">=16.20.0"
package/readme.md CHANGED
@@ -137,6 +137,20 @@ const $img = document.getElementById('img');
137
137
  const res = await file.isImageExists(url, $img);
138
138
  ```
139
139
 
140
+ - `getFilenameFromContentDispositionHeader(header: { ['content-disposition']: string }): string`
141
+
142
+ 从`Content-Disposition` response header中获取`filename`
143
+
144
+ ```ts
145
+ import { file } from '@d-matrix/utils';
146
+
147
+ const header = {
148
+ 'content-disposition': 'attachment;filename=%E5%A4%A7%E8%A1%8C%E6%8C%87%E5%AF%BC2024-06-27-2024-06-28.xlsx'
149
+ };
150
+ const filename = file.getFilenameFromContentDispositionHeader(header);
151
+ // '大行指导2024-06-27-2024-06-28.xlsx'
152
+ ```
153
+
140
154
  ## support
141
155
 
142
156
  - `isBrowserEnv(): boolean`