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
package/esm/VERSION.js CHANGED
@@ -1,4 +1,4 @@
1
- var VERSION = "4.22.2";
1
+ var VERSION = "4.23.1";
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
@@ -35,6 +35,7 @@ export { default as normalizeString } from './normalizeString.js';
35
35
  export { default as safeDate } from './safeDate.js';
36
36
  export { default as formatMobile } from './formatMobile.js';
37
37
  export { default as padZero } from './padZero.js';
38
+ export { default as transformObjectValue } from './transformObjectValue.js';
38
39
  export { default as plus } from './plus.js';
39
40
  export { default as minus } from './minus.js';
40
41
  export { default as times } from './times.js';
@@ -65,6 +66,6 @@ export { setDisableWarning } from './utils/config.js';
65
66
  export { default as VERSION } from './VERSION.js';
66
67
  export { default as AsyncMemo } from './AsyncMemo.js';
67
68
 
68
- var version = "4.22.2";
69
+ var version = "4.23.1";
69
70
 
70
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 };
@@ -1,20 +1,34 @@
1
- import { toNumber } from 'ut2';
1
+ import { toNumber, randomInt } from 'ut2';
2
2
 
3
- var numberChars = '0123456789';
4
- var letterChars = 'abcdefghijklmnopqrstuvwxyz';
5
- var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
6
- function internalRandomString(len, optionalChars, prefix) {
3
+ var letter = 'abcdefghijklmnopqrstuvwxyz';
4
+ var chars = {
5
+ number: '0123456789',
6
+ lower: letter,
7
+ upper: letter.toUpperCase()
8
+ };
9
+ var allChars = chars.number + chars.lower + chars.upper;
10
+ function internalRandomString(len, pool, prefix) {
7
11
  if (prefix === void 0) { prefix = ''; }
8
12
  while (len-- > 0) {
9
- var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
10
- return internalRandomString(len, optionalChars, prefix + r);
13
+ var r = pool[randomInt(0, pool.length - 1)];
14
+ return internalRandomString(len, pool, prefix + r);
11
15
  }
12
16
  return prefix;
13
17
  }
14
- function randomString(len, optionalChars) {
18
+ var randomString = function (len, pool) {
15
19
  if (len === void 0) { len = 0; }
16
- var realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
17
- return internalRandomString(toNumber(len), realChars);
18
- }
20
+ var _pool;
21
+ if (typeof pool !== 'string') {
22
+ _pool = allChars;
23
+ }
24
+ else if (chars[pool]) {
25
+ _pool = chars[pool];
26
+ }
27
+ else {
28
+ _pool = pool;
29
+ }
30
+ return internalRandomString(toNumber(len), _pool);
31
+ };
32
+ var randomString$1 = randomString;
19
33
 
20
- export { randomString as default };
34
+ export { randomString$1 as default };
@@ -0,0 +1,25 @@
1
+ import { isPlainObject, forEach, isArray } from 'ut2';
2
+
3
+ var transformObjectValue = function (data, fn, deep) {
4
+ if (deep === void 0) { deep = true; }
5
+ if (isPlainObject(data)) {
6
+ var result_1 = {};
7
+ forEach(data, function (value, key) {
8
+ var newValue = deep && (isPlainObject(value) || isArray(value)) ? transformObjectValue(value, fn) : fn(value, key);
9
+ result_1[key] = newValue;
10
+ });
11
+ return result_1;
12
+ }
13
+ else if (isArray(data)) {
14
+ var result_2 = [];
15
+ forEach(data, function (value, index) {
16
+ var newValue = deep && (isPlainObject(value) || isArray(value)) ? transformObjectValue(value, fn) : fn(value, index);
17
+ result_2.push(newValue);
18
+ });
19
+ return result_2;
20
+ }
21
+ return data;
22
+ };
23
+ var transformObjectValue$1 = transformObjectValue;
24
+
25
+ export { transformObjectValue$1 as default };
@@ -1,18 +1,9 @@
1
1
  import devWarn from './utils/devWarn.js';
2
2
 
3
- var regNumber = /[\d]/;
3
+ var regNumber = /\d/;
4
4
  var regLowerCaseLetter = /[a-z]/;
5
5
  var regUpperCaseLetter = /[A-Z]/;
6
6
  var regAllNumberAndLetter = /[\d|a-z]/gi;
7
- function hasNumber(val) {
8
- return regNumber.test(val);
9
- }
10
- function hasLowerCaseLetter(val) {
11
- return regLowerCaseLetter.test(val);
12
- }
13
- function hasUpperCaseLetter(val) {
14
- return regUpperCaseLetter.test(val);
15
- }
16
7
  function hasHex(val) {
17
8
  return val.indexOf('\\x') > -1 || val.indexOf('\\u') > -1;
18
9
  }
@@ -70,9 +61,9 @@ function validatePassword(value, options) {
70
61
  valStr = '';
71
62
  }
72
63
  var currentLevel = 0;
73
- var containesNumber = hasNumber(valStr);
74
- var containesLowerCaseLetter = hasLowerCaseLetter(valStr);
75
- var containesUpperCaseLetter = hasUpperCaseLetter(valStr);
64
+ var containesNumber = regNumber.test(valStr);
65
+ var containesLowerCaseLetter = regLowerCaseLetter.test(valStr);
66
+ var containesUpperCaseLetter = regUpperCaseLetter.test(valStr);
76
67
  var containesSpecialCharacter = hasSpecialCharacter(valStr, special);
77
68
  var containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
78
69
  if (containesNumber) {
package/lib/VERSION.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "4.22.2";
3
+ var VERSION = "4.23.1";
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
@@ -37,6 +37,7 @@ var normalizeString = require('./normalizeString.js');
37
37
  var safeDate = require('./safeDate.js');
38
38
  var formatMobile = require('./formatMobile.js');
39
39
  var padZero = require('./padZero.js');
40
+ var transformObjectValue = require('./transformObjectValue.js');
40
41
  var plus = require('./plus.js');
41
42
  var minus = require('./minus.js');
42
43
  var times = require('./times.js');
@@ -67,7 +68,7 @@ var config = require('./utils/config.js');
67
68
  var VERSION = require('./VERSION.js');
68
69
  var AsyncMemo = require('./AsyncMemo.js');
69
70
 
70
- exports.version = "4.22.2";
71
+ exports.version = "4.23.1";
71
72
 
72
73
  exports.isMobile = isMobile;
73
74
  exports.isTelephone = isTelephone;
@@ -106,6 +107,7 @@ exports.normalizeString = normalizeString;
106
107
  exports.safeDate = safeDate;
107
108
  exports.formatMobile = formatMobile;
108
109
  exports.padZero = padZero;
110
+ exports.transformObjectValue = transformObjectValue;
109
111
  exports.plus = plus;
110
112
  exports.minus = minus;
111
113
  exports.times = times;
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;