util-helpers 5.3.0 → 5.4.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.3.0";
1
+ var VERSION = "5.4.0";
2
2
  var VERSION$1 = VERSION;
3
3
 
4
4
  export { VERSION$1 as default };
@@ -1,5 +1,6 @@
1
1
  import { isFile, isString, toString } from 'ut2';
2
- import { isUploadFile, testExt } from './utils/file.util.js';
2
+ import { isUploadFile } from './utils/file.util.js';
3
+ import getExtname from './getExtname.js';
3
4
 
4
5
  function checkFileType(file, accept) {
5
6
  var _a, _b;
@@ -20,7 +21,7 @@ function checkFileType(file, accept) {
20
21
  var fileType = file.type || (!isFileType && ((_b = file.originFileObj) === null || _b === void 0 ? void 0 : _b.type)) || '';
21
22
  var fileUrl = (!isFileType && file.url) || '';
22
23
  types.some(function (type) {
23
- if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (testExt(fileName, type) || testExt(fileUrl, type)))) {
24
+ if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (getExtname(fileName) === type || getExtname(fileUrl) === type))) {
24
25
  ret = true;
25
26
  }
26
27
  else if (type.includes('/*') && fileType.includes('/')) {
@@ -0,0 +1,7 @@
1
+ import { isString, nth } from 'ut2';
2
+
3
+ function getExtname(path) {
4
+ return isString(path) && path.indexOf('.') > 0 ? '.' + nth(path.split('.'), -1) : '';
5
+ }
6
+
7
+ export { getExtname as default };
@@ -1,5 +1,5 @@
1
+ import getExtname from './getExtname.js';
1
2
  import { nativeUndefined } from './utils/native.js';
2
- import { getExtname } from './utils/file.util.js';
3
3
 
4
4
  var mimeTypes = [
5
5
  ['text/plain', ['txt']],
package/esm/index.js CHANGED
@@ -53,6 +53,7 @@ export { default as loadImage } from './loadImage.js';
53
53
  export { default as loadImageWithBlob } from './loadImageWithBlob.js';
54
54
  export { default as loadScript } from './loadScript.js';
55
55
  export { default as calculateCursorPosition } from './calculateCursorPosition.js';
56
+ export { default as getExtname } from './getExtname.js';
56
57
  export { default as randomString } from './randomString.js';
57
58
  export { default as strlen } from './strlen.js';
58
59
  export { default as transformFieldNames } from './transformFieldNames.js';
@@ -1,5 +1,5 @@
1
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;
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, onBefore = _a.onBefore;
3
3
  var style = document.createElement('style');
4
4
  if (style.styleSheet) {
5
5
  style.styleSheet.cssText = css;
@@ -7,6 +7,9 @@ function injectStyle(css, options) {
7
7
  else {
8
8
  style.appendChild(document.createTextNode(css));
9
9
  }
10
+ if (typeof onBefore === 'function') {
11
+ onBefore(style);
12
+ }
10
13
  var atTop = insertAt === 'top';
11
14
  if (atTop && container.prepend) {
12
15
  container.prepend(style);
@@ -1,11 +1,5 @@
1
- import { isString, nth, isObjectLike } from 'ut2';
1
+ import { isObjectLike, isString } from 'ut2';
2
2
 
3
- function getExtname(name) {
4
- return isString(name) && name.indexOf('.') > 0 ? '.' + nth(name.split('.'), -1) : '';
5
- }
6
- function testExt(name, ext) {
7
- return !!name && getExtname(name) === ext;
8
- }
9
3
  function isUploadFile(fileObj) {
10
4
  if (isObjectLike(fileObj) && isString(fileObj.name)) {
11
5
  return true;
@@ -13,4 +7,4 @@ function isUploadFile(fileObj) {
13
7
  return false;
14
8
  }
15
9
 
16
- export { getExtname, isUploadFile, testExt };
10
+ export { isUploadFile };
package/lib/VERSION.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "5.3.0";
3
+ var VERSION = "5.4.0";
4
4
  var VERSION$1 = VERSION;
5
5
 
6
6
  module.exports = VERSION$1;
@@ -2,6 +2,7 @@
2
2
 
3
3
  var ut2 = require('ut2');
4
4
  var file_util = require('./utils/file.util.js');
5
+ var getExtname = require('./getExtname.js');
5
6
 
6
7
  function checkFileType(file, accept) {
7
8
  var _a, _b;
@@ -22,7 +23,7 @@ function checkFileType(file, accept) {
22
23
  var fileType = file.type || (!isFileType && ((_b = file.originFileObj) === null || _b === void 0 ? void 0 : _b.type)) || '';
23
24
  var fileUrl = (!isFileType && file.url) || '';
24
25
  types.some(function (type) {
25
- if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (file_util.testExt(fileName, type) || file_util.testExt(fileUrl, type)))) {
26
+ if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (getExtname(fileName) === type || getExtname(fileUrl) === type))) {
26
27
  ret = true;
27
28
  }
28
29
  else if (type.includes('/*') && fileType.includes('/')) {
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ var ut2 = require('ut2');
4
+
5
+ function getExtname(path) {
6
+ return ut2.isString(path) && path.indexOf('.') > 0 ? '.' + ut2.nth(path.split('.'), -1) : '';
7
+ }
8
+
9
+ module.exports = getExtname;
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var getExtname = require('./getExtname.js');
3
4
  var native = require('./utils/native.js');
4
- var file_util = require('./utils/file.util.js');
5
5
 
6
6
  var mimeTypes = [
7
7
  ['text/plain', ['txt']],
@@ -38,7 +38,7 @@ var mimeTypes = [
38
38
  ];
39
39
  function getMimeType(fileName) {
40
40
  var _a;
41
- var ext = file_util.getExtname(fileName).slice(1);
41
+ var ext = 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
@@ -55,6 +55,7 @@ var loadImage = require('./loadImage.js');
55
55
  var loadImageWithBlob = require('./loadImageWithBlob.js');
56
56
  var loadScript = require('./loadScript.js');
57
57
  var calculateCursorPosition = require('./calculateCursorPosition.js');
58
+ var getExtname = require('./getExtname.js');
58
59
  var randomString = require('./randomString.js');
59
60
  var strlen = require('./strlen.js');
60
61
  var transformFieldNames = require('./transformFieldNames.js');
@@ -125,6 +126,7 @@ exports.loadImage = loadImage;
125
126
  exports.loadImageWithBlob = loadImageWithBlob;
126
127
  exports.loadScript = loadScript;
127
128
  exports.calculateCursorPosition = calculateCursorPosition;
129
+ exports.getExtname = getExtname;
128
130
  exports.randomString = randomString;
129
131
  exports.strlen = strlen;
130
132
  exports.transformFieldNames = transformFieldNames;
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
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;
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, onBefore = _a.onBefore;
5
5
  var style = document.createElement('style');
6
6
  if (style.styleSheet) {
7
7
  style.styleSheet.cssText = css;
@@ -9,6 +9,9 @@ function injectStyle(css, options) {
9
9
  else {
10
10
  style.appendChild(document.createTextNode(css));
11
11
  }
12
+ if (typeof onBefore === 'function') {
13
+ onBefore(style);
14
+ }
12
15
  var atTop = insertAt === 'top';
13
16
  if (atTop && container.prepend) {
14
17
  container.prepend(style);
@@ -2,12 +2,6 @@
2
2
 
3
3
  var ut2 = require('ut2');
4
4
 
5
- function getExtname(name) {
6
- return ut2.isString(name) && name.indexOf('.') > 0 ? '.' + ut2.nth(name.split('.'), -1) : '';
7
- }
8
- function testExt(name, ext) {
9
- return !!name && getExtname(name) === ext;
10
- }
11
5
  function isUploadFile(fileObj) {
12
6
  if (ut2.isObjectLike(fileObj) && ut2.isString(fileObj.name)) {
13
7
  return true;
@@ -15,6 +9,4 @@ function isUploadFile(fileObj) {
15
9
  return false;
16
10
  }
17
11
 
18
- exports.getExtname = getExtname;
19
12
  exports.isUploadFile = isUploadFile;
20
- exports.testExt = testExt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "5.3.0",
3
+ "version": "5.4.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -51,9 +51,9 @@
51
51
  },
52
52
  "homepage": "https://doly-dev.github.io/util-helpers/index.html",
53
53
  "devDependencies": {
54
- "@babel/core": "^7.25.8",
55
- "@babel/preset-env": "^7.25.8",
56
- "@babel/preset-typescript": "^7.25.7",
54
+ "@babel/core": "^7.26.0",
55
+ "@babel/preset-env": "^7.26.0",
56
+ "@babel/preset-typescript": "^7.26.0",
57
57
  "@commitlint/cli": "^17.8.1",
58
58
  "@commitlint/config-conventional": "^17.8.1",
59
59
  "@commitlint/cz-commitlint": "^17.8.1",
@@ -62,7 +62,7 @@
62
62
  "@rollup/plugin-replace": "^5.0.7",
63
63
  "@rollup/plugin-terser": "^0.4.4",
64
64
  "@rollup/plugin-typescript": "^11.1.6",
65
- "@types/jest": "^29.5.13",
65
+ "@types/jest": "^29.5.14",
66
66
  "@typescript-eslint/eslint-plugin": "^7.18.0",
67
67
  "@typescript-eslint/parser": "^7.18.0",
68
68
  "babel-jest": "^29.7.0",
@@ -71,16 +71,16 @@
71
71
  "cross-env": "^7.0.3",
72
72
  "docdash": "^2.0.2",
73
73
  "eslint": "^8.57.1",
74
- "husky": "^9.1.6",
74
+ "husky": "^9.1.7",
75
75
  "inquirer": "^8.2.6",
76
76
  "jest": "^29.7.0",
77
77
  "jest-canvas-mock": "^2.5.2",
78
78
  "jest-environment-jsdom": "^29.7.0",
79
79
  "jsdoc": "^4.0.4",
80
80
  "lint-staged": "^13.3.0",
81
- "prettier": "^3.3.3",
82
- "rollup": "^4.24.0",
83
- "typescript": "^5.6.3"
81
+ "prettier": "^3.4.2",
82
+ "rollup": "^4.28.0",
83
+ "typescript": "^5.7.2"
84
84
  },
85
85
  "lint-staged": {
86
86
  "**/*.ts": "eslint",
@@ -92,9 +92,9 @@
92
92
  }
93
93
  },
94
94
  "dependencies": {
95
- "cache2": "^3.1.1",
96
- "tslib": "^2.8.0",
97
- "ut2": "^1.13.0"
95
+ "cache2": "^3.1.2",
96
+ "tslib": "^2.8.1",
97
+ "ut2": "^1.15.0"
98
98
  },
99
99
  "publishConfig": {
100
100
  "registry": "https://registry.npmjs.org/"
@@ -38,6 +38,15 @@ declare class AsyncMemo<DataType = any> {
38
38
  * @param {number} [options.ttl] 数据存活时间
39
39
  * @param {boolean} [options.persisted=true] 数据持久化,默认`true`。如果存在缓存数据,直接返回缓存数据,不再执行异步方法。<br/>即使不开启该配置,不影响在异步执行成功后缓存数据。
40
40
  * @returns {Promise<*>} 异步结果
41
+ * @example
42
+ *
43
+ * const asyncMemo = new AsyncMemo();
44
+ * asyncMemo.run(()=>download({ fssid: 'a' }), 'a');
45
+ * asyncMemo.run(()=>download({ fssid: 'b' }), 'b');
46
+ * asyncMemo.run(()=>download({ fssid: 'a' }), 'a'); // 如果有缓存结果直接返回,如果有异步执行中,不会重复触发异步,但共享异步结果。
47
+ *
48
+ * asyncMemo.run(()=>download({ fssid: 'a' }), 'a', { persisted: false }); // 不读取缓存结果,但是异步执行结果还是会缓存。
49
+ * asyncMemo.run(()=>download({ fssid: 'a' })); // 没有缓存键时,直接执行异步方法,不读取缓存结果,也不会缓存异步结果。
41
50
  */
42
51
  run(asyncFn: (...args: any[]) => Promise<DataType>, key?: string, options?: {
43
52
  ttl?: number;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 获取路径的扩展名。
3
+ *
4
+ * @alias module:Other.getExtname
5
+ * @since 5.4.0
6
+ * @see {@link https://nodejs.org/docs/latest/api/path.html#pathextnamepath | Node.js path.extname}
7
+ * @param {string} path 路径。
8
+ * @returns 返回从最后一次出现 `.` 字符到路径最后一部分的字符串结尾。如果路径没有 `.` 或者除了第一个字符之外没有其他 `.` 字符,则返回空字符串。
9
+ * @example
10
+ * getExtname('index.html'); // '.html'
11
+ * getExtname('index.coffee.md'); // '.md'
12
+ * getExtname('index.'); // '.'
13
+ * getExtname('index'); // ''
14
+ * getExtname('.index'); // ''
15
+ * getExtname('index.md'); // '.md'
16
+ */
17
+ declare function getExtname(path: string): string;
18
+ export default getExtname;
package/types/index.d.ts CHANGED
@@ -113,6 +113,7 @@ export { default as loadScript } from './loadScript';
113
113
  * @module Other
114
114
  */
115
115
  export { default as calculateCursorPosition } from './calculateCursorPosition';
116
+ export { default as getExtname } from './getExtname';
116
117
  export { default as randomString } from './randomString';
117
118
  export { default as strlen } from './strlen';
118
119
  /**
@@ -7,10 +7,25 @@
7
7
  * @param {Object} [options] 配置项。
8
8
  * @param {HTMLElement} [options.container=document.head] 要注入样式的容器。默认 `document.head`。如果 `document.head` 不存在,默认 `document.body`。
9
9
  * @param {'top' | 'bottom'} [options.insertAt='top'] 注入容器内容前面还是后面。默认 `top`。
10
+ * @param {Function} [options.onBefore] 注入样式前的回调方法。
10
11
  * @returns {HTMLStyleElement} `style` 元素。
12
+ * @example
13
+ * const css1 = 'body { background-color: red; }';
14
+ * injectStyle(css1);
15
+ *
16
+ * // 自定义配置
17
+ * const css2 = 'p { margin: 10px; } a { color: blue; }';
18
+ * injectStyle(css2, {
19
+ * container: document.body,
20
+ * insertAt: 'bottom',
21
+ * onBefore(style){
22
+ * style.nonce = '...';
23
+ * }
24
+ * });
11
25
  */
12
26
  declare function injectStyle(css: string, options?: {
13
27
  container?: HTMLElement;
14
28
  insertAt?: 'top' | 'bottom';
29
+ onBefore?: (style: HTMLStyleElement) => void;
15
30
  }): HTMLStyleElement;
16
31
  export default injectStyle;
@@ -1,5 +1,3 @@
1
- export declare function getExtname(name: string): string;
2
- export declare function testExt(name: string | undefined, ext: string): boolean;
3
1
  export type UploadFile = {
4
2
  name: string;
5
3
  type?: string;