util-helpers 4.23.1 → 5.0.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 (47) hide show
  1. package/dist/util-helpers.js +844 -961
  2. package/dist/util-helpers.js.map +1 -1
  3. package/dist/util-helpers.min.js +1 -1
  4. package/dist/util-helpers.min.js.map +1 -1
  5. package/esm/VERSION.js +1 -1
  6. package/esm/compressImage.js +2 -2
  7. package/esm/formatBankCard.js +3 -5
  8. package/esm/formatMobile.js +4 -6
  9. package/esm/getImageInfo.js +16 -43
  10. package/esm/index.js +0 -8
  11. package/esm/isBusinessLicense.js +1 -2
  12. package/esm/isSocialCreditCode.js +1 -3
  13. package/esm/loadImage.js +20 -45
  14. package/esm/loadImageWithBlob.js +19 -46
  15. package/lib/VERSION.js +1 -1
  16. package/lib/compressImage.js +2 -2
  17. package/lib/formatBankCard.js +3 -5
  18. package/lib/formatMobile.js +4 -6
  19. package/lib/getImageInfo.js +15 -42
  20. package/lib/index.js +1 -9
  21. package/lib/isBusinessLicense.js +1 -2
  22. package/lib/isSocialCreditCode.js +1 -3
  23. package/lib/loadImage.js +18 -43
  24. package/lib/loadImageWithBlob.js +18 -45
  25. package/package.json +1 -1
  26. package/types/compressImage.d.ts +2 -4
  27. package/types/formatBankCard.d.ts +1 -3
  28. package/types/formatMobile.d.ts +0 -2
  29. package/types/getImageInfo.d.ts +10 -12
  30. package/types/index.d.ts +0 -8
  31. package/types/isBusinessLicense.d.ts +0 -2
  32. package/types/isSocialCreditCode.d.ts +0 -2
  33. package/types/loadImage.d.ts +1 -12
  34. package/types/loadImageWithBlob.d.ts +6 -10
  35. package/esm/blobToDataURL.js +0 -7
  36. package/esm/isPromiseLike.js +0 -7
  37. package/esm/normalizeString.js +0 -7
  38. package/esm/waitTime.js +0 -8
  39. package/lib/blobToDataURL.js +0 -9
  40. package/lib/isPromiseLike.js +0 -9
  41. package/lib/normalizeString.js +0 -9
  42. package/lib/waitTime.js +0 -10
  43. package/types/blobToDataURL.d.ts +0 -29
  44. package/types/isPromiseLike.d.ts +0 -19
  45. package/types/normalizeString.d.ts +0 -26
  46. package/types/utils/Cache.d.ts +0 -14
  47. package/types/waitTime.d.ts +0 -24
@@ -2,52 +2,25 @@
2
2
 
3
3
  var native = require('./utils/native.js');
4
4
  var getFileBlob = require('./getFileBlob.js');
5
- var AsyncMemo = require('./AsyncMemo.js');
6
- var ut2 = require('ut2');
7
5
 
8
- var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
9
- asyncMemo.cache.on('del', function (k, v) {
10
- try {
11
- if (v.r) {
12
- native.revokeObjectURL(v.data.image.src);
13
- }
14
- }
15
- catch (_a) {
16
- }
17
- });
18
- function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
19
- if (cacheOptions === void 0) { cacheOptions = true; }
20
- var cacheOptionsIsObject = typeof cacheOptions === 'object';
21
- var _cacheOptions = {
22
- useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
23
- autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
24
- cacheKey: ut2.defaultTo(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
25
- };
26
- return asyncMemo
27
- .run(function () {
28
- return new Promise(function (resolve, reject) {
29
- getFileBlob(img, ajaxOptions)
30
- .then(function (blob) {
31
- var url = native.createObjectURL(blob);
32
- var image = new Image();
33
- image.onload = function () {
34
- var data = { blob: blob, image: image };
35
- resolve({
36
- data: data,
37
- r: _cacheOptions.autoRevokeOnDel
38
- });
39
- };
40
- image.onerror = function (err) {
41
- native.revokeObjectURL(url);
42
- console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
43
- reject(err);
44
- };
45
- image.src = url;
46
- })
47
- .catch(reject);
48
- });
49
- }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
50
- .then(function (res) { return res.data; });
6
+ function loadImageWithBlob(img, ajaxOptions) {
7
+ return new Promise(function (resolve, reject) {
8
+ getFileBlob(img, ajaxOptions)
9
+ .then(function (blob) {
10
+ var url = native.createObjectURL(blob);
11
+ var image = new Image();
12
+ image.onload = function () {
13
+ resolve({ blob: blob, image: image });
14
+ };
15
+ image.onerror = function (err) {
16
+ native.revokeObjectURL(url);
17
+ console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
18
+ reject(err);
19
+ };
20
+ image.src = url;
21
+ })
22
+ .catch(reject);
23
+ });
51
24
  }
52
25
 
53
26
  module.exports = loadImageWithBlob;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.23.1",
3
+ "version": "5.0.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -19,8 +19,7 @@ type Options = {
19
19
  beforeCompress?: (imageWithBlob: Pick<Info, 'image' | 'blob'>, options: Options) => void;
20
20
  beforeDraw?: (info: Info, options: Options) => void;
21
21
  afterDraw?: (info: Info, options: Options) => void;
22
- cacheImage?: boolean | Parameters<typeof loadImageWithBlob>[1];
23
- ajaxOptions?: Parameters<typeof loadImageWithBlob>[2];
22
+ ajaxOptions?: Parameters<typeof loadImageWithBlob>[1];
24
23
  };
25
24
  interface CompressImage {
26
25
  (img: string | Blob, options: Omit<Options, 'format'> & {
@@ -54,8 +53,7 @@ interface CompressImage {
54
53
  * @param {function} [options.beforeCompress] 图片加载完成,画布创建之前调用
55
54
  * @param {function} [options.beforeDraw] 图片载入画布之前调用
56
55
  * @param {function} [options.afterDraw] 图片载入画布之后调用
57
- * @param {boolean | CacheOptions} [options.cacheImage=true] 是否使用 `loadImageWithBlob` 缓存。
58
- * @param {AjaxOptions} [options.ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
56
+ * @param {AjaxOptions} [options.ajaxOptions] ajax 请求配置项,当传入图片地址时才会触发ajax请求。
59
57
  * @returns {Promise<Blob | string>} blob 对象 或 data url 图片
60
58
  * @example
61
59
  *
@@ -1,6 +1,4 @@
1
1
  type Options = {
2
- /** @deprecated */
3
- char?: string;
4
2
  spaceMark?: string;
5
3
  length?: number;
6
4
  };
@@ -26,7 +24,7 @@ type Options = {
26
24
  * // 脱敏银行卡
27
25
  * formatBankCard('6228********890'); // 6228 **** **** 890
28
26
  *
29
- * // 16位银行卡,"-"间隔
27
+ * // 自定义间隔符
30
28
  * formatBankCard('6228480402564890', {spaceMark: '-'}); // 6228-4804-0256-4890
31
29
  *
32
30
  */
@@ -1,6 +1,4 @@
1
1
  type Options = {
2
- /** @deprecated */
3
- char?: string;
4
2
  spaceMark?: string;
5
3
  };
6
4
  /**
@@ -1,14 +1,4 @@
1
1
  import loadImageWithBlob from './loadImageWithBlob';
2
- type Result = {
3
- width: number;
4
- height: number;
5
- contrast: string;
6
- measure: string;
7
- size: string;
8
- bytes: number;
9
- image: HTMLImageElement;
10
- blob: Blob;
11
- };
12
2
  /**
13
3
  * @typedef {Object} ImageInfo 图片信息
14
4
  * @property {number} width 宽度
@@ -29,7 +19,6 @@ type Result = {
29
19
  * @alias module:Other.getImageInfo
30
20
  * @since 4.20.0
31
21
  * @param {string | Blob} img 图片地址或 blob 对象
32
- * @param {boolean | CacheOptions} [cacheOptions=true] 是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 `autoRevokeOnDel` 设置不释放缓存。
33
22
  * @param {AjaxOptions} [ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
34
23
  * @returns {Promise<ImageInfo>} 图片信息
35
24
  * @example
@@ -47,5 +36,14 @@ type Result = {
47
36
  * });
48
37
  *
49
38
  */
50
- declare function getImageInfo(img: string | Blob, cacheOptions?: boolean | Parameters<typeof loadImageWithBlob>[1], ajaxOptions?: Parameters<typeof loadImageWithBlob>[2]): Promise<Result>;
39
+ declare function getImageInfo(img: string | Blob, ajaxOptions?: Parameters<typeof loadImageWithBlob>[1]): Promise<{
40
+ width: number;
41
+ height: number;
42
+ contrast: string;
43
+ measure: string;
44
+ size: string;
45
+ bytes: number;
46
+ image: HTMLImageElement;
47
+ blob: Blob;
48
+ }>;
51
49
  export default getImageInfo;
package/types/index.d.ts CHANGED
@@ -22,7 +22,6 @@ export { default as isIPv6 } from './isIPv6';
22
22
  export { default as isUrl } from './isUrl';
23
23
  export { default as isBusinessLicense } from './isBusinessLicense';
24
24
  export { default as validatePassword } from './validatePassword';
25
- export { default as isPromiseLike } from './isPromiseLike';
26
25
  export { default as isHMCard } from './isHMCard';
27
26
  export { default as isTWCard } from './isTWCard';
28
27
  export { default as isSwiftCode } from './isSwiftCode';
@@ -39,11 +38,9 @@ export { default as replaceChar } from './replaceChar';
39
38
  export { default as numberToChinese } from './numberToChinese';
40
39
  export { default as bytesToSize } from './bytesToSize';
41
40
  export { default as parseIdCard } from './parseIdCard';
42
- export { default as blobToDataURL } from './blobToDataURL';
43
41
  export { default as fileReader } from './fileReader';
44
42
  export { default as dataURLToBlob } from './dataURLToBlob';
45
43
  export { default as setDataURLPrefix } from './setDataURLPrefix';
46
- export { default as normalizeString } from './normalizeString';
47
44
  export { default as safeDate } from './safeDate';
48
45
  export { default as formatMobile } from './formatMobile';
49
46
  export { default as padZero } from './padZero';
@@ -111,7 +108,6 @@ export { default as loadImageWithBlob } from './loadImageWithBlob';
111
108
  export { default as loadScript } from './loadScript';
112
109
  export { default as randomString } from './randomString';
113
110
  export { default as strlen } from './strlen';
114
- export { default as waitTime } from './waitTime';
115
111
  /**
116
112
  * 树结构数据查询、过滤、转换等处理方法
117
113
  *
@@ -135,8 +131,4 @@ export { default as findTreeSelect } from './findTreeSelect';
135
131
  export { setDisableWarning } from './utils/config';
136
132
  import VERSION from './VERSION';
137
133
  export { VERSION };
138
- /**
139
- * @deprecated 即将废弃,请使用 `VERSION`
140
- */
141
- export declare const version: string;
142
134
  export { default as AsyncMemo } from './AsyncMemo';
@@ -1,6 +1,4 @@
1
1
  type Options = {
2
- /** @deprecated */
3
- loose?: boolean;
4
2
  checkCode?: boolean;
5
3
  };
6
4
  /**
@@ -1,6 +1,4 @@
1
1
  type Options = {
2
- /** @deprecated */
3
- loose?: boolean;
4
2
  checkCode?: boolean;
5
3
  };
6
4
  /**
@@ -1,9 +1,3 @@
1
- /**
2
- * @typedef {Object} CacheOptions 缓存配置
3
- * @property {boolean} [useCache=true] 是否使用缓存
4
- * @property {boolean} [autoRevokeOnDel=true] 删除时自动释放缓存
5
- * @property {string} [cacheKey] 缓存键
6
- */
7
1
  /**
8
2
  * 加载图片。
9
3
  *
@@ -13,7 +7,6 @@
13
7
  * @alias module:Other.loadImage
14
8
  * @since 4.20.0
15
9
  * @param {string | Blob} img 图片地址或 blob 对象
16
- * @param {boolean | CacheOptions} [cacheOptions=true] 是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 `autoRevokeOnDel` 设置不释放缓存。
17
10
  * @returns {Promise<HTMLImageElement>} HTML图片元素
18
11
  * @example
19
12
  *
@@ -30,9 +23,5 @@
30
23
  * });
31
24
  *
32
25
  */
33
- declare function loadImage(img: string | Blob, cacheOptions?: boolean | {
34
- useCache?: boolean;
35
- cacheKey?: string;
36
- autoRevokeOnDel?: boolean;
37
- }): Promise<HTMLImageElement>;
26
+ declare function loadImage(img: string | Blob): Promise<HTMLImageElement>;
38
27
  export default loadImage;
@@ -1,8 +1,4 @@
1
1
  import getFileBlob from './getFileBlob';
2
- type Result = {
3
- image: HTMLImageElement;
4
- blob: Blob;
5
- };
6
2
  /**
7
3
  * @typedef {Object} ImageWithBlob HTML图片元素和 blob 对象
8
4
  * @property {HTMLImageElement} image HTML图片元素
@@ -11,13 +7,14 @@ type Result = {
11
7
  /**
12
8
  * 加载图片,返回图片元素和 blob 对象。
13
9
  *
10
+ * 如果传入图片地址,将通过 ajax 请求转为 blob 格式。
11
+ *
14
12
  * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
15
13
  *
16
14
  * @method
17
15
  * @alias module:Other.loadImageWithBlob
18
16
  * @since 4.20.0
19
17
  * @param {string | Blob} img 图片地址或 blob 对象
20
- * @param {boolean | CacheOptions} [cacheOptions=true] 是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 `autoRevokeOnDel` 设置不释放缓存。
21
18
  * @param {AjaxOptions} [ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
22
19
  * @returns {Promise<ImageWithBlob>} HTML图片元素和 blob 对象
23
20
  * @example
@@ -35,9 +32,8 @@ type Result = {
35
32
  * });
36
33
  *
37
34
  */
38
- declare function loadImageWithBlob(img: string | Blob, cacheOptions?: boolean | {
39
- useCache?: boolean;
40
- cacheKey?: string;
41
- autoRevokeOnDel?: boolean;
42
- }, ajaxOptions?: Parameters<typeof getFileBlob>[1]): Promise<Result>;
35
+ declare function loadImageWithBlob(img: string | Blob, ajaxOptions?: Parameters<typeof getFileBlob>[1]): Promise<{
36
+ image: HTMLImageElement;
37
+ blob: Blob;
38
+ }>;
43
39
  export default loadImageWithBlob;
@@ -1,7 +0,0 @@
1
- import fileReader from './fileReader.js';
2
-
3
- function blobToDataURL(blob) {
4
- return fileReader(blob);
5
- }
6
-
7
- export { blobToDataURL as default };
@@ -1,7 +0,0 @@
1
- import { isPromiseLike as isPromiseLike$1 } from 'ut2';
2
-
3
- function isPromiseLike(obj) {
4
- return isPromiseLike$1(obj);
5
- }
6
-
7
- export { isPromiseLike as default };
@@ -1,7 +0,0 @@
1
- import { toString } from 'ut2';
2
-
3
- function normalizeString(value) {
4
- return toString(value);
5
- }
6
-
7
- export { normalizeString as default };
package/esm/waitTime.js DELETED
@@ -1,8 +0,0 @@
1
- import { sleep } from 'ut2';
2
-
3
- function waitTime(time) {
4
- if (time === void 0) { time = 1000; }
5
- return sleep(time);
6
- }
7
-
8
- export { waitTime as default };
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- var fileReader = require('./fileReader.js');
4
-
5
- function blobToDataURL(blob) {
6
- return fileReader(blob);
7
- }
8
-
9
- module.exports = blobToDataURL;
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- var ut2 = require('ut2');
4
-
5
- function isPromiseLike(obj) {
6
- return ut2.isPromiseLike(obj);
7
- }
8
-
9
- module.exports = isPromiseLike;
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- var ut2 = require('ut2');
4
-
5
- function normalizeString(value) {
6
- return ut2.toString(value);
7
- }
8
-
9
- module.exports = normalizeString;
package/lib/waitTime.js DELETED
@@ -1,10 +0,0 @@
1
- 'use strict';
2
-
3
- var ut2 = require('ut2');
4
-
5
- function waitTime(time) {
6
- if (time === void 0) { time = 1000; }
7
- return ut2.sleep(time);
8
- }
9
-
10
- module.exports = waitTime;
@@ -1,29 +0,0 @@
1
- /**
2
- * 将 Blob 或 File 对象转成 data:URL 格式的 Base64 字符串
3
- *
4
- * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
5
- *
6
- * @ignore
7
- * @static
8
- * @alias module:Processor.blobToDataURL
9
- * @since 4.1.0
10
- * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader/readAsDataURL FileReader.readAsDataURL()}
11
- * @deprecated 请使用 `fileReader` 方法
12
- * @param {Blob} blob Blob 或 File 对象
13
- * @returns {Promise<string>} data:URL 格式的 Base64 字符串。
14
- * @example
15
- * const aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // 一个包含DOMString的数组
16
- * const htmlBlob = new Blob(aFileParts, { type: 'text/html' }); // 得到 blob
17
- *
18
- * blobToDataURL(htmlBlob).then(data=>{
19
- * console.log(data); // data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
20
- * });
21
- *
22
- * const textBlob = new Blob(aFileParts, { type: 'text/plain' });
23
- *
24
- * blobToDataURL(textBlob).then(data=>{
25
- * console.log(data); // data:text/plain;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
26
- * });
27
- */
28
- declare function blobToDataURL(blob: Blob): Promise<string>;
29
- export default blobToDataURL;
@@ -1,19 +0,0 @@
1
- /**
2
- * 检测值是否类似Promise对象
3
- *
4
- * @ignore
5
- * @static
6
- * @alias module:Validator.isPromiseLike
7
- * @since 3.8.0
8
- * @deprecated 即将废弃,请使用 `import { isPromiseLike } 'ut2'`
9
- * @param {*} obj 要检测的值
10
- * @returns {boolean} 是否类似Promise对象
11
- * @example
12
- *
13
- * isPromiseLike([]); // false
14
- * isPromiseLike({ then: () => { } }); // true
15
- * isPromiseLike(Promise.resolve()); // true
16
- *
17
- */
18
- declare function isPromiseLike(obj: any): boolean;
19
- export default isPromiseLike;
@@ -1,26 +0,0 @@
1
- /**
2
- * 规整化字符串。如果值为 undefined 或 null 将转为空字符串,如果值不是字符串类型将转为字符串。
3
- *
4
- * @ignore
5
- * @static
6
- * @alias module:Processor.normalizeString
7
- * @since 4.3.0
8
- * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances String}
9
- * @deprecated 即将废弃,请使用 `import { toString } 'ut2'`
10
- * @param {*} value 待处理的值
11
- * @returns {string} 规整化的值
12
- * @example
13
- *
14
- * normalizeString(); // ''
15
- * normalizeString(undefined); // ''
16
- * normalizeString(void 0); // ''
17
- * normalizeString(null); // ''
18
- *
19
- * normalizeString(true); // 'true'
20
- * normalizeString(NaN); // 'NaN'
21
- * normalizeString(1); // '1'
22
- * normalizeString('a'); // 'a'
23
- *
24
- */
25
- declare function normalizeString(value: any): string;
26
- export default normalizeString;
@@ -1,14 +0,0 @@
1
- import Emitter from 'emitter-pro';
2
- type Options = {
3
- max: number;
4
- };
5
- declare class Cache<V = any, K = any> extends Emitter<(v: V, k: K) => void> {
6
- private data;
7
- private options;
8
- constructor(options?: Options);
9
- has(k: K): boolean;
10
- get(k: K): V | undefined;
11
- private checkLimit;
12
- set(k: K, v: V): void;
13
- }
14
- export default Cache;
@@ -1,24 +0,0 @@
1
- /**
2
- * 等待时间返回 Promise 。常用于异步方法中延时。
3
- *
4
- * @ignore
5
- * @static
6
- * @alias module:Other.waitTime
7
- * @since 4.2.0
8
- * @deprecated 即将废弃,请使用 `import { sleep } from 'ut2'`
9
- * @param {number} [time=1000] 延时时间,单位毫秒
10
- * @returns {Promise<void>}
11
- * @example
12
- *
13
- * const test = async ()=>{
14
- * await waitTime();
15
- * // do something
16
- * }
17
- *
18
- * waitTime(500).then(()=>{
19
- * // do something
20
- * });
21
- *
22
- */
23
- declare function waitTime(time?: number): Promise<void>;
24
- export default waitTime;