xianniu-ui 0.8.16 → 0.8.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xianniu-ui",
3
- "version": "0.8.16",
3
+ "version": "0.8.17",
4
4
  "private": false,
5
5
  "main": "lib/xianniu-ui.umd.min.js",
6
6
  "scripts": {
@@ -58,14 +58,13 @@ const deepClone = (source) => {
58
58
  * @param {object} params
59
59
  * @param {string} name 文件名
60
60
  * @param {string} url 文件地址
61
+ * @returns
61
62
  */
62
63
  const download = (params = { name: '', url: '' }) => {
63
- const defaultParams = {
64
- url: '',
65
- name: '下载模板'
66
- }
67
- const _params = Object.assign(defaultParams, params)
68
- const { url, name } = _params
64
+ if(!params.url) return
65
+
66
+ let { url, name } = params
67
+ name = name ? name : getFileNameFromUrl(params.url)
69
68
  const x = new XMLHttpRequest();
70
69
  x.open("GET", url, true);
71
70
  x.responseType = "blob";
@@ -82,6 +81,23 @@ const download = (params = { name: '', url: '' }) => {
82
81
  };
83
82
  x.send();
84
83
  }
84
+
85
+ /**
86
+ * 从url中获取文件名
87
+ * @param {string} url
88
+ * @returns {string} filename
89
+ * @example
90
+ * getFileNameFromUrl('http://www.baidu.com/abc/def/123.jpg') // 123.jpg
91
+ * getFileNameFromUrl('http://www.baidu.com/abc/def/') // def
92
+ */
93
+ const getFileNameFromUrl = function(url){
94
+ var parsedUrl = new URL(url);
95
+ var pathname = parsedUrl.pathname;
96
+ var pathComponents = pathname.split('/');
97
+ var filename = pathComponents[pathComponents.length - 1];
98
+ return decodeURIComponent(filename);
99
+ }
100
+
85
101
  /**
86
102
  * 根据某个key 对数组去重合并
87
103
  * @param {array} arr 需要合并的数组