util-helpers 4.16.2 → 4.17.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/dist/util-helpers.js +1542 -4040
- 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/ajax.js +50 -146
- package/esm/blobToDataURL.js +4 -34
- package/esm/bytesToSize.js +12 -37
- package/esm/calculateCursorPosition.js +30 -52
- package/esm/dataURLToBlob.js +14 -28
- package/esm/divide.js +24 -48
- package/esm/download.js +83 -164
- package/esm/fileReader.js +18 -64
- package/esm/filterTree.js +25 -55
- package/esm/findTreeNode.js +39 -67
- package/esm/findTreeNodes.js +35 -66
- package/esm/findTreeSelect.js +38 -74
- package/esm/formatBankCard.js +13 -43
- package/esm/formatMobile.js +17 -43
- package/esm/formatMoney.js +59 -139
- package/esm/index.js +55 -130
- package/esm/isBankCard.js +22 -57
- package/esm/isBusinessLicense.js +32 -78
- package/esm/isChinese.js +23 -66
- package/esm/isEmail.js +6 -21
- package/esm/isHMCard.js +5 -28
- package/esm/isIPv4.js +5 -22
- package/esm/isIPv6.js +5 -36
- package/esm/isIdCard.js +27 -66
- package/esm/isMobile.js +5 -20
- package/esm/isPassport.js +5 -22
- package/esm/isPassword.js +7 -48
- package/esm/isPostcode.js +5 -20
- package/esm/isPromiseLike.js +3 -18
- package/esm/isQQ.js +5 -20
- package/esm/isSocialCreditCode.js +28 -83
- package/esm/isSwiftCode.js +6 -25
- package/esm/isTWCard.js +11 -37
- package/esm/isTelephone.js +6 -27
- package/esm/isUrl.js +5 -44
- package/esm/isVehicle.js +5 -28
- package/esm/isWX.js +5 -20
- package/esm/listToTree.js +51 -120
- package/esm/minus.js +21 -39
- package/esm/normalizeString.js +8 -29
- package/esm/numberToChinese.js +96 -215
- package/esm/padZero.js +14 -36
- package/esm/parseIdCard.js +64 -129
- package/esm/plus.js +21 -39
- package/esm/randomString.js +13 -45
- package/esm/replaceChar.js +27 -77
- package/esm/round.js +14 -31
- package/esm/safeDate.js +12 -55
- package/esm/setDataURLPrefix.js +6 -29
- package/esm/strlen.js +14 -30
- package/esm/times.js +24 -42
- package/esm/transformFieldNames.js +37 -93
- package/esm/treeToList.js +25 -84
- package/esm/utils/config.js +4 -14
- package/esm/utils/constants.js +4 -4
- package/esm/utils/convertToString.js +4 -9
- package/esm/utils/devWarn.js +11 -11
- package/esm/utils/math.util.js +95 -181
- package/esm/utils/type/isBlob.js +4 -24
- package/esm/utils/type/isNaN.js +4 -19
- package/esm/utils/type/isNil.js +5 -13
- package/esm/utils/type/isNull.js +3 -18
- package/esm/utils/type/isNumber.js +4 -28
- package/esm/utils/type/isObject.js +4 -23
- package/esm/utils/type/isString.js +4 -19
- package/esm/utils/type/isSymbol.js +4 -19
- package/esm/utils/type/isType.js +3 -12
- package/esm/utils/type/isUndefined.js +3 -21
- package/esm/validatePassword.js +86 -237
- package/esm/waitTime.js +7 -26
- package/lib/ajax.js +51 -152
- package/lib/blobToDataURL.js +5 -40
- package/lib/bytesToSize.js +13 -43
- package/lib/calculateCursorPosition.js +32 -58
- package/lib/dataURLToBlob.js +15 -34
- package/lib/divide.js +26 -60
- package/lib/download.js +85 -169
- package/lib/fileReader.js +19 -70
- package/lib/filterTree.js +27 -66
- package/lib/findTreeNode.js +40 -77
- package/lib/findTreeNodes.js +36 -76
- package/lib/findTreeSelect.js +40 -82
- package/lib/formatBankCard.js +15 -50
- package/lib/formatMobile.js +19 -50
- package/lib/formatMoney.js +60 -150
- package/lib/index.js +117 -395
- package/lib/isBankCard.js +24 -64
- package/lib/isBusinessLicense.js +34 -85
- package/lib/isChinese.js +24 -73
- package/lib/isEmail.js +7 -27
- package/lib/isHMCard.js +7 -35
- package/lib/isIPv4.js +7 -29
- package/lib/isIPv6.js +7 -43
- package/lib/isIdCard.js +29 -73
- package/lib/isMobile.js +7 -27
- package/lib/isPassport.js +7 -29
- package/lib/isPassword.js +9 -55
- package/lib/isPostcode.js +7 -27
- package/lib/isPromiseLike.js +4 -24
- package/lib/isQQ.js +7 -27
- package/lib/isSocialCreditCode.js +30 -90
- package/lib/isSwiftCode.js +7 -32
- package/lib/isTWCard.js +13 -44
- package/lib/isTelephone.js +7 -33
- package/lib/isUrl.js +6 -49
- package/lib/isVehicle.js +7 -35
- package/lib/isWX.js +7 -27
- package/lib/listToTree.js +53 -131
- package/lib/minus.js +23 -51
- package/lib/normalizeString.js +10 -36
- package/lib/numberToChinese.js +98 -222
- package/lib/padZero.js +16 -43
- package/lib/parseIdCard.js +65 -138
- package/lib/plus.js +23 -51
- package/lib/randomString.js +14 -51
- package/lib/replaceChar.js +28 -83
- package/lib/round.js +15 -37
- package/lib/safeDate.js +13 -63
- package/lib/setDataURLPrefix.js +7 -35
- package/lib/strlen.js +16 -37
- package/lib/times.js +26 -53
- package/lib/transformFieldNames.js +38 -101
- package/lib/treeToList.js +27 -94
- package/lib/utils/config.js +6 -23
- package/lib/utils/constants.js +3 -11
- package/lib/utils/convertToString.js +6 -16
- package/lib/utils/devWarn.js +13 -17
- package/lib/utils/math.util.js +95 -185
- package/lib/utils/type/isBlob.js +6 -31
- package/lib/utils/type/isNaN.js +6 -26
- package/lib/utils/type/isNil.js +7 -20
- package/lib/utils/type/isNull.js +4 -24
- package/lib/utils/type/isNumber.js +6 -35
- package/lib/utils/type/isObject.js +5 -29
- package/lib/utils/type/isString.js +6 -26
- package/lib/utils/type/isSymbol.js +6 -26
- package/lib/utils/type/isType.js +4 -18
- package/lib/utils/type/isUndefined.js +4 -27
- package/lib/validatePassword.js +87 -244
- package/lib/waitTime.js +8 -32
- package/package.json +21 -25
- package/types/ajax.d.ts +38 -95
- package/types/blobToDataURL.d.ts +2 -2
- package/types/bytesToSize.d.ts +7 -6
- package/types/calculateCursorPosition.d.ts +10 -9
- package/types/dataURLToBlob.d.ts +5 -5
- package/types/divide.d.ts +2 -2
- package/types/download.d.ts +22 -38
- package/types/fileReader.d.ts +35 -2
- package/types/filterTree.d.ts +6 -7
- package/types/findTreeNode.d.ts +5 -7
- package/types/findTreeNodes.d.ts +5 -7
- package/types/findTreeSelect.d.ts +5 -7
- package/types/formatBankCard.d.ts +8 -5
- package/types/formatMobile.d.ts +7 -4
- package/types/formatMoney.d.ts +8 -7
- package/types/index.d.ts +125 -55
- package/types/isBankCard.d.ts +9 -8
- package/types/isBusinessLicense.d.ts +8 -5
- package/types/isChinese.d.ts +9 -8
- package/types/isEmail.d.ts +1 -1
- package/types/isHMCard.d.ts +2 -2
- package/types/isIPv4.d.ts +1 -1
- package/types/isIPv6.d.ts +1 -1
- package/types/isIdCard.d.ts +8 -7
- package/types/isMobile.d.ts +1 -1
- package/types/isPassport.d.ts +2 -2
- package/types/isPassword.d.ts +4 -7
- package/types/isPostcode.d.ts +1 -1
- package/types/isPromiseLike.d.ts +1 -1
- package/types/isQQ.d.ts +1 -1
- package/types/isSocialCreditCode.d.ts +8 -5
- package/types/isSwiftCode.d.ts +2 -2
- package/types/isTWCard.d.ts +6 -5
- package/types/isTelephone.d.ts +1 -1
- package/types/isUrl.d.ts +2 -2
- package/types/isVehicle.d.ts +2 -2
- package/types/isWX.d.ts +1 -1
- package/types/listToTree.d.ts +12 -12
- package/types/minus.d.ts +3 -2
- package/types/normalizeString.d.ts +2 -2
- package/types/numberToChinese.d.ts +13 -12
- package/types/padZero.d.ts +2 -2
- package/types/parseIdCard.d.ts +17 -56
- package/types/plus.d.ts +3 -2
- package/types/randomString.d.ts +2 -2
- package/types/replaceChar.d.ts +15 -11
- package/types/round.d.ts +2 -2
- package/types/safeDate.d.ts +24 -2
- package/types/setDataURLPrefix.d.ts +5 -5
- package/types/strlen.d.ts +1 -1
- package/types/times.d.ts +3 -2
- package/types/transformFieldNames.d.ts +12 -10
- package/types/treeToList.d.ts +1 -1
- package/types/utils/config.d.ts +6 -5
- package/types/utils/constants.d.ts +2 -2
- package/types/utils/convertToString.d.ts +1 -1
- package/types/utils/devWarn.d.ts +1 -1
- package/types/utils/math.util.d.ts +15 -9
- package/types/utils/type/index.d.ts +7 -0
- package/types/utils/type/isArguments.d.ts +1 -1
- package/types/utils/type/isArray.d.ts +1 -1
- package/types/utils/type/isArrayBuffer.d.ts +1 -1
- package/types/utils/type/isBlob.d.ts +1 -1
- package/types/utils/type/isBoolean.d.ts +1 -1
- package/types/utils/type/isDate.d.ts +1 -1
- package/types/utils/type/isError.d.ts +1 -1
- package/types/utils/type/isFunction.d.ts +1 -1
- package/types/utils/type/isMap.d.ts +1 -1
- package/types/utils/type/isNaN.d.ts +1 -1
- package/types/utils/type/isNil.d.ts +1 -1
- package/types/utils/type/isNull.d.ts +1 -1
- package/types/utils/type/isNumber.d.ts +1 -1
- package/types/utils/type/isObject.d.ts +1 -1
- package/types/utils/type/isRegExp.d.ts +1 -1
- package/types/utils/type/isSet.d.ts +1 -1
- package/types/utils/type/isString.d.ts +1 -1
- package/types/utils/type/isSymbol.d.ts +1 -1
- package/types/utils/type/isType.d.ts +1 -1
- package/types/utils/type/isUndefined.d.ts +1 -1
- package/types/utils/type/isWeakMap.d.ts +1 -1
- package/types/utils/type/isWeakSet.d.ts +1 -1
- package/types/validatePassword.d.ts +17 -48
- package/types/waitTime.d.ts +2 -2
- package/esm/interface.doc.js +0 -125
- package/esm/interface.type.js +0 -17
- package/esm/utils/type/index.js +0 -29
- package/esm/utils/type/isArguments.js +0 -22
- package/esm/utils/type/isArray.js +0 -22
- package/esm/utils/type/isArrayBuffer.js +0 -25
- package/esm/utils/type/isBoolean.js +0 -22
- package/esm/utils/type/isDate.js +0 -22
- package/esm/utils/type/isError.js +0 -22
- package/esm/utils/type/isFunction.js +0 -22
- package/esm/utils/type/isMap.js +0 -22
- package/esm/utils/type/isRegExp.js +0 -22
- package/esm/utils/type/isSet.js +0 -22
- package/esm/utils/type/isWeakMap.js +0 -22
- package/esm/utils/type/isWeakSet.js +0 -22
- package/lib/interface.doc.js +0 -127
- package/lib/interface.type.js +0 -5
- package/lib/utils/type/index.js +0 -146
- package/lib/utils/type/isArguments.js +0 -29
- package/lib/utils/type/isArray.js +0 -29
- package/lib/utils/type/isArrayBuffer.js +0 -32
- package/lib/utils/type/isBoolean.js +0 -29
- package/lib/utils/type/isDate.js +0 -29
- package/lib/utils/type/isError.js +0 -29
- package/lib/utils/type/isFunction.js +0 -29
- package/lib/utils/type/isMap.js +0 -29
- package/lib/utils/type/isRegExp.js +0 -29
- package/lib/utils/type/isSet.js +0 -29
- package/lib/utils/type/isWeakMap.js +0 -29
- package/lib/utils/type/isWeakSet.js +0 -29
- package/types/interface.type.d.ts +0 -2
package/lib/fileReader.js
CHANGED
|
@@ -1,74 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
3
|
var FileReaderMethodMap = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
arrayBuffer: 'readAsArrayBuffer',
|
|
5
|
+
binaryString: 'readAsBinaryString',
|
|
6
|
+
dataURL: 'readAsDataURL',
|
|
7
|
+
text: 'readAsText'
|
|
12
8
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* @returns {Promise<string>}
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 读取 Blob 或 File 对象,转为 Base64/String/ArrayBuffer<br/><br/>
|
|
30
|
-
*
|
|
31
|
-
* <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
|
|
32
|
-
*
|
|
33
|
-
* @static
|
|
34
|
-
* @alias module:Processor.fileReader
|
|
35
|
-
* @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader|FileReader}
|
|
36
|
-
* @since 4.16.0
|
|
37
|
-
* @param {Blob} blob Blob 或 File 对象
|
|
38
|
-
* @param {'arrayBuffer'|'binaryString'|'dataURL'|'text'} [type='dataURL'] Blob 或 File 对象
|
|
39
|
-
* @returns {Promise<string|ArrayBuffer>} 文件的内容
|
|
40
|
-
* @example
|
|
41
|
-
* const aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // 一个包含DOMString的数组
|
|
42
|
-
* const htmlBlob = new Blob(aFileParts, { type: 'text/html' }); // 得到 blob
|
|
43
|
-
*
|
|
44
|
-
* fileReader(htmlBlob).then(data=>{
|
|
45
|
-
* console.log(data); // data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
|
|
46
|
-
* });
|
|
47
|
-
*
|
|
48
|
-
* const textBlob = new Blob(aFileParts, { type: 'text/plain' });
|
|
49
|
-
*
|
|
50
|
-
* fileReader(textBlob, 'text').then(data=>{
|
|
51
|
-
* console.log(data); // <a id="a"><b id="b">hey!</b></a>
|
|
52
|
-
* });
|
|
53
|
-
*/
|
|
54
|
-
function fileReader(blob) {
|
|
55
|
-
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'dataURL';
|
|
56
|
-
var method = FileReaderMethodMap[type];
|
|
57
|
-
if (!method) {
|
|
58
|
-
method = FileReaderMethodMap.dataURL;
|
|
59
|
-
}
|
|
60
|
-
return new Promise(function (resolve, reject) {
|
|
61
|
-
var reader = new FileReader();
|
|
62
|
-
// @ts-ignore
|
|
63
|
-
reader[method](blob);
|
|
64
|
-
// @ts-ignore
|
|
65
|
-
reader.onload = function () {
|
|
66
|
-
return resolve(reader.result);
|
|
67
|
-
};
|
|
68
|
-
reader.onerror = function (error) {
|
|
69
|
-
return reject(error);
|
|
70
|
-
};
|
|
71
|
-
});
|
|
9
|
+
function fileReader(blob, type) {
|
|
10
|
+
if (type === void 0) { type = 'dataURL'; }
|
|
11
|
+
var method = FileReaderMethodMap[type];
|
|
12
|
+
if (!method) {
|
|
13
|
+
method = FileReaderMethodMap.dataURL;
|
|
14
|
+
}
|
|
15
|
+
return new Promise(function (resolve, reject) {
|
|
16
|
+
var reader = new FileReader();
|
|
17
|
+
reader[method](blob);
|
|
18
|
+
reader.onload = function () { return resolve(reader.result); };
|
|
19
|
+
reader.onerror = function (error) { return reject(error); };
|
|
20
|
+
});
|
|
72
21
|
}
|
|
73
|
-
|
|
74
|
-
exports
|
|
22
|
+
|
|
23
|
+
module.exports = fileReader;
|
package/lib/filterTree.js
CHANGED
|
@@ -1,70 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
13
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
14
|
-
/**
|
|
15
|
-
* 过滤/筛选树节点。<br/><br/>如果某节点被过滤掉,它的子节点也一并抛弃
|
|
16
|
-
*
|
|
17
|
-
* @static
|
|
18
|
-
* @alias module:Tree.filterTree
|
|
19
|
-
* @since 4.15.0
|
|
20
|
-
* @template {any} T
|
|
21
|
-
* @template {(item: T) => boolean} F
|
|
22
|
-
* @param {T[]} tree 树结构数据
|
|
23
|
-
* @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,结果将包含该节点
|
|
24
|
-
* @param {string} [childrenField='children'] 子级字段名
|
|
25
|
-
* @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
|
|
26
|
-
* @returns {T[]}
|
|
27
|
-
* @example
|
|
28
|
-
* const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
|
|
29
|
-
*
|
|
30
|
-
* filterTree(menus, item=>item.name.indexOf('管理') > -1);
|
|
31
|
-
* // [{"id":"2","name":"交易管理","code":"trade","pid":null,"children":[]},{"id":"5","name":"权限管理","code":"authorization","pid":null,"children":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]}]
|
|
32
|
-
*
|
|
33
|
-
* // 如果某节点被过滤掉,它的子节点也一并抛弃
|
|
34
|
-
* filterTree(menus, item=>item.id === '7');
|
|
35
|
-
* // []
|
|
36
|
-
*
|
|
37
|
-
* filterTree(menus, item=>item.id === 'not found');
|
|
38
|
-
* // []
|
|
39
|
-
*/
|
|
40
|
-
function filterTree(tree, predicate) {
|
|
41
|
-
var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
|
|
42
|
-
var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
|
|
43
|
-
/** @type {T[]} */
|
|
44
|
-
var result = [];
|
|
45
|
-
if (!Array.isArray(tree)) {
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
tree.forEach(function (item) {
|
|
49
|
-
var newItem = item;
|
|
50
|
-
if ((0, _type.isObject)(item)) {
|
|
51
|
-
// @ts-ignore
|
|
52
|
-
newItem = nodeAssign === 'spread' ? _objectSpread({}, item) : item;
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var isObject = require('./utils/type/isObject.js');
|
|
5
|
+
|
|
6
|
+
function filterTree(tree, predicate, childrenField, nodeAssign) {
|
|
7
|
+
if (childrenField === void 0) { childrenField = 'children'; }
|
|
8
|
+
if (nodeAssign === void 0) { nodeAssign = 'spread'; }
|
|
9
|
+
var result = [];
|
|
10
|
+
if (!Array.isArray(tree)) {
|
|
11
|
+
return result;
|
|
53
12
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
var childs = newItem[childrenField];
|
|
59
|
-
if (Array.isArray(childs) && childs.length > 0) {
|
|
60
|
-
// @ts-ignore
|
|
61
|
-
newItem[childrenField] = filterTree(childs, predicate, childrenField, nodeAssign);
|
|
13
|
+
tree.forEach(function (item) {
|
|
14
|
+
var newItem = item;
|
|
15
|
+
if (isObject(item)) {
|
|
16
|
+
newItem = nodeAssign === 'spread' ? tslib.__assign({}, item) : item;
|
|
62
17
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
18
|
+
if (predicate(newItem)) {
|
|
19
|
+
if (isObject(newItem)) {
|
|
20
|
+
var childs = newItem[childrenField];
|
|
21
|
+
if (Array.isArray(childs) && childs.length > 0) {
|
|
22
|
+
newItem[childrenField] = filterTree(childs, predicate, childrenField, nodeAssign);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
result.push(newItem);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return result;
|
|
68
29
|
}
|
|
69
|
-
|
|
70
|
-
exports
|
|
30
|
+
|
|
31
|
+
module.exports = filterTree;
|
package/lib/findTreeNode.js
CHANGED
|
@@ -1,83 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var _type = require("./utils/type");
|
|
8
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
9
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
11
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
12
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
13
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
14
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
15
|
-
/**
|
|
16
|
-
* 查找树结构数据节点
|
|
17
|
-
*
|
|
18
|
-
* @static
|
|
19
|
-
* @alias module:Tree.findTreeNode
|
|
20
|
-
* @since 4.14.0
|
|
21
|
-
* @template {any} T
|
|
22
|
-
* @template {(item: T) => boolean} F
|
|
23
|
-
* @param {T[]} tree 树结构数据
|
|
24
|
-
* @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,将返回该节点
|
|
25
|
-
* @param {string} [childrenField='children'] 子级字段名
|
|
26
|
-
* @returns {T|undefined}
|
|
27
|
-
* @example
|
|
28
|
-
* const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
|
|
29
|
-
*
|
|
30
|
-
* findTreeNode(menus, item=>item.id === '2');
|
|
31
|
-
* // {"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]}
|
|
32
|
-
*
|
|
33
|
-
* findTreeNode(menus, item=>item.id === '7');
|
|
34
|
-
* // {"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}
|
|
35
|
-
*
|
|
36
|
-
* findTreeNode(menus, item=>item.id === 'not found');
|
|
37
|
-
* // undefined
|
|
38
|
-
*/
|
|
39
|
-
function findTreeNode(tree, predicate) {
|
|
40
|
-
var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
|
|
41
|
-
var stack = [];
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var isObject = require('./utils/type/isObject.js');
|
|
42
5
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
6
|
+
function findTreeNode(tree, predicate, childrenField) {
|
|
7
|
+
var e_1, _a;
|
|
8
|
+
if (childrenField === void 0) { childrenField = 'children'; }
|
|
9
|
+
var stack = [];
|
|
10
|
+
var node;
|
|
11
|
+
if (!Array.isArray(tree)) {
|
|
12
|
+
return node;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
for (var tree_1 = tslib.__values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
|
|
16
|
+
var item = tree_1_1.value;
|
|
17
|
+
stack.push(item);
|
|
18
|
+
while (stack.length) {
|
|
19
|
+
var temp = stack.pop();
|
|
20
|
+
if (predicate(temp)) {
|
|
21
|
+
node = temp;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
if (isObject(temp)) {
|
|
25
|
+
var childs = temp[childrenField];
|
|
26
|
+
if (Array.isArray(childs) && childs.length > 0) {
|
|
27
|
+
stack.push.apply(stack, tslib.__spreadArray([], tslib.__read(childs), false));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (node) {
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
61
34
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
stack.push.apply(stack, _toConsumableArray(childs));
|
|
68
|
-
}
|
|
35
|
+
}
|
|
36
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
37
|
+
finally {
|
|
38
|
+
try {
|
|
39
|
+
if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
|
|
69
40
|
}
|
|
70
|
-
|
|
71
|
-
if (node) {
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
41
|
+
finally { if (e_1) throw e_1.error; }
|
|
74
42
|
}
|
|
75
|
-
|
|
76
|
-
_iterator.e(err);
|
|
77
|
-
} finally {
|
|
78
|
-
_iterator.f();
|
|
79
|
-
}
|
|
80
|
-
return node;
|
|
43
|
+
return node;
|
|
81
44
|
}
|
|
82
|
-
|
|
83
|
-
exports
|
|
45
|
+
|
|
46
|
+
module.exports = findTreeNode;
|
package/lib/findTreeNodes.js
CHANGED
|
@@ -1,82 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var _type = require("./utils/type");
|
|
8
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
9
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
11
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
12
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
13
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
14
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
15
|
-
/**
|
|
16
|
-
* 查找树结构数据多个节点
|
|
17
|
-
*
|
|
18
|
-
* @static
|
|
19
|
-
* @alias module:Tree.findTreeNodes
|
|
20
|
-
* @since 4.15.0
|
|
21
|
-
* @template {any} T
|
|
22
|
-
* @template {(item: T) => boolean} F
|
|
23
|
-
* @param {T[]} tree 树结构数据
|
|
24
|
-
* @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,返回结果将包含该节点
|
|
25
|
-
* @param {string} [childrenField='children'] 子级字段名
|
|
26
|
-
* @returns {T[]}
|
|
27
|
-
* @example
|
|
28
|
-
* const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
|
|
29
|
-
*
|
|
30
|
-
* findTreeNodes(menus, item=>item.id === '2');
|
|
31
|
-
* // [{"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]}]
|
|
32
|
-
*
|
|
33
|
-
* findTreeNodes(menus, item=>item.name.indexOf('管理') > -1);
|
|
34
|
-
* // [{"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]},{"id":"5","name":"权限管理","code":"authorization","pid":null,"children":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"},{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"}]
|
|
35
|
-
*
|
|
36
|
-
* findTreeNodes(menus, item=>item.id === '1' || item.id === '7');
|
|
37
|
-
* // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
|
|
38
|
-
*
|
|
39
|
-
* findTreeNodes(menus, item=>item.id === 'not found');
|
|
40
|
-
* // []
|
|
41
|
-
*/
|
|
42
|
-
function findTreeNodes(tree, predicate) {
|
|
43
|
-
var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
|
|
44
|
-
var stack = [];
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var isObject = require('./utils/type/isObject.js');
|
|
45
5
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
6
|
+
function findTreeNodes(tree, predicate, childrenField) {
|
|
7
|
+
var e_1, _a;
|
|
8
|
+
if (childrenField === void 0) { childrenField = 'children'; }
|
|
9
|
+
var stack = [];
|
|
10
|
+
var nodes = [];
|
|
11
|
+
if (!Array.isArray(tree)) {
|
|
12
|
+
return nodes;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
for (var tree_1 = tslib.__values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
|
|
16
|
+
var item = tree_1_1.value;
|
|
17
|
+
stack.push(item);
|
|
18
|
+
while (stack.length) {
|
|
19
|
+
var temp = stack.pop();
|
|
20
|
+
if (predicate(temp)) {
|
|
21
|
+
nodes.push(temp);
|
|
22
|
+
}
|
|
23
|
+
if (isObject(temp)) {
|
|
24
|
+
var childs = temp[childrenField];
|
|
25
|
+
if (Array.isArray(childs) && childs.length > 0) {
|
|
26
|
+
stack.push.apply(stack, tslib.__spreadArray([], tslib.__read(childs), false));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
63
30
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
stack.push.apply(stack, _toConsumableArray(childs));
|
|
70
|
-
}
|
|
31
|
+
}
|
|
32
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
33
|
+
finally {
|
|
34
|
+
try {
|
|
35
|
+
if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
|
|
71
36
|
}
|
|
72
|
-
|
|
37
|
+
finally { if (e_1) throw e_1.error; }
|
|
73
38
|
}
|
|
74
|
-
|
|
75
|
-
_iterator.e(err);
|
|
76
|
-
} finally {
|
|
77
|
-
_iterator.f();
|
|
78
|
-
}
|
|
79
|
-
return nodes;
|
|
39
|
+
return nodes;
|
|
80
40
|
}
|
|
81
|
-
|
|
82
|
-
exports
|
|
41
|
+
|
|
42
|
+
module.exports = findTreeNodes;
|
package/lib/findTreeSelect.js
CHANGED
|
@@ -1,87 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return path;
|
|
36
|
-
}
|
|
37
|
-
if ((0, _type.isObject)(item)) {
|
|
38
|
-
/** @type {T[]} */
|
|
39
|
-
// @ts-ignore
|
|
40
|
-
var childs = item[childrenField];
|
|
41
|
-
if (Array.isArray(childs) && childs.length > 0) {
|
|
42
|
-
var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
|
|
43
|
-
if (findChildren.length > 0) {
|
|
44
|
-
return findChildren;
|
|
45
|
-
}
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var isObject = require('./utils/type/isObject.js');
|
|
5
|
+
|
|
6
|
+
function internalFindTreeSelect(tree, predicate, childrenField, path) {
|
|
7
|
+
var e_1, _a;
|
|
8
|
+
if (path === void 0) { path = []; }
|
|
9
|
+
if (!Array.isArray(tree)) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
for (var tree_1 = tslib.__values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
|
|
14
|
+
var item = tree_1_1.value;
|
|
15
|
+
path.push(item);
|
|
16
|
+
if (predicate(item)) {
|
|
17
|
+
return path;
|
|
18
|
+
}
|
|
19
|
+
if (isObject(item)) {
|
|
20
|
+
var childs = item[childrenField];
|
|
21
|
+
if (Array.isArray(childs) && childs.length > 0) {
|
|
22
|
+
var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
|
|
23
|
+
if (findChildren.length > 0) {
|
|
24
|
+
return findChildren;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
path.pop();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
32
|
+
finally {
|
|
33
|
+
try {
|
|
34
|
+
if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
|
|
46
35
|
}
|
|
47
|
-
|
|
48
|
-
path.pop();
|
|
36
|
+
finally { if (e_1) throw e_1.error; }
|
|
49
37
|
}
|
|
50
|
-
|
|
51
|
-
_iterator.e(err);
|
|
52
|
-
} finally {
|
|
53
|
-
_iterator.f();
|
|
54
|
-
}
|
|
55
|
-
return [];
|
|
38
|
+
return [];
|
|
56
39
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
*
|
|
61
|
-
* @static
|
|
62
|
-
* @alias module:Tree.findTreeSelect
|
|
63
|
-
* @since 4.14.0
|
|
64
|
-
* @template {any} T
|
|
65
|
-
* @template {(item: T) => boolean} F
|
|
66
|
-
* @param {T[]} tree 树结构数据
|
|
67
|
-
* @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
|
|
68
|
-
* @param {string} [childrenField='children'] 子级字段名
|
|
69
|
-
* @returns {T[]}
|
|
70
|
-
* @example
|
|
71
|
-
* const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
|
|
72
|
-
*
|
|
73
|
-
* findTreeSelect(menus, item => item.id === '2');
|
|
74
|
-
* // [{"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]}]
|
|
75
|
-
*
|
|
76
|
-
* findTreeSelect(menus, item => item.id === '7');
|
|
77
|
-
* // [{"id":"5","name":"权限管理","code":"authorization","pid":null,"children":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
|
|
78
|
-
*
|
|
79
|
-
* findTreeSelect(menus, item => item.id === 'not found');
|
|
80
|
-
* // []
|
|
81
|
-
*/
|
|
82
|
-
function findTreeSelect(tree, predicate) {
|
|
83
|
-
var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
|
|
84
|
-
return internalFindTreeSelect(tree, predicate, childrenField);
|
|
40
|
+
function findTreeSelect(tree, predicate, childrenField) {
|
|
41
|
+
if (childrenField === void 0) { childrenField = 'children'; }
|
|
42
|
+
return internalFindTreeSelect(tree, predicate, childrenField);
|
|
85
43
|
}
|
|
86
|
-
|
|
87
|
-
exports
|
|
44
|
+
|
|
45
|
+
module.exports = findTreeSelect;
|