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
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ /* eslint-disable no-unused-vars */
4
+ // 该文件用于 jsdoc 生成文件。因为一些 typescript 语法, jsdoc 不支持,导致生成文档报错。
5
+
6
+ /**
7
+ * 转换字段名,返回一个转换字段后的值,不改变原值。
8
+ *
9
+ * @static
10
+ * @alias module:Tree.transformFieldNames
11
+ * @since 4.14.0
12
+ * @param {object[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
13
+ * @param {object} fieldNames 字段名映射
14
+ * @param {string} [childrenField] 子级数据字段名
15
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
16
+ * @returns {object[]}
17
+ * @example
18
+ *
19
+ * const options = [{code: '1', name: 'one'},{code:'2', name:'two'}];
20
+ * const newOptions = transformFieldNames(options, {label: 'name', value: 'code'});
21
+ * // [{value: '1', label: 'one'},{value:'2', label:'two'}]
22
+ *
23
+ * // 嵌套数据,指定子级字段名 children
24
+ * 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'}]}]}];
25
+ * const newOptions2 = transformFieldNames(options2, {label: 'name', value: 'code'}, 'children');
26
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one', children: [{value: '2-1-1', label:'two-one-one'}]}]}]
27
+ *
28
+ * const options3 = [{code: '1', name: 'one'},{code:'2', name:'two', childs: [{code:'2-1', name:'two-one'}]}];
29
+ * const newOptions3 = transformFieldNames(options3, {label: 'name', value: 'code'}, 'childs');
30
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', childs: [{value: '2-1', label:'two-one'}]}]
31
+ *
32
+ * // 嵌套数据,并替换子集字段名
33
+ * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
34
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
35
+ */
36
+ function transformFieldNames(data, fieldNames, childrenField) {}
37
+
38
+ /**
39
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
40
+ * @typedef {Object} AjaxOptions ajax配置项
41
+ * @property {string} [method="get"] 创建请求时使用的方法
42
+ * @property {*} [data=null] 请求体被发送的数据
43
+ * @property {object} [headers] 自定义请求头
44
+ * @property {string} [responseType] 响应类型
45
+ * @property {number} [timeout] 请求超时的毫秒数
46
+ * @property {boolean} [withCredentials=false] 跨域请求时是否需要使用凭证
47
+ * @property {boolean} [async=true] 是否异步执行操作
48
+ * @property {string|null} [user=null] 用户名,用于认证用途
49
+ * @property {string|null} [password=null] 密码,用于认证用途
50
+ * @property {function} [onLoadStart] 接收到响应数据时触发
51
+ * @property {function} [onProgress] 请求接收到更多数据时,周期性地触发
52
+ * @property {function} [onAbort] 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时
53
+ * @property {function} [onTimeout] 在预设时间内没有接收到响应时触发
54
+ * @property {function} [onError] 当 request 遭遇错误时触发
55
+ * @property {function} [onLoad] 请求成功完成时触发
56
+ * @property {function} [onLoadEnd] 请求结束时触发,无论请求成功 (load) 还是失败 (abort 或 error)
57
+ */
58
+
59
+ /**
60
+ * 请求<br/><br/>
61
+ *
62
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
63
+ *
64
+ * @static
65
+ * @alias module:Other.ajax
66
+ * @since 4.16.0
67
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
68
+ * @param {string} url 地址
69
+ * @param {AjaxOptions} [options] 配置项
70
+ * @returns {Promise<object>} XHR 事件对象
71
+ * @example
72
+ * ajax('/somefile').then(res=>{
73
+ * // do something
74
+ * });
75
+ *
76
+ * ajax('/api', { method: 'post' }).then(res=>{
77
+ * // do something
78
+ * });
79
+ */
80
+ function ajax(url, options) {}
81
+
82
+ /**
83
+ * @callback TransformRequest
84
+ * @param {AjaxOptions} options ajax 配置项
85
+ * @returns {AjaxOptions}
86
+ */
87
+
88
+ /**
89
+ * @callback TransformResponse
90
+ * @param {Blob} res 响应的Blob对象。如果你通过 transformRequest 修改了 responseType ,该参数将是该类型响应值。
91
+ * @returns {Blob}
92
+ */
93
+
94
+ /**
95
+ * @typedef {Object} DownloadOptions 下载配置项
96
+ * @property {string} [options.fileName] 文件名称
97
+ * @property {string} [options.type] MIME 类型
98
+ * @property {'url'|'text'} [options.dataType] 手动设置数据类型,默认会根据传入的数据判断类型,主要是为了区分 url 和 text 。<br/>如果你要下载的文本是 url ,请设置 'text' ;如果你要下载的 url 是绝对/相对路径,请设置 'url' 。
99
+ * @property {TransformRequest} [options.transformRequest] 请求前触发,XHR 对象或配置调整
100
+ * @property {TransformResponse} [options.transformResponse] 请求成功后触发,在传递给 then/catch 前,允许修改响应数据
101
+ */
102
+
103
+ /**
104
+ * 下载<br/><br/>
105
+ *
106
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。</em>
107
+ *
108
+ * @static
109
+ * @alias module:Other.download
110
+ * @since 4.16.0
111
+ * @see {@link https://zh.wikipedia.org/wiki/多用途互聯網郵件擴展|MIME}
112
+ * @param {string|Blob|TypedArray} data 字符串、blob数据或url地址
113
+ * @param {string|DownloadOptions} [options] 文件名称 或 配置项
114
+ * @returns {Promise<void>}
115
+ * @example
116
+ * // 文本
117
+ * download('hello world', 'text.txt');
118
+ *
119
+ * // 远程文件
120
+ * download('/xxx.jpg', { dataType: 'url' });
121
+ *
122
+ * // blob文件
123
+ * download(new Blob(['hello world']), 'text.txt');
124
+ *
125
+ */
126
+ function download(data, fileName, options) {}
@@ -136,8 +136,9 @@ function mapNumberChar(num) {
136
136
  }
137
137
 
138
138
  /**
139
- * 数字转中文数字
140
- * 不在安全数字 -9007199254740991~9007199254740991 内,处理会有异常
139
+ * 数字转中文数字<br/><br/>
140
+ *
141
+ * 如果数字不在安全数字 -9007199254740991~9007199254740991 范围内,处理会有异常。
141
142
  *
142
143
  * @static
143
144
  * @alias module:Processor.numberToChinese
@@ -77,7 +77,7 @@ var Provinces = [
77
77
  * @since 4.0.0
78
78
  * @see 参考 {@link https://baike.baidu.com/item/居民身份证号码|居民身份证号码}
79
79
  * @param {string} id 身份证号码,支持15位
80
- * @returns {null|IdCardInfo} null 或 省份、生日、性别,省/市/区/年/月/日/性别编码
80
+ * @returns {IdCardInfo | null} 省份、生日、性别,省/市/区/年/月/日/性别编码。如果解析失败将返回 null
81
81
  * @example
82
82
  *
83
83
  * parseIdCard('123456789123456'); // null
package/lib/safeDate.js CHANGED
@@ -4,16 +4,37 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
- var _isNil = _interopRequireDefault(require("./utils/type/isNil"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
7
  function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
10
8
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
11
9
  function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
12
- // TODO: 函数重载,类型参照 Date
10
+ /**
11
+ * @overload
12
+ * @return {Date}
13
+ */
14
+
15
+ /**
16
+ * @overload
17
+ * @param {number | string | Date} value Unix时间戳、时间戳字符串 dateString 、Date 日期对象
18
+ * @return {Date}
19
+ */
20
+
21
+ /**
22
+ * @overload
23
+ * @param {number} year 表示年份的整数值。0 到 99 会被映射至 1900 年至 1999 年,其他值代表实际年份。
24
+ * @param {number} monthIndex 表示月份的整数值,从 0(1 月)到 11(12 月)。
25
+ * @param {number} [date] 表示一个月中的第几天的整数值,从 1 开始。默认值为 1。
26
+ * @param {number} [hours] 表示一天中的小时数的整数值 (24 小时制)。默认值为 0(午夜)。
27
+ * @param {number} [minutes] 表示一个完整时间(如 01:10:00)中的分钟部分的整数值。默认值为 0。
28
+ * @param {number} [seconds] 表示一个完整时间(如 01:10:00)中的秒部分的整数值。默认值为 0。
29
+ * @param {number} [ms] 表示一个完整时间的毫秒部分的整数值。默认值为 0。
30
+ * @return {Date}
31
+ */
13
32
 
14
33
  /**
15
- * 创建一个 Date 实例日期对象,同 new Date() 。<br/>
16
- * 规避了苹果设备浏览器不支持部分格式(YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。
34
+ * 创建一个 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/>
35
+ *
36
+ * 规避了苹果设备浏览器不支持部分格式(例如,YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。<br/>
37
+ * 如果参数为 undefined 正常返回 Date 。
17
38
  *
18
39
  * @static
19
40
  * @alias module:Processor.safeDate
@@ -30,8 +51,6 @@ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Objec
30
51
  * safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
31
52
  * safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
32
53
  * safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
33
- *
34
- *
35
54
  */
36
55
  function safeDate(value) {
37
56
  var safeValue = typeof value === 'string' ? value.replace(/[\\.-]/g, '/') : value;
@@ -42,9 +61,7 @@ function safeDate(value) {
42
61
  // @ts-ignore
43
62
  return _construct(Date, [safeValue].concat(args));
44
63
  }
45
-
46
- // @ts-ignore
47
- return (0, _isNil["default"])(safeValue) ? new Date() : new Date(safeValue);
64
+ return typeof safeValue === 'undefined' ? new Date() : new Date(safeValue);
48
65
  }
49
66
  var _default = safeDate;
50
67
  exports["default"] = _default;
@@ -24,7 +24,7 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
24
24
  * @param {F} fieldNames 字段名映射
25
25
  * @param {C} [childrenField] 子级数据字段名
26
26
  * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
27
- * @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
27
+ * @returns {import('./interface.type.js').TransformFieldNames<D, F, C>}
28
28
  * @example
29
29
  *
30
30
  * const options = [{code: '1', name: 'one'},{code:'2', name:'two'}];
@@ -24,5 +24,5 @@ function setDisableWarning(bool) {
24
24
  }
25
25
 
26
26
  // eslint-disable-next-line no-undef
27
- var version = "4.15.3";
27
+ var version = "4.16.0";
28
28
  exports.version = version;
@@ -15,6 +15,18 @@ Object.defineProperty(exports, "isArray", {
15
15
  return _isArray["default"];
16
16
  }
17
17
  });
18
+ Object.defineProperty(exports, "isArrayBuffer", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _isArrayBuffer["default"];
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "isBlob", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _isBlob["default"];
28
+ }
29
+ });
18
30
  Object.defineProperty(exports, "isBoolean", {
19
31
  enumerable: true,
20
32
  get: function get() {
@@ -113,6 +125,8 @@ Object.defineProperty(exports, "isWeakSet", {
113
125
  });
114
126
  var _isArguments = _interopRequireDefault(require("./isArguments"));
115
127
  var _isArray = _interopRequireDefault(require("./isArray"));
128
+ var _isArrayBuffer = _interopRequireDefault(require("./isArrayBuffer"));
129
+ var _isBlob = _interopRequireDefault(require("./isBlob"));
116
130
  var _isBoolean = _interopRequireDefault(require("./isBoolean"));
117
131
  var _isDate = _interopRequireDefault(require("./isDate"));
118
132
  var _isError = _interopRequireDefault(require("./isError"));
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _isType = _interopRequireDefault(require("./isType"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
+ /**
10
+ * 检查值是否为ArrayBuffer对象
11
+ *
12
+ * @static
13
+ * @alias module:Type.isArrayBuffer
14
+ * @since 4.16.0
15
+ * @param {*} value 检查值
16
+ * @returns {boolean} 是否为ArrayBuffer对象
17
+ * @example
18
+ *
19
+ * isArrayBuffer(new ArrayBuffer(8))
20
+ * // => true
21
+ *
22
+ * isArrayBuffer({})
23
+ * // => false
24
+ *
25
+ * isArrayBuffer('2012')
26
+ * // => false
27
+ */
28
+ function isArrayBuffer(value) {
29
+ return (0, _isType["default"])(value, 'ArrayBuffer');
30
+ }
31
+ var _default = isArrayBuffer;
32
+ exports["default"] = _default;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _isType = _interopRequireDefault(require("./isType"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
+ /**
10
+ * 检查值是否为Blob对象<br/><br/>
11
+ *
12
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
13
+ *
14
+ * @static
15
+ * @alias module:Type.isBlob
16
+ * @since 4.16.0
17
+ * @param {*} value 检查值
18
+ * @returns {boolean} 是否为Blob对象
19
+ * @example
20
+ *
21
+ * isBlob(new Blob(['a']))
22
+ * // => true
23
+ *
24
+ * isBlob({})
25
+ * // => false
26
+ *
27
+ * isBlob('2012')
28
+ * // => false
29
+ */
30
+ function isBlob(value) {
31
+ return (0, _isType["default"])(value, 'Blob');
32
+ }
33
+ var _default = isBlob;
34
+ exports["default"] = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.15.3",
3
+ "version": "4.16.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -84,7 +84,7 @@
84
84
  "prettier": "^2.3.2",
85
85
  "rollup": "^2.49.0",
86
86
  "rollup-plugin-terser": "^7.0.2",
87
- "typescript": "^4.9.3"
87
+ "typescript": "^5.0.4"
88
88
  },
89
89
  "lint-staged": {
90
90
  "**/*.js": "npm run lint-staged:js",
@@ -0,0 +1,121 @@
1
+ export default ajax;
2
+ /**
3
+ * XMLHttpRequest 事件对象
4
+ */
5
+ export type XMLHttpRequestEvent = XMLHttpRequest['onloadstart'];
6
+ /**
7
+ * ajax配置项
8
+ */
9
+ export type AjaxOptions = {
10
+ /**
11
+ * 创建请求时使用的方法
12
+ */
13
+ method?: string | undefined;
14
+ /**
15
+ * 请求体被发送的数据
16
+ */
17
+ data?: Document | XMLHttpRequestBodyInit | null | undefined;
18
+ /**
19
+ * 自定义请求头
20
+ */
21
+ headers?: {
22
+ [x: string]: string;
23
+ } | undefined;
24
+ /**
25
+ * 响应类型
26
+ */
27
+ responseType?: XMLHttpRequestResponseType | undefined;
28
+ /**
29
+ * 请求超时的毫秒数
30
+ */
31
+ timeout?: number | undefined;
32
+ /**
33
+ * 跨域请求时是否需要使用凭证
34
+ */
35
+ withCredentials?: boolean | undefined;
36
+ /**
37
+ * 是否异步执行操作
38
+ */
39
+ async?: boolean | undefined;
40
+ /**
41
+ * 用户名,用于认证用途
42
+ */
43
+ user?: string | null | undefined;
44
+ /**
45
+ * 密码,用于认证用途
46
+ */
47
+ password?: string | null | undefined;
48
+ /**
49
+ * 接收到响应数据时触发
50
+ */
51
+ onLoadStart?: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null | undefined;
52
+ /**
53
+ * 请求接收到更多数据时,周期性地触发
54
+ */
55
+ onProgress?: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null | undefined;
56
+ /**
57
+ * 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时
58
+ */
59
+ onAbort?: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null | undefined;
60
+ /**
61
+ * 在预设时间内没有接收到响应时触发
62
+ */
63
+ onTimeout?: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null | undefined;
64
+ /**
65
+ * 当 request 遭遇错误时触发
66
+ */
67
+ onError?: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null | undefined;
68
+ /**
69
+ * 请求成功完成时触发
70
+ */
71
+ onLoad?: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null | undefined;
72
+ /**
73
+ * 请求结束时触发,无论请求成功 (load) 还是失败 (abort 或 error)
74
+ */
75
+ onLoadEnd?: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null | undefined;
76
+ };
77
+ /**
78
+ * @typedef {XMLHttpRequest['onloadstart']} XMLHttpRequestEvent XMLHttpRequest 事件对象
79
+ */
80
+ /**
81
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
82
+ * @typedef {Object} AjaxOptions ajax配置项
83
+ * @property {string} [method="get"] 创建请求时使用的方法
84
+ * @property {Document | XMLHttpRequestBodyInit | null} [data=null] 请求体被发送的数据
85
+ * @property {Object.<string, string>} [headers] 自定义请求头
86
+ * @property {XMLHttpRequestResponseType} [responseType] 响应类型
87
+ * @property {number} [timeout] 请求超时的毫秒数
88
+ * @property {boolean} [withCredentials=false] 跨域请求时是否需要使用凭证
89
+ * @property {boolean} [async=true] 是否异步执行操作
90
+ * @property {string|null} [user=null] 用户名,用于认证用途
91
+ * @property {string|null} [password=null] 密码,用于认证用途
92
+ * @property {XMLHttpRequestEvent} [onLoadStart] 接收到响应数据时触发
93
+ * @property {XMLHttpRequestEvent} [onProgress] 请求接收到更多数据时,周期性地触发
94
+ * @property {XMLHttpRequestEvent} [onAbort] 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时
95
+ * @property {XMLHttpRequestEvent} [onTimeout] 在预设时间内没有接收到响应时触发
96
+ * @property {XMLHttpRequestEvent} [onError] 当 request 遭遇错误时触发
97
+ * @property {XMLHttpRequestEvent} [onLoad] 请求成功完成时触发
98
+ * @property {XMLHttpRequestEvent} [onLoadEnd] 请求结束时触发,无论请求成功 (load) 还是失败 (abort 或 error)
99
+ */
100
+ /**
101
+ * 请求<br/><br/>
102
+ *
103
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
104
+ *
105
+ * @static
106
+ * @alias module:Other.ajax
107
+ * @since 4.16.0
108
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest|XMLHttpRequest}
109
+ * @param {string} url 地址
110
+ * @param {AjaxOptions} [options] 配置项
111
+ * @returns {Promise<ProgressEvent<EventTarget>>}
112
+ * @example
113
+ * ajax('/somefile').then(res=>{
114
+ * // do something
115
+ * });
116
+ *
117
+ * ajax('/api', { method: 'post' }).then(res=>{
118
+ * // do something
119
+ * });
120
+ */
121
+ declare function ajax(url: string, options?: AjaxOptions | undefined): Promise<ProgressEvent<EventTarget>>;
@@ -1,11 +1,15 @@
1
1
  export default blobToDataURL;
2
2
  /**
3
- * 将 Blob 或 File 对象转成 data:URL 格式的 Base64 字符串。
3
+ * 将 Blob 或 File 对象转成 data:URL 格式的 Base64 字符串<br/><br/>
4
+ *
5
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
4
6
  *
5
7
  * @static
6
8
  * @alias module:Processor.blobToDataURL
7
9
  * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader/readAsDataURL|FileReader.readAsDataURL()}
8
10
  * @since 4.1.0
11
+ * @ignore
12
+ * @deprecated 请使用 `fileReader` 方法
9
13
  * @param {Blob} blob Blob 或 File 对象
10
14
  * @returns {Promise<string>} data:URL 格式的 Base64 字符串。
11
15
  * @example
@@ -0,0 +1,77 @@
1
+ export default download;
2
+ /**
3
+ * ajax 配置项
4
+ */
5
+ export type AjaxOptions = import('./ajax.js').AjaxOptions;
6
+ export type TransformRequest = (options: AjaxOptions) => AjaxOptions;
7
+ export type TransformResponse = (res: Blob) => Blob;
8
+ /**
9
+ * 下载配置项
10
+ */
11
+ export type DownloadOptions = {
12
+ /**
13
+ * 文件名称
14
+ */
15
+ fileName?: string | undefined;
16
+ /**
17
+ * MIME 类型
18
+ */
19
+ type?: string | undefined;
20
+ /**
21
+ * 手动设置数据类型,默认会根据传入的数据判断类型,主要是为了区分 url 和 text 。<br/>如果你要下载的文本是 url ,请设置 'text' ;如果你要下载的 url 是绝对/相对路径,请设置 'url' 。
22
+ */
23
+ dataType?: "text" | "url" | undefined;
24
+ /**
25
+ * 请求前触发,XHR 对象或配置调整
26
+ */
27
+ transformRequest?: TransformRequest | undefined;
28
+ /**
29
+ * 请求成功后触发,在传递给 then/catch 前,允许修改响应数据
30
+ */
31
+ transformResponse?: TransformResponse | undefined;
32
+ };
33
+ /**
34
+ * @typedef {import('./ajax.js').AjaxOptions} AjaxOptions ajax 配置项
35
+ */
36
+ /**
37
+ * @callback TransformRequest
38
+ * @param {AjaxOptions} options ajax 配置项
39
+ * @returns {AjaxOptions}
40
+ */
41
+ /**
42
+ * @callback TransformResponse
43
+ * @param {Blob} res 响应的Blob对象。如果你通过 transformRequest 修改了 responseType ,该参数将是该类型响应值。
44
+ * @returns {Blob}
45
+ */
46
+ /**
47
+ * @typedef {Object} DownloadOptions 下载配置项
48
+ * @property {string} [options.fileName] 文件名称
49
+ * @property {string} [options.type] MIME 类型
50
+ * @property {'url'|'text'} [options.dataType] 手动设置数据类型,默认会根据传入的数据判断类型,主要是为了区分 url 和 text 。<br/>如果你要下载的文本是 url ,请设置 'text' ;如果你要下载的 url 是绝对/相对路径,请设置 'url' 。
51
+ * @property {TransformRequest} [options.transformRequest] 请求前触发,XHR 对象或配置调整
52
+ * @property {TransformResponse} [options.transformResponse] 请求成功后触发,在传递给 then/catch 前,允许修改响应数据
53
+ */
54
+ /**
55
+ * 下载<br/><br/>
56
+ *
57
+ * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。</em>
58
+ *
59
+ * @static
60
+ * @alias module:Other.download
61
+ * @since 4.16.0
62
+ * @see {@link https://zh.wikipedia.org/wiki/多用途互聯網郵件擴展|多用途互联网邮件扩展}
63
+ * @param {string|Blob|ArrayBuffer|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|BigInt64Array|BigUint64Array} data 字符串、blob数据或url地址
64
+ * @param {string|DownloadOptions} [options] 文件名称 或 配置项
65
+ * @returns {Promise<void>}
66
+ * @example
67
+ * // 文本
68
+ * download('hello world', 'text.txt');
69
+ *
70
+ * // 远程文件
71
+ * download('/xxx.jpg', { dataType: 'url' });
72
+ *
73
+ * // blob文件
74
+ * download(new Blob(['hello world']), 'text.txt');
75
+ *
76
+ */
77
+ declare function download(data: string | Blob | ArrayBuffer | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array, options?: string | DownloadOptions | undefined): Promise<void>;
@@ -0,0 +1,3 @@
1
+ export default fileReader;
2
+ declare function fileReader(blob: Blob, type: 'arrayBuffer'): Promise<ArrayBuffer>;
3
+ declare function fileReader(blob: Blob, type?: "text" | "binaryString" | "dataURL" | undefined): Promise<string>;
package/types/index.d.ts CHANGED
@@ -27,6 +27,7 @@ export { default as numberToChinese } from "./numberToChinese";
27
27
  export { default as bytesToSize } from "./bytesToSize";
28
28
  export { default as parseIdCard } from "./parseIdCard";
29
29
  export { default as blobToDataURL } from "./blobToDataURL";
30
+ export { default as fileReader } from "./fileReader";
30
31
  export { default as dataURLToBlob } from "./dataURLToBlob";
31
32
  export { default as setDataURLPrefix } from "./setDataURLPrefix";
32
33
  export { default as normalizeString } from "./normalizeString";
@@ -38,10 +39,12 @@ export { default as minus } from "./minus";
38
39
  export { default as times } from "./times";
39
40
  export { default as divide } from "./divide";
40
41
  export { default as round } from "./round";
41
- export { default as waitTime } from "./waitTime";
42
+ export { default as ajax } from "./ajax";
42
43
  export { default as calculateCursorPosition } from "./calculateCursorPosition";
44
+ export { default as download } from "./download";
43
45
  export { default as randomString } from "./randomString";
44
46
  export { default as strlen } from "./strlen";
47
+ export { default as waitTime } from "./waitTime";
45
48
  export { default as transformFieldNames } from "./transformFieldNames";
46
49
  export { default as listToTree } from "./listToTree";
47
50
  export { default as treeToList } from "./treeToList";
@@ -1,7 +1,8 @@
1
1
  export default numberToChinese;
2
2
  /**
3
- * 数字转中文数字
4
- * 不在安全数字 -9007199254740991~9007199254740991 内,处理会有异常
3
+ * 数字转中文数字<br/><br/>
4
+ *
5
+ * 如果数字不在安全数字 -9007199254740991~9007199254740991 范围内,处理会有异常。
5
6
  *
6
7
  * @static
7
8
  * @alias module:Processor.numberToChinese
@@ -78,7 +78,7 @@ export type IdCardInfo = {
78
78
  * @since 4.0.0
79
79
  * @see 参考 {@link https://baike.baidu.com/item/居民身份证号码|居民身份证号码}
80
80
  * @param {string} id 身份证号码,支持15位
81
- * @returns {null|IdCardInfo} null 或 省份、生日、性别,省/市/区/年/月/日/性别编码
81
+ * @returns {IdCardInfo | null} 省份、生日、性别,省/市/区/年/月/日/性别编码。如果解析失败将返回 null
82
82
  * @example
83
83
  *
84
84
  * parseIdCard('123456789123456'); // null
@@ -104,4 +104,4 @@ export type IdCardInfo = {
104
104
  * }
105
105
  *
106
106
  */
107
- declare function parseIdCard(id: string): null | IdCardInfo;
107
+ declare function parseIdCard(id: string): IdCardInfo | null;
@@ -1,24 +1,4 @@
1
1
  export default safeDate;
2
- /**
3
- * 创建一个 Date 实例日期对象,同 new Date() 。<br/>
4
- * 规避了苹果设备浏览器不支持部分格式(YYYY-MM-DD HH-mm YYYY.MM.DD)。
5
- *
6
- * @static
7
- * @alias module:Processor.safeDate
8
- * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date|Date}
9
- * @since 4.4.0
10
- * @param {string|number|Date} [value] 日期时间字符串、毫秒数、日期对象
11
- * @param {...number} args 月/日/时/分/秒/毫秒
12
- * @returns {Date} Date 实例日期对象
13
- * @example
14
- *
15
- * safeDate('2022-1-1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
16
- * safeDate('2022/1/1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
17
- * safeDate('2022.1.1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
18
- * safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
19
- * safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
20
- * safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
21
- *
22
- *
23
- */
24
- declare function safeDate(value?: string | number | Date | undefined, ...args: number[]): Date;
2
+ declare function safeDate(): Date;
3
+ declare function safeDate(value: number | string | Date): Date;
4
+ declare function safeDate(year: number, monthIndex: number, date?: number | undefined, hours?: number | undefined, minutes?: number | undefined, seconds?: number | undefined, ms?: number | undefined): Date;