util-helpers 4.19.4 → 4.20.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.
package/esm/VERSION.js CHANGED
@@ -1,4 +1,4 @@
1
- var VERSION = "4.19.4";
1
+ var VERSION = "4.20.0";
2
2
  var VERSION$1 = VERSION;
3
3
 
4
4
  export { VERSION$1 as default };
@@ -0,0 +1,60 @@
1
+ import { toNumber } from 'ut2';
2
+ import loadImageWithBlob from './loadImageWithBlob.js';
3
+
4
+ function canvasToBlob(canvas, type, quality) {
5
+ return new Promise(function (resolve) {
6
+ canvas.toBlob(function (blob) {
7
+ resolve(blob);
8
+ }, type, quality);
9
+ });
10
+ }
11
+ function compressImage(img, options) {
12
+ if (options === void 0) { options = {}; }
13
+ return new Promise(function (resolve, reject) {
14
+ var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw;
15
+ loadImageWithBlob(img, cacheImage)
16
+ .then(function (_a) {
17
+ var image = _a.image, blob = _a.blob;
18
+ var numWidth = toNumber(width);
19
+ var numHeight = toNumber(height);
20
+ var numQuality = toNumber(quality);
21
+ if (numWidth) {
22
+ image.width = numWidth;
23
+ }
24
+ if (numHeight) {
25
+ image.height = numHeight;
26
+ }
27
+ beforeCompress === null || beforeCompress === void 0 ? void 0 : beforeCompress({ image: image, blob: blob }, options);
28
+ var canvas = document.createElement('canvas');
29
+ var ctx = canvas.getContext('2d');
30
+ var info = { image: image, blob: blob, canvas: canvas, context: ctx };
31
+ var numCanvasWidth = toNumber(typeof canvasWidth === 'function' ? canvasWidth(info, options) : canvasWidth);
32
+ var numCanvasHeight = toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
33
+ canvas.width = numCanvasWidth || image.width;
34
+ canvas.height = numCanvasHeight || image.height;
35
+ if (background && background !== 'none') {
36
+ ctx.fillStyle = background;
37
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
38
+ }
39
+ var internalOffset = [0, 0];
40
+ if (rotate !== undefined) {
41
+ ctx.translate(image.width / 2, image.height / 2);
42
+ internalOffset = [-image.width / 2, -image.height / 2];
43
+ ctx.rotate((rotate * Math.PI) / 180);
44
+ }
45
+ var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
46
+ beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
47
+ ctx.drawImage(image, internalOffset[0] + toNumber(outOffset[0]), internalOffset[1] + toNumber(outOffset[1]), image.width, image.height);
48
+ afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
49
+ if (format === 'blob') {
50
+ canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
51
+ }
52
+ else {
53
+ resolve(canvas.toDataURL(type, numQuality));
54
+ }
55
+ })
56
+ .catch(reject);
57
+ });
58
+ }
59
+
60
+ export { compressImage as default };
package/esm/gcd.js ADDED
@@ -0,0 +1,42 @@
1
+ import { round, isNaN, toNumber } from 'ut2';
2
+
3
+ function gcd() {
4
+ var nums = [];
5
+ for (var _i = 0; _i < arguments.length; _i++) {
6
+ nums[_i] = arguments[_i];
7
+ }
8
+ var num1 = nums[0];
9
+ var num2 = nums[1] === void 0 ? 0 : nums[1];
10
+ var rest = nums.slice(2);
11
+ if (rest.length > 0) {
12
+ return gcd.apply(void 0, [gcd(num1, num2)].concat(rest));
13
+ }
14
+ num1 = Math.abs(round(num1));
15
+ num2 = Math.abs(round(num2));
16
+ if (isNaN(num1) || isNaN(num2)) {
17
+ return Number.NaN;
18
+ }
19
+ if (num1 === 0 && num2 === 0) {
20
+ return 0;
21
+ }
22
+ if (num1 === 0) {
23
+ return num2;
24
+ }
25
+ if (num2 === 0) {
26
+ return num1;
27
+ }
28
+ var temp = num2;
29
+ if (num1 < num2) {
30
+ temp = num1;
31
+ num1 = num2;
32
+ num2 = temp;
33
+ }
34
+ while (temp) {
35
+ temp = num1 % num2;
36
+ num1 = num2;
37
+ num2 = temp;
38
+ }
39
+ return toNumber(num1);
40
+ }
41
+
42
+ export { gcd as default };
@@ -0,0 +1,45 @@
1
+ import { round } from 'ut2';
2
+ import divide from './divide.js';
3
+ import gcd from './gcd.js';
4
+ import loadImageWithBlob from './loadImageWithBlob.js';
5
+ import bytesToSize from './bytesToSize.js';
6
+
7
+ function calcContrast(w, h) {
8
+ var n = gcd(w, h);
9
+ return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
10
+ }
11
+ var cacheImage;
12
+ var cacheResult;
13
+ function getImageInfo(img, useCache) {
14
+ if (useCache === void 0) { useCache = true; }
15
+ return new Promise(function (resolve, reject) {
16
+ if (useCache && cacheImage === img && cacheResult) {
17
+ resolve(cacheResult);
18
+ }
19
+ else {
20
+ loadImageWithBlob(img, false)
21
+ .then(function (_a) {
22
+ var image = _a.image, blob = _a.blob;
23
+ var width = image.width, height = image.height;
24
+ var result = {
25
+ width: width,
26
+ height: height,
27
+ contrast: calcContrast(width, height),
28
+ measure: "".concat(width, " \u00D7 ").concat(height, " px"),
29
+ size: bytesToSize(blob.size),
30
+ bytes: blob.size,
31
+ image: image,
32
+ blob: blob
33
+ };
34
+ if (useCache) {
35
+ cacheImage = img;
36
+ cacheResult = result;
37
+ }
38
+ resolve(result);
39
+ })
40
+ .catch(reject);
41
+ }
42
+ });
43
+ }
44
+
45
+ export { getImageInfo as default };
package/esm/index.js CHANGED
@@ -40,9 +40,15 @@ export { default as minus } from './minus.js';
40
40
  export { default as times } from './times.js';
41
41
  export { default as divide } from './divide.js';
42
42
  export { default as round } from './round.js';
43
+ export { default as gcd } from './gcd.js';
44
+ export { default as lcm } from './lcm.js';
43
45
  export { default as ajax } from './ajax.js';
44
46
  export { default as calculateCursorPosition } from './calculateCursorPosition.js';
47
+ export { default as compressImage } from './compressImage.js';
45
48
  export { default as download } from './download.js';
49
+ export { default as getImageInfo } from './getImageInfo.js';
50
+ export { default as loadImage } from './loadImage.js';
51
+ export { default as loadImageWithBlob } from './loadImageWithBlob.js';
46
52
  export { default as loadScript } from './loadScript.js';
47
53
  export { default as randomString } from './randomString.js';
48
54
  export { default as strlen } from './strlen.js';
@@ -57,6 +63,6 @@ export { default as findTreeSelect } from './findTreeSelect.js';
57
63
  export { setDisableWarning } from './utils/config.js';
58
64
  export { default as VERSION } from './VERSION.js';
59
65
 
60
- var version = "4.19.4";
66
+ var version = "4.20.0";
61
67
 
62
68
  export { version };
package/esm/lcm.js ADDED
@@ -0,0 +1,20 @@
1
+ import { __spreadArray, __read } from 'tslib';
2
+ import divide from './divide.js';
3
+ import times from './times.js';
4
+ import gcd from './gcd.js';
5
+ import { round } from 'ut2';
6
+
7
+ function lcm() {
8
+ var nums = [];
9
+ for (var _i = 0; _i < arguments.length; _i++) {
10
+ nums[_i] = arguments[_i];
11
+ }
12
+ var args = nums.map(function (item) { return Math.abs(round(item)); });
13
+ if (args.length === 1) {
14
+ args = args.concat([1]);
15
+ }
16
+ var product = args.indexOf(0) > -1 ? 0 : times.apply(void 0, __spreadArray([], __read(args), false));
17
+ return divide(product, gcd.apply(void 0, __spreadArray([], __read(args), false)));
18
+ }
19
+
20
+ export { lcm as default };
@@ -0,0 +1,40 @@
1
+ import { isBlob } from 'ut2';
2
+
3
+ var cacheImage;
4
+ var cacheResult;
5
+ function loadImage(img, useCache) {
6
+ if (useCache === void 0) { useCache = true; }
7
+ return new Promise(function (resolve, reject) {
8
+ if (useCache && cacheImage === img && cacheResult) {
9
+ resolve(cacheResult);
10
+ }
11
+ else {
12
+ var imgIsBlob_1 = isBlob(img);
13
+ var url_1 = imgIsBlob_1 ? URL.createObjectURL(img) : img;
14
+ var image_1 = new Image();
15
+ if (!imgIsBlob_1) {
16
+ image_1.crossOrigin = 'anonymous';
17
+ }
18
+ image_1.onload = function () {
19
+ if (imgIsBlob_1) {
20
+ URL.revokeObjectURL(url_1);
21
+ }
22
+ if (useCache) {
23
+ cacheImage = img;
24
+ cacheResult = image_1;
25
+ }
26
+ resolve(image_1);
27
+ };
28
+ image_1.onerror = function (err) {
29
+ if (imgIsBlob_1) {
30
+ URL.revokeObjectURL(url_1);
31
+ }
32
+ console.error("[loadImage] The image load failed, '".concat(img, "'."));
33
+ reject(err);
34
+ };
35
+ image_1.src = url_1;
36
+ }
37
+ });
38
+ }
39
+
40
+ export { loadImage as default };
@@ -0,0 +1,59 @@
1
+ import { isBlob } from 'ut2';
2
+ import ajax from './ajax.js';
3
+
4
+ var SuccessResponseStatus = [200, 304];
5
+ function getBlob(img) {
6
+ return new Promise(function (resolve, reject) {
7
+ if (isBlob(img)) {
8
+ resolve(img);
9
+ }
10
+ else {
11
+ ajax(img, { responseType: 'blob' })
12
+ .then(function (ev) {
13
+ var responseStatus = ev.target.status;
14
+ if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
15
+ resolve(ev.target.response);
16
+ }
17
+ else {
18
+ reject(new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'.")));
19
+ }
20
+ })
21
+ .catch(reject);
22
+ }
23
+ });
24
+ }
25
+ var cacheImage;
26
+ var cacheResult;
27
+ function loadImageWithBlob(img, useCache) {
28
+ if (useCache === void 0) { useCache = true; }
29
+ return new Promise(function (resolve, reject) {
30
+ if (useCache && cacheImage === img && cacheResult) {
31
+ resolve(cacheResult);
32
+ }
33
+ else {
34
+ getBlob(img)
35
+ .then(function (blob) {
36
+ var url = URL.createObjectURL(blob);
37
+ var image = new Image();
38
+ image.onload = function () {
39
+ URL.revokeObjectURL(url);
40
+ var result = { blob: blob, image: image };
41
+ if (useCache) {
42
+ cacheImage = img;
43
+ cacheResult = result;
44
+ }
45
+ resolve(result);
46
+ };
47
+ image.onerror = function (err) {
48
+ URL.revokeObjectURL(url);
49
+ console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
50
+ reject(err);
51
+ };
52
+ image.src = url;
53
+ })
54
+ .catch(reject);
55
+ }
56
+ });
57
+ }
58
+
59
+ export { loadImageWithBlob as default };
package/lib/VERSION.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "4.19.4";
3
+ var VERSION = "4.20.0";
4
4
  var VERSION$1 = VERSION;
5
5
 
6
6
  module.exports = VERSION$1;
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ var ut2 = require('ut2');
4
+ var loadImageWithBlob = require('./loadImageWithBlob.js');
5
+
6
+ function canvasToBlob(canvas, type, quality) {
7
+ return new Promise(function (resolve) {
8
+ canvas.toBlob(function (blob) {
9
+ resolve(blob);
10
+ }, type, quality);
11
+ });
12
+ }
13
+ function compressImage(img, options) {
14
+ if (options === void 0) { options = {}; }
15
+ return new Promise(function (resolve, reject) {
16
+ var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw;
17
+ loadImageWithBlob(img, cacheImage)
18
+ .then(function (_a) {
19
+ var image = _a.image, blob = _a.blob;
20
+ var numWidth = ut2.toNumber(width);
21
+ var numHeight = ut2.toNumber(height);
22
+ var numQuality = ut2.toNumber(quality);
23
+ if (numWidth) {
24
+ image.width = numWidth;
25
+ }
26
+ if (numHeight) {
27
+ image.height = numHeight;
28
+ }
29
+ beforeCompress === null || beforeCompress === void 0 ? void 0 : beforeCompress({ image: image, blob: blob }, options);
30
+ var canvas = document.createElement('canvas');
31
+ var ctx = canvas.getContext('2d');
32
+ var info = { image: image, blob: blob, canvas: canvas, context: ctx };
33
+ var numCanvasWidth = ut2.toNumber(typeof canvasWidth === 'function' ? canvasWidth(info, options) : canvasWidth);
34
+ var numCanvasHeight = ut2.toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
35
+ canvas.width = numCanvasWidth || image.width;
36
+ canvas.height = numCanvasHeight || image.height;
37
+ if (background && background !== 'none') {
38
+ ctx.fillStyle = background;
39
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
40
+ }
41
+ var internalOffset = [0, 0];
42
+ if (rotate !== undefined) {
43
+ ctx.translate(image.width / 2, image.height / 2);
44
+ internalOffset = [-image.width / 2, -image.height / 2];
45
+ ctx.rotate((rotate * Math.PI) / 180);
46
+ }
47
+ var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
48
+ beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
49
+ ctx.drawImage(image, internalOffset[0] + ut2.toNumber(outOffset[0]), internalOffset[1] + ut2.toNumber(outOffset[1]), image.width, image.height);
50
+ afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
51
+ if (format === 'blob') {
52
+ canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
53
+ }
54
+ else {
55
+ resolve(canvas.toDataURL(type, numQuality));
56
+ }
57
+ })
58
+ .catch(reject);
59
+ });
60
+ }
61
+
62
+ module.exports = compressImage;
package/lib/gcd.js ADDED
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ var ut2 = require('ut2');
4
+
5
+ function gcd() {
6
+ var nums = [];
7
+ for (var _i = 0; _i < arguments.length; _i++) {
8
+ nums[_i] = arguments[_i];
9
+ }
10
+ var num1 = nums[0];
11
+ var num2 = nums[1] === void 0 ? 0 : nums[1];
12
+ var rest = nums.slice(2);
13
+ if (rest.length > 0) {
14
+ return gcd.apply(void 0, [gcd(num1, num2)].concat(rest));
15
+ }
16
+ num1 = Math.abs(ut2.round(num1));
17
+ num2 = Math.abs(ut2.round(num2));
18
+ if (ut2.isNaN(num1) || ut2.isNaN(num2)) {
19
+ return Number.NaN;
20
+ }
21
+ if (num1 === 0 && num2 === 0) {
22
+ return 0;
23
+ }
24
+ if (num1 === 0) {
25
+ return num2;
26
+ }
27
+ if (num2 === 0) {
28
+ return num1;
29
+ }
30
+ var temp = num2;
31
+ if (num1 < num2) {
32
+ temp = num1;
33
+ num1 = num2;
34
+ num2 = temp;
35
+ }
36
+ while (temp) {
37
+ temp = num1 % num2;
38
+ num1 = num2;
39
+ num2 = temp;
40
+ }
41
+ return ut2.toNumber(num1);
42
+ }
43
+
44
+ module.exports = gcd;
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ var ut2 = require('ut2');
4
+ var divide = require('./divide.js');
5
+ var gcd = require('./gcd.js');
6
+ var loadImageWithBlob = require('./loadImageWithBlob.js');
7
+ var bytesToSize = require('./bytesToSize.js');
8
+
9
+ function calcContrast(w, h) {
10
+ var n = gcd(w, h);
11
+ return "".concat(divide(ut2.round(w), n), ":").concat(divide(ut2.round(h), n));
12
+ }
13
+ var cacheImage;
14
+ var cacheResult;
15
+ function getImageInfo(img, useCache) {
16
+ if (useCache === void 0) { useCache = true; }
17
+ return new Promise(function (resolve, reject) {
18
+ if (useCache && cacheImage === img && cacheResult) {
19
+ resolve(cacheResult);
20
+ }
21
+ else {
22
+ loadImageWithBlob(img, false)
23
+ .then(function (_a) {
24
+ var image = _a.image, blob = _a.blob;
25
+ var width = image.width, height = image.height;
26
+ var result = {
27
+ width: width,
28
+ height: height,
29
+ contrast: calcContrast(width, height),
30
+ measure: "".concat(width, " \u00D7 ").concat(height, " px"),
31
+ size: bytesToSize(blob.size),
32
+ bytes: blob.size,
33
+ image: image,
34
+ blob: blob
35
+ };
36
+ if (useCache) {
37
+ cacheImage = img;
38
+ cacheResult = result;
39
+ }
40
+ resolve(result);
41
+ })
42
+ .catch(reject);
43
+ }
44
+ });
45
+ }
46
+
47
+ module.exports = getImageInfo;
package/lib/index.js CHANGED
@@ -42,9 +42,15 @@ var minus = require('./minus.js');
42
42
  var times = require('./times.js');
43
43
  var divide = require('./divide.js');
44
44
  var round = require('./round.js');
45
+ var gcd = require('./gcd.js');
46
+ var lcm = require('./lcm.js');
45
47
  var ajax = require('./ajax.js');
46
48
  var calculateCursorPosition = require('./calculateCursorPosition.js');
49
+ var compressImage = require('./compressImage.js');
47
50
  var download = require('./download.js');
51
+ var getImageInfo = require('./getImageInfo.js');
52
+ var loadImage = require('./loadImage.js');
53
+ var loadImageWithBlob = require('./loadImageWithBlob.js');
48
54
  var loadScript = require('./loadScript.js');
49
55
  var randomString = require('./randomString.js');
50
56
  var strlen = require('./strlen.js');
@@ -59,7 +65,7 @@ var findTreeSelect = require('./findTreeSelect.js');
59
65
  var config = require('./utils/config.js');
60
66
  var VERSION = require('./VERSION.js');
61
67
 
62
- exports.version = "4.19.4";
68
+ exports.version = "4.20.0";
63
69
 
64
70
  exports.isMobile = isMobile;
65
71
  exports.isTelephone = isTelephone;
@@ -103,9 +109,15 @@ exports.minus = minus;
103
109
  exports.times = times;
104
110
  exports.divide = divide;
105
111
  exports.round = round;
112
+ exports.gcd = gcd;
113
+ exports.lcm = lcm;
106
114
  exports.ajax = ajax;
107
115
  exports.calculateCursorPosition = calculateCursorPosition;
116
+ exports.compressImage = compressImage;
108
117
  exports.download = download;
118
+ exports.getImageInfo = getImageInfo;
119
+ exports.loadImage = loadImage;
120
+ exports.loadImageWithBlob = loadImageWithBlob;
109
121
  exports.loadScript = loadScript;
110
122
  exports.randomString = randomString;
111
123
  exports.strlen = strlen;
package/lib/lcm.js ADDED
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ var tslib = require('tslib');
4
+ var divide = require('./divide.js');
5
+ var times = require('./times.js');
6
+ var gcd = require('./gcd.js');
7
+ var ut2 = require('ut2');
8
+
9
+ function lcm() {
10
+ var nums = [];
11
+ for (var _i = 0; _i < arguments.length; _i++) {
12
+ nums[_i] = arguments[_i];
13
+ }
14
+ var args = nums.map(function (item) { return Math.abs(ut2.round(item)); });
15
+ if (args.length === 1) {
16
+ args = args.concat([1]);
17
+ }
18
+ var product = args.indexOf(0) > -1 ? 0 : times.apply(void 0, tslib.__spreadArray([], tslib.__read(args), false));
19
+ return divide(product, gcd.apply(void 0, tslib.__spreadArray([], tslib.__read(args), false)));
20
+ }
21
+
22
+ module.exports = lcm;
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ var ut2 = require('ut2');
4
+
5
+ var cacheImage;
6
+ var cacheResult;
7
+ function loadImage(img, useCache) {
8
+ if (useCache === void 0) { useCache = true; }
9
+ return new Promise(function (resolve, reject) {
10
+ if (useCache && cacheImage === img && cacheResult) {
11
+ resolve(cacheResult);
12
+ }
13
+ else {
14
+ var imgIsBlob_1 = ut2.isBlob(img);
15
+ var url_1 = imgIsBlob_1 ? URL.createObjectURL(img) : img;
16
+ var image_1 = new Image();
17
+ if (!imgIsBlob_1) {
18
+ image_1.crossOrigin = 'anonymous';
19
+ }
20
+ image_1.onload = function () {
21
+ if (imgIsBlob_1) {
22
+ URL.revokeObjectURL(url_1);
23
+ }
24
+ if (useCache) {
25
+ cacheImage = img;
26
+ cacheResult = image_1;
27
+ }
28
+ resolve(image_1);
29
+ };
30
+ image_1.onerror = function (err) {
31
+ if (imgIsBlob_1) {
32
+ URL.revokeObjectURL(url_1);
33
+ }
34
+ console.error("[loadImage] The image load failed, '".concat(img, "'."));
35
+ reject(err);
36
+ };
37
+ image_1.src = url_1;
38
+ }
39
+ });
40
+ }
41
+
42
+ module.exports = loadImage;
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ var ut2 = require('ut2');
4
+ var ajax = require('./ajax.js');
5
+
6
+ var SuccessResponseStatus = [200, 304];
7
+ function getBlob(img) {
8
+ return new Promise(function (resolve, reject) {
9
+ if (ut2.isBlob(img)) {
10
+ resolve(img);
11
+ }
12
+ else {
13
+ ajax(img, { responseType: 'blob' })
14
+ .then(function (ev) {
15
+ var responseStatus = ev.target.status;
16
+ if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
17
+ resolve(ev.target.response);
18
+ }
19
+ else {
20
+ reject(new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'.")));
21
+ }
22
+ })
23
+ .catch(reject);
24
+ }
25
+ });
26
+ }
27
+ var cacheImage;
28
+ var cacheResult;
29
+ function loadImageWithBlob(img, useCache) {
30
+ if (useCache === void 0) { useCache = true; }
31
+ return new Promise(function (resolve, reject) {
32
+ if (useCache && cacheImage === img && cacheResult) {
33
+ resolve(cacheResult);
34
+ }
35
+ else {
36
+ getBlob(img)
37
+ .then(function (blob) {
38
+ var url = URL.createObjectURL(blob);
39
+ var image = new Image();
40
+ image.onload = function () {
41
+ URL.revokeObjectURL(url);
42
+ var result = { blob: blob, image: image };
43
+ if (useCache) {
44
+ cacheImage = img;
45
+ cacheResult = result;
46
+ }
47
+ resolve(result);
48
+ };
49
+ image.onerror = function (err) {
50
+ URL.revokeObjectURL(url);
51
+ console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
52
+ reject(err);
53
+ };
54
+ image.src = url;
55
+ })
56
+ .catch(reject);
57
+ }
58
+ });
59
+ }
60
+
61
+ module.exports = loadImageWithBlob;