util-helpers 4.22.2 → 4.23.1

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 (58) hide show
  1. package/README.md +3 -0
  2. package/dist/util-helpers.js +594 -560
  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/VERSION.js +1 -1
  7. package/esm/getImageInfo.js +34 -40
  8. package/esm/index.js +2 -1
  9. package/esm/loadImage.js +32 -34
  10. package/esm/loadImageWithBlob.js +23 -24
  11. package/esm/randomString.js +26 -12
  12. package/esm/transformObjectValue.js +25 -0
  13. package/esm/validatePassword.js +4 -13
  14. package/lib/VERSION.js +1 -1
  15. package/lib/getImageInfo.js +33 -39
  16. package/lib/index.js +3 -1
  17. package/lib/loadImage.js +31 -33
  18. package/lib/loadImageWithBlob.js +23 -24
  19. package/lib/randomString.js +25 -11
  20. package/lib/transformObjectValue.js +27 -0
  21. package/lib/validatePassword.js +4 -13
  22. package/package.json +11 -11
  23. package/types/AsyncMemo.d.ts +16 -2
  24. package/types/ajax.d.ts +2 -2
  25. package/types/blobToDataURL.d.ts +1 -1
  26. package/types/calculateCursorPosition.d.ts +2 -2
  27. package/types/compressImage.d.ts +1 -1
  28. package/types/dataURLToBlob.d.ts +1 -1
  29. package/types/download.d.ts +3 -3
  30. package/types/fileReader.d.ts +1 -1
  31. package/types/gcd.d.ts +1 -1
  32. package/types/getImageInfo.d.ts +11 -10
  33. package/types/index.d.ts +3 -2
  34. package/types/isBankCard.d.ts +1 -1
  35. package/types/isBusinessLicense.d.ts +1 -1
  36. package/types/isChinese.d.ts +3 -3
  37. package/types/isHMCard.d.ts +1 -1
  38. package/types/isIdCard.d.ts +2 -2
  39. package/types/isPassport.d.ts +1 -1
  40. package/types/isPassword.d.ts +1 -1
  41. package/types/isSocialCreditCode.d.ts +1 -1
  42. package/types/isSwiftCode.d.ts +1 -1
  43. package/types/isTWCard.d.ts +1 -1
  44. package/types/isUrl.d.ts +1 -1
  45. package/types/isVehicle.d.ts +1 -1
  46. package/types/lcm.d.ts +1 -1
  47. package/types/loadImage.d.ts +2 -0
  48. package/types/loadImageWithBlob.d.ts +6 -4
  49. package/types/normalizeString.d.ts +1 -1
  50. package/types/numberToChinese.d.ts +1 -1
  51. package/types/parseIdCard.d.ts +1 -1
  52. package/types/randomString.d.ts +7 -3
  53. package/types/safeDate.d.ts +1 -1
  54. package/types/setDataURLPrefix.d.ts +2 -2
  55. package/types/transformObjectValue.d.ts +67 -0
  56. package/types/validatePassword.d.ts +1 -1
  57. package/esm/utils/Cache.js +0 -47
  58. package/lib/utils/Cache.js +0 -49
@@ -1,11 +1,15 @@
1
+ interface RandomString {
2
+ (len: number, poll: 'number' | 'lower' | 'upper'): string;
3
+ (len: number, poll?: string): string;
4
+ }
1
5
  /**
2
6
  * 生成随机字符串
3
7
  *
4
- * @static
8
+ * @function
5
9
  * @alias module:Other.randomString
6
10
  * @since 4.8.0
7
11
  * @param {number} [len=0] 长度
8
- * @param {string} [optionalChars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'] 允许的字符,默认为数字和大小写字母
12
+ * @param {'number' | 'lower' | 'upper' | string} [pool='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'] 字符池,默认为数字和大小写字母,支持number/lower/upper
9
13
  * @returns {string} 随机字符串
10
14
  * @example
11
15
  *
@@ -17,5 +21,5 @@
17
21
  * randomString(8, 'abcefg'); // bcgcfabg
18
22
  *
19
23
  */
20
- declare function randomString(len?: number, optionalChars?: string): string;
24
+ declare const randomString: RandomString;
21
25
  export default randomString;
@@ -13,7 +13,7 @@ interface SafeDate {
13
13
  * @function
14
14
  * @alias module:Processor.safeDate
15
15
  * @since 4.4.0
16
- * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date | Date}
16
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date Date}
17
17
  * @param {string|number|Date} [value] 日期时间字符串、毫秒数、日期对象
18
18
  * @param {...number} [args] 月/日/时/分/秒/毫秒
19
19
  * @returns {Date} Date 实例日期对象
@@ -6,8 +6,8 @@
6
6
  * @static
7
7
  * @alias module:Processor.setDataURLPrefix
8
8
  * @since 4.1.0
9
- * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | Data URLs}
10
- * @see {@link https://mimesniff.spec.whatwg.org/#understanding-mime-types | MIME types}
9
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/Data_URIs Data URLs}
10
+ * @see {@link https://mimesniff.spec.whatwg.org/#understanding-mime-types MIME types}
11
11
  * @param {string} data 数据本身
12
12
  * @param {string} [mimeType="image/png"] MIME 类型
13
13
  * @param {boolean} [base64=true] 添加 base64 标识
@@ -0,0 +1,67 @@
1
+ type PropertyName = string | symbol;
2
+ interface TransformObjectValue {
3
+ <V>(arr: V[], fn: (value: V, index: number) => any, deep: false): any[];
4
+ <V>(arr: V[], fn: (value: any, index: number) => any, deep?: boolean): any[];
5
+ <T extends object>(obj: T, fn: (value: T[keyof T], key: keyof T) => any, deep: false): Record<PropertyName, any>;
6
+ <T extends object>(obj: T, fn: (value: T[keyof T] extends object ? any : T[keyof T], key: T[keyof T] extends object ? any : keyof T) => any, deep?: boolean): Record<PropertyName, any>;
7
+ <T>(obj: T, fn: (...args: any[]) => any): T;
8
+ }
9
+ /**
10
+ * 创建一个转换值的新对象或数组。
11
+ *
12
+ * 例如 antd Form 中的设置表单值时,如果值为空字符串可能需要转为 `undefined` ,才不影响表单的初始值。
13
+ *
14
+ * @static
15
+ * @alias module:Processor.transformObjectValue
16
+ * @since 4.23.0
17
+ * @param {Array | Object} data 要转换值的普通对象或数组
18
+ * @param {Function} fn 遍历普通对象或数组键值方法
19
+ * @param {boolean} [deep=true] 深度遍历,检测值为普通对象或数组时递归处理。默认 true 。
20
+ * @returns {Array | Object} 如果是普通对象或数组,返回一个新的对象或数组,否则返回自身
21
+ * @example
22
+ *
23
+ * const data1 = { foo: 'bar', baz: 42 }
24
+ * // 数字转为字符串
25
+ * transformObjectValue(data1, value => {
26
+ * if(typeof value === 'number'){
27
+ * return String(value)
28
+ * }
29
+ * return value;
30
+ * });
31
+ * // { foo: 'bar', baz: '42' }
32
+ *
33
+ * const data2 = [1,2,3,4];
34
+ * // 数字转为字符串
35
+ * transformObjectValue(data2, value=>{
36
+ * if(typeof value === 'number'){
37
+ * return String(value)
38
+ * }
39
+ * });
40
+ * // ['1', '2', '3', '4']
41
+ *
42
+ * // 嵌套普通对象或数组
43
+ * const data3 = { foo: 'bar', baz: 42, c: [1,2,3,4], d: '' }
44
+ * // 数字转为字符串,空字符串转为undefined
45
+ * transformObjectValue(data3, value=>{
46
+ * if(typeof value === 'number'){
47
+ * return String(value);
48
+ * }
49
+ * retrun value === '' ? undefined : value
50
+ * });
51
+ * // { foo: 'bar', baz: '42', ['1', '2', '3', '4'], d: undefined }
52
+ *
53
+ * // 数组的值不处理,对象值为数字转为字符串,空字符串转为undefined
54
+ * transformObjectValue(data3, (value, key)=>{
55
+ * if(typeof key === 'number'){
56
+ * return value;
57
+ * }
58
+ * if(typeof value === 'number'){
59
+ * return String(value);
60
+ * }
61
+ * retrun value === '' ? undefined : value
62
+ * });
63
+ * // { foo: 'bar', baz: '42', [1, 2, 3, 4], d: undefined }
64
+ *
65
+ */
66
+ declare const transformObjectValue: TransformObjectValue;
67
+ export default transformObjectValue;
@@ -23,7 +23,7 @@ type Options = {
23
23
  * @static
24
24
  * @alias module:Validator.validatePassword
25
25
  * @since 3.7.0
26
- * @see {@link https://baike.baidu.com/item/ASCII#3 | ASCII}
26
+ * @see {@link https://baike.baidu.com/item/ASCII#3 ASCII}
27
27
  * @param {string} value 要检测的值
28
28
  * @param {Object} [options] 配置项
29
29
  * @param {number} [options.level=2] 密码强度 1-包含一种字符 2-包含两种字符 3-包含三种字符。(大写字母、小写字母、数字、特殊字符)
@@ -1,47 +0,0 @@
1
- import { __extends, __assign } from 'tslib';
2
- import Emitter from 'emitter-pro';
3
- import { forEach } from 'ut2';
4
-
5
- var Cache = (function (_super) {
6
- __extends(Cache, _super);
7
- function Cache(options) {
8
- var _this = _super.call(this) || this;
9
- _this.data = [];
10
- _this.options = __assign({ max: 10 }, options);
11
- return _this;
12
- }
13
- Cache.prototype.has = function (k) {
14
- return !!this.data.find(function (item) { return item.k === k; });
15
- };
16
- Cache.prototype.get = function (k) {
17
- var _a;
18
- return (_a = this.data.find(function (item) { return item.k === k; })) === null || _a === void 0 ? void 0 : _a.v;
19
- };
20
- Cache.prototype.checkLimit = function () {
21
- var _this = this;
22
- if (this.options.max !== 0) {
23
- var limit = this.data.length - this.options.max;
24
- if (limit >= 0) {
25
- var delArr = this.data.splice(0, limit + 1);
26
- forEach(delArr, function (item) {
27
- _this.emit('del', item.v, item.k);
28
- });
29
- }
30
- }
31
- };
32
- Cache.prototype.set = function (k, v) {
33
- var newData = { k: k, v: v };
34
- if (this.has(k)) {
35
- var index = this.data.findIndex(function (item) { return item.k === k; });
36
- this.data.splice(index, 1, newData);
37
- }
38
- else {
39
- this.checkLimit();
40
- this.data.push(newData);
41
- }
42
- };
43
- return Cache;
44
- }(Emitter));
45
- var Cache$1 = Cache;
46
-
47
- export { Cache$1 as default };
@@ -1,49 +0,0 @@
1
- 'use strict';
2
-
3
- var tslib = require('tslib');
4
- var Emitter = require('emitter-pro');
5
- var ut2 = require('ut2');
6
-
7
- var Cache = (function (_super) {
8
- tslib.__extends(Cache, _super);
9
- function Cache(options) {
10
- var _this = _super.call(this) || this;
11
- _this.data = [];
12
- _this.options = tslib.__assign({ max: 10 }, options);
13
- return _this;
14
- }
15
- Cache.prototype.has = function (k) {
16
- return !!this.data.find(function (item) { return item.k === k; });
17
- };
18
- Cache.prototype.get = function (k) {
19
- var _a;
20
- return (_a = this.data.find(function (item) { return item.k === k; })) === null || _a === void 0 ? void 0 : _a.v;
21
- };
22
- Cache.prototype.checkLimit = function () {
23
- var _this = this;
24
- if (this.options.max !== 0) {
25
- var limit = this.data.length - this.options.max;
26
- if (limit >= 0) {
27
- var delArr = this.data.splice(0, limit + 1);
28
- ut2.forEach(delArr, function (item) {
29
- _this.emit('del', item.v, item.k);
30
- });
31
- }
32
- }
33
- };
34
- Cache.prototype.set = function (k, v) {
35
- var newData = { k: k, v: v };
36
- if (this.has(k)) {
37
- var index = this.data.findIndex(function (item) { return item.k === k; });
38
- this.data.splice(index, 1, newData);
39
- }
40
- else {
41
- this.checkLimit();
42
- this.data.push(newData);
43
- }
44
- };
45
- return Cache;
46
- }(Emitter));
47
- var Cache$1 = Cache;
48
-
49
- module.exports = Cache$1;