util-helpers 5.0.4 → 5.1.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/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 对象
@@ -40,9 +40,12 @@
40
40
  return Array.isArray(value);
41
41
  }
42
42
 
43
+ function isObjectLike(value) {
44
+ return value !== null && typeof value === 'object';
45
+ }
46
+
43
47
  function isObject(value) {
44
- var type = typeof value;
45
- return type === 'function' || (type === 'object' && !!value);
48
+ return typeof value === 'function' || isObjectLike(value);
46
49
  }
47
50
 
48
51
  function getTag(value) {
@@ -131,10 +134,6 @@
131
134
  return mathFloor(min + mathRandom() * (max - min + 1));
132
135
  }
133
136
 
134
- function isObjectLike(value) {
135
- return value !== null && typeof value === 'object';
136
- }
137
-
138
137
  function getSymbols(object) {
139
138
  if (!objectGetOwnPropertySymbols || object === null) {
140
139
  return [];
@@ -264,7 +263,7 @@
264
263
  function noop() { }
265
264
 
266
265
  var idCounter = 0;
267
- '_' + Math.random().toString(36).substring(2, 4);
266
+ '_' + mathRandom().toString(36).substring(2, 4);
268
267
  function uniqueId(prefix) {
269
268
  return '' + prefix + ++idCounter;
270
269
  }
@@ -395,11 +394,11 @@
395
394
  return lastCode === checkCode;
396
395
  }
397
396
 
398
- var config = {
397
+ var config$1 = {
399
398
  disableWarning: true
400
399
  };
401
400
  function setDisableWarning(bool) {
402
- config.disableWarning = !!bool;
401
+ config$1.disableWarning = !!bool;
403
402
  }
404
403
 
405
404
  function devWarn() {
@@ -407,7 +406,7 @@
407
406
  for (var _i = 0; _i < arguments.length; _i++) {
408
407
  args[_i] = arguments[_i];
409
408
  }
410
- if (!config.disableWarning) {
409
+ if (!config$1.disableWarning) {
411
410
  console.warn.apply(void 0, args);
412
411
  }
413
412
  }
@@ -1469,6 +1468,40 @@
1469
1468
  return pos;
1470
1469
  }
1471
1470
 
1471
+ function testExt(name, ext) {
1472
+ return !!name && name.slice(-ext.length) === ext;
1473
+ }
1474
+ function checkFileType(file, accept) {
1475
+ if (!isBlob(file)) {
1476
+ return false;
1477
+ }
1478
+ if (!isString(accept)) {
1479
+ accept = toString(accept);
1480
+ }
1481
+ accept = accept.trim();
1482
+ if (!accept || accept === '*') {
1483
+ return true;
1484
+ }
1485
+ var ret = false;
1486
+ var types = accept.toLowerCase().split(/,(?:\s+)?/);
1487
+ var fileName = file.name.toLowerCase();
1488
+ var fileType = file.type;
1489
+ types.some(function (type) {
1490
+ if (fileType === type || (type.indexOf('.') === 0 && testExt(fileName, type))) {
1491
+ ret = true;
1492
+ }
1493
+ else if (type.includes('/*') && fileType.includes('/')) {
1494
+ var match = type.match(/(.*)\/\*/);
1495
+ var fileParentType = fileType.split('/')[0];
1496
+ if (match && match[1] === fileParentType) {
1497
+ ret = true;
1498
+ }
1499
+ }
1500
+ return ret;
1501
+ });
1502
+ return ret;
1503
+ }
1504
+
1472
1505
  var SuccessResponseStatus = [200, 304];
1473
1506
  function getFileBlob(file, ajaxOptions) {
1474
1507
  return new Promise(function (resolve, reject) {
@@ -1674,6 +1707,27 @@
1674
1707
  });
1675
1708
  }
1676
1709
 
1710
+ var config = {
1711
+ image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
1712
+ audio: 'audio/*,.mp3,.wav',
1713
+ video: 'video/*,.mp4,.webm,.ogg,.ogv,.ogm',
1714
+ pdf: 'application/pdf,.pdf',
1715
+ word: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,.doc,.docx',
1716
+ excel: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,.xls,.xlsx'
1717
+ };
1718
+ function getFileType(file) {
1719
+ var type;
1720
+ if (isBlob(file)) {
1721
+ forEach(config, function (accept, fileType) {
1722
+ if (checkFileType(file, accept)) {
1723
+ type = fileType;
1724
+ return false;
1725
+ }
1726
+ });
1727
+ }
1728
+ return type;
1729
+ }
1730
+
1677
1731
  function calcContrast(w, h) {
1678
1732
  var n = gcd(w, h);
1679
1733
  return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
@@ -2006,7 +2060,7 @@
2006
2060
  return internalFindTreeSelect(tree, predicate, childrenField);
2007
2061
  }
2008
2062
 
2009
- var VERSION = "5.0.4";
2063
+ var VERSION = "5.1.0";
2010
2064
 
2011
2065
  var EmitterPro = /** @class */ (function () {
2012
2066
  function EmitterPro() {
@@ -2474,6 +2528,7 @@
2474
2528
  exports.ajax = ajax;
2475
2529
  exports.bytesToSize = bytesToSize;
2476
2530
  exports.calculateCursorPosition = calculateCursorPosition;
2531
+ exports.checkFileType = checkFileType;
2477
2532
  exports.compressImage = compressImage;
2478
2533
  exports.dataURLToBlob = dataURLToBlob;
2479
2534
  exports.divide = divide;
@@ -2488,6 +2543,7 @@
2488
2543
  exports.formatMoney = formatMoney;
2489
2544
  exports.gcd = gcd;
2490
2545
  exports.getFileBlob = getFileBlob;
2546
+ exports.getFileType = getFileType;
2491
2547
  exports.getImageInfo = getImageInfo;
2492
2548
  exports.isBankCard = isBankCard;
2493
2549
  exports.isBusinessLicense = isBusinessLicense;