util-helpers 4.19.4 → 4.20.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.
- package/README.md +6 -0
- package/dist/util-helpers.js +269 -120
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/VERSION.js +1 -1
- package/esm/compressImage.js +60 -0
- package/esm/gcd.js +42 -0
- package/esm/getImageInfo.js +45 -0
- package/esm/index.js +7 -1
- package/esm/lcm.js +20 -0
- package/esm/loadImage.js +40 -0
- package/esm/loadImageWithBlob.js +59 -0
- package/lib/VERSION.js +1 -1
- package/lib/compressImage.js +62 -0
- package/lib/gcd.js +44 -0
- package/lib/getImageInfo.js +47 -0
- package/lib/index.js +13 -1
- package/lib/lcm.js +22 -0
- package/lib/loadImage.js +42 -0
- package/lib/loadImageWithBlob.js +61 -0
- package/package.json +23 -22
- package/types/compressImage.d.ts +27 -0
- package/types/divide.d.ts +1 -1
- package/types/download.d.ts +2 -0
- package/types/filterTree.d.ts +4 -3
- package/types/findTreeNode.d.ts +4 -4
- package/types/findTreeNodes.d.ts +4 -4
- package/types/findTreeSelect.d.ts +3 -3
- package/types/gcd.d.ts +29 -0
- package/types/getImageInfo.d.ts +37 -0
- package/types/index.d.ts +6 -0
- package/types/lcm.d.ts +31 -0
- package/types/listToTree.d.ts +2 -2
- package/types/loadImage.d.ts +28 -0
- package/types/loadImageWithBlob.d.ts +36 -0
- package/types/loadScript.d.ts +5 -3
- package/types/minus.d.ts +1 -1
- package/types/plus.d.ts +1 -1
- package/types/times.d.ts +1 -1
- package/types/treeToList.d.ts +2 -2
package/README.md
CHANGED
|
@@ -47,6 +47,8 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
|
|
|
47
47
|
- [times](https://doly-dev.github.io/util-helpers/module-Math.html#.times) - 乘
|
|
48
48
|
- [divide](https://doly-dev.github.io/util-helpers/module-Math.html#.divide) - 除
|
|
49
49
|
- [round](https://doly-dev.github.io/util-helpers/module-Math.html#.round) - 四舍五入
|
|
50
|
+
- [gcd](https://doly-dev.github.io/util-helpers/module-Math.html#.gcd) - 最大公约数
|
|
51
|
+
- [lcm](https://doly-dev.github.io/util-helpers/module-Math.html#.lcm) - 最小公倍数
|
|
50
52
|
- 数据处理
|
|
51
53
|
- [bytesToSize](https://doly-dev.github.io/util-helpers/module-Processor.html#.bytesToSize) - 字节转换为存储单位
|
|
52
54
|
- [dataURLToBlob](https://doly-dev.github.io/util-helpers/module-Processor.html#.dataURLToBlob) - 将 DataURL 转为 Blob 对象
|
|
@@ -94,7 +96,11 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
|
|
|
94
96
|
- 其他
|
|
95
97
|
- [ajax](https://doly-dev.github.io/util-helpers/module-Other.html#.ajax) - 请求
|
|
96
98
|
- [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
|
|
99
|
+
- [compressImage](https://doly-dev.github.io/util-helpers/module-Other.html#.compressImage) - 压缩图片
|
|
97
100
|
- [download](https://doly-dev.github.io/util-helpers/module-Other.html#.download) - 下载
|
|
101
|
+
- [getImageInfo](https://doly-dev.github.io/util-helpers/module-Other.html#.getImageInfo) - 获取图片信息
|
|
102
|
+
- [loadImage](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImage) - 加载图片
|
|
103
|
+
- [loadImageWithBlob](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImageWithBlob) - 加载图片和 blob 对象
|
|
98
104
|
- [loadScript](https://doly-dev.github.io/util-helpers/module-Other.html#.loadScript) - 加载 js 文件
|
|
99
105
|
- [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
|
|
100
106
|
- [strlen](https://doly-dev.github.io/util-helpers/module-Other.html#.strlen) - 字符长度
|
package/dist/util-helpers.js
CHANGED
|
@@ -8,127 +8,24 @@
|
|
|
8
8
|
return Array.isArray(value);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
var freeGlobalThis = typeof globalThis === 'object' && globalThis && globalThis.Object === Object && globalThis;
|
|
12
|
-
var freeGlobal = typeof global === 'object' && global && global.Object === Object && global;
|
|
13
|
-
var freeSelf = typeof self === 'object' && self && self.Object === Object && self;
|
|
14
|
-
var root = freeGlobalThis || freeGlobal || freeSelf || Function('return this')() || {};
|
|
15
11
|
var objectProto = Object.prototype;
|
|
16
12
|
var objectToString = objectProto.toString;
|
|
17
|
-
var hasOwnProperty = objectProto.hasOwnProperty;
|
|
18
|
-
var functionToString = Function.prototype.toString;
|
|
19
|
-
functionToString.call(Object);
|
|
20
13
|
var symbolProto = Symbol ? Symbol.prototype : undefined;
|
|
21
|
-
|
|
22
|
-
symbolProto ? symbolProto.toString : undefined;
|
|
23
|
-
var symToStringTag = Symbol ? Symbol.toStringTag : undefined;
|
|
14
|
+
var numberIsFinite = Number.isFinite;
|
|
24
15
|
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
|
|
25
16
|
var MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -9007199254740991;
|
|
26
|
-
var
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return functionToString.call(func);
|
|
31
|
-
}
|
|
32
|
-
catch (e) {
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
return func + '';
|
|
36
|
-
}
|
|
37
|
-
catch (e) {
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return '';
|
|
41
|
-
}
|
|
42
|
-
var objectTag = '[object Object]';
|
|
43
|
-
var dataViewTag = '[object DataView]';
|
|
44
|
-
var mapTag = '[object Map]';
|
|
45
|
-
var promiseTag = '[object Promise]';
|
|
46
|
-
var setTag = '[object Set]';
|
|
47
|
-
var weakMapTag = '[object WeakMap]';
|
|
48
|
-
var dataViewExisted = typeof DataView !== 'undefined';
|
|
49
|
-
var mapExisted = typeof Map !== 'undefined';
|
|
50
|
-
var promiseExisted = typeof Promise !== 'undefined';
|
|
51
|
-
var setExisted = typeof Set !== 'undefined';
|
|
52
|
-
var weakMapExisted = typeof WeakMap !== 'undefined';
|
|
53
|
-
var initSource = function (existed, str) { return (existed ? str : ''); };
|
|
54
|
-
var dataViewCtorString = initSource(dataViewExisted, toSource(DataView));
|
|
55
|
-
var mapCtorString = initSource(mapExisted, toSource(Map));
|
|
56
|
-
var promiseCtorString = initSource(promiseExisted, toSource(Promise));
|
|
57
|
-
var setCtorString = initSource(setExisted, toSource(Set));
|
|
58
|
-
var weakMapCtorString = initSource(weakMapExisted, toSource(WeakMap));
|
|
17
|
+
var numberTag = '[object Number]';
|
|
18
|
+
var stringTag = '[object String]';
|
|
19
|
+
var symbolTag = '[object Symbol]';
|
|
20
|
+
var blobTag = '[object Blob]';
|
|
59
21
|
|
|
60
22
|
function isObject(value) {
|
|
61
23
|
var type = typeof value;
|
|
62
24
|
return value != null && (type === 'object' || type === 'function');
|
|
63
25
|
}
|
|
64
26
|
|
|
65
|
-
function getRawTag(value) {
|
|
66
|
-
var isOwn = hasOwnProperty.call(value, symToStringTag);
|
|
67
|
-
var tag = value[symToStringTag];
|
|
68
|
-
var unmasked = false;
|
|
69
|
-
try {
|
|
70
|
-
value[symToStringTag] = undefined;
|
|
71
|
-
unmasked = true;
|
|
72
|
-
}
|
|
73
|
-
catch (e) {
|
|
74
|
-
}
|
|
75
|
-
var result = objectToString.call(value);
|
|
76
|
-
if (unmasked) {
|
|
77
|
-
if (isOwn) {
|
|
78
|
-
value[symToStringTag] = tag;
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
delete value[symToStringTag];
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
86
|
-
function _getTag(value) {
|
|
87
|
-
return symToStringTag && symToStringTag in Object(value) ? getRawTag(value) : objectToString.call(value);
|
|
88
|
-
}
|
|
89
|
-
var getTag = _getTag;
|
|
90
|
-
if ((dataViewExisted && objectToString.call(new DataView(new ArrayBuffer(1))) !== dataViewTag) ||
|
|
91
|
-
(mapExisted && objectToString.call(new Map()) !== mapTag) ||
|
|
92
|
-
(promiseExisted && objectToString.call(Promise.resolve()) !== promiseTag) ||
|
|
93
|
-
(setExisted && objectToString.call(new Set()) !== setTag) ||
|
|
94
|
-
(weakMapExisted && objectToString.call(new WeakMap()) !== weakMapTag)) {
|
|
95
|
-
getTag = function (value) {
|
|
96
|
-
var result = _getTag(value);
|
|
97
|
-
var Ctor = result === objectTag ? value.constructor : undefined;
|
|
98
|
-
var ctorString = Ctor ? toSource(Ctor) : '';
|
|
99
|
-
if (ctorString) {
|
|
100
|
-
switch (ctorString) {
|
|
101
|
-
case dataViewCtorString:
|
|
102
|
-
return dataViewTag;
|
|
103
|
-
case mapCtorString:
|
|
104
|
-
return mapTag;
|
|
105
|
-
case promiseCtorString:
|
|
106
|
-
return promiseTag;
|
|
107
|
-
case setCtorString:
|
|
108
|
-
return setTag;
|
|
109
|
-
case weakMapCtorString:
|
|
110
|
-
return weakMapTag;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return result;
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
var getTag$1 = getTag;
|
|
117
|
-
|
|
118
|
-
function isType(value, type) {
|
|
119
|
-
var nativeTypeString = getTag$1(value);
|
|
120
|
-
if (typeof type === 'string') {
|
|
121
|
-
return nativeTypeString === '[object ' + type + ']';
|
|
122
|
-
}
|
|
123
|
-
return type.some(function (item) { return nativeTypeString === '[object ' + item + ']'; });
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function isObjectLike(value) {
|
|
127
|
-
return value != null && typeof value === 'object';
|
|
128
|
-
}
|
|
129
|
-
|
|
130
27
|
function isSymbol(value) {
|
|
131
|
-
return typeof value === 'symbol' || (
|
|
28
|
+
return typeof value === 'symbol' || objectToString.call(value) === symbolTag;
|
|
132
29
|
}
|
|
133
30
|
|
|
134
31
|
var reIsBinary = /^0b[01]+$/i;
|
|
@@ -165,9 +62,7 @@
|
|
|
165
62
|
return remainder ? result - remainder : result;
|
|
166
63
|
}
|
|
167
64
|
|
|
168
|
-
var
|
|
169
|
-
isType((function () { return arguments; })(), argType);
|
|
170
|
-
var numberIsFinite = Number.isFinite;
|
|
65
|
+
var symbolToString = symbolProto ? symbolProto.toString : undefined;
|
|
171
66
|
|
|
172
67
|
function baseToString(value) {
|
|
173
68
|
if (typeof value === 'string') {
|
|
@@ -177,7 +72,7 @@
|
|
|
177
72
|
return "".concat(value.map(baseToString));
|
|
178
73
|
}
|
|
179
74
|
if (isSymbol(value)) {
|
|
180
|
-
return
|
|
75
|
+
return symbolToString ? symbolToString.call(value) : '';
|
|
181
76
|
}
|
|
182
77
|
var result = '' + value;
|
|
183
78
|
return result == '0' && 1 / value === -Infinity ? '-0' : result;
|
|
@@ -186,20 +81,30 @@
|
|
|
186
81
|
return value == null ? '' : baseToString(value);
|
|
187
82
|
}
|
|
188
83
|
|
|
84
|
+
var freeGlobalThis = typeof globalThis === 'object' && globalThis && globalThis.Object === Object && globalThis;
|
|
85
|
+
var freeGlobal = typeof global === 'object' && global && global.Object === Object && global;
|
|
86
|
+
var freeSelf = typeof self === 'object' && self && self.Object === Object && self;
|
|
87
|
+
var root = freeGlobalThis || freeGlobal || freeSelf || Function('return this')() || {};
|
|
88
|
+
var root$1 = root;
|
|
89
|
+
|
|
90
|
+
var blobExisted = typeof Blob !== 'undefined';
|
|
189
91
|
function isBlob(value) {
|
|
190
|
-
|
|
92
|
+
if (blobExisted && value instanceof Blob) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return objectToString.call(value) === blobTag;
|
|
191
96
|
}
|
|
192
97
|
|
|
193
98
|
function isFinite$1(value) {
|
|
194
|
-
return numberIsFinite ? numberIsFinite(value) : typeof value === 'number' && root.isFinite(value);
|
|
99
|
+
return numberIsFinite ? numberIsFinite(value) : typeof value === 'number' && root$1.isFinite(value);
|
|
195
100
|
}
|
|
196
101
|
|
|
197
102
|
function isNumber(value) {
|
|
198
|
-
return typeof value === 'number' || (
|
|
103
|
+
return typeof value === 'number' || objectToString.call(value) === numberTag;
|
|
199
104
|
}
|
|
200
105
|
|
|
201
106
|
function isNaN$1(value) {
|
|
202
|
-
return isNumber(value) && root.isNaN(value);
|
|
107
|
+
return isNumber(value) && root$1.isNaN(value);
|
|
203
108
|
}
|
|
204
109
|
|
|
205
110
|
function isPromiseLike$1(value) {
|
|
@@ -207,7 +112,7 @@
|
|
|
207
112
|
}
|
|
208
113
|
|
|
209
114
|
function isString(value) {
|
|
210
|
-
return typeof value === 'string' || (
|
|
115
|
+
return typeof value === 'string' || objectToString.call(value) === stringTag;
|
|
211
116
|
}
|
|
212
117
|
|
|
213
118
|
function decimalAdjust(type, value, precision) {
|
|
@@ -1327,6 +1232,58 @@
|
|
|
1327
1232
|
return round(num, precision);
|
|
1328
1233
|
}
|
|
1329
1234
|
|
|
1235
|
+
function gcd() {
|
|
1236
|
+
var nums = [];
|
|
1237
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1238
|
+
nums[_i] = arguments[_i];
|
|
1239
|
+
}
|
|
1240
|
+
var num1 = nums[0];
|
|
1241
|
+
var num2 = nums[1] === void 0 ? 0 : nums[1];
|
|
1242
|
+
var rest = nums.slice(2);
|
|
1243
|
+
if (rest.length > 0) {
|
|
1244
|
+
return gcd.apply(void 0, [gcd(num1, num2)].concat(rest));
|
|
1245
|
+
}
|
|
1246
|
+
num1 = Math.abs(round(num1));
|
|
1247
|
+
num2 = Math.abs(round(num2));
|
|
1248
|
+
if (isNaN$1(num1) || isNaN$1(num2)) {
|
|
1249
|
+
return Number.NaN;
|
|
1250
|
+
}
|
|
1251
|
+
if (num1 === 0 && num2 === 0) {
|
|
1252
|
+
return 0;
|
|
1253
|
+
}
|
|
1254
|
+
if (num1 === 0) {
|
|
1255
|
+
return num2;
|
|
1256
|
+
}
|
|
1257
|
+
if (num2 === 0) {
|
|
1258
|
+
return num1;
|
|
1259
|
+
}
|
|
1260
|
+
var temp = num2;
|
|
1261
|
+
if (num1 < num2) {
|
|
1262
|
+
temp = num1;
|
|
1263
|
+
num1 = num2;
|
|
1264
|
+
num2 = temp;
|
|
1265
|
+
}
|
|
1266
|
+
while (temp) {
|
|
1267
|
+
temp = num1 % num2;
|
|
1268
|
+
num1 = num2;
|
|
1269
|
+
num2 = temp;
|
|
1270
|
+
}
|
|
1271
|
+
return toNumber(num1);
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
function lcm() {
|
|
1275
|
+
var nums = [];
|
|
1276
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1277
|
+
nums[_i] = arguments[_i];
|
|
1278
|
+
}
|
|
1279
|
+
var args = nums.map(function (item) { return Math.abs(round(item)); });
|
|
1280
|
+
if (args.length === 1) {
|
|
1281
|
+
args = args.concat([1]);
|
|
1282
|
+
}
|
|
1283
|
+
var product = args.indexOf(0) > -1 ? 0 : times.apply(void 0, __spreadArray([], __read(args), false));
|
|
1284
|
+
return divide(product, gcd.apply(void 0, __spreadArray([], __read(args), false)));
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1330
1287
|
function ajax(url, options) {
|
|
1331
1288
|
var _a = options || {}, _b = _a.method, method = _b === void 0 ? 'get' : _b, _c = _a.data, data = _c === void 0 ? null : _c, timeout = _a.timeout, headers = _a.headers, _d = _a.withCredentials, withCredentials = _d === void 0 ? false : _d, _e = _a.async, async = _e === void 0 ? true : _e, _f = _a.user, user = _f === void 0 ? null : _f, _g = _a.password, password = _g === void 0 ? null : _g, responseType = _a.responseType, onReadyStateChange = _a.onReadyStateChange, onLoadStart = _a.onLoadStart, onProgress = _a.onProgress, onAbort = _a.onAbort, onTimeout = _a.onTimeout, onError = _a.onError, onLoad = _a.onLoad, onLoadEnd = _a.onLoadEnd;
|
|
1332
1289
|
return new Promise(function (resolve, reject) {
|
|
@@ -1409,6 +1366,117 @@
|
|
|
1409
1366
|
return pos;
|
|
1410
1367
|
}
|
|
1411
1368
|
|
|
1369
|
+
var SuccessResponseStatus = [200, 304];
|
|
1370
|
+
function getBlob(img) {
|
|
1371
|
+
return new Promise(function (resolve, reject) {
|
|
1372
|
+
if (isBlob(img)) {
|
|
1373
|
+
resolve(img);
|
|
1374
|
+
}
|
|
1375
|
+
else {
|
|
1376
|
+
ajax(img, { responseType: 'blob' })
|
|
1377
|
+
.then(function (ev) {
|
|
1378
|
+
var responseStatus = ev.target.status;
|
|
1379
|
+
if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
|
|
1380
|
+
resolve(ev.target.response);
|
|
1381
|
+
}
|
|
1382
|
+
else {
|
|
1383
|
+
reject(new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'.")));
|
|
1384
|
+
}
|
|
1385
|
+
})
|
|
1386
|
+
.catch(reject);
|
|
1387
|
+
}
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
var cacheImage$2;
|
|
1391
|
+
var cacheResult$2;
|
|
1392
|
+
function loadImageWithBlob(img, useCache) {
|
|
1393
|
+
if (useCache === void 0) { useCache = true; }
|
|
1394
|
+
return new Promise(function (resolve, reject) {
|
|
1395
|
+
if (useCache && cacheImage$2 === img && cacheResult$2) {
|
|
1396
|
+
resolve(cacheResult$2);
|
|
1397
|
+
}
|
|
1398
|
+
else {
|
|
1399
|
+
getBlob(img)
|
|
1400
|
+
.then(function (blob) {
|
|
1401
|
+
var url = URL.createObjectURL(blob);
|
|
1402
|
+
var image = new Image();
|
|
1403
|
+
image.onload = function () {
|
|
1404
|
+
URL.revokeObjectURL(url);
|
|
1405
|
+
var result = { blob: blob, image: image };
|
|
1406
|
+
if (useCache) {
|
|
1407
|
+
cacheImage$2 = img;
|
|
1408
|
+
cacheResult$2 = result;
|
|
1409
|
+
}
|
|
1410
|
+
resolve(result);
|
|
1411
|
+
};
|
|
1412
|
+
image.onerror = function (err) {
|
|
1413
|
+
URL.revokeObjectURL(url);
|
|
1414
|
+
console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
|
|
1415
|
+
reject(err);
|
|
1416
|
+
};
|
|
1417
|
+
image.src = url;
|
|
1418
|
+
})
|
|
1419
|
+
.catch(reject);
|
|
1420
|
+
}
|
|
1421
|
+
});
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
function canvasToBlob(canvas, type, quality) {
|
|
1425
|
+
return new Promise(function (resolve) {
|
|
1426
|
+
canvas.toBlob(function (blob) {
|
|
1427
|
+
resolve(blob);
|
|
1428
|
+
}, type, quality);
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
function compressImage(img, options) {
|
|
1432
|
+
if (options === void 0) { options = {}; }
|
|
1433
|
+
return new Promise(function (resolve, reject) {
|
|
1434
|
+
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;
|
|
1435
|
+
loadImageWithBlob(img, cacheImage)
|
|
1436
|
+
.then(function (_a) {
|
|
1437
|
+
var image = _a.image, blob = _a.blob;
|
|
1438
|
+
var numWidth = toNumber(width);
|
|
1439
|
+
var numHeight = toNumber(height);
|
|
1440
|
+
var numQuality = toNumber(quality);
|
|
1441
|
+
if (numWidth) {
|
|
1442
|
+
image.width = numWidth;
|
|
1443
|
+
}
|
|
1444
|
+
if (numHeight) {
|
|
1445
|
+
image.height = numHeight;
|
|
1446
|
+
}
|
|
1447
|
+
beforeCompress === null || beforeCompress === void 0 ? void 0 : beforeCompress({ image: image, blob: blob }, options);
|
|
1448
|
+
var canvas = document.createElement('canvas');
|
|
1449
|
+
var ctx = canvas.getContext('2d');
|
|
1450
|
+
var info = { image: image, blob: blob, canvas: canvas, context: ctx };
|
|
1451
|
+
var numCanvasWidth = toNumber(typeof canvasWidth === 'function' ? canvasWidth(info, options) : canvasWidth);
|
|
1452
|
+
var numCanvasHeight = toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
|
|
1453
|
+
canvas.width = numCanvasWidth || image.width;
|
|
1454
|
+
canvas.height = numCanvasHeight || image.height;
|
|
1455
|
+
if (background && background !== 'none') {
|
|
1456
|
+
ctx.fillStyle = background;
|
|
1457
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
1458
|
+
}
|
|
1459
|
+
var internalOffset = [0, 0];
|
|
1460
|
+
if (rotate !== undefined) {
|
|
1461
|
+
ctx.translate(image.width / 2, image.height / 2);
|
|
1462
|
+
internalOffset = [-image.width / 2, -image.height / 2];
|
|
1463
|
+
ctx.rotate((rotate * Math.PI) / 180);
|
|
1464
|
+
}
|
|
1465
|
+
var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
|
|
1466
|
+
beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
|
|
1467
|
+
ctx.drawImage(image, internalOffset[0] + toNumber(outOffset[0]), internalOffset[1] + toNumber(outOffset[1]), image.width, image.height);
|
|
1468
|
+
afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
|
|
1469
|
+
if (format === 'blob') {
|
|
1470
|
+
canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
|
|
1471
|
+
}
|
|
1472
|
+
else {
|
|
1473
|
+
resolve(canvas.toDataURL(type, numQuality));
|
|
1474
|
+
}
|
|
1475
|
+
})
|
|
1476
|
+
.catch(reject);
|
|
1477
|
+
});
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1412
1480
|
function saver(blobUrl, fileName) {
|
|
1413
1481
|
if (fileName === void 0) { fileName = ''; }
|
|
1414
1482
|
var anchor = document.createElement('a');
|
|
@@ -1491,6 +1559,81 @@
|
|
|
1491
1559
|
});
|
|
1492
1560
|
}
|
|
1493
1561
|
|
|
1562
|
+
function calcContrast(w, h) {
|
|
1563
|
+
var n = gcd(w, h);
|
|
1564
|
+
return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
|
|
1565
|
+
}
|
|
1566
|
+
var cacheImage$1;
|
|
1567
|
+
var cacheResult$1;
|
|
1568
|
+
function getImageInfo(img, useCache) {
|
|
1569
|
+
if (useCache === void 0) { useCache = true; }
|
|
1570
|
+
return new Promise(function (resolve, reject) {
|
|
1571
|
+
if (useCache && cacheImage$1 === img && cacheResult$1) {
|
|
1572
|
+
resolve(cacheResult$1);
|
|
1573
|
+
}
|
|
1574
|
+
else {
|
|
1575
|
+
loadImageWithBlob(img, false)
|
|
1576
|
+
.then(function (_a) {
|
|
1577
|
+
var image = _a.image, blob = _a.blob;
|
|
1578
|
+
var width = image.width, height = image.height;
|
|
1579
|
+
var result = {
|
|
1580
|
+
width: width,
|
|
1581
|
+
height: height,
|
|
1582
|
+
contrast: calcContrast(width, height),
|
|
1583
|
+
measure: "".concat(width, " \u00D7 ").concat(height, " px"),
|
|
1584
|
+
size: bytesToSize(blob.size),
|
|
1585
|
+
bytes: blob.size,
|
|
1586
|
+
image: image,
|
|
1587
|
+
blob: blob
|
|
1588
|
+
};
|
|
1589
|
+
if (useCache) {
|
|
1590
|
+
cacheImage$1 = img;
|
|
1591
|
+
cacheResult$1 = result;
|
|
1592
|
+
}
|
|
1593
|
+
resolve(result);
|
|
1594
|
+
})
|
|
1595
|
+
.catch(reject);
|
|
1596
|
+
}
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
var cacheImage;
|
|
1601
|
+
var cacheResult;
|
|
1602
|
+
function loadImage(img, useCache) {
|
|
1603
|
+
if (useCache === void 0) { useCache = true; }
|
|
1604
|
+
return new Promise(function (resolve, reject) {
|
|
1605
|
+
if (useCache && cacheImage === img && cacheResult) {
|
|
1606
|
+
resolve(cacheResult);
|
|
1607
|
+
}
|
|
1608
|
+
else {
|
|
1609
|
+
var imgIsBlob_1 = isBlob(img);
|
|
1610
|
+
var url_1 = imgIsBlob_1 ? URL.createObjectURL(img) : img;
|
|
1611
|
+
var image_1 = new Image();
|
|
1612
|
+
if (!imgIsBlob_1) {
|
|
1613
|
+
image_1.crossOrigin = 'anonymous';
|
|
1614
|
+
}
|
|
1615
|
+
image_1.onload = function () {
|
|
1616
|
+
if (imgIsBlob_1) {
|
|
1617
|
+
URL.revokeObjectURL(url_1);
|
|
1618
|
+
}
|
|
1619
|
+
if (useCache) {
|
|
1620
|
+
cacheImage = img;
|
|
1621
|
+
cacheResult = image_1;
|
|
1622
|
+
}
|
|
1623
|
+
resolve(image_1);
|
|
1624
|
+
};
|
|
1625
|
+
image_1.onerror = function (err) {
|
|
1626
|
+
if (imgIsBlob_1) {
|
|
1627
|
+
URL.revokeObjectURL(url_1);
|
|
1628
|
+
}
|
|
1629
|
+
console.error("[loadImage] The image load failed, '".concat(img, "'."));
|
|
1630
|
+
reject(err);
|
|
1631
|
+
};
|
|
1632
|
+
image_1.src = url_1;
|
|
1633
|
+
}
|
|
1634
|
+
});
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1494
1637
|
function loadScript(src, options) {
|
|
1495
1638
|
return new Promise(function (resolve, reject) {
|
|
1496
1639
|
var head = document.head;
|
|
@@ -1824,15 +1967,16 @@
|
|
|
1824
1967
|
return internalFindTreeSelect(tree, predicate, childrenField);
|
|
1825
1968
|
}
|
|
1826
1969
|
|
|
1827
|
-
var VERSION = "4.
|
|
1970
|
+
var VERSION = "4.20.1";
|
|
1828
1971
|
|
|
1829
|
-
var version = "4.
|
|
1972
|
+
var version = "4.20.1";
|
|
1830
1973
|
|
|
1831
1974
|
exports.VERSION = VERSION;
|
|
1832
1975
|
exports.ajax = ajax;
|
|
1833
1976
|
exports.blobToDataURL = blobToDataURL;
|
|
1834
1977
|
exports.bytesToSize = bytesToSize;
|
|
1835
1978
|
exports.calculateCursorPosition = calculateCursorPosition;
|
|
1979
|
+
exports.compressImage = compressImage;
|
|
1836
1980
|
exports.dataURLToBlob = dataURLToBlob;
|
|
1837
1981
|
exports.divide = divide;
|
|
1838
1982
|
exports.download = download;
|
|
@@ -1844,6 +1988,8 @@
|
|
|
1844
1988
|
exports.formatBankCard = formatBankCard;
|
|
1845
1989
|
exports.formatMobile = formatMobile;
|
|
1846
1990
|
exports.formatMoney = formatMoney;
|
|
1991
|
+
exports.gcd = gcd;
|
|
1992
|
+
exports.getImageInfo = getImageInfo;
|
|
1847
1993
|
exports.isBankCard = isBankCard;
|
|
1848
1994
|
exports.isBusinessLicense = isBusinessLicense;
|
|
1849
1995
|
exports.isChinese = isChinese;
|
|
@@ -1866,7 +2012,10 @@
|
|
|
1866
2012
|
exports.isValidNumber = isValidNumber;
|
|
1867
2013
|
exports.isVehicle = isVehicle;
|
|
1868
2014
|
exports.isWX = isWX;
|
|
2015
|
+
exports.lcm = lcm;
|
|
1869
2016
|
exports.listToTree = listToTree;
|
|
2017
|
+
exports.loadImage = loadImage;
|
|
2018
|
+
exports.loadImageWithBlob = loadImageWithBlob;
|
|
1870
2019
|
exports.loadScript = loadScript;
|
|
1871
2020
|
exports.minus = minus;
|
|
1872
2021
|
exports.normalizeString = normalizeString;
|