util-helpers 4.15.3 → 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 (53) hide show
  1. package/README.md +6 -5
  2. package/dist/util-helpers.js +452 -48
  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/index.js +4 -1
  12. package/esm/interface.doc.js +124 -0
  13. package/esm/numberToChinese.js +3 -2
  14. package/esm/parseIdCard.js +1 -1
  15. package/esm/safeDate.js +26 -9
  16. package/esm/transformFieldNames.js +2 -3
  17. package/esm/utils/config.js +1 -1
  18. package/esm/utils/type/index.js +3 -1
  19. package/esm/utils/type/isArrayBuffer.js +25 -0
  20. package/esm/utils/type/isBlob.js +27 -0
  21. package/lib/ajax.js +156 -0
  22. package/lib/blobToDataURL.js +8 -12
  23. package/lib/dataURLToBlob.js +6 -5
  24. package/lib/download.js +161 -0
  25. package/lib/fileReader.js +74 -0
  26. package/lib/index.js +22 -1
  27. package/lib/interface.doc.js +126 -0
  28. package/lib/numberToChinese.js +3 -2
  29. package/lib/parseIdCard.js +1 -1
  30. package/lib/safeDate.js +27 -10
  31. package/lib/transformFieldNames.js +1 -1
  32. package/lib/utils/config.js +1 -1
  33. package/lib/utils/type/index.js +14 -0
  34. package/lib/utils/type/isArrayBuffer.js +32 -0
  35. package/lib/utils/type/isBlob.js +34 -0
  36. package/package.json +2 -2
  37. package/types/ajax.d.ts +121 -0
  38. package/types/blobToDataURL.d.ts +5 -1
  39. package/types/download.d.ts +77 -0
  40. package/types/fileReader.d.ts +3 -0
  41. package/types/index.d.ts +4 -1
  42. package/types/numberToChinese.d.ts +3 -2
  43. package/types/parseIdCard.d.ts +2 -2
  44. package/types/safeDate.d.ts +3 -23
  45. package/types/transformFieldNames.d.ts +2 -2
  46. package/types/utils/type/index.d.ts +21 -19
  47. package/types/utils/type/isArrayBuffer.d.ts +21 -0
  48. package/types/utils/type/isBlob.d.ts +23 -0
  49. package/esm/transformFieldNames.doc.js +0 -35
  50. package/lib/transformFieldNames.doc.js +0 -42
  51. /package/esm/{transformFieldNames.type.js → interface.type.js} +0 -0
  52. /package/lib/{transformFieldNames.type.js → interface.type.js} +0 -0
  53. /package/types/{transformFieldNames.type.d.ts → interface.type.d.ts} +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/index.js CHANGED
@@ -40,6 +40,7 @@ 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';
@@ -98,10 +99,12 @@ export { default as round } from './round';
98
99
  * @module Other
99
100
  * @since 4.2.0
100
101
  */
101
- export { default as waitTime } from './waitTime';
102
+ export { default as ajax } from './ajax';
102
103
  export { default as calculateCursorPosition } from './calculateCursorPosition';
104
+ export { default as download } from './download';
103
105
  export { default as randomString } from './randomString';
104
106
  export { default as strlen } from './strlen';
107
+ export { default as waitTime } from './waitTime';
105
108
 
106
109
  /**
107
110
  * 树结构数据查询、过滤、转换等处理方法
@@ -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) {}
@@ -130,8 +130,9 @@ function mapNumberChar(num) {
130
130
  }
131
131
 
132
132
  /**
133
- * 数字转中文数字
134
- * 不在安全数字 -9007199254740991~9007199254740991 内,处理会有异常
133
+ * 数字转中文数字<br/><br/>
134
+ *
135
+ * 如果数字不在安全数字 -9007199254740991~9007199254740991 范围内,处理会有异常。
135
136
  *
136
137
  * @static
137
138
  * @alias module:Processor.numberToChinese
@@ -71,7 +71,7 @@ var Provinces = [
71
71
  * @since 4.0.0
72
72
  * @see 参考 {@link https://baike.baidu.com/item/居民身份证号码|居民身份证号码}
73
73
  * @param {string} id 身份证号码,支持15位
74
- * @returns {null|IdCardInfo} null 或 省份、生日、性别,省/市/区/年/月/日/性别编码
74
+ * @returns {IdCardInfo | null} 省份、生日、性别,省/市/区/年/月/日/性别编码。如果解析失败将返回 null
75
75
  * @example
76
76
  *
77
77
  * parseIdCard('123456789123456'); // null
package/esm/safeDate.js CHANGED
@@ -1,11 +1,32 @@
1
1
  import _construct from "@babel/runtime/helpers/construct";
2
- import isNil from './utils/type/isNil';
2
+ /**
3
+ * @overload
4
+ * @return {Date}
5
+ */
3
6
 
4
- // TODO: 函数重载,类型参照 Date
7
+ /**
8
+ * @overload
9
+ * @param {number | string | Date} value Unix时间戳、时间戳字符串 dateString 、Date 日期对象
10
+ * @return {Date}
11
+ */
5
12
 
6
13
  /**
7
- * 创建一个 Date 实例日期对象,同 new Date() 。<br/>
8
- * 规避了苹果设备浏览器不支持部分格式(YYYY-MM-DD HH-mm YYYY.MM.DD)。
14
+ * @overload
15
+ * @param {number} year 表示年份的整数值。0 到 99 会被映射至 1900 年至 1999 年,其他值代表实际年份。
16
+ * @param {number} monthIndex 表示月份的整数值,从 0(1 月)到 11(12 月)。
17
+ * @param {number} [date] 表示一个月中的第几天的整数值,从 1 开始。默认值为 1。
18
+ * @param {number} [hours] 表示一天中的小时数的整数值 (24 小时制)。默认值为 0(午夜)。
19
+ * @param {number} [minutes] 表示一个完整时间(如 01:10:00)中的分钟部分的整数值。默认值为 0。
20
+ * @param {number} [seconds] 表示一个完整时间(如 01:10:00)中的秒部分的整数值。默认值为 0。
21
+ * @param {number} [ms] 表示一个完整时间的毫秒部分的整数值。默认值为 0。
22
+ * @return {Date}
23
+ */
24
+
25
+ /**
26
+ * 创建一个 Date 实例日期对象,同 <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date#%E5%8F%82%E6%95%B0">new Date()</a> <br/><br/>
27
+ *
28
+ * 规避了苹果设备浏览器不支持部分格式(例如,YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。<br/>
29
+ * 如果参数为 undefined 正常返回 Date 。
9
30
  *
10
31
  * @static
11
32
  * @alias module:Processor.safeDate
@@ -22,8 +43,6 @@ import isNil from './utils/type/isNil';
22
43
  * safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
23
44
  * safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
24
45
  * safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
25
- *
26
- *
27
46
  */
28
47
  function safeDate(value) {
29
48
  var safeValue = typeof value === 'string' ? value.replace(/[\\.-]/g, '/') : value;
@@ -34,8 +53,6 @@ function safeDate(value) {
34
53
  // @ts-ignore
35
54
  return _construct(Date, [safeValue].concat(args));
36
55
  }
37
-
38
- // @ts-ignore
39
- return isNil(safeValue) ? new Date() : new Date(safeValue);
56
+ return typeof safeValue === 'undefined' ? new Date() : new Date(safeValue);
40
57
  }
41
58
  export default safeDate;