util-helpers 5.0.4 → 5.1.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 CHANGED
@@ -54,9 +54,11 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
54
54
  - 其他
55
55
  - [ajax](https://doly-dev.github.io/util-helpers/module-Other.html#.ajax) - 请求
56
56
  - [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
57
+ - [checkFileType](https://doly-dev.github.io/util-helpers/module-Other.html#.checkFileType) - 检查文件类型
57
58
  - [compressImage](https://doly-dev.github.io/util-helpers/module-Other.html#.compressImage) - 压缩图片
58
59
  - [download](https://doly-dev.github.io/util-helpers/module-Other.html#.download) - 下载
59
60
  - [getFileBlob](https://doly-dev.github.io/util-helpers/module-Other.html#.getFileBlob) - 获取文件 Blob
61
+ - [getFileType](https://doly-dev.github.io/util-helpers/module-Other.html#.getFileType) - 获取文件类型
60
62
  - [getImageInfo](https://doly-dev.github.io/util-helpers/module-Other.html#.getImageInfo) - 获取图片信息
61
63
  - [loadImage](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImage) - 加载图片
62
64
  - [loadImageWithBlob](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImageWithBlob) - 加载图片和 blob 对象
@@ -34,15 +34,19 @@
34
34
  var symbolTag = '[object Symbol]';
35
35
  var functionTags = ['Function', 'AsyncFunction', 'GeneratorFunction', 'Proxy'].map(function (item) { return '[object ' + item + ']'; });
36
36
  var blobTag = '[object Blob]';
37
+ var fileTag = '[object File]';
37
38
  var objectTag = '[object Object]';
38
39
 
39
40
  function isArray(value) {
40
41
  return Array.isArray(value);
41
42
  }
42
43
 
44
+ function isObjectLike(value) {
45
+ return value !== null && typeof value === 'object';
46
+ }
47
+
43
48
  function isObject(value) {
44
- var type = typeof value;
45
- return type === 'function' || (type === 'object' && !!value);
49
+ return typeof value === 'function' || isObjectLike(value);
46
50
  }
47
51
 
48
52
  function getTag(value) {
@@ -131,10 +135,6 @@
131
135
  return mathFloor(min + mathRandom() * (max - min + 1));
132
136
  }
133
137
 
134
- function isObjectLike(value) {
135
- return value !== null && typeof value === 'object';
136
- }
137
-
138
138
  function getSymbols(object) {
139
139
  if (!objectGetOwnPropertySymbols || object === null) {
140
140
  return [];
@@ -216,6 +216,14 @@
216
216
  return typeof Ctor === 'function' && Ctor instanceof Ctor && functionProtoToString.call(Ctor) === objectCtorString;
217
217
  }
218
218
 
219
+ var fileExisted = typeof File !== stringUndefined;
220
+ function isFile(value) {
221
+ if (fileExisted && value instanceof File) {
222
+ return true;
223
+ }
224
+ return getTag(value) === fileTag;
225
+ }
226
+
219
227
  var freeGlobalThis = globalThisExisted && globalThis.Object === Object && globalThis;
220
228
  var freeGlobal = globalExisted && global.Object === Object && global;
221
229
  var freeSelf = selfExisted && self.Object === Object && self;
@@ -264,7 +272,7 @@
264
272
  function noop() { }
265
273
 
266
274
  var idCounter = 0;
267
- '_' + Math.random().toString(36).substring(2, 4);
275
+ '_' + mathRandom().toString(36).substring(2, 4);
268
276
  function uniqueId(prefix) {
269
277
  return '' + prefix + ++idCounter;
270
278
  }
@@ -395,11 +403,11 @@
395
403
  return lastCode === checkCode;
396
404
  }
397
405
 
398
- var config = {
406
+ var config$1 = {
399
407
  disableWarning: true
400
408
  };
401
409
  function setDisableWarning(bool) {
402
- config.disableWarning = !!bool;
410
+ config$1.disableWarning = !!bool;
403
411
  }
404
412
 
405
413
  function devWarn() {
@@ -407,7 +415,7 @@
407
415
  for (var _i = 0; _i < arguments.length; _i++) {
408
416
  args[_i] = arguments[_i];
409
417
  }
410
- if (!config.disableWarning) {
418
+ if (!config$1.disableWarning) {
411
419
  console.warn.apply(void 0, args);
412
420
  }
413
421
  }
@@ -1469,6 +1477,48 @@
1469
1477
  return pos;
1470
1478
  }
1471
1479
 
1480
+ function testExt(name, ext) {
1481
+ return !!name && name.slice(-ext.length) === ext;
1482
+ }
1483
+ function isUploadFile(fileObj) {
1484
+ if (isObjectLike(fileObj) && isString(fileObj.uid) && isString(fileObj.name)) {
1485
+ return true;
1486
+ }
1487
+ return false;
1488
+ }
1489
+
1490
+ function checkFileType(file, accept) {
1491
+ if (!isFile(file) && !isUploadFile(file)) {
1492
+ return false;
1493
+ }
1494
+ if (!isString(accept)) {
1495
+ accept = toString(accept);
1496
+ }
1497
+ accept = accept.trim();
1498
+ if (!accept || accept === '*') {
1499
+ return true;
1500
+ }
1501
+ var ret = false;
1502
+ var types = accept.toLowerCase().split(/,(?:\s+)?/);
1503
+ var fileName = file.name.toLowerCase();
1504
+ var fileType = file.type || '';
1505
+ var fileUrl = file.url || '';
1506
+ types.some(function (type) {
1507
+ if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (testExt(fileName, type) || testExt(fileUrl, type)))) {
1508
+ ret = true;
1509
+ }
1510
+ else if (type.includes('/*') && fileType.includes('/')) {
1511
+ var match = type.match(/(.*)\/\*/);
1512
+ var fileParentType = fileType.split('/')[0];
1513
+ if (match && match[1] === fileParentType) {
1514
+ ret = true;
1515
+ }
1516
+ }
1517
+ return ret;
1518
+ });
1519
+ return ret;
1520
+ }
1521
+
1472
1522
  var SuccessResponseStatus = [200, 304];
1473
1523
  function getFileBlob(file, ajaxOptions) {
1474
1524
  return new Promise(function (resolve, reject) {
@@ -1674,6 +1724,27 @@
1674
1724
  });
1675
1725
  }
1676
1726
 
1727
+ var config = {
1728
+ image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
1729
+ audio: 'audio/*,.mp3,.wav',
1730
+ video: 'video/*,.mp4,.webm,.ogg,.ogv,.ogm',
1731
+ pdf: 'application/pdf,.pdf',
1732
+ word: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,.doc,.docx',
1733
+ excel: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,.xls,.xlsx'
1734
+ };
1735
+ function getFileType(file) {
1736
+ var type;
1737
+ if (isBlob(file) || isUploadFile(file)) {
1738
+ forEach(config, function (accept, fileType) {
1739
+ if (checkFileType(file, accept)) {
1740
+ type = fileType;
1741
+ return false;
1742
+ }
1743
+ });
1744
+ }
1745
+ return type;
1746
+ }
1747
+
1677
1748
  function calcContrast(w, h) {
1678
1749
  var n = gcd(w, h);
1679
1750
  return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
@@ -2006,7 +2077,7 @@
2006
2077
  return internalFindTreeSelect(tree, predicate, childrenField);
2007
2078
  }
2008
2079
 
2009
- var VERSION = "5.0.4";
2080
+ var VERSION = "5.1.1";
2010
2081
 
2011
2082
  var EmitterPro = /** @class */ (function () {
2012
2083
  function EmitterPro() {
@@ -2474,6 +2545,7 @@
2474
2545
  exports.ajax = ajax;
2475
2546
  exports.bytesToSize = bytesToSize;
2476
2547
  exports.calculateCursorPosition = calculateCursorPosition;
2548
+ exports.checkFileType = checkFileType;
2477
2549
  exports.compressImage = compressImage;
2478
2550
  exports.dataURLToBlob = dataURLToBlob;
2479
2551
  exports.divide = divide;
@@ -2488,6 +2560,7 @@
2488
2560
  exports.formatMoney = formatMoney;
2489
2561
  exports.gcd = gcd;
2490
2562
  exports.getFileBlob = getFileBlob;
2563
+ exports.getFileType = getFileType;
2491
2564
  exports.getImageInfo = getImageInfo;
2492
2565
  exports.isBankCard = isBankCard;
2493
2566
  exports.isBusinessLicense = isBusinessLicense;