util-helpers 5.7.4 → 5.7.5
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 +2 -1
- package/dist/util-helpers.js +98 -91
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/VERSION.js +1 -1
- package/esm/loadScript.js +30 -23
- package/lib/VERSION.js +1 -1
- package/lib/loadScript.js +29 -22
- package/package.json +24 -24
- package/types/filterTree.d.ts +1 -1
- package/types/findTreeNode.d.ts +1 -1
- package/types/findTreeNodes.d.ts +1 -1
- package/types/findTreeSelect.d.ts +1 -1
- package/types/getMimeType.d.ts +1 -0
- package/types/isEmail.d.ts +7 -1
- package/types/listToTree.d.ts +1 -1
- package/types/loadScript.d.ts +15 -2
- package/types/transformFieldNames.d.ts +3 -3
package/esm/VERSION.js
CHANGED
package/esm/loadScript.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { __assign, __rest } from 'tslib';
|
|
2
2
|
import { objectKeys } from './utils/native.js';
|
|
3
3
|
|
|
4
|
-
function
|
|
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 =
|
|
9
|
-
var props = __assign(
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
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
|
|
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 =
|
|
11
|
-
var props = tslib.__assign(
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "util-helpers",
|
|
3
|
-
"version": "5.7.
|
|
3
|
+
"version": "5.7.5",
|
|
4
4
|
"description": "一个基于业务场景的工具方法库",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"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
18
|
"doc:open": "open ./docs/index.html",
|
|
19
19
|
"prettier": "prettier -w **/*",
|
|
20
|
-
"lint": "eslint .
|
|
21
|
-
"lint:fix": "
|
|
20
|
+
"lint": "eslint .",
|
|
21
|
+
"lint:fix": "npm run lint -- --fix",
|
|
22
22
|
"commit": "cz",
|
|
23
23
|
"prepublishOnly": "npm test && npm run build",
|
|
24
24
|
"tsc": "tsc --noEmit",
|
|
@@ -49,32 +49,32 @@
|
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://doly-dev.github.io/util-helpers/index.html",
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@commitlint/cli": "^
|
|
53
|
-
"@commitlint/config-conventional": "^
|
|
54
|
-
"@commitlint/cz-commitlint": "^
|
|
55
|
-
"@
|
|
56
|
-
"@rollup/plugin-
|
|
57
|
-
"@rollup/plugin-
|
|
52
|
+
"@commitlint/cli": "^19.8.1",
|
|
53
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
54
|
+
"@commitlint/cz-commitlint": "^19.8.1",
|
|
55
|
+
"@eslint/js": "^9.39.1",
|
|
56
|
+
"@rollup/plugin-commonjs": "^28.0.9",
|
|
57
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
58
|
+
"@rollup/plugin-replace": "^6.0.3",
|
|
58
59
|
"@rollup/plugin-terser": "^0.4.4",
|
|
59
|
-
"@rollup/plugin-typescript": "^
|
|
60
|
-
"@types/jest": "^
|
|
61
|
-
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
62
|
-
"@typescript-eslint/parser": "^7.18.0",
|
|
60
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
61
|
+
"@types/jest": "^30.0.0",
|
|
63
62
|
"commitizen": "^4.3.1",
|
|
64
63
|
"cross-env": "^7.0.3",
|
|
65
64
|
"docdash": "^2.0.2",
|
|
66
|
-
"eslint": "^
|
|
65
|
+
"eslint": "^9.39.1",
|
|
66
|
+
"globals": "^16.5.0",
|
|
67
67
|
"husky": "^9.1.7",
|
|
68
|
-
"
|
|
69
|
-
"jest": "^29.7.0",
|
|
68
|
+
"jest": "^30.2.0",
|
|
70
69
|
"jest-canvas-mock": "^2.5.2",
|
|
71
|
-
"jest-environment-jsdom": "^
|
|
72
|
-
"jsdoc": "^4.0.
|
|
73
|
-
"lint-staged": "^
|
|
74
|
-
"prettier": "^3.
|
|
75
|
-
"rollup": "^4.
|
|
76
|
-
"ts-jest": "^29.
|
|
77
|
-
"typescript": "^5.
|
|
70
|
+
"jest-environment-jsdom": "^30.2.0",
|
|
71
|
+
"jsdoc": "^4.0.5",
|
|
72
|
+
"lint-staged": "^15.5.2",
|
|
73
|
+
"prettier": "^3.7.4",
|
|
74
|
+
"rollup": "^4.53.3",
|
|
75
|
+
"ts-jest": "^29.4.6",
|
|
76
|
+
"typescript": "^5.9.3",
|
|
77
|
+
"typescript-eslint": "^8.48.1"
|
|
78
78
|
},
|
|
79
79
|
"lint-staged": {
|
|
80
80
|
"**/*.ts": "eslint",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"dependencies": {
|
|
89
89
|
"cache2": "^3.1.2",
|
|
90
90
|
"tslib": "^2.8.1",
|
|
91
|
-
"ut2": "^1.
|
|
91
|
+
"ut2": "^1.20.1"
|
|
92
92
|
},
|
|
93
93
|
"publishConfig": {
|
|
94
94
|
"registry": "https://registry.npmjs.org/"
|
package/types/filterTree.d.ts
CHANGED
|
@@ -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
|
|
28
|
+
declare function filterTree<T, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string, nodeAssign?: NodeAssign): T[];
|
|
29
29
|
export default filterTree;
|
package/types/findTreeNode.d.ts
CHANGED
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
* findTreeNode(menus, item=>item.id === 'not found');
|
|
20
20
|
* // undefined
|
|
21
21
|
*/
|
|
22
|
-
declare function findTreeNode<T
|
|
22
|
+
declare function findTreeNode<T, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T | undefined;
|
|
23
23
|
export default findTreeNode;
|
package/types/findTreeNodes.d.ts
CHANGED
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
* findTreeNodes(menus, item=>item.id === 'not found');
|
|
23
23
|
* // []
|
|
24
24
|
*/
|
|
25
|
-
declare function findTreeNodes<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
|
|
22
|
+
declare function findTreeSelect<T, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string): T[];
|
|
23
23
|
export default findTreeSelect;
|
package/types/getMimeType.d.ts
CHANGED
|
@@ -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
|
package/types/isEmail.d.ts
CHANGED
|
@@ -7,9 +7,15 @@
|
|
|
7
7
|
* @returns {boolean} 值是否为Email
|
|
8
8
|
* @example
|
|
9
9
|
*
|
|
10
|
-
* isEmail('
|
|
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;
|
package/types/listToTree.d.ts
CHANGED
|
@@ -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
|
|
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;
|
package/types/loadScript.d.ts
CHANGED
|
@@ -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
|
|
43
|
+
declare const loadScript: LoadScript;
|
|
31
44
|
export default loadScript;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
type NodeAssign = 'spread' | 'self';
|
|
2
|
-
type ExchangeFieldNames<D
|
|
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
|
|
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
|
|
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;
|