util-helpers 5.2.2 → 5.3.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
@@ -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,38 @@
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, onBefore = _a.onBefore;
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
+ if (typeof onBefore === 'function') {
1801
+ onBefore(style);
1802
+ }
1803
+ var atTop = insertAt === 'top';
1804
+ if (atTop && container.prepend) {
1805
+ container.prepend(style);
1806
+ }
1807
+ else if (atTop && container.firstChild) {
1808
+ container.insertBefore(style, container.firstChild);
1809
+ }
1810
+ else {
1811
+ container.appendChild(style);
1812
+ }
1813
+ return style;
1814
+ }
1815
+
1791
1816
  function loadScript(src, options) {
1792
1817
  return new Promise(function (resolve, reject) {
1793
- var head = document.head;
1818
+ var container = document.head || document.getElementsByTagName('head')[0] || document.body;
1794
1819
  var script = document.createElement('script');
1795
1820
  var _a = options || {}, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, restOptions = __rest(_a, ["attrs", "destroyOnError"]);
1796
1821
  var props = __assign(__assign({ async: true, type: 'text/javascript' }, restOptions), { src: src });
@@ -1816,11 +1841,11 @@
1816
1841
  this.onerror = this.onload = null;
1817
1842
  (_a = props.onerror) === null || _a === void 0 ? void 0 : _a.call(this, ev);
1818
1843
  if (destroyOnError) {
1819
- head.removeChild(script);
1844
+ container.removeChild(script);
1820
1845
  }
1821
1846
  reject(new URIError('Failed to load ' + this.src));
1822
1847
  };
1823
- head.appendChild(script);
1848
+ container.appendChild(script);
1824
1849
  });
1825
1850
  }
1826
1851
 
@@ -2129,7 +2154,7 @@
2129
2154
  return internalFindTreeSelect(tree, predicate, childrenField);
2130
2155
  }
2131
2156
 
2132
- var VERSION = "5.2.2";
2157
+ var VERSION = "5.3.1";
2133
2158
 
2134
2159
  /**
2135
2160
  * 事件触发器,支持浏览器端和 node 端。
@@ -3175,6 +3200,7 @@
3175
3200
  exports.getFileType = getFileType;
3176
3201
  exports.getImageInfo = getImageInfo;
3177
3202
  exports.getMimeType = getMimeType;
3203
+ exports.injectStyle = injectStyle;
3178
3204
  exports.isBankCard = isBankCard;
3179
3205
  exports.isBusinessLicense = isBusinessLicense;
3180
3206
  exports.isChinese = isChinese;