util-helpers 5.2.2 → 5.3.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
@@ -62,6 +62,7 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
62
62
  - [getFileType](https://doly-dev.github.io/util-helpers/module-Browser.html#.getFileType) - 获取文件类型
63
63
  - [getImageInfo](https://doly-dev.github.io/util-helpers/module-Browser.html#.getImageInfo) - 获取图片信息
64
64
  - [getMimeType](https://doly-dev.github.io/util-helpers/module-Browser.html#.getMimeType) - 获取文件 MIME 类型
65
+ - [injectStyle](https://doly-dev.github.io/util-helpers/module-Browser.html#.injectStyle) - 注入样式
65
66
  - [loadImage](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadImage) - 加载图片
66
67
  - [loadImageWithBlob](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadImageWithBlob) - 加载图片和 blob 对象
67
68
  - [loadScript](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadScript) - 加载 js 文件
@@ -1421,11 +1421,11 @@
1421
1421
  });
1422
1422
  }
1423
1423
 
1424
- function getFileNameExt(name) {
1424
+ function getExtname(name) {
1425
1425
  return isString(name) && name.indexOf('.') > 0 ? '.' + nth(name.split('.'), -1) : '';
1426
1426
  }
1427
1427
  function testExt(name, ext) {
1428
- return !!name && getFileNameExt(name) === ext;
1428
+ return !!name && getExtname(name) === ext;
1429
1429
  }
1430
1430
  function isUploadFile(fileObj) {
1431
1431
  if (isObjectLike(fileObj) && isString(fileObj.name)) {
@@ -1784,13 +1784,35 @@
1784
1784
  ];
1785
1785
  function getMimeType(fileName) {
1786
1786
  var _a;
1787
- var ext = getFileNameExt(fileName).slice(1);
1787
+ var ext = getExtname(fileName).slice(1);
1788
1788
  return ext ? (_a = mimeTypes.find(function (item) { return item[1].includes(ext); })) === null || _a === void 0 ? void 0 : _a[0] : nativeUndefined;
1789
1789
  }
1790
1790
 
1791
+ function injectStyle(css, options) {
1792
+ var _a = options || {}, _b = _a.container, container = _b === void 0 ? document.head || document.getElementsByTagName('head')[0] || document.body : _b, _c = _a.insertAt, insertAt = _c === void 0 ? 'top' : _c;
1793
+ var style = document.createElement('style');
1794
+ if (style.styleSheet) {
1795
+ style.styleSheet.cssText = css;
1796
+ }
1797
+ else {
1798
+ style.appendChild(document.createTextNode(css));
1799
+ }
1800
+ var atTop = insertAt === 'top';
1801
+ if (atTop && container.prepend) {
1802
+ container.prepend(style);
1803
+ }
1804
+ else if (atTop && container.firstChild) {
1805
+ container.insertBefore(style, container.firstChild);
1806
+ }
1807
+ else {
1808
+ container.appendChild(style);
1809
+ }
1810
+ return style;
1811
+ }
1812
+
1791
1813
  function loadScript(src, options) {
1792
1814
  return new Promise(function (resolve, reject) {
1793
- var head = document.head;
1815
+ var container = document.head || document.getElementsByTagName('head')[0] || document.body;
1794
1816
  var script = document.createElement('script');
1795
1817
  var _a = options || {}, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, restOptions = __rest(_a, ["attrs", "destroyOnError"]);
1796
1818
  var props = __assign(__assign({ async: true, type: 'text/javascript' }, restOptions), { src: src });
@@ -1816,11 +1838,11 @@
1816
1838
  this.onerror = this.onload = null;
1817
1839
  (_a = props.onerror) === null || _a === void 0 ? void 0 : _a.call(this, ev);
1818
1840
  if (destroyOnError) {
1819
- head.removeChild(script);
1841
+ container.removeChild(script);
1820
1842
  }
1821
1843
  reject(new URIError('Failed to load ' + this.src));
1822
1844
  };
1823
- head.appendChild(script);
1845
+ container.appendChild(script);
1824
1846
  });
1825
1847
  }
1826
1848
 
@@ -2129,7 +2151,7 @@
2129
2151
  return internalFindTreeSelect(tree, predicate, childrenField);
2130
2152
  }
2131
2153
 
2132
- var VERSION = "5.2.2";
2154
+ var VERSION = "5.3.0";
2133
2155
 
2134
2156
  /**
2135
2157
  * 事件触发器,支持浏览器端和 node 端。
@@ -3175,6 +3197,7 @@
3175
3197
  exports.getFileType = getFileType;
3176
3198
  exports.getImageInfo = getImageInfo;
3177
3199
  exports.getMimeType = getMimeType;
3200
+ exports.injectStyle = injectStyle;
3178
3201
  exports.isBankCard = isBankCard;
3179
3202
  exports.isBusinessLicense = isBusinessLicense;
3180
3203
  exports.isChinese = isChinese;