util-helpers 4.23.0 → 4.23.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.
Files changed (49) hide show
  1. package/dist/util-helpers.js +453 -504
  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/getImageInfo.js +34 -40
  7. package/esm/index.js +1 -1
  8. package/esm/loadImage.js +32 -34
  9. package/esm/loadImageWithBlob.js +23 -24
  10. package/lib/VERSION.js +1 -1
  11. package/lib/getImageInfo.js +33 -39
  12. package/lib/index.js +1 -1
  13. package/lib/loadImage.js +31 -33
  14. package/lib/loadImageWithBlob.js +23 -24
  15. package/package.json +14 -13
  16. package/types/AsyncMemo.d.ts +2 -2
  17. package/types/ajax.d.ts +2 -2
  18. package/types/blobToDataURL.d.ts +1 -1
  19. package/types/calculateCursorPosition.d.ts +2 -2
  20. package/types/compressImage.d.ts +1 -1
  21. package/types/dataURLToBlob.d.ts +1 -1
  22. package/types/download.d.ts +3 -3
  23. package/types/fileReader.d.ts +1 -1
  24. package/types/gcd.d.ts +1 -1
  25. package/types/getImageInfo.d.ts +11 -10
  26. package/types/index.d.ts +2 -2
  27. package/types/isBankCard.d.ts +1 -1
  28. package/types/isBusinessLicense.d.ts +1 -1
  29. package/types/isChinese.d.ts +3 -3
  30. package/types/isHMCard.d.ts +1 -1
  31. package/types/isIdCard.d.ts +2 -2
  32. package/types/isPassport.d.ts +1 -1
  33. package/types/isPassword.d.ts +1 -1
  34. package/types/isSocialCreditCode.d.ts +1 -1
  35. package/types/isSwiftCode.d.ts +1 -1
  36. package/types/isTWCard.d.ts +1 -1
  37. package/types/isUrl.d.ts +1 -1
  38. package/types/isVehicle.d.ts +1 -1
  39. package/types/lcm.d.ts +1 -1
  40. package/types/loadImage.d.ts +2 -0
  41. package/types/loadImageWithBlob.d.ts +6 -4
  42. package/types/normalizeString.d.ts +1 -1
  43. package/types/numberToChinese.d.ts +1 -1
  44. package/types/parseIdCard.d.ts +1 -1
  45. package/types/safeDate.d.ts +1 -1
  46. package/types/setDataURLPrefix.d.ts +2 -2
  47. package/types/validatePassword.d.ts +1 -1
  48. package/esm/utils/Cache.js +0 -47
  49. package/lib/utils/Cache.js +0 -49
package/esm/VERSION.js CHANGED
@@ -1,4 +1,4 @@
1
- var VERSION = "4.23.0";
1
+ var VERSION = "4.23.2";
2
2
  var VERSION$1 = VERSION;
3
3
 
4
4
  export { VERSION$1 as default };
@@ -1,19 +1,19 @@
1
- import { round } from 'ut2';
1
+ import { defaultTo, round } from 'ut2';
2
2
  import divide from './divide.js';
3
3
  import gcd from './gcd.js';
4
4
  import loadImageWithBlob from './loadImageWithBlob.js';
5
5
  import bytesToSize from './bytesToSize.js';
6
- import Cache from './utils/Cache.js';
7
6
  import { revokeObjectURL } from './utils/native.js';
7
+ import AsyncMemo from './AsyncMemo.js';
8
8
 
9
- var cache = new Cache({ max: 1 });
10
- cache.on('del', function (v) {
11
- if (v.r) {
12
- try {
9
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
10
+ asyncMemo.cache.on('del', function (k, v) {
11
+ try {
12
+ if (v.r) {
13
13
  revokeObjectURL(v.data.image.src);
14
14
  }
15
- catch (_a) {
16
- }
15
+ }
16
+ catch (_a) {
17
17
  }
18
18
  });
19
19
  function calcContrast(w, h) {
@@ -22,40 +22,34 @@ function calcContrast(w, h) {
22
22
  }
23
23
  function getImageInfo(img, cacheOptions, ajaxOptions) {
24
24
  if (cacheOptions === void 0) { cacheOptions = true; }
25
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
25
26
  var _cacheOptions = {
26
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
27
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
27
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
28
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
29
+ cacheKey: defaultTo(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
28
30
  };
29
- return new Promise(function (resolve, reject) {
30
- if (_cacheOptions.useCache && cache.has(img)) {
31
- resolve(cache.get(img).data);
32
- }
33
- else {
34
- loadImageWithBlob(img, false, ajaxOptions)
35
- .then(function (_a) {
36
- var image = _a.image, blob = _a.blob;
37
- var width = image.width, height = image.height;
38
- var result = {
39
- width: width,
40
- height: height,
41
- contrast: calcContrast(width, height),
42
- measure: "".concat(width, " \u00D7 ").concat(height, " px"),
43
- size: bytesToSize(blob.size),
44
- bytes: blob.size,
45
- image: image,
46
- blob: blob
47
- };
48
- if (_cacheOptions.useCache) {
49
- cache.set(img, {
50
- data: result,
51
- r: _cacheOptions.autoRevokeOnDel
52
- });
53
- }
54
- resolve(result);
55
- })
56
- .catch(reject);
57
- }
58
- });
31
+ return asyncMemo
32
+ .run(function () {
33
+ return loadImageWithBlob(img, false, ajaxOptions).then(function (_a) {
34
+ var image = _a.image, blob = _a.blob;
35
+ var width = image.width, height = image.height;
36
+ var data = {
37
+ width: width,
38
+ height: height,
39
+ contrast: calcContrast(width, height),
40
+ measure: "".concat(width, " \u00D7 ").concat(height, " px"),
41
+ size: bytesToSize(blob.size),
42
+ bytes: blob.size,
43
+ image: image,
44
+ blob: blob
45
+ };
46
+ return {
47
+ data: data,
48
+ r: _cacheOptions.autoRevokeOnDel
49
+ };
50
+ });
51
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
52
+ .then(function (res) { return res.data; });
59
53
  }
60
54
 
61
55
  export { getImageInfo as default };
package/esm/index.js CHANGED
@@ -66,6 +66,6 @@ export { setDisableWarning } from './utils/config.js';
66
66
  export { default as VERSION } from './VERSION.js';
67
67
  export { default as AsyncMemo } from './AsyncMemo.js';
68
68
 
69
- var version = "4.23.0";
69
+ var version = "4.23.2";
70
70
 
71
71
  export { version };
package/esm/loadImage.js CHANGED
@@ -1,53 +1,51 @@
1
- import { isBlob } from 'ut2';
1
+ import { defaultTo, isBlob } from 'ut2';
2
2
  import { revokeObjectURL, createObjectURL } from './utils/native.js';
3
- import Cache from './utils/Cache.js';
3
+ import AsyncMemo from './AsyncMemo.js';
4
4
 
5
- var cache = new Cache({ max: 1 });
6
- cache.on('del', function (v) {
7
- if (v.r) {
8
- try {
5
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
6
+ asyncMemo.cache.on('del', function (k, v) {
7
+ try {
8
+ if (v.r) {
9
9
  revokeObjectURL(v.data.src);
10
10
  }
11
- catch (_a) {
12
- }
11
+ }
12
+ catch (_a) {
13
13
  }
14
14
  });
15
15
  function loadImage(img, cacheOptions) {
16
16
  if (cacheOptions === void 0) { cacheOptions = true; }
17
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
17
18
  var _cacheOptions = {
18
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
19
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
19
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
20
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
21
+ cacheKey: defaultTo(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
20
22
  };
21
- return new Promise(function (resolve, reject) {
22
- if (_cacheOptions.useCache && cache.has(img)) {
23
- resolve(cache.get(img).data);
24
- }
25
- else {
26
- var imgIsBlob_1 = isBlob(img);
27
- var url_1 = imgIsBlob_1 ? createObjectURL(img) : img;
28
- var image_1 = new Image();
29
- if (!imgIsBlob_1) {
30
- image_1.crossOrigin = 'anonymous';
23
+ return asyncMemo
24
+ .run(function () {
25
+ return new Promise(function (resolve, reject) {
26
+ var imgIsBlob = isBlob(img);
27
+ var url = imgIsBlob ? createObjectURL(img) : img;
28
+ var image = new Image();
29
+ if (!imgIsBlob) {
30
+ image.crossOrigin = 'anonymous';
31
31
  }
32
- image_1.onload = function () {
33
- if (_cacheOptions.useCache) {
34
- cache.set(img, {
35
- data: image_1,
36
- r: _cacheOptions.autoRevokeOnDel
37
- });
38
- }
39
- resolve(image_1);
32
+ image.onload = function () {
33
+ resolve({
34
+ data: image,
35
+ r: _cacheOptions.autoRevokeOnDel
36
+ });
40
37
  };
41
- image_1.onerror = function (err) {
42
- if (imgIsBlob_1) {
43
- revokeObjectURL(url_1);
38
+ image.onerror = function (err) {
39
+ if (imgIsBlob) {
40
+ revokeObjectURL(url);
44
41
  }
45
42
  console.error("[loadImage] The image load failed, '".concat(img, "'."));
46
43
  reject(err);
47
44
  };
48
- image_1.src = url_1;
49
- }
50
- });
45
+ image.src = url;
46
+ });
47
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
48
+ .then(function (res) { return res.data; });
51
49
  }
52
50
 
53
51
  export { loadImage as default };
@@ -1,41 +1,39 @@
1
1
  import { revokeObjectURL, createObjectURL } from './utils/native.js';
2
2
  import getFileBlob from './getFileBlob.js';
3
- import Cache from './utils/Cache.js';
3
+ import AsyncMemo from './AsyncMemo.js';
4
+ import { defaultTo } from 'ut2';
4
5
 
5
- var cache = new Cache({ max: 1 });
6
- cache.on('del', function (v) {
7
- if (v.r) {
8
- try {
6
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
7
+ asyncMemo.cache.on('del', function (k, v) {
8
+ try {
9
+ if (v.r) {
9
10
  revokeObjectURL(v.data.image.src);
10
11
  }
11
- catch (_a) {
12
- }
12
+ }
13
+ catch (_a) {
13
14
  }
14
15
  });
15
16
  function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
16
17
  if (cacheOptions === void 0) { cacheOptions = true; }
18
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
17
19
  var _cacheOptions = {
18
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
19
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
20
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
21
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
22
+ cacheKey: defaultTo(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
20
23
  };
21
- return new Promise(function (resolve, reject) {
22
- if (_cacheOptions.useCache && cache.has(img)) {
23
- resolve(cache.get(img).data);
24
- }
25
- else {
24
+ return asyncMemo
25
+ .run(function () {
26
+ return new Promise(function (resolve, reject) {
26
27
  getFileBlob(img, ajaxOptions)
27
28
  .then(function (blob) {
28
29
  var url = createObjectURL(blob);
29
30
  var image = new Image();
30
31
  image.onload = function () {
31
- var result = { blob: blob, image: image };
32
- if (_cacheOptions.useCache) {
33
- cache.set(img, {
34
- data: result,
35
- r: _cacheOptions.autoRevokeOnDel
36
- });
37
- }
38
- resolve(result);
32
+ var data = { blob: blob, image: image };
33
+ resolve({
34
+ data: data,
35
+ r: _cacheOptions.autoRevokeOnDel
36
+ });
39
37
  };
40
38
  image.onerror = function (err) {
41
39
  revokeObjectURL(url);
@@ -45,8 +43,9 @@ function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
45
43
  image.src = url;
46
44
  })
47
45
  .catch(reject);
48
- }
49
- });
46
+ });
47
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
48
+ .then(function (res) { return res.data; });
50
49
  }
51
50
 
52
51
  export { loadImageWithBlob as default };
package/lib/VERSION.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "4.23.0";
3
+ var VERSION = "4.23.2";
4
4
  var VERSION$1 = VERSION;
5
5
 
6
6
  module.exports = VERSION$1;
@@ -5,17 +5,17 @@ var divide = require('./divide.js');
5
5
  var gcd = require('./gcd.js');
6
6
  var loadImageWithBlob = require('./loadImageWithBlob.js');
7
7
  var bytesToSize = require('./bytesToSize.js');
8
- var Cache = require('./utils/Cache.js');
9
8
  var native = require('./utils/native.js');
9
+ var AsyncMemo = require('./AsyncMemo.js');
10
10
 
11
- var cache = new Cache({ max: 1 });
12
- cache.on('del', function (v) {
13
- if (v.r) {
14
- try {
11
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
12
+ asyncMemo.cache.on('del', function (k, v) {
13
+ try {
14
+ if (v.r) {
15
15
  native.revokeObjectURL(v.data.image.src);
16
16
  }
17
- catch (_a) {
18
- }
17
+ }
18
+ catch (_a) {
19
19
  }
20
20
  });
21
21
  function calcContrast(w, h) {
@@ -24,40 +24,34 @@ function calcContrast(w, h) {
24
24
  }
25
25
  function getImageInfo(img, cacheOptions, ajaxOptions) {
26
26
  if (cacheOptions === void 0) { cacheOptions = true; }
27
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
27
28
  var _cacheOptions = {
28
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
29
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
29
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
30
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
31
+ cacheKey: ut2.defaultTo(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
30
32
  };
31
- return new Promise(function (resolve, reject) {
32
- if (_cacheOptions.useCache && cache.has(img)) {
33
- resolve(cache.get(img).data);
34
- }
35
- else {
36
- loadImageWithBlob(img, false, ajaxOptions)
37
- .then(function (_a) {
38
- var image = _a.image, blob = _a.blob;
39
- var width = image.width, height = image.height;
40
- var result = {
41
- width: width,
42
- height: height,
43
- contrast: calcContrast(width, height),
44
- measure: "".concat(width, " \u00D7 ").concat(height, " px"),
45
- size: bytesToSize(blob.size),
46
- bytes: blob.size,
47
- image: image,
48
- blob: blob
49
- };
50
- if (_cacheOptions.useCache) {
51
- cache.set(img, {
52
- data: result,
53
- r: _cacheOptions.autoRevokeOnDel
54
- });
55
- }
56
- resolve(result);
57
- })
58
- .catch(reject);
59
- }
60
- });
33
+ return asyncMemo
34
+ .run(function () {
35
+ return loadImageWithBlob(img, false, ajaxOptions).then(function (_a) {
36
+ var image = _a.image, blob = _a.blob;
37
+ var width = image.width, height = image.height;
38
+ var data = {
39
+ width: width,
40
+ height: height,
41
+ contrast: calcContrast(width, height),
42
+ measure: "".concat(width, " \u00D7 ").concat(height, " px"),
43
+ size: bytesToSize(blob.size),
44
+ bytes: blob.size,
45
+ image: image,
46
+ blob: blob
47
+ };
48
+ return {
49
+ data: data,
50
+ r: _cacheOptions.autoRevokeOnDel
51
+ };
52
+ });
53
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
54
+ .then(function (res) { return res.data; });
61
55
  }
62
56
 
63
57
  module.exports = getImageInfo;
package/lib/index.js CHANGED
@@ -68,7 +68,7 @@ var config = require('./utils/config.js');
68
68
  var VERSION = require('./VERSION.js');
69
69
  var AsyncMemo = require('./AsyncMemo.js');
70
70
 
71
- exports.version = "4.23.0";
71
+ exports.version = "4.23.2";
72
72
 
73
73
  exports.isMobile = isMobile;
74
74
  exports.isTelephone = isTelephone;
package/lib/loadImage.js CHANGED
@@ -2,54 +2,52 @@
2
2
 
3
3
  var ut2 = require('ut2');
4
4
  var native = require('./utils/native.js');
5
- var Cache = require('./utils/Cache.js');
5
+ var AsyncMemo = require('./AsyncMemo.js');
6
6
 
7
- var cache = new Cache({ max: 1 });
8
- cache.on('del', function (v) {
9
- if (v.r) {
10
- try {
7
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
8
+ asyncMemo.cache.on('del', function (k, v) {
9
+ try {
10
+ if (v.r) {
11
11
  native.revokeObjectURL(v.data.src);
12
12
  }
13
- catch (_a) {
14
- }
13
+ }
14
+ catch (_a) {
15
15
  }
16
16
  });
17
17
  function loadImage(img, cacheOptions) {
18
18
  if (cacheOptions === void 0) { cacheOptions = true; }
19
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
19
20
  var _cacheOptions = {
20
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
21
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
21
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
22
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
23
+ cacheKey: ut2.defaultTo(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
22
24
  };
23
- return new Promise(function (resolve, reject) {
24
- if (_cacheOptions.useCache && cache.has(img)) {
25
- resolve(cache.get(img).data);
26
- }
27
- else {
28
- var imgIsBlob_1 = ut2.isBlob(img);
29
- var url_1 = imgIsBlob_1 ? native.createObjectURL(img) : img;
30
- var image_1 = new Image();
31
- if (!imgIsBlob_1) {
32
- image_1.crossOrigin = 'anonymous';
25
+ return asyncMemo
26
+ .run(function () {
27
+ return new Promise(function (resolve, reject) {
28
+ var imgIsBlob = ut2.isBlob(img);
29
+ var url = imgIsBlob ? native.createObjectURL(img) : img;
30
+ var image = new Image();
31
+ if (!imgIsBlob) {
32
+ image.crossOrigin = 'anonymous';
33
33
  }
34
- image_1.onload = function () {
35
- if (_cacheOptions.useCache) {
36
- cache.set(img, {
37
- data: image_1,
38
- r: _cacheOptions.autoRevokeOnDel
39
- });
40
- }
41
- resolve(image_1);
34
+ image.onload = function () {
35
+ resolve({
36
+ data: image,
37
+ r: _cacheOptions.autoRevokeOnDel
38
+ });
42
39
  };
43
- image_1.onerror = function (err) {
44
- if (imgIsBlob_1) {
45
- native.revokeObjectURL(url_1);
40
+ image.onerror = function (err) {
41
+ if (imgIsBlob) {
42
+ native.revokeObjectURL(url);
46
43
  }
47
44
  console.error("[loadImage] The image load failed, '".concat(img, "'."));
48
45
  reject(err);
49
46
  };
50
- image_1.src = url_1;
51
- }
52
- });
47
+ image.src = url;
48
+ });
49
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
50
+ .then(function (res) { return res.data; });
53
51
  }
54
52
 
55
53
  module.exports = loadImage;
@@ -2,42 +2,40 @@
2
2
 
3
3
  var native = require('./utils/native.js');
4
4
  var getFileBlob = require('./getFileBlob.js');
5
- var Cache = require('./utils/Cache.js');
5
+ var AsyncMemo = require('./AsyncMemo.js');
6
+ var ut2 = require('ut2');
6
7
 
7
- var cache = new Cache({ max: 1 });
8
- cache.on('del', function (v) {
9
- if (v.r) {
10
- try {
8
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
9
+ asyncMemo.cache.on('del', function (k, v) {
10
+ try {
11
+ if (v.r) {
11
12
  native.revokeObjectURL(v.data.image.src);
12
13
  }
13
- catch (_a) {
14
- }
14
+ }
15
+ catch (_a) {
15
16
  }
16
17
  });
17
18
  function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
18
19
  if (cacheOptions === void 0) { cacheOptions = true; }
20
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
19
21
  var _cacheOptions = {
20
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
21
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!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)
22
25
  };
23
- return new Promise(function (resolve, reject) {
24
- if (_cacheOptions.useCache && cache.has(img)) {
25
- resolve(cache.get(img).data);
26
- }
27
- else {
26
+ return asyncMemo
27
+ .run(function () {
28
+ return new Promise(function (resolve, reject) {
28
29
  getFileBlob(img, ajaxOptions)
29
30
  .then(function (blob) {
30
31
  var url = native.createObjectURL(blob);
31
32
  var image = new Image();
32
33
  image.onload = function () {
33
- var result = { blob: blob, image: image };
34
- if (_cacheOptions.useCache) {
35
- cache.set(img, {
36
- data: result,
37
- r: _cacheOptions.autoRevokeOnDel
38
- });
39
- }
40
- resolve(result);
34
+ var data = { blob: blob, image: image };
35
+ resolve({
36
+ data: data,
37
+ r: _cacheOptions.autoRevokeOnDel
38
+ });
41
39
  };
42
40
  image.onerror = function (err) {
43
41
  native.revokeObjectURL(url);
@@ -47,8 +45,9 @@ function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
47
45
  image.src = url;
48
46
  })
49
47
  .catch(reject);
50
- }
51
- });
48
+ });
49
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
50
+ .then(function (res) { return res.data; });
52
51
  }
53
52
 
54
53
  module.exports = loadImageWithBlob;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.23.0",
3
+ "version": "4.23.2",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -51,20 +51,20 @@
51
51
  },
52
52
  "homepage": "https://doly-dev.github.io/util-helpers/index.html",
53
53
  "devDependencies": {
54
- "@babel/core": "^7.24.5",
55
- "@babel/preset-env": "^7.24.5",
56
- "@babel/preset-typescript": "^7.24.1",
54
+ "@babel/core": "^7.24.7",
55
+ "@babel/preset-env": "^7.24.7",
56
+ "@babel/preset-typescript": "^7.24.7",
57
57
  "@commitlint/cli": "^17.8.1",
58
58
  "@commitlint/config-conventional": "^17.8.1",
59
59
  "@commitlint/cz-commitlint": "^17.8.1",
60
- "@rollup/plugin-commonjs": "^25.0.7",
60
+ "@rollup/plugin-commonjs": "^25.0.8",
61
61
  "@rollup/plugin-node-resolve": "^15.2.3",
62
- "@rollup/plugin-replace": "^5.0.5",
62
+ "@rollup/plugin-replace": "^5.0.7",
63
63
  "@rollup/plugin-terser": "^0.4.4",
64
64
  "@rollup/plugin-typescript": "^11.1.6",
65
65
  "@types/jest": "^29.5.12",
66
- "@typescript-eslint/eslint-plugin": "^7.9.0",
67
- "@typescript-eslint/parser": "^7.9.0",
66
+ "@typescript-eslint/eslint-plugin": "^7.13.0",
67
+ "@typescript-eslint/parser": "^7.13.0",
68
68
  "babel-jest": "^29.7.0",
69
69
  "babel-plugin-minify-replace": "^0.5.0",
70
70
  "commitizen": "^4.3.0",
@@ -78,8 +78,8 @@
78
78
  "jest-environment-jsdom": "^29.7.0",
79
79
  "jsdoc": "^4.0.3",
80
80
  "lint-staged": "^13.3.0",
81
- "prettier": "^3.2.5",
82
- "rollup": "^4.17.2",
81
+ "prettier": "^3.3.2",
82
+ "rollup": "^4.18.0",
83
83
  "typescript": "^5.4.5"
84
84
  },
85
85
  "lint-staged": {
@@ -92,12 +92,13 @@
92
92
  }
93
93
  },
94
94
  "dependencies": {
95
- "cache2": "^2.0.5",
95
+ "cache2": "^3.0.0",
96
96
  "emitter-pro": "^1.2.1",
97
- "tslib": "^2.6.2",
97
+ "tslib": "^2.6.3",
98
98
  "ut2": "^1.9.1"
99
99
  },
100
100
  "publishConfig": {
101
- "registry": "https://registry.npmjs.org/"
101
+ "registry": "https://registry.npmjs.org/",
102
+ "tag": "latest-4"
102
103
  }
103
104
  }
@@ -8,7 +8,7 @@ import { Cache, CacheOptions } from 'cache2';
8
8
  * 3. 每个实例都有独立的缓存空间。相互之间隔离,缓存灵活配置,更多配置请查阅 [`cache2`](https://www.npmjs.com/package/cache2)。
9
9
  *
10
10
  * @class
11
- * @see {@link https://www.npmjs.com/package/cache2 | cache2}
11
+ * @see {@link https://www.npmjs.com/package/cache2 cache2}
12
12
  * @param {Object} [options] 缓存配置项,更多配置项可参考 [`cache2`](https://www.npmjs.com/package/cache2)
13
13
  * @param {number} [options.max] 最大缓存数量
14
14
  * @param {'replaced' | 'limited'} [options.maxStrategy] 缓存策略
@@ -42,6 +42,6 @@ declare class AsyncMemo<DataType = any> {
42
42
  run(asyncFn: (...args: any[]) => Promise<DataType>, key?: string, options?: {
43
43
  ttl?: number;
44
44
  persisted?: boolean;
45
- }): Promise<any>;
45
+ }): Promise<DataType>;
46
46
  }
47
47
  export default AsyncMemo;
package/types/ajax.d.ts CHANGED
@@ -19,7 +19,7 @@ type AjaxOptions = {
19
19
  onLoadEnd?: XMLHttpRequestListener;
20
20
  };
21
21
  /**
22
- * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest | XMLHttpRequest}
22
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest XMLHttpRequest}
23
23
  * @typedef {Object} AjaxOptions ajax 配置项
24
24
  * @property {string} [method="get"] 创建请求时使用的方法
25
25
  * @property {boolean} [async=true] 是否异步执行操作
@@ -47,7 +47,7 @@ type AjaxOptions = {
47
47
  * @static
48
48
  * @alias module:Other.ajax
49
49
  * @since 4.16.0
50
- * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest | XMLHttpRequest}
50
+ * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest XMLHttpRequest}
51
51
  * @param {string} url 地址
52
52
  * @param {AjaxOptions} [options] 配置项
53
53
  * @returns {Promise<object>} XHR 事件对象