util-helpers 5.7.4 → 5.7.6

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,3 +1,3 @@
1
- var VERSION = "5.7.4";
1
+ var VERSION = "5.7.6";
2
2
 
3
3
  export { VERSION as default };
package/esm/loadScript.js CHANGED
@@ -1,16 +1,20 @@
1
- import { __rest, __assign } from 'tslib';
1
+ import { __assign, __rest } from 'tslib';
2
2
  import { objectKeys } from './utils/native.js';
3
3
 
4
- function loadScript(src, options) {
4
+ var loadScript = function (_src, options) {
5
+ var finalOptions;
6
+ if (typeof _src === 'object') {
7
+ finalOptions = _src;
8
+ }
9
+ else if (typeof _src === 'string') {
10
+ finalOptions = __assign({ src: _src }, options);
11
+ }
5
12
  return new Promise(function (resolve, reject) {
6
13
  var container = document.head || document.getElementsByTagName('head')[0] || document.body;
7
14
  var script = document.createElement('script');
8
- var _a = options || {}, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, restOptions = __rest(_a, ["attrs", "destroyOnError"]);
9
- var props = __assign(__assign({ async: true, type: 'text/javascript' }, restOptions), { src: src });
15
+ var _a = finalOptions || {}, src = _a.src, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, onload = _a.onload, onerror = _a.onerror, restOptions = __rest(_a, ["src", "attrs", "destroyOnError", "onload", "onerror"]);
16
+ var props = __assign({ async: true, type: 'text/javascript' }, restOptions);
10
17
  for (var key in props) {
11
- if (key === 'onload' || key === 'onerror') {
12
- continue;
13
- }
14
18
  script[key] = props[key];
15
19
  }
16
20
  if (typeof attrs === 'object') {
@@ -18,23 +22,26 @@ function loadScript(src, options) {
18
22
  script.setAttribute(key, attrs[key]);
19
23
  });
20
24
  }
21
- script.onload = function (ev) {
22
- var _a;
23
- this.onerror = this.onload = null;
24
- (_a = props.onload) === null || _a === void 0 ? void 0 : _a.call(this, ev);
25
- resolve(script);
26
- };
27
- script.onerror = function (ev) {
28
- var _a;
29
- this.onerror = this.onload = null;
30
- (_a = props.onerror) === null || _a === void 0 ? void 0 : _a.call(this, ev);
31
- if (destroyOnError) {
32
- container.removeChild(script);
33
- }
34
- reject(new URIError('Failed to load ' + this.src));
35
- };
25
+ if (src) {
26
+ script.src = src;
27
+ script.onload = function (ev) {
28
+ onload === null || onload === void 0 ? void 0 : onload.call(this, ev);
29
+ resolve(script);
30
+ };
31
+ script.onerror = function (ev) {
32
+ this.onerror = this.onload = null;
33
+ onerror === null || onerror === void 0 ? void 0 : onerror.call(this, ev);
34
+ if (destroyOnError) {
35
+ container.removeChild(script);
36
+ }
37
+ reject(new URIError('Failed to load ' + this.src));
38
+ };
39
+ }
36
40
  container.appendChild(script);
41
+ if (!src) {
42
+ resolve(script);
43
+ }
37
44
  });
38
- }
45
+ };
39
46
 
40
47
  export { loadScript as default };
package/lib/VERSION.js CHANGED
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "5.7.4";
3
+ var VERSION = "5.7.6";
4
4
 
5
5
  module.exports = VERSION;
package/lib/loadScript.js CHANGED
@@ -3,16 +3,20 @@
3
3
  var tslib = require('tslib');
4
4
  var native = require('./utils/native.js');
5
5
 
6
- function loadScript(src, options) {
6
+ var loadScript = function (_src, options) {
7
+ var finalOptions;
8
+ if (typeof _src === 'object') {
9
+ finalOptions = _src;
10
+ }
11
+ else if (typeof _src === 'string') {
12
+ finalOptions = tslib.__assign({ src: _src }, options);
13
+ }
7
14
  return new Promise(function (resolve, reject) {
8
15
  var container = document.head || document.getElementsByTagName('head')[0] || document.body;
9
16
  var script = document.createElement('script');
10
- var _a = options || {}, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, restOptions = tslib.__rest(_a, ["attrs", "destroyOnError"]);
11
- var props = tslib.__assign(tslib.__assign({ async: true, type: 'text/javascript' }, restOptions), { src: src });
17
+ var _a = finalOptions || {}, src = _a.src, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, onload = _a.onload, onerror = _a.onerror, restOptions = tslib.__rest(_a, ["src", "attrs", "destroyOnError", "onload", "onerror"]);
18
+ var props = tslib.__assign({ async: true, type: 'text/javascript' }, restOptions);
12
19
  for (var key in props) {
13
- if (key === 'onload' || key === 'onerror') {
14
- continue;
15
- }
16
20
  script[key] = props[key];
17
21
  }
18
22
  if (typeof attrs === 'object') {
@@ -20,23 +24,26 @@ function loadScript(src, options) {
20
24
  script.setAttribute(key, attrs[key]);
21
25
  });
22
26
  }
23
- script.onload = function (ev) {
24
- var _a;
25
- this.onerror = this.onload = null;
26
- (_a = props.onload) === null || _a === void 0 ? void 0 : _a.call(this, ev);
27
- resolve(script);
28
- };
29
- script.onerror = function (ev) {
30
- var _a;
31
- this.onerror = this.onload = null;
32
- (_a = props.onerror) === null || _a === void 0 ? void 0 : _a.call(this, ev);
33
- if (destroyOnError) {
34
- container.removeChild(script);
35
- }
36
- reject(new URIError('Failed to load ' + this.src));
37
- };
27
+ if (src) {
28
+ script.src = src;
29
+ script.onload = function (ev) {
30
+ onload === null || onload === void 0 ? void 0 : onload.call(this, ev);
31
+ resolve(script);
32
+ };
33
+ script.onerror = function (ev) {
34
+ this.onerror = this.onload = null;
35
+ onerror === null || onerror === void 0 ? void 0 : onerror.call(this, ev);
36
+ if (destroyOnError) {
37
+ container.removeChild(script);
38
+ }
39
+ reject(new URIError('Failed to load ' + this.src));
40
+ };
41
+ }
38
42
  container.appendChild(script);
43
+ if (!src) {
44
+ resolve(script);
45
+ }
39
46
  });
40
- }
47
+ };
41
48
 
42
49
  module.exports = loadScript;
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "5.7.4",
3
+ "version": "5.7.6",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
7
7
  "unpkg": "dist/util-helpers.min.js",
8
+ "browser": "dist/util-helpers.min.js",
8
9
  "sideEffects": false,
9
10
  "types": "types/index.d.ts",
10
11
  "scripts": {
@@ -17,8 +18,8 @@
17
18
  "doc": "rm -rf docs && rm -rf docs-src && tsc -p tsconfig.build.json -t esnext --outDir docs-src && jsdoc -c conf.json && rm -rf docs-src",
18
19
  "doc:open": "open ./docs/index.html",
19
20
  "prettier": "prettier -w **/*",
20
- "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
21
- "lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
21
+ "lint": "eslint .",
22
+ "lint:fix": "npm run lint -- --fix",
22
23
  "commit": "cz",
23
24
  "prepublishOnly": "npm test && npm run build",
24
25
  "tsc": "tsc --noEmit",
@@ -49,32 +50,32 @@
49
50
  },
50
51
  "homepage": "https://doly-dev.github.io/util-helpers/index.html",
51
52
  "devDependencies": {
52
- "@commitlint/cli": "^17.8.1",
53
- "@commitlint/config-conventional": "^17.8.1",
54
- "@commitlint/cz-commitlint": "^17.8.1",
55
- "@rollup/plugin-commonjs": "^25.0.8",
56
- "@rollup/plugin-node-resolve": "^15.3.1",
57
- "@rollup/plugin-replace": "^5.0.7",
53
+ "@commitlint/cli": "^19.8.1",
54
+ "@commitlint/config-conventional": "^19.8.1",
55
+ "@commitlint/cz-commitlint": "^19.8.1",
56
+ "@eslint/js": "^9.39.1",
57
+ "@rollup/plugin-commonjs": "^28.0.9",
58
+ "@rollup/plugin-node-resolve": "^16.0.3",
59
+ "@rollup/plugin-replace": "^6.0.3",
58
60
  "@rollup/plugin-terser": "^0.4.4",
59
- "@rollup/plugin-typescript": "^11.1.6",
60
- "@types/jest": "^29.5.14",
61
- "@typescript-eslint/eslint-plugin": "^7.18.0",
62
- "@typescript-eslint/parser": "^7.18.0",
61
+ "@rollup/plugin-typescript": "^12.3.0",
62
+ "@types/jest": "^30.0.0",
63
63
  "commitizen": "^4.3.1",
64
64
  "cross-env": "^7.0.3",
65
65
  "docdash": "^2.0.2",
66
- "eslint": "^8.57.1",
66
+ "eslint": "^9.39.1",
67
+ "globals": "^16.5.0",
67
68
  "husky": "^9.1.7",
68
- "inquirer": "^8.2.6",
69
- "jest": "^29.7.0",
69
+ "jest": "^30.2.0",
70
70
  "jest-canvas-mock": "^2.5.2",
71
- "jest-environment-jsdom": "^29.7.0",
72
- "jsdoc": "^4.0.4",
73
- "lint-staged": "^13.3.0",
74
- "prettier": "^3.5.3",
75
- "rollup": "^4.36.0",
76
- "ts-jest": "^29.2.6",
77
- "typescript": "^5.8.2"
71
+ "jest-environment-jsdom": "^30.2.0",
72
+ "jsdoc": "^4.0.5",
73
+ "lint-staged": "^15.5.2",
74
+ "prettier": "^3.7.4",
75
+ "rollup": "^4.53.3",
76
+ "ts-jest": "^29.4.6",
77
+ "typescript": "^5.9.3",
78
+ "typescript-eslint": "^8.48.1"
78
79
  },
79
80
  "lint-staged": {
80
81
  "**/*.ts": "eslint",
@@ -88,7 +89,7 @@
88
89
  "dependencies": {
89
90
  "cache2": "^3.1.2",
90
91
  "tslib": "^2.8.1",
91
- "ut2": "^1.17.0"
92
+ "ut2": "^1.20.1"
92
93
  },
93
94
  "publishConfig": {
94
95
  "registry": "https://registry.npmjs.org/"
@@ -25,5 +25,5 @@ type NodeAssign = 'spread' | 'self';
25
25
  * filterTree(menus, item=>item.id === 'not found');
26
26
  * // []
27
27
  */
28
- declare function filterTree<T extends any, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string, nodeAssign?: NodeAssign): T[];
28
+ declare function filterTree<T, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string, nodeAssign?: NodeAssign): T[];
29
29
  export default filterTree;
@@ -19,5 +19,5 @@
19
19
  * findTreeNode(menus, item=>item.id === 'not found');
20
20
  * // undefined
21
21
  */
22
- declare function findTreeNode<T extends any, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T | undefined;
22
+ declare function findTreeNode<T, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T | undefined;
23
23
  export default findTreeNode;
@@ -22,5 +22,5 @@
22
22
  * findTreeNodes(menus, item=>item.id === 'not found');
23
23
  * // []
24
24
  */
25
- declare function findTreeNodes<T extends any, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T[];
25
+ declare function findTreeNodes<T, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T[];
26
26
  export default findTreeNodes;
@@ -19,5 +19,5 @@
19
19
  * findTreeSelect(menus, item => item.id === 'not found');
20
20
  * // []
21
21
  */
22
- declare function findTreeSelect<T extends any, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T[];
22
+ declare function findTreeSelect<T, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T[];
23
23
  export default findTreeSelect;
@@ -5,6 +5,7 @@
5
5
  * @since 5.2.0
6
6
  * @see {@link https://developer.mozilla.org/zh-CN/docs/Web/HTTP/MIME_types MIME 类型(IANA 媒体类型)}
7
7
  * @see {@link https://www.iana.org/assignments/media-types/media-types.xhtml Media Types}
8
+ * @see 如果需要完整的MIME类型,推荐使用 {@link https://www.npmjs.com/package/mime-type | mime-type}
8
9
  * @param {string} fileName 文件名。
9
10
  * @returns 如果找到,返回 MIME 类型字符串,否则返回 `undefined`。
10
11
  * @example
@@ -7,9 +7,15 @@
7
7
  * @returns {boolean} 值是否为Email
8
8
  * @example
9
9
  *
10
- * isEmail('1232@qq.com'); // true
10
+ * isEmail('123@qq.com'); // true
11
+ * isEmail('1-23@qq.com'); // true
11
12
  * isEmail('123@'); // false
12
13
  *
14
+ * // 包含非法字符(除 -、_、. 以外的符号)
15
+ * isEmail('1 23@qq.com'); // false
16
+ * isEmail('1&23@qq.com'); // false
17
+ * isEmail('1%23@qq.com'); // false
18
+ *
13
19
  */
14
20
  declare function isEmail(value: any): boolean;
15
21
  export default isEmail;
@@ -38,5 +38,5 @@ type Options = {
38
38
  * // [{id:'1',name:'首页',code:'trade',pid:null},{id:'2',name:'交易管理',code:'trade',pid:null,childs:[{id:'3',name:'交易查询',code:'trade-1',pid:'2',childs:[{id:'4',name:'交易查询-查询操作',code:'trade-1-1',pid:'3'}]}]},{id:'5',name:'权限管理',code:'authorization',pid:null,childs:[{id:'6',name:'角色管理',code:'authorization-1',pid:'5'},{id:'7',name:'用户管理',code:'authorization-2',pid:'5'}]}]
39
39
  *
40
40
  */
41
- declare function listToTree<T extends Record<string, any> = Record<string, any>, R extends unknown = T & Record<string, any>>(list: T[], options?: Options): R[];
41
+ declare function listToTree<T extends Record<string, any> = Record<string, any>, R = T & Record<string, any>>(list: T[], options?: Options): R[];
42
42
  export default listToTree;
@@ -2,6 +2,12 @@ type ScriptAttribute = Pick<HTMLScriptElement, 'async' | 'crossOrigin' | 'defer'
2
2
  attrs: Record<string, string>;
3
3
  destroyOnError: boolean;
4
4
  };
5
+ interface LoadScript {
6
+ (src: string, options?: Partial<ScriptAttribute>): Promise<HTMLScriptElement>;
7
+ (options?: Partial<ScriptAttribute & {
8
+ src?: string;
9
+ }>): Promise<HTMLScriptElement>;
10
+ }
5
11
  /**
6
12
  * 加载 js 文件。
7
13
  *
@@ -9,7 +15,7 @@ type ScriptAttribute = Pick<HTMLScriptElement, 'async' | 'crossOrigin' | 'defer'
9
15
  *
10
16
  * @alias module:Browser.loadScript
11
17
  * @since 4.19.0
12
- * @param {string} src js 地址。
18
+ * @param {string} [src] js 地址。
13
19
  * @param {Object} [options] script 标签属性。比如 `defer` `onload` `onerror` `id` 等,下面列举部分带有默认值或额外扩展的配置。
14
20
  * @param {boolean} [options.destroyOnError=true] 如果加载失败或错误,自动删除 dom 中的 script 标签。默认`true`
15
21
  * @param {Object} [options.attrs] 自定义 script 属性,通过 script.setAttribute 设置。
@@ -26,6 +32,13 @@ type ScriptAttribute = Pick<HTMLScriptElement, 'async' | 'crossOrigin' | 'defer'
26
32
  * // do something
27
33
  * })
28
34
  *
35
+ * // 注入 script
36
+ * loadScript({
37
+ * text: 'console.log("hello world");'
38
+ * }).then(script=>{
39
+ * // do something
40
+ * })
41
+ *
29
42
  */
30
- declare function loadScript(src: string, options?: Partial<ScriptAttribute>): Promise<HTMLScriptElement>;
43
+ declare const loadScript: LoadScript;
31
44
  export default loadScript;
@@ -1,8 +1,8 @@
1
1
  type NodeAssign = 'spread' | 'self';
2
- type ExchangeFieldNames<D extends any, F extends Record<string, keyof D>> = Omit<D, F[keyof F]> & {
2
+ type ExchangeFieldNames<D, F extends Record<string, keyof D>> = Omit<D, F[keyof F]> & {
3
3
  [P in keyof F]: D[F[P]];
4
4
  };
5
- type TransformFieldNames<D extends any, F extends Record<string, any>, C extends string> = (C extends keyof D ? ExchangeFieldNames<Omit<D, C> & Record<C, TransformFieldNames<D, F, C>>, F> : ExchangeFieldNames<D, F>)[];
5
+ type TransformFieldNames<D, F extends Record<string, any>, C extends string> = (C extends keyof D ? ExchangeFieldNames<Omit<D, C> & Record<C, TransformFieldNames<D, F, C>>, F> : ExchangeFieldNames<D, F>)[];
6
6
  /**
7
7
  * 转换字段名,返回一个转换字段后的值,不改变原值。
8
8
  *
@@ -32,5 +32,5 @@ type TransformFieldNames<D extends any, F extends Record<string, any>, C extends
32
32
  * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
33
33
  * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
34
34
  */
35
- declare function transformFieldNames<D extends any, F extends Record<string, keyof D>, C extends string>(data: D[], fieldNames: F, childrenField?: C, nodeAssign?: NodeAssign): TransformFieldNames<D, F, C>;
35
+ declare function transformFieldNames<D, F extends Record<string, keyof D>, C extends string>(data: D[], fieldNames: F, childrenField?: C, nodeAssign?: NodeAssign): TransformFieldNames<D, F, C>;
36
36
  export default transformFieldNames;