util-helpers 4.15.2 → 4.16.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.
Files changed (74) hide show
  1. package/README.md +6 -5
  2. package/dist/util-helpers.js +887 -479
  3. package/dist/util-helpers.js.map +1 -1
  4. package/dist/util-helpers.min.js +1 -1
  5. package/dist/util-helpers.min.js.map +1 -1
  6. package/esm/ajax.js +149 -0
  7. package/esm/blobToDataURL.js +8 -12
  8. package/esm/dataURLToBlob.js +6 -5
  9. package/esm/download.js +156 -0
  10. package/esm/fileReader.js +67 -0
  11. package/esm/filterTree.js +1 -1
  12. package/esm/findTreeNode.js +1 -1
  13. package/esm/findTreeNodes.js +1 -1
  14. package/esm/findTreeSelect.js +1 -1
  15. package/esm/index.js +15 -5
  16. package/esm/interface.doc.js +124 -0
  17. package/esm/{transformFieldNames.type.js → interface.type.js} +2 -4
  18. package/esm/listToTree.js +1 -1
  19. package/esm/numberToChinese.js +3 -2
  20. package/esm/parseIdCard.js +1 -1
  21. package/esm/replaceChar.js +11 -7
  22. package/esm/safeDate.js +26 -9
  23. package/esm/transformFieldNames.js +2 -3
  24. package/esm/treeToList.js +1 -1
  25. package/esm/utils/config.js +1 -1
  26. package/esm/utils/type/index.js +3 -1
  27. package/esm/utils/type/isArrayBuffer.js +25 -0
  28. package/esm/utils/type/isBlob.js +27 -0
  29. package/lib/ajax.js +156 -0
  30. package/lib/blobToDataURL.js +8 -12
  31. package/lib/dataURLToBlob.js +6 -5
  32. package/lib/download.js +161 -0
  33. package/lib/fileReader.js +74 -0
  34. package/lib/filterTree.js +1 -1
  35. package/lib/findTreeNode.js +1 -1
  36. package/lib/findTreeNodes.js +1 -1
  37. package/lib/findTreeSelect.js +1 -1
  38. package/lib/index.js +26 -5
  39. package/lib/interface.doc.js +126 -0
  40. package/lib/listToTree.js +1 -1
  41. package/lib/numberToChinese.js +3 -2
  42. package/lib/parseIdCard.js +1 -1
  43. package/lib/replaceChar.js +11 -7
  44. package/lib/safeDate.js +27 -10
  45. package/lib/transformFieldNames.js +1 -1
  46. package/lib/treeToList.js +1 -1
  47. package/lib/utils/config.js +1 -1
  48. package/lib/utils/type/index.js +14 -0
  49. package/lib/utils/type/isArrayBuffer.js +32 -0
  50. package/lib/utils/type/isBlob.js +34 -0
  51. package/package.json +2 -2
  52. package/types/ajax.d.ts +121 -0
  53. package/types/blobToDataURL.d.ts +5 -1
  54. package/types/download.d.ts +77 -0
  55. package/types/fileReader.d.ts +3 -0
  56. package/types/filterTree.d.ts +1 -1
  57. package/types/findTreeNode.d.ts +1 -1
  58. package/types/findTreeNodes.d.ts +1 -1
  59. package/types/findTreeSelect.d.ts +1 -1
  60. package/types/index.d.ts +8 -5
  61. package/types/{transformFieldNames.type.d.ts → interface.type.d.ts} +1 -1
  62. package/types/listToTree.d.ts +1 -1
  63. package/types/numberToChinese.d.ts +3 -2
  64. package/types/parseIdCard.d.ts +2 -2
  65. package/types/replaceChar.d.ts +11 -7
  66. package/types/safeDate.d.ts +3 -23
  67. package/types/transformFieldNames.d.ts +2 -2
  68. package/types/treeToList.d.ts +1 -1
  69. package/types/utils/type/index.d.ts +21 -19
  70. package/types/utils/type/isArrayBuffer.d.ts +21 -0
  71. package/types/utils/type/isBlob.d.ts +23 -0
  72. package/esm/transformFieldNames.doc.js +0 -35
  73. package/lib/transformFieldNames.doc.js +0 -42
  74. /package/lib/{transformFieldNames.type.js → interface.type.js} +0 -0
package/esm/ajax.js ADDED
@@ -0,0 +1,149 @@
1
+ import _typeof from "@babel/runtime/helpers/typeof";
2
+ /**
3
+ * @typedef {XMLHttpRequest['onloadstart']} XMLHttpRequestEvent XMLHttpRequest 事件对象
4
+ */
5
+
6
+ /**
7
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
8
+ * @typedef {Object} AjaxOptions ajax配置项
9
+ * @property {string} [method="get"] 创建请求时使用的方法
10
+ * @property {Document | XMLHttpRequestBodyInit | null} [data=null] 请求体被发送的数据
11
+ * @property {Object.<string, string>} [headers] 自定义请求头
12
+ * @property {XMLHttpRequestResponseType} [responseType] 响应类型
13
+ * @property {number} [timeout] 请求超时的毫秒数
14
+ * @property {boolean} [withCredentials=false] 跨域请求时是否需要使用凭证
15
+ * @property {boolean} [async=true] 是否异步执行操作
16
+ * @property {string|null} [user=null] 用户名,用于认证用途
17
+ * @property {string|null} [password=null] 密码,用于认证用途
18
+ * @property {XMLHttpRequestEvent} [onLoadStart] 接收到响应数据时触发
19
+ * @property {XMLHttpRequestEvent} [onProgress] 请求接收到更多数据时,周期性地触发
20
+ * @property {XMLHttpRequestEvent} [onAbort] 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时
21
+ * @property {XMLHttpRequestEvent} [onTimeout] 在预设时间内没有接收到响应时触发
22
+ * @property {XMLHttpRequestEvent} [onError] 当 request 遭遇错误时触发
23
+ * @property {XMLHttpRequestEvent} [onLoad] 请求成功完成时触发
24
+ * @property {XMLHttpRequestEvent} [onLoadEnd] 请求结束时触发,无论请求成功 (load) 还是失败 (abort 或 error)
25
+ */
26
+
27
+ /**
28
+ * 请求<br/><br/>
29
+ *
30
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
31
+ *
32
+ * @static
33
+ * @alias module:Other.ajax
34
+ * @since 4.16.0
35
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
36
+ * @param {string} url 地址
37
+ * @param {AjaxOptions} [options] 配置项
38
+ * @returns {Promise<ProgressEvent<EventTarget>>}
39
+ * @example
40
+ * ajax('/somefile').then(res=>{
41
+ * // do something
42
+ * });
43
+ *
44
+ * ajax('/api', { method: 'post' }).then(res=>{
45
+ * // do something
46
+ * });
47
+ */
48
+ function ajax(url, options) {
49
+ var _ref = options || {},
50
+ _ref$method = _ref.method,
51
+ method = _ref$method === void 0 ? 'get' : _ref$method,
52
+ _ref$data = _ref.data,
53
+ data = _ref$data === void 0 ? null : _ref$data,
54
+ timeout = _ref.timeout,
55
+ headers = _ref.headers,
56
+ _ref$withCredentials = _ref.withCredentials,
57
+ withCredentials = _ref$withCredentials === void 0 ? false : _ref$withCredentials,
58
+ _ref$async = _ref.async,
59
+ async = _ref$async === void 0 ? true : _ref$async,
60
+ _ref$user = _ref.user,
61
+ user = _ref$user === void 0 ? null : _ref$user,
62
+ _ref$password = _ref.password,
63
+ password = _ref$password === void 0 ? null : _ref$password,
64
+ responseType = _ref.responseType,
65
+ onAbort = _ref.onAbort,
66
+ onError = _ref.onError,
67
+ onLoad = _ref.onLoad,
68
+ onLoadEnd = _ref.onLoadEnd,
69
+ onLoadStart = _ref.onLoadStart,
70
+ onProgress = _ref.onProgress,
71
+ onTimeout = _ref.onTimeout;
72
+ return new Promise(function (resolve, reject) {
73
+ var xhr = new XMLHttpRequest();
74
+ xhr.open(method.toLowerCase(), url, async, user, password);
75
+
76
+ // 设置请求超时
77
+ if (typeof timeout === 'number' && timeout > 0) {
78
+ xhr.timeout = timeout;
79
+ }
80
+
81
+ // 跨域请求时是否需要使用凭证
82
+ xhr.withCredentials = withCredentials;
83
+
84
+ // 设置响应类型
85
+ if (responseType) {
86
+ xhr.responseType = responseType;
87
+ }
88
+
89
+ // 设置请求头
90
+ if (_typeof(headers) === 'object') {
91
+ Object.keys(headers).map(function (item) {
92
+ xhr.setRequestHeader(item, headers[item]);
93
+ });
94
+ }
95
+
96
+ /**
97
+ * 请求成功异步调用
98
+ * @param {XMLHttpRequestEvent} [cb] 回调方法
99
+ */
100
+ var wrapSuccess = function wrapSuccess(cb) {
101
+ /**
102
+ * 内部方法
103
+ * @param {ProgressEvent<EventTarget>} e 事件对象
104
+ */
105
+ return function (e) {
106
+ resolve(e);
107
+ cb === null || cb === void 0 ? void 0 : cb.call(xhr, e);
108
+ };
109
+ };
110
+
111
+ /**
112
+ * 请求失败(中断/超时/失败)处理
113
+ * @param {XMLHttpRequestEvent} [cb] 回调方法
114
+ */
115
+ var wrapError = function wrapError(cb) {
116
+ /**
117
+ * 内部方法
118
+ * @param {ProgressEvent<EventTarget>} e 事件对象
119
+ */
120
+ return function (e) {
121
+ reject(e);
122
+ cb === null || cb === void 0 ? void 0 : cb.call(xhr, e);
123
+ };
124
+ };
125
+
126
+ // 事件处理
127
+ /**@type {Object.<keyof XMLHttpRequestEventTargetEventMap, XMLHttpRequestEvent | undefined>} */
128
+ var events = {
129
+ loadstart: onLoadStart,
130
+ progress: onProgress,
131
+ abort: wrapError(onAbort),
132
+ timeout: wrapError(onTimeout),
133
+ error: wrapError(onError),
134
+ load: wrapSuccess(onLoad),
135
+ loadend: onLoadEnd
136
+ };
137
+ /**@type {(keyof XMLHttpRequestEventTargetEventMap)[]} */
138
+ // @ts-ignore
139
+ var eventNames = Object.keys(events);
140
+ eventNames.map(function (item) {
141
+ var func = events[item];
142
+ if (func) {
143
+ xhr.addEventListener(item, func);
144
+ }
145
+ });
146
+ xhr.send(data);
147
+ });
148
+ }
149
+ export default ajax;
@@ -2,13 +2,19 @@
2
2
  // 方法1:将file或者blob类型文件转成base64数据,再作为src赋值给img标签
3
3
  // 方法2:使用 window.URL.createObjectURL(blob) 为blob、file 创建一个指向该参数对象的URL
4
4
 
5
+ import fileReader from "./fileReader";
6
+
5
7
  /**
6
- * 将 Blob 或 File 对象转成 data:URL 格式的 Base64 字符串。
8
+ * 将 Blob 或 File 对象转成 data:URL 格式的 Base64 字符串<br/><br/>
9
+ *
10
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
7
11
  *
8
12
  * @static
9
13
  * @alias module:Processor.blobToDataURL
10
14
  * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader/readAsDataURL|FileReader.readAsDataURL()}
11
15
  * @since 4.1.0
16
+ * @ignore
17
+ * @deprecated 请使用 `fileReader` 方法
12
18
  * @param {Blob} blob Blob 或 File 对象
13
19
  * @returns {Promise<string>} data:URL 格式的 Base64 字符串。
14
20
  * @example
@@ -26,16 +32,6 @@
26
32
  * });
27
33
  */
28
34
  function blobToDataURL(blob) {
29
- return new Promise(function (resolve, reject) {
30
- var reader = new FileReader();
31
- reader.readAsDataURL(blob);
32
- // @ts-ignore
33
- reader.onload = function () {
34
- return resolve(reader.result);
35
- };
36
- reader.onerror = function (error) {
37
- return reject(error);
38
- };
39
- });
35
+ return fileReader(blob);
40
36
  }
41
37
  export default blobToDataURL;
@@ -12,17 +12,18 @@
12
12
  * dataURLToBlob(dataurl); // Blob {size: 32, type: 'text/html'}
13
13
  */
14
14
  function dataURLToBlob(dataurl) {
15
- var arr = dataurl.split(',');
16
- // @ts-ignore
17
- var mime = arr[0].match(/:(.*?);/)[1];
18
- var bstr = atob(arr[1]);
15
+ var parts = dataurl.split(',');
16
+ var meta = parts[0].substring(5).split(';');
17
+ var type = meta[0];
18
+ var decoder = meta.indexOf('base64') !== -1 ? atob : decodeURIComponent;
19
+ var bstr = decoder(parts[1]);
19
20
  var n = bstr.length;
20
21
  var u8arr = new Uint8Array(n);
21
22
  while (n--) {
22
23
  u8arr[n] = bstr.charCodeAt(n);
23
24
  }
24
25
  return new Blob([u8arr], {
25
- type: mime
26
+ type: type
26
27
  });
27
28
  }
28
29
  export default dataURLToBlob;
@@ -0,0 +1,156 @@
1
+ import _typeof from "@babel/runtime/helpers/typeof";
2
+ // 如果修改文档,请同步修改 interface.doc.js
3
+
4
+ import dataURLToBlob from "./dataURLToBlob";
5
+ import isUrl from "./isUrl";
6
+ import ajax from "./ajax";
7
+ import { isBlob } from "./utils/type";
8
+
9
+ /**
10
+ * 下载文件
11
+ *
12
+ * @param {string} blobUrl blob 地址
13
+ * @param {string} [fileName] 文件名称
14
+ */
15
+ function saver(blobUrl, fileName) {
16
+ var anchor = document.createElement('a');
17
+ // anchor.href = decodeURIComponent(blobUrl);
18
+ anchor.href = blobUrl;
19
+ anchor.style.display = 'none';
20
+ anchor.setAttribute('download', fileName || '');
21
+
22
+ /**
23
+ * 处理点击事件,防止事件冒泡到 body/html 的点击事件。
24
+ *
25
+ * @param {MouseEvent} e 鼠标事件对象
26
+ */
27
+ function handleClick(e) {
28
+ e.stopPropagation();
29
+ anchor.removeEventListener('click', handleClick);
30
+ }
31
+ anchor.addEventListener('click', handleClick);
32
+ document.body.appendChild(anchor);
33
+ anchor.click();
34
+ document.body.removeChild(anchor);
35
+ }
36
+
37
+ /**
38
+ * @typedef {import('./ajax.js').AjaxOptions} AjaxOptions ajax 配置项
39
+ */
40
+
41
+ /**
42
+ * @callback TransformRequest
43
+ * @param {AjaxOptions} options ajax 配置项
44
+ * @returns {AjaxOptions}
45
+ */
46
+
47
+ /**
48
+ * @callback TransformResponse
49
+ * @param {Blob} res 响应的Blob对象。如果你通过 transformRequest 修改了 responseType ,该参数将是该类型响应值。
50
+ * @returns {Blob}
51
+ */
52
+
53
+ /**
54
+ * @typedef {Object} DownloadOptions 下载配置项
55
+ * @property {string} [options.fileName] 文件名称
56
+ * @property {string} [options.type] MIME 类型
57
+ * @property {'url'|'text'} [options.dataType] 手动设置数据类型,默认会根据传入的数据判断类型,主要是为了区分 url 和 text 。<br/>如果你要下载的文本是 url ,请设置 'text' ;如果你要下载的 url 是绝对/相对路径,请设置 'url' 。
58
+ * @property {TransformRequest} [options.transformRequest] 请求前触发,XHR 对象或配置调整
59
+ * @property {TransformResponse} [options.transformResponse] 请求成功后触发,在传递给 then/catch 前,允许修改响应数据
60
+ */
61
+
62
+ /**
63
+ * 下载<br/><br/>
64
+ *
65
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。</em>
66
+ *
67
+ * @static
68
+ * @alias module:Other.download
69
+ * @since 4.16.0
70
+ * @see {@link https://zh.wikipedia.org/wiki/多用途互聯網郵件擴展|多用途互联网邮件扩展}
71
+ * @param {string|Blob|ArrayBuffer|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|BigInt64Array|BigUint64Array} data 字符串、blob数据或url地址
72
+ * @param {string|DownloadOptions} [options] 文件名称 或 配置项
73
+ * @returns {Promise<void>}
74
+ * @example
75
+ * // 文本
76
+ * download('hello world', 'text.txt');
77
+ *
78
+ * // 远程文件
79
+ * download('/xxx.jpg', { dataType: 'url' });
80
+ *
81
+ * // blob文件
82
+ * download(new Blob(['hello world']), 'text.txt');
83
+ *
84
+ */
85
+ function download(data, options) {
86
+ var config = _typeof(options) === 'object' ? options : {};
87
+ if (typeof options === 'string') {
88
+ config.fileName = options;
89
+ }
90
+ var fileName = config.fileName,
91
+ type = config.type,
92
+ dataType = config.dataType,
93
+ transformRequest = config.transformRequest,
94
+ transformResponse = config.transformResponse;
95
+
96
+ /** @type {Blob|undefined} */
97
+ var payload;
98
+
99
+ // dataURLs、blob url、url、string
100
+ if (typeof data === 'string') {
101
+ if (!dataType && /^blob:.*?\/.*/.test(data)) {
102
+ // blob url
103
+ saver(data, fileName);
104
+ return Promise.resolve();
105
+ } else if (!dataType && /^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(data)) {
106
+ // dataURLs
107
+ payload = dataURLToBlob(data);
108
+ } else if (dataType === 'url' || !dataType && isUrl(data)) {
109
+ // url
110
+ /** @type {AjaxOptions} */
111
+ var defaultAjaxOptions = {
112
+ responseType: 'blob'
113
+ };
114
+ // 请求前配置调整
115
+ var ajaxOptions = typeof transformRequest === 'function' ? transformRequest(defaultAjaxOptions) : defaultAjaxOptions;
116
+ return ajax(data, ajaxOptions).then(function (e) {
117
+ /** @type {Blob} */
118
+ // @ts-ignore
119
+ // 响应结果调整
120
+ var res = typeof transformResponse === 'function' ? transformResponse(e.target.response) : e.target.response;
121
+ var currentFileName = fileName || data.split("?")[0].split("#")[0].split("/").pop();
122
+ return download(res, {
123
+ fileName: currentFileName,
124
+ type: type || (isBlob(res) ? res.type : undefined)
125
+ });
126
+ });
127
+ } else {
128
+ // string
129
+ payload = new Blob([data], {
130
+ type: type || 'text/plain'
131
+ });
132
+ }
133
+ } else if (isBlob(data)) {
134
+ // @ts-ignore
135
+ payload = data;
136
+ }
137
+
138
+ // html、TypedArray
139
+ if (!(payload instanceof Blob)) {
140
+ payload = new Blob([data], {
141
+ type: type
142
+ });
143
+ }
144
+
145
+ // @ts-ignore
146
+ if (navigator.msSaveBlob) {
147
+ // @ts-ignore
148
+ navigator.msSaveBlob(payload, fileName || 'download');
149
+ } else {
150
+ var url = URL.createObjectURL(payload);
151
+ saver(url, fileName);
152
+ URL.revokeObjectURL(url);
153
+ }
154
+ return Promise.resolve();
155
+ }
156
+ export default download;
@@ -0,0 +1,67 @@
1
+ var FileReaderMethodMap = {
2
+ arrayBuffer: 'readAsArrayBuffer',
3
+ binaryString: 'readAsBinaryString',
4
+ dataURL: 'readAsDataURL',
5
+ text: 'readAsText'
6
+ };
7
+
8
+ /**
9
+ * @overload
10
+ * @param {Blob} blob
11
+ * @param {'arrayBuffer'} type
12
+ * @returns {Promise<ArrayBuffer>}
13
+ */
14
+
15
+ /**
16
+ * @overload
17
+ * @param {Blob} blob
18
+ * @param {'binaryString'|'binaryString'|'dataURL'|'text'} [type='dataURL']
19
+ * @returns {Promise<string>}
20
+ */
21
+
22
+ /**
23
+ * 读取 Blob 或 File 对象<br/><br/>
24
+ *
25
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
26
+ *
27
+ * @static
28
+ * @alias module:Processor.fileReader
29
+ * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader|FileReader}
30
+ * @since 4.16.0
31
+ * @param {Blob} blob Blob 或 File 对象
32
+ * @param {'arrayBuffer'|'binaryString'|'dataURL'|'text'} [type='dataURL'] Blob 或 File 对象
33
+ * @returns {Promise<string|ArrayBuffer>} 文件的内容
34
+ * @example
35
+ * const aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // 一个包含DOMString的数组
36
+ * const htmlBlob = new Blob(aFileParts, { type: 'text/html' }); // 得到 blob
37
+ *
38
+ * fileReader(htmlBlob).then(data=>{
39
+ * console.log(data); // data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
40
+ * });
41
+ *
42
+ * const textBlob = new Blob(aFileParts, { type: 'text/plain' });
43
+ *
44
+ * fileReader(textBlob, 'text').then(data=>{
45
+ * console.log(data); // <a id="a"><b id="b">hey!</b></a>
46
+ * });
47
+ */
48
+ function fileReader(blob) {
49
+ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'dataURL';
50
+ var method = FileReaderMethodMap[type];
51
+ if (!method) {
52
+ method = FileReaderMethodMap.dataURL;
53
+ }
54
+ return new Promise(function (resolve, reject) {
55
+ var reader = new FileReader();
56
+ // @ts-ignore
57
+ reader[method](blob);
58
+ // @ts-ignore
59
+ reader.onload = function () {
60
+ return resolve(reader.result);
61
+ };
62
+ reader.onerror = function (error) {
63
+ return reject(error);
64
+ };
65
+ });
66
+ }
67
+ export default fileReader;
package/esm/filterTree.js CHANGED
@@ -7,7 +7,7 @@ import { isObject } from "./utils/type";
7
7
  * 过滤/筛选树节点。<br/><br/>如果某节点被过滤掉,它的子节点也一并抛弃
8
8
  *
9
9
  * @static
10
- * @alias module:Processor.filterTree
10
+ * @alias module:Tree.filterTree
11
11
  * @since 4.15.0
12
12
  * @template {any} T
13
13
  * @template {(item: T) => boolean} F
@@ -8,7 +8,7 @@ import { isObject } from "./utils/type";
8
8
  * 查找树结构数据节点
9
9
  *
10
10
  * @static
11
- * @alias module:Other.findTreeNode
11
+ * @alias module:Tree.findTreeNode
12
12
  * @since 4.14.0
13
13
  * @template {any} T
14
14
  * @template {(item: T) => boolean} F
@@ -8,7 +8,7 @@ import { isObject } from "./utils/type";
8
8
  * 查找树结构数据多个节点
9
9
  *
10
10
  * @static
11
- * @alias module:Other.findTreeNodes
11
+ * @alias module:Tree.findTreeNodes
12
12
  * @since 4.15.0
13
13
  * @template {any} T
14
14
  * @template {(item: T) => boolean} F
@@ -54,7 +54,7 @@ function internalFindTreeSelect(tree, predicate, childrenField) {
54
54
  * 查找包含当前节点的所有父级节点
55
55
  *
56
56
  * @static
57
- * @alias module:Other.findTreeSelect
57
+ * @alias module:Tree.findTreeSelect
58
58
  * @since 4.14.0
59
59
  * @template {any} T
60
60
  * @template {(item: T) => boolean} F
package/esm/index.js CHANGED
@@ -40,16 +40,13 @@ export { default as numberToChinese } from './numberToChinese';
40
40
  export { default as bytesToSize } from './bytesToSize';
41
41
  export { default as parseIdCard } from './parseIdCard';
42
42
  export { default as blobToDataURL } from './blobToDataURL';
43
+ export { default as fileReader } from './fileReader';
43
44
  export { default as dataURLToBlob } from './dataURLToBlob';
44
45
  export { default as setDataURLPrefix } from './setDataURLPrefix';
45
46
  export { default as normalizeString } from './normalizeString';
46
47
  export { default as safeDate } from './safeDate';
47
48
  export { default as formatMobile } from './formatMobile';
48
49
  export { default as padZero } from './padZero';
49
- export { default as transformFieldNames } from './transformFieldNames';
50
- export { default as listToTree } from './listToTree';
51
- export { default as treeToList } from './treeToList';
52
- export { default as filterTree } from './filterTree';
53
50
 
54
51
  /**
55
52
  * 数学计算,修正浮点数计算问题
@@ -102,10 +99,23 @@ export { default as round } from './round';
102
99
  * @module Other
103
100
  * @since 4.2.0
104
101
  */
105
- export { default as waitTime } from './waitTime';
102
+ export { default as ajax } from './ajax';
106
103
  export { default as calculateCursorPosition } from './calculateCursorPosition';
104
+ export { default as download } from './download';
107
105
  export { default as randomString } from './randomString';
108
106
  export { default as strlen } from './strlen';
107
+ export { default as waitTime } from './waitTime';
108
+
109
+ /**
110
+ * 树结构数据查询、过滤、转换等处理方法
111
+ *
112
+ * @module Tree
113
+ * @since 4.14.0
114
+ */
115
+ export { default as transformFieldNames } from './transformFieldNames';
116
+ export { default as listToTree } from './listToTree';
117
+ export { default as treeToList } from './treeToList';
118
+ export { default as filterTree } from './filterTree';
109
119
  export { default as findTreeNode } from './findTreeNode';
110
120
  export { default as findTreeNodes } from './findTreeNodes';
111
121
  export { default as findTreeSelect } from './findTreeSelect';
@@ -0,0 +1,124 @@
1
+ /* eslint-disable no-unused-vars */
2
+ // 该文件用于 jsdoc 生成文件。因为一些 typescript 语法, jsdoc 不支持,导致生成文档报错。
3
+
4
+ /**
5
+ * 转换字段名,返回一个转换字段后的值,不改变原值。
6
+ *
7
+ * @static
8
+ * @alias module:Tree.transformFieldNames
9
+ * @since 4.14.0
10
+ * @param {object[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
11
+ * @param {object} fieldNames 字段名映射
12
+ * @param {string} [childrenField] 子级数据字段名
13
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
14
+ * @returns {object[]}
15
+ * @example
16
+ *
17
+ * const options = [{code: '1', name: 'one'},{code:'2', name:'two'}];
18
+ * const newOptions = transformFieldNames(options, {label: 'name', value: 'code'});
19
+ * // [{value: '1', label: 'one'},{value:'2', label:'two'}]
20
+ *
21
+ * // 嵌套数据,指定子级字段名 children
22
+ * const options2 = [{code: '1', name: 'one'},{code:'2', name:'two', children: [{code:'2-1', name:'two-one', children: [{code: '2-1-1', name:'two-one-one'}]}]}];
23
+ * const newOptions2 = transformFieldNames(options2, {label: 'name', value: 'code'}, 'children');
24
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one', children: [{value: '2-1-1', label:'two-one-one'}]}]}]
25
+ *
26
+ * const options3 = [{code: '1', name: 'one'},{code:'2', name:'two', childs: [{code:'2-1', name:'two-one'}]}];
27
+ * const newOptions3 = transformFieldNames(options3, {label: 'name', value: 'code'}, 'childs');
28
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', childs: [{value: '2-1', label:'two-one'}]}]
29
+ *
30
+ * // 嵌套数据,并替换子集字段名
31
+ * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
32
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
33
+ */
34
+ function transformFieldNames(data, fieldNames, childrenField) {}
35
+
36
+ /**
37
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
38
+ * @typedef {Object} AjaxOptions ajax配置项
39
+ * @property {string} [method="get"] 创建请求时使用的方法
40
+ * @property {*} [data=null] 请求体被发送的数据
41
+ * @property {object} [headers] 自定义请求头
42
+ * @property {string} [responseType] 响应类型
43
+ * @property {number} [timeout] 请求超时的毫秒数
44
+ * @property {boolean} [withCredentials=false] 跨域请求时是否需要使用凭证
45
+ * @property {boolean} [async=true] 是否异步执行操作
46
+ * @property {string|null} [user=null] 用户名,用于认证用途
47
+ * @property {string|null} [password=null] 密码,用于认证用途
48
+ * @property {function} [onLoadStart] 接收到响应数据时触发
49
+ * @property {function} [onProgress] 请求接收到更多数据时,周期性地触发
50
+ * @property {function} [onAbort] 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时
51
+ * @property {function} [onTimeout] 在预设时间内没有接收到响应时触发
52
+ * @property {function} [onError] 当 request 遭遇错误时触发
53
+ * @property {function} [onLoad] 请求成功完成时触发
54
+ * @property {function} [onLoadEnd] 请求结束时触发,无论请求成功 (load) 还是失败 (abort 或 error)
55
+ */
56
+
57
+ /**
58
+ * 请求<br/><br/>
59
+ *
60
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
61
+ *
62
+ * @static
63
+ * @alias module:Other.ajax
64
+ * @since 4.16.0
65
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
66
+ * @param {string} url 地址
67
+ * @param {AjaxOptions} [options] 配置项
68
+ * @returns {Promise<object>} XHR 事件对象
69
+ * @example
70
+ * ajax('/somefile').then(res=>{
71
+ * // do something
72
+ * });
73
+ *
74
+ * ajax('/api', { method: 'post' }).then(res=>{
75
+ * // do something
76
+ * });
77
+ */
78
+ function ajax(url, options) {}
79
+
80
+ /**
81
+ * @callback TransformRequest
82
+ * @param {AjaxOptions} options ajax 配置项
83
+ * @returns {AjaxOptions}
84
+ */
85
+
86
+ /**
87
+ * @callback TransformResponse
88
+ * @param {Blob} res 响应的Blob对象。如果你通过 transformRequest 修改了 responseType ,该参数将是该类型响应值。
89
+ * @returns {Blob}
90
+ */
91
+
92
+ /**
93
+ * @typedef {Object} DownloadOptions 下载配置项
94
+ * @property {string} [options.fileName] 文件名称
95
+ * @property {string} [options.type] MIME 类型
96
+ * @property {'url'|'text'} [options.dataType] 手动设置数据类型,默认会根据传入的数据判断类型,主要是为了区分 url 和 text 。<br/>如果你要下载的文本是 url ,请设置 'text' ;如果你要下载的 url 是绝对/相对路径,请设置 'url' 。
97
+ * @property {TransformRequest} [options.transformRequest] 请求前触发,XHR 对象或配置调整
98
+ * @property {TransformResponse} [options.transformResponse] 请求成功后触发,在传递给 then/catch 前,允许修改响应数据
99
+ */
100
+
101
+ /**
102
+ * 下载<br/><br/>
103
+ *
104
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。</em>
105
+ *
106
+ * @static
107
+ * @alias module:Other.download
108
+ * @since 4.16.0
109
+ * @see {@link https://zh.wikipedia.org/wiki/多用途互聯網郵件擴展|MIME}
110
+ * @param {string|Blob|TypedArray} data 字符串、blob数据或url地址
111
+ * @param {string|DownloadOptions} [options] 文件名称 或 配置项
112
+ * @returns {Promise<void>}
113
+ * @example
114
+ * // 文本
115
+ * download('hello world', 'text.txt');
116
+ *
117
+ * // 远程文件
118
+ * download('/xxx.jpg', { dataType: 'url' });
119
+ *
120
+ * // blob文件
121
+ * download(new Blob(['hello world']), 'text.txt');
122
+ *
123
+ */
124
+ function download(data, fileName, options) {}
@@ -1,5 +1,3 @@
1
- // @ts-nocheck
2
-
3
1
  // 交换字段名
4
2
  /**
5
3
  * @template {*} D
@@ -8,10 +6,10 @@
8
6
  */
9
7
 
10
8
  // 交换字段名,支持嵌套
11
- // 先排除子级字段名,再交换字段名,然后加上子级字段名,再替换一次。这里的 F 有类型问题,因为排除了子级字段名,暂时没有比较好的方案处理。
9
+ // 先排除子级字段名,再交换字段名,然后加上子级字段名,再替换一次。
12
10
  /**
13
11
  * @template {*} D
14
- * @template {Record<string, keyof D>} F
12
+ * @template {Record<string, any>} F
15
13
  * @template {string} C
16
14
  * @typedef {(C extends keyof D ? ExchangeFieldNames<Omit<D, C> & Record<C, TransformFieldNames<D, F, C>>, F> : ExchangeFieldNames<D, F>)[]} TransformFieldNames
17
15
  */
package/esm/listToTree.js CHANGED
@@ -40,7 +40,7 @@ function processEmptyChildren(arr, options) {
40
40
  * 列表数据转树结构
41
41
  *
42
42
  * @static
43
- * @alias module:Processor.listToTree
43
+ * @alias module:Tree.listToTree
44
44
  * @since 4.14.0
45
45
  * @template {Record<string,any>} [T=Record<string,any>]
46
46
  * @template {*} [R=T&Record<string,any>]