util-helpers 4.21.1 → 4.21.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.21.1",
3
+ "version": "4.21.2",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -51,17 +51,17 @@
51
51
  },
52
52
  "homepage": "https://doly-dev.github.io/util-helpers/index.html",
53
53
  "devDependencies": {
54
- "@babel/core": "^7.23.0",
55
- "@babel/preset-env": "^7.22.20",
56
- "@babel/preset-typescript": "^7.23.0",
57
- "@commitlint/cli": "^17.7.2",
58
- "@commitlint/config-conventional": "^17.7.0",
59
- "@rollup/plugin-commonjs": "^25.0.5",
54
+ "@babel/core": "^7.23.9",
55
+ "@babel/preset-env": "^7.23.9",
56
+ "@babel/preset-typescript": "^7.23.3",
57
+ "@commitlint/cli": "^17.8.1",
58
+ "@commitlint/config-conventional": "^17.8.1",
59
+ "@rollup/plugin-commonjs": "^25.0.7",
60
60
  "@rollup/plugin-node-resolve": "^15.2.3",
61
- "@rollup/plugin-replace": "^5.0.3",
61
+ "@rollup/plugin-replace": "^5.0.5",
62
62
  "@rollup/plugin-terser": "^0.4.4",
63
- "@rollup/plugin-typescript": "^11.1.5",
64
- "@types/jest": "^29.5.5",
63
+ "@rollup/plugin-typescript": "^11.1.6",
64
+ "@types/jest": "^29.5.12",
65
65
  "@typescript-eslint/eslint-plugin": "^5.62.0",
66
66
  "@typescript-eslint/parser": "^5.62.0",
67
67
  "babel-jest": "^29.7.0",
@@ -69,7 +69,7 @@
69
69
  "cross-env": "^7.0.3",
70
70
  "cz-conventional-changelog": "^3.3.0",
71
71
  "docdash": "^2.0.2",
72
- "eslint": "^8.51.0",
72
+ "eslint": "^8.56.0",
73
73
  "husky": "^8.0.3",
74
74
  "jest": "^29.7.0",
75
75
  "jest-canvas-mock": "^2.5.2",
@@ -78,7 +78,7 @@
78
78
  "lint-staged": "^13.3.0",
79
79
  "prettier": "^2.8.8",
80
80
  "rollup": "^3.29.4",
81
- "typescript": "^5.2.2"
81
+ "typescript": "^5.3.3"
82
82
  },
83
83
  "lint-staged": {
84
84
  "**/*.ts": "eslint",
@@ -90,8 +90,9 @@
90
90
  }
91
91
  },
92
92
  "dependencies": {
93
+ "emitter-pro": "^1.0.3",
93
94
  "tslib": "^2.6.2",
94
- "ut2": "^1.5.2"
95
+ "ut2": "^1.7.1"
95
96
  },
96
97
  "publishConfig": {
97
98
  "registry": "https://registry.npmjs.org/"
@@ -19,7 +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;
22
+ cacheImage?: boolean | Parameters<typeof loadImageWithBlob>[1];
23
23
  ajaxOptions?: Parameters<typeof loadImageWithBlob>[2];
24
24
  };
25
25
  declare function compressImage(img: string | Blob, options: Omit<Options, 'format'> & {
@@ -1,16 +1,25 @@
1
1
  import loadImageWithBlob from './loadImageWithBlob';
2
+ /**
3
+ * @typedef {Object} ImageInfo 图片信息
4
+ * @property {number} width 宽度
5
+ * @property {number} height 高度
6
+ * @property {string} contrast 宽高比
7
+ * @property {string} measure 尺寸
8
+ * @property {string} size 大小
9
+ * @property {number} bytes 大小,单位字节
10
+ * @property {HTMLImageElement} image HTML图片元素
11
+ * @property {Blob} blob 图片 Blob 对象
12
+ */
2
13
  /**
3
14
  * 获取图片信息。
4
15
  *
5
16
  * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
6
17
  *
7
- * <em style="font-weight: bold;">如果不需要该图片,建议手动调用 `URL.revokeObjectURL(image.src)` 释放缓存。</em>
8
- *
9
18
  * @static
10
19
  * @alias module:Other.getImageInfo
11
20
  * @since 4.20.0
12
21
  * @param {string | Blob} img 图片地址或 blob 对象
13
- * @param {boolean} [useCache=true] 缓存最近一次成功结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载问题。
22
+ * @param {boolean | CacheOptions} [cacheOptions=true] 是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 `autoRevokeOnDel` 设置不释放缓存。
14
23
  * @param {AjaxOptions} [ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
15
24
  * @returns {Promise<ImageInfo>} 图片信息
16
25
  * @example
@@ -28,7 +37,7 @@ import loadImageWithBlob from './loadImageWithBlob';
28
37
  * });
29
38
  *
30
39
  */
31
- declare function getImageInfo(img: string | Blob, useCache?: boolean, ajaxOptions?: Parameters<typeof loadImageWithBlob>[2]): Promise<{
40
+ declare function getImageInfo(img: string | Blob, cacheOptions?: boolean | Parameters<typeof loadImageWithBlob>[1], ajaxOptions?: Parameters<typeof loadImageWithBlob>[2]): Promise<{
32
41
  width: number;
33
42
  height: number;
34
43
  contrast: string;
@@ -1,15 +1,18 @@
1
+ /**
2
+ * @typedef {Object} CacheOptions 缓存配置
3
+ * @property {boolean} [useCache=true] 是否使用缓存
4
+ * @property {boolean} [autoRevokeOnDel=true] 删除时自动释放缓存
5
+ */
1
6
  /**
2
7
  * 加载图片。
3
8
  *
4
9
  * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
5
10
  *
6
- * <em style="font-weight: bold;">如果不需要该图片,建议手动调用 `URL.revokeObjectURL(image.src)` 释放缓存。</em>
7
- *
8
11
  * @static
9
12
  * @alias module:Other.loadImage
10
13
  * @since 4.20.0
11
14
  * @param {string | Blob} img 图片地址或 blob 对象
12
- * @param {boolean} [useCache=true] 缓存最近一次成功结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载问题。
15
+ * @param {boolean | CacheOptions} [cacheOptions=true] 是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 `autoRevokeOnDel` 设置不释放缓存。
13
16
  * @returns {Promise<HTMLImageElement>} HTML图片元素
14
17
  * @example
15
18
  *
@@ -26,5 +29,8 @@
26
29
  * });
27
30
  *
28
31
  */
29
- declare function loadImage(img: string | Blob, useCache?: boolean): Promise<HTMLImageElement>;
32
+ declare function loadImage(img: string | Blob, cacheOptions?: boolean | {
33
+ useCache?: boolean;
34
+ autoRevokeOnDel?: boolean;
35
+ }): Promise<HTMLImageElement>;
30
36
  export default loadImage;
@@ -9,13 +9,11 @@ import getFileBlob from './getFileBlob';
9
9
  *
10
10
  * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
11
11
  *
12
- * <em style="font-weight: bold;">如果不需要该图片,建议手动调用 `URL.revokeObjectURL(image.src)` 释放缓存。</em>
13
- *
14
12
  * @method
15
13
  * @alias module:Other.loadImageWithBlob
16
14
  * @since 4.20.0
17
15
  * @param {string | Blob} img 图片地址或 blob 对象
18
- * @param {boolean} [useCache=true] 缓存最近一次成功结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载问题。
16
+ * @param {boolean | CacheOptions} [cacheOptions=true] 是否使用缓存。开启后,自动缓存最近上一次成功的结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载。当缓存下一次成功加载的图片时,会自动释放上一次缓存的图片,也可以通过 `autoRevokeOnDel` 设置不释放缓存。
19
17
  * @param {AjaxOptions} [ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
20
18
  * @returns {Promise<ImageWithBlob>} HTML图片元素和 blob 对象
21
19
  * @example
@@ -33,7 +31,10 @@ import getFileBlob from './getFileBlob';
33
31
  * });
34
32
  *
35
33
  */
36
- declare function loadImageWithBlob(img: string | Blob, useCache?: boolean, ajaxOptions?: Parameters<typeof getFileBlob>[1]): Promise<{
34
+ declare function loadImageWithBlob(img: string | Blob, cacheOptions?: boolean | {
35
+ useCache?: boolean;
36
+ autoRevokeOnDel?: boolean;
37
+ }, ajaxOptions?: Parameters<typeof getFileBlob>[1]): Promise<{
37
38
  image: HTMLImageElement;
38
39
  blob: Blob;
39
40
  }>;
@@ -0,0 +1,14 @@
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;