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/esm/VERSION.js CHANGED
@@ -1,4 +1,4 @@
1
- var VERSION = "5.2.2";
1
+ var VERSION = "5.3.0";
2
2
  var VERSION$1 = VERSION;
3
3
 
4
4
  export { VERSION$1 as default };
@@ -1,5 +1,5 @@
1
1
  import { nativeUndefined } from './utils/native.js';
2
- import { getFileNameExt } from './utils/file.util.js';
2
+ import { getExtname } from './utils/file.util.js';
3
3
 
4
4
  var mimeTypes = [
5
5
  ['text/plain', ['txt']],
@@ -36,7 +36,7 @@ var mimeTypes = [
36
36
  ];
37
37
  function getMimeType(fileName) {
38
38
  var _a;
39
- var ext = getFileNameExt(fileName).slice(1);
39
+ var ext = getExtname(fileName).slice(1);
40
40
  return ext ? (_a = mimeTypes.find(function (item) { return item[1].includes(ext); })) === null || _a === void 0 ? void 0 : _a[0] : nativeUndefined;
41
41
  }
42
42
 
package/esm/index.js CHANGED
@@ -48,6 +48,7 @@ export { default as getFileBlob } from './getFileBlob.js';
48
48
  export { default as getFileType } from './getFileType.js';
49
49
  export { default as getImageInfo } from './getImageInfo.js';
50
50
  export { default as getMimeType } from './getMimeType.js';
51
+ export { default as injectStyle } from './injectStyle.js';
51
52
  export { default as loadImage } from './loadImage.js';
52
53
  export { default as loadImageWithBlob } from './loadImageWithBlob.js';
53
54
  export { default as loadScript } from './loadScript.js';
@@ -0,0 +1,23 @@
1
+ function injectStyle(css, options) {
2
+ 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;
3
+ var style = document.createElement('style');
4
+ if (style.styleSheet) {
5
+ style.styleSheet.cssText = css;
6
+ }
7
+ else {
8
+ style.appendChild(document.createTextNode(css));
9
+ }
10
+ var atTop = insertAt === 'top';
11
+ if (atTop && container.prepend) {
12
+ container.prepend(style);
13
+ }
14
+ else if (atTop && container.firstChild) {
15
+ container.insertBefore(style, container.firstChild);
16
+ }
17
+ else {
18
+ container.appendChild(style);
19
+ }
20
+ return style;
21
+ }
22
+
23
+ export { injectStyle as default };
package/esm/loadScript.js CHANGED
@@ -3,7 +3,7 @@ import { objectKeys } from './utils/native.js';
3
3
 
4
4
  function loadScript(src, options) {
5
5
  return new Promise(function (resolve, reject) {
6
- var head = document.head;
6
+ var container = document.head || document.getElementsByTagName('head')[0] || document.body;
7
7
  var script = document.createElement('script');
8
8
  var _a = options || {}, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, restOptions = __rest(_a, ["attrs", "destroyOnError"]);
9
9
  var props = __assign(__assign({ async: true, type: 'text/javascript' }, restOptions), { src: src });
@@ -29,11 +29,11 @@ function loadScript(src, options) {
29
29
  this.onerror = this.onload = null;
30
30
  (_a = props.onerror) === null || _a === void 0 ? void 0 : _a.call(this, ev);
31
31
  if (destroyOnError) {
32
- head.removeChild(script);
32
+ container.removeChild(script);
33
33
  }
34
34
  reject(new URIError('Failed to load ' + this.src));
35
35
  };
36
- head.appendChild(script);
36
+ container.appendChild(script);
37
37
  });
38
38
  }
39
39
 
@@ -1,10 +1,10 @@
1
1
  import { isString, nth, isObjectLike } from 'ut2';
2
2
 
3
- function getFileNameExt(name) {
3
+ function getExtname(name) {
4
4
  return isString(name) && name.indexOf('.') > 0 ? '.' + nth(name.split('.'), -1) : '';
5
5
  }
6
6
  function testExt(name, ext) {
7
- return !!name && getFileNameExt(name) === ext;
7
+ return !!name && getExtname(name) === ext;
8
8
  }
9
9
  function isUploadFile(fileObj) {
10
10
  if (isObjectLike(fileObj) && isString(fileObj.name)) {
@@ -13,4 +13,4 @@ function isUploadFile(fileObj) {
13
13
  return false;
14
14
  }
15
15
 
16
- export { getFileNameExt, isUploadFile, testExt };
16
+ export { getExtname, isUploadFile, testExt };
package/lib/VERSION.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "5.2.2";
3
+ var VERSION = "5.3.0";
4
4
  var VERSION$1 = VERSION;
5
5
 
6
6
  module.exports = VERSION$1;
@@ -38,7 +38,7 @@ var mimeTypes = [
38
38
  ];
39
39
  function getMimeType(fileName) {
40
40
  var _a;
41
- var ext = file_util.getFileNameExt(fileName).slice(1);
41
+ var ext = file_util.getExtname(fileName).slice(1);
42
42
  return ext ? (_a = mimeTypes.find(function (item) { return item[1].includes(ext); })) === null || _a === void 0 ? void 0 : _a[0] : native.nativeUndefined;
43
43
  }
44
44
 
package/lib/index.js CHANGED
@@ -50,6 +50,7 @@ var getFileBlob = require('./getFileBlob.js');
50
50
  var getFileType = require('./getFileType.js');
51
51
  var getImageInfo = require('./getImageInfo.js');
52
52
  var getMimeType = require('./getMimeType.js');
53
+ var injectStyle = require('./injectStyle.js');
53
54
  var loadImage = require('./loadImage.js');
54
55
  var loadImageWithBlob = require('./loadImageWithBlob.js');
55
56
  var loadScript = require('./loadScript.js');
@@ -119,6 +120,7 @@ exports.getFileBlob = getFileBlob;
119
120
  exports.getFileType = getFileType;
120
121
  exports.getImageInfo = getImageInfo;
121
122
  exports.getMimeType = getMimeType;
123
+ exports.injectStyle = injectStyle;
122
124
  exports.loadImage = loadImage;
123
125
  exports.loadImageWithBlob = loadImageWithBlob;
124
126
  exports.loadScript = loadScript;
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ function injectStyle(css, options) {
4
+ 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;
5
+ var style = document.createElement('style');
6
+ if (style.styleSheet) {
7
+ style.styleSheet.cssText = css;
8
+ }
9
+ else {
10
+ style.appendChild(document.createTextNode(css));
11
+ }
12
+ var atTop = insertAt === 'top';
13
+ if (atTop && container.prepend) {
14
+ container.prepend(style);
15
+ }
16
+ else if (atTop && container.firstChild) {
17
+ container.insertBefore(style, container.firstChild);
18
+ }
19
+ else {
20
+ container.appendChild(style);
21
+ }
22
+ return style;
23
+ }
24
+
25
+ module.exports = injectStyle;
package/lib/loadScript.js CHANGED
@@ -5,7 +5,7 @@ var native = require('./utils/native.js');
5
5
 
6
6
  function loadScript(src, options) {
7
7
  return new Promise(function (resolve, reject) {
8
- var head = document.head;
8
+ var container = document.head || document.getElementsByTagName('head')[0] || document.body;
9
9
  var script = document.createElement('script');
10
10
  var _a = options || {}, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, restOptions = tslib.__rest(_a, ["attrs", "destroyOnError"]);
11
11
  var props = tslib.__assign(tslib.__assign({ async: true, type: 'text/javascript' }, restOptions), { src: src });
@@ -31,11 +31,11 @@ function loadScript(src, options) {
31
31
  this.onerror = this.onload = null;
32
32
  (_a = props.onerror) === null || _a === void 0 ? void 0 : _a.call(this, ev);
33
33
  if (destroyOnError) {
34
- head.removeChild(script);
34
+ container.removeChild(script);
35
35
  }
36
36
  reject(new URIError('Failed to load ' + this.src));
37
37
  };
38
- head.appendChild(script);
38
+ container.appendChild(script);
39
39
  });
40
40
  }
41
41
 
@@ -2,11 +2,11 @@
2
2
 
3
3
  var ut2 = require('ut2');
4
4
 
5
- function getFileNameExt(name) {
5
+ function getExtname(name) {
6
6
  return ut2.isString(name) && name.indexOf('.') > 0 ? '.' + ut2.nth(name.split('.'), -1) : '';
7
7
  }
8
8
  function testExt(name, ext) {
9
- return !!name && getFileNameExt(name) === ext;
9
+ return !!name && getExtname(name) === ext;
10
10
  }
11
11
  function isUploadFile(fileObj) {
12
12
  if (ut2.isObjectLike(fileObj) && ut2.isString(fileObj.name)) {
@@ -15,6 +15,6 @@ function isUploadFile(fileObj) {
15
15
  return false;
16
16
  }
17
17
 
18
- exports.getFileNameExt = getFileNameExt;
18
+ exports.getExtname = getExtname;
19
19
  exports.isUploadFile = isUploadFile;
20
20
  exports.testExt = testExt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "5.2.2",
3
+ "version": "5.3.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
File without changes
package/types/index.d.ts CHANGED
@@ -103,6 +103,7 @@ export { default as getFileBlob } from './getFileBlob';
103
103
  export { default as getFileType } from './getFileType';
104
104
  export { default as getImageInfo } from './getImageInfo';
105
105
  export { default as getMimeType } from './getMimeType';
106
+ export { default as injectStyle } from './injectStyle';
106
107
  export { default as loadImage } from './loadImage';
107
108
  export { default as loadImageWithBlob } from './loadImageWithBlob';
108
109
  export { default as loadScript } from './loadScript';
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 注入样式。
3
+ *
4
+ * @alias module:Browser.injectStyle
5
+ * @since 5.3.0
6
+ * @param {string} css 样式内容。
7
+ * @param {Object} [options] 配置项。
8
+ * @param {HTMLElement} [options.container=document.head] 要注入样式的容器。默认 `document.head`。如果 `document.head` 不存在,默认 `document.body`。
9
+ * @param {'top' | 'bottom'} [options.insertAt='top'] 注入容器内容前面还是后面。默认 `top`。
10
+ * @returns {HTMLStyleElement} `style` 元素。
11
+ */
12
+ declare function injectStyle(css: string, options?: {
13
+ container?: HTMLElement;
14
+ insertAt?: 'top' | 'bottom';
15
+ }): HTMLStyleElement;
16
+ export default injectStyle;
@@ -1,4 +1,4 @@
1
- export declare function getFileNameExt(name: string): string;
1
+ export declare function getExtname(name: string): string;
2
2
  export declare function testExt(name: string | undefined, ext: string): boolean;
3
3
  export type UploadFile = {
4
4
  name: string;