util-helpers 4.14.1 → 4.15.1

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.
@@ -537,7 +537,7 @@
537
537
  }
538
538
 
539
539
  // eslint-disable-next-line no-undef
540
- var version = "4.14.1";
540
+ var version = "4.15.1";
541
541
 
542
542
  /**
543
543
  * 打印警告信息
@@ -2441,24 +2441,21 @@
2441
2441
  *
2442
2442
  */
2443
2443
  function parseIdCard(id) {
2444
- if (!regIdCard.test(id)) {
2444
+ var match = regIdCard.exec(id);
2445
+ if (!match) {
2445
2446
  return null;
2446
2447
  }
2447
2448
 
2448
- /** @type {RegExpExecArray} */
2449
- // @ts-ignore
2450
- var info = regIdCard.exec(id);
2451
-
2452
2449
  /** @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }} */
2453
2450
  // @ts-ignore
2454
- var origin = (info === null || info === void 0 ? void 0 : info.groups) || {
2455
- province: info[1],
2456
- city: info[2],
2457
- area: info[3],
2458
- year: info[4],
2459
- month: info[5],
2460
- day: info[6],
2461
- gender: info[7]
2451
+ var origin = match.groups || {
2452
+ province: match[1],
2453
+ city: match[2],
2454
+ area: match[3],
2455
+ year: match[4],
2456
+ month: match[5],
2457
+ day: match[6],
2458
+ gender: match[7]
2462
2459
  };
2463
2460
  var province = Provinces.find(function (item) {
2464
2461
  return item[0] === origin.province;
@@ -2704,9 +2701,9 @@
2704
2701
  * @template {*} D
2705
2702
  * @template {Record<string, keyof D>} F
2706
2703
  * @template {string} C
2707
- * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
2704
+ * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
2708
2705
  * @param {F} fieldNames 字段名映射
2709
- * @param {C} [childrenFieldName] 子级数据字段名
2706
+ * @param {C} [childrenField] 子级数据字段名
2710
2707
  * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
2711
2708
  * @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
2712
2709
  * @example
@@ -2728,7 +2725,7 @@
2728
2725
  * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
2729
2726
  * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
2730
2727
  */
2731
- function transformFieldNames(data, fieldNames, childrenFieldName) {
2728
+ function transformFieldNames(data, fieldNames, childrenField) {
2732
2729
  var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
2733
2730
  if (!Array.isArray(data)) {
2734
2731
  return data;
@@ -2744,8 +2741,7 @@
2744
2741
  * @param {Array.<object>} arr 列表数据
2745
2742
  * @returns {*}
2746
2743
  */
2747
- function recusion() {
2748
- var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2744
+ function recusion(arr) {
2749
2745
  return arr.map(function (item) {
2750
2746
  if (!isObject(item)) {
2751
2747
  return item;
@@ -2756,9 +2752,9 @@
2756
2752
 
2757
2753
  // 树形数据子节点
2758
2754
  // @ts-ignore
2759
- if (childrenFieldName && Array.isArray(newItem[childrenFieldName]) && newItem[childrenFieldName].length > 0) {
2755
+ if (childrenField && Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
2760
2756
  // @ts-ignore
2761
- newItem[childrenFieldName] = recusion(newItem[childrenFieldName].slice());
2757
+ newItem[childrenField] = recusion(newItem[childrenField].slice());
2762
2758
  }
2763
2759
 
2764
2760
  // 替换字段名
@@ -2793,29 +2789,30 @@
2793
2789
  * @private
2794
2790
  * @template {Record<string,any>} [T=Record<string,any>]
2795
2791
  * @param {T[]} arr 列表数据
2796
- * @param {object} options 配置项
2792
+ * @param {object} [options] 配置项
2797
2793
  * @param {string} [options.childrenField='children'] 子级字段名称
2798
- * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
2799
- */
2800
- function processEmptyChildren(arr) {
2801
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2802
- var _options$childrenFiel = options.childrenField,
2803
- childrenField = _options$childrenFiel === void 0 ? 'children' : _options$childrenFiel,
2804
- _options$emptyChildre = options.emptyChildrenValue,
2805
- emptyChildrenValue = _options$emptyChildre === void 0 ? 'none' : _options$emptyChildre;
2794
+ * @param {'none'|'null'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
2795
+ */
2796
+ function processEmptyChildren(arr, options) {
2797
+ var _ref = options || {},
2798
+ _ref$childrenField = _ref.childrenField,
2799
+ childrenField = _ref$childrenField === void 0 ? 'children' : _ref$childrenField,
2800
+ _ref$emptyChildrenVal = _ref.emptyChildrenValue,
2801
+ emptyChildrenValue = _ref$emptyChildrenVal === void 0 ? 'none' : _ref$emptyChildrenVal;
2806
2802
  arr.forEach(function (item) {
2807
- if (isObject(item) && Array.isArray(item[childrenField])) {
2808
- if (item[childrenField].length <= 0) {
2809
- if (emptyChildrenValue === 'null') {
2810
- // @ts-ignore
2811
- item[childrenField] = null;
2812
- } else if (emptyChildrenValue === 'none') {
2813
- delete item[childrenField];
2814
- }
2803
+ // if (isObject(item) && Array.isArray(item[childrenField])) {
2804
+ if (item[childrenField].length <= 0) {
2805
+ if (emptyChildrenValue === 'null') {
2806
+ // @ts-ignore
2807
+ item[childrenField] = null;
2808
+ // } else if (emptyChildrenValue === 'none') { // emptyChildrenValue='array' 不会执行该内部方法
2815
2809
  } else {
2816
- processEmptyChildren(item[childrenField], options);
2810
+ delete item[childrenField];
2817
2811
  }
2812
+ } else {
2813
+ processEmptyChildren(item[childrenField], options);
2818
2814
  }
2815
+ // }
2819
2816
  });
2820
2817
  }
2821
2818
 
@@ -2828,7 +2825,7 @@
2828
2825
  * @template {Record<string,any>} [T=Record<string,any>]
2829
2826
  * @template {*} [R=T&Record<string,any>]
2830
2827
  * @param {T[]} list 列表数据
2831
- * @param {object} options 配置项
2828
+ * @param {object} [options] 配置项
2832
2829
  * @param {string} [options.keyField='id'] 当前数据的键值字段名称
2833
2830
  * @param {string} [options.parentField='pid'] 当前数据的父级字段名称
2834
2831
  * @param {string} [options.childrenField='children'] 子级字段名称
@@ -2854,24 +2851,27 @@
2854
2851
  * // [{"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"}]}]
2855
2852
  *
2856
2853
  */
2857
- function listToTree(list) {
2858
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2859
- var _options$keyField = options.keyField,
2860
- keyField = _options$keyField === void 0 ? 'id' : _options$keyField,
2861
- _options$parentField = options.parentField,
2862
- parentField = _options$parentField === void 0 ? 'pid' : _options$parentField,
2863
- _options$childrenFiel2 = options.childrenField,
2864
- childrenField = _options$childrenFiel2 === void 0 ? 'children' : _options$childrenFiel2,
2865
- _options$emptyChildre2 = options.emptyChildrenValue,
2866
- emptyChildrenValue = _options$emptyChildre2 === void 0 ? 'none' : _options$emptyChildre2,
2867
- _options$nodeAssign = options.nodeAssign,
2868
- nodeAssign = _options$nodeAssign === void 0 ? 'spread' : _options$nodeAssign;
2854
+ function listToTree(list, options) {
2855
+ var _ref2 = options || {},
2856
+ _ref2$keyField = _ref2.keyField,
2857
+ keyField = _ref2$keyField === void 0 ? 'id' : _ref2$keyField,
2858
+ _ref2$parentField = _ref2.parentField,
2859
+ parentField = _ref2$parentField === void 0 ? 'pid' : _ref2$parentField,
2860
+ _ref2$childrenField = _ref2.childrenField,
2861
+ childrenField = _ref2$childrenField === void 0 ? 'children' : _ref2$childrenField,
2862
+ _ref2$emptyChildrenVa = _ref2.emptyChildrenValue,
2863
+ emptyChildrenValue = _ref2$emptyChildrenVa === void 0 ? 'none' : _ref2$emptyChildrenVa,
2864
+ _ref2$nodeAssign = _ref2.nodeAssign,
2865
+ nodeAssign = _ref2$nodeAssign === void 0 ? 'spread' : _ref2$nodeAssign;
2869
2866
 
2870
2867
  /** @type {R[]} */
2871
2868
  var tree = [];
2872
2869
 
2873
2870
  /** @type {Object.<string, T[]>} */
2874
2871
  var record = {};
2872
+ if (!Array.isArray(list)) {
2873
+ return tree;
2874
+ }
2875
2875
  list.forEach(function (item) {
2876
2876
  if (isObject(item)) {
2877
2877
  var newItem = nodeAssign === 'spread' ? _objectSpread2({}, item) : item;
@@ -2928,6 +2928,9 @@
2928
2928
  function treeToList(tree, childrenField) {
2929
2929
  /** @type {R[]} */
2930
2930
  var list = [];
2931
+ if (!Array.isArray(tree)) {
2932
+ return list;
2933
+ }
2931
2934
 
2932
2935
  /**
2933
2936
  * 递归遍历
@@ -2939,7 +2942,7 @@
2939
2942
  var newItem = _objectSpread2({}, item);
2940
2943
  // @ts-ignore
2941
2944
  list.push(newItem);
2942
- if (newItem !== null && newItem !== void 0 && newItem[childrenField]) {
2945
+ if (newItem[childrenField]) {
2943
2946
  if (Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
2944
2947
  recusion(newItem[childrenField]);
2945
2948
  }
@@ -2956,140 +2959,59 @@
2956
2959
  }
2957
2960
 
2958
2961
  /**
2959
- * 查找树结构数据节点
2962
+ * 过滤/筛选树节点。<br/><br/>如果某节点被过滤掉,它的子节点也一并抛弃
2960
2963
  *
2961
2964
  * @static
2962
- * @alias module:Other.findTreeNode
2963
- * @since 4.14.0
2965
+ * @alias module:Processor.filterTree
2966
+ * @since 4.15.0
2964
2967
  * @template {any} T
2965
2968
  * @template {(item: T) => boolean} F
2966
2969
  * @param {T[]} tree 树结构数据
2967
- * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回该节点
2970
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,结果将包含该节点
2968
2971
  * @param {string} [childrenField='children'] 子级字段名
2969
- * @returns {T|undefined}
2972
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
2973
+ * @returns {T[]}
2970
2974
  * @example
2971
2975
  * 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" }] }];
2972
2976
  *
2973
- * findTreeNode(menus, item=>item.id === '2');
2974
- * // {"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"}]}]}
2975
- *
2976
- * findTreeNode(menus, item=>item.id === '7');
2977
- * // {"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}
2977
+ * filterTree(menus, item=>item.name.indexOf('管理') > -1);
2978
+ * // [{"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"}]}]
2978
2979
  *
2979
- * findTreeNode(menus, item=>item.id === 'not found');
2980
- * // undefined
2981
- */
2982
- function findTreeNode(tree, predicate) {
2983
- var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
2984
- var stack = [];
2985
-
2986
- /** @type {T|undefined} */
2987
- var node;
2988
- var _iterator = _createForOfIteratorHelper(tree),
2989
- _step;
2990
- try {
2991
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
2992
- var item = _step.value;
2993
- stack.push(item);
2994
- while (stack.length) {
2995
- /** @type {T} */
2996
- // @ts-ignore
2997
- var temp = stack.pop();
2998
- if (predicate(temp)) {
2999
- node = temp;
3000
- break;
3001
- }
3002
-
3003
- /** @type {T[]} */
3004
- // @ts-ignore
3005
- var children = temp[childrenField] || [];
3006
- stack.push.apply(stack, _toConsumableArray(children));
3007
- }
3008
- if (node) {
3009
- break;
3010
- }
3011
- }
3012
- } catch (err) {
3013
- _iterator.e(err);
3014
- } finally {
3015
- _iterator.f();
3016
- }
3017
- return node;
3018
- }
3019
-
3020
- /**
3021
- * 内部实现
2980
+ * // 如果某节点被过滤掉,它的子节点也一并抛弃
2981
+ * filterTree(menus, item=>item.id === '7');
2982
+ * // []
3022
2983
  *
3023
- * @private
3024
- * @template {any} T
3025
- * @template {(item: T) => boolean} F
3026
- * @param {T[]} tree 树结构数据
3027
- * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
3028
- * @param {string} [childrenField='children'] 子级字段名
3029
- * @param {T[]} [path=[]] 当前遍历路径
3030
- * @returns {T[]}
2984
+ * filterTree(menus, item=>item.id === 'not found');
2985
+ * // []
3031
2986
  */
3032
- function internalFindTreeSelect(tree, predicate) {
2987
+ function filterTree(tree, predicate) {
3033
2988
  var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
3034
- var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
3035
- if (!tree) {
3036
- return [];
2989
+ var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
2990
+ /** @type {T[]} */
2991
+ var result = [];
2992
+ if (!Array.isArray(tree)) {
2993
+ return result;
3037
2994
  }
3038
- var _iterator = _createForOfIteratorHelper(tree),
3039
- _step;
3040
- try {
3041
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
3042
- var item = _step.value;
3043
- path.push(item);
3044
- if (predicate(item)) {
3045
- return path;
3046
- }
3047
-
2995
+ tree.forEach(function (item) {
2996
+ var newItem = item;
2997
+ if (isObject(item)) {
3048
2998
  // @ts-ignore
3049
- if (isObject(item) && Array.isArray(item[childrenField]) && item[childrenField].length > 0) {
2999
+ newItem = nodeAssign === 'spread' ? _objectSpread2({}, item) : item;
3000
+ }
3001
+ if (predicate(newItem)) {
3002
+ if (isObject(newItem)) {
3003
+ /** @type {T[]|undefined} */
3050
3004
  // @ts-ignore
3051
- var findChildren = internalFindTreeSelect(item[childrenField], predicate, childrenField, path);
3052
- if (findChildren.length > 0) {
3053
- return findChildren;
3005
+ var childs = newItem[childrenField];
3006
+ if (Array.isArray(childs) && childs.length > 0) {
3007
+ // @ts-ignore
3008
+ newItem[childrenField] = filterTree(childs, predicate, childrenField, nodeAssign);
3054
3009
  }
3055
3010
  }
3056
- path.pop();
3011
+ result.push(newItem);
3057
3012
  }
3058
- } catch (err) {
3059
- _iterator.e(err);
3060
- } finally {
3061
- _iterator.f();
3062
- }
3063
- return [];
3064
- }
3065
-
3066
- /**
3067
- * 查找包含当前节点的所有父级节点
3068
- *
3069
- * @static
3070
- * @alias module:Other.findTreeSelect
3071
- * @since 4.14.0
3072
- * @template {any} T
3073
- * @template {(item: T) => boolean} F
3074
- * @param {T[]} tree 树结构数据
3075
- * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
3076
- * @param {string} [childrenField='children'] 子级字段名
3077
- * @returns {T[]}
3078
- * @example
3079
- * 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" }] }];
3080
- *
3081
- * findTreeSelect(menus, item => item.id === '2');
3082
- * // [{"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"}]}]}]
3083
- *
3084
- * findTreeSelect(menus, item => item.id === '7');
3085
- * // [{"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"}]
3086
- *
3087
- * findTreeSelect(menus, item => item.id === 'not found');
3088
- * // []
3089
- */
3090
- function findTreeSelect(tree, predicate) {
3091
- var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
3092
- return internalFindTreeSelect(tree, predicate, childrenField);
3013
+ });
3014
+ return result;
3093
3015
  }
3094
3016
 
3095
3017
  /**
@@ -3313,7 +3235,7 @@
3313
3235
  });
3314
3236
  }
3315
3237
 
3316
- // 参考了: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
3238
+ // ref: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
3317
3239
 
3318
3240
  /**
3319
3241
  * 计算输入框的值格式化后光标位置
@@ -3321,8 +3243,6 @@
3321
3243
  * @static
3322
3244
  * @alias module:Other.calculateCursorPosition
3323
3245
  * @since 4.6.0
3324
- * @see 格式化手机号码 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile|formatMobile}
3325
- * @see 格式化银行卡号 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard|formatBankCard}
3326
3246
  * @see h5示例 {@link https://2950v9.csb.app/|点击查看}
3327
3247
  * @see react示例 {@link https://33ccy9.csb.app/|点击查看}
3328
3248
  * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
@@ -3374,14 +3294,12 @@
3374
3294
 
3375
3295
  /**
3376
3296
  * @private
3377
- * @param {number} [len=0] 长度
3378
- * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
3297
+ * @param {number} len 长度
3298
+ * @param {string} optionalChars 允许的字符,默认为数字和大小写字母
3379
3299
  * @param {string} [prefix=''] 前缀部分,不计入长度
3380
3300
  * @returns {string}
3381
3301
  */
3382
- function internalRandomString() {
3383
- var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
3384
- var optionalChars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultChars;
3302
+ function internalRandomString(len, optionalChars) {
3385
3303
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
3386
3304
  while (len-- > 0) {
3387
3305
  var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
@@ -3448,12 +3366,225 @@
3448
3366
  return len;
3449
3367
  }
3450
3368
 
3369
+ /**
3370
+ * 查找树结构数据节点
3371
+ *
3372
+ * @static
3373
+ * @alias module:Other.findTreeNode
3374
+ * @since 4.14.0
3375
+ * @template {any} T
3376
+ * @template {(item: T) => boolean} F
3377
+ * @param {T[]} tree 树结构数据
3378
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,将返回该节点
3379
+ * @param {string} [childrenField='children'] 子级字段名
3380
+ * @returns {T|undefined}
3381
+ * @example
3382
+ * 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" }] }];
3383
+ *
3384
+ * findTreeNode(menus, item=>item.id === '2');
3385
+ * // {"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"}]}]}
3386
+ *
3387
+ * findTreeNode(menus, item=>item.id === '7');
3388
+ * // {"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}
3389
+ *
3390
+ * findTreeNode(menus, item=>item.id === 'not found');
3391
+ * // undefined
3392
+ */
3393
+ function findTreeNode(tree, predicate) {
3394
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
3395
+ var stack = [];
3396
+
3397
+ /** @type {T|undefined} */
3398
+ var node;
3399
+ if (!Array.isArray(tree)) {
3400
+ return node;
3401
+ }
3402
+ var _iterator = _createForOfIteratorHelper(tree),
3403
+ _step;
3404
+ try {
3405
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
3406
+ var item = _step.value;
3407
+ stack.push(item);
3408
+ while (stack.length) {
3409
+ /** @type {T} */
3410
+ // @ts-ignore
3411
+ var temp = stack.pop();
3412
+ if (predicate(temp)) {
3413
+ node = temp;
3414
+ break;
3415
+ }
3416
+ if (isObject(temp)) {
3417
+ /** @type {T[]} */
3418
+ // @ts-ignore
3419
+ var childs = temp[childrenField];
3420
+ if (Array.isArray(childs) && childs.length > 0) {
3421
+ stack.push.apply(stack, _toConsumableArray(childs));
3422
+ }
3423
+ }
3424
+ }
3425
+ if (node) {
3426
+ break;
3427
+ }
3428
+ }
3429
+ } catch (err) {
3430
+ _iterator.e(err);
3431
+ } finally {
3432
+ _iterator.f();
3433
+ }
3434
+ return node;
3435
+ }
3436
+
3437
+ /**
3438
+ * 查找树结构数据多个节点
3439
+ *
3440
+ * @static
3441
+ * @alias module:Other.findTreeNodes
3442
+ * @since 4.15.0
3443
+ * @template {any} T
3444
+ * @template {(item: T) => boolean} F
3445
+ * @param {T[]} tree 树结构数据
3446
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,返回结果将包含该节点
3447
+ * @param {string} [childrenField='children'] 子级字段名
3448
+ * @returns {T[]}
3449
+ * @example
3450
+ * 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" }] }];
3451
+ *
3452
+ * findTreeNodes(menus, item=>item.id === '2');
3453
+ * // [{"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"}]}]}]
3454
+ *
3455
+ * findTreeNodes(menus, item=>item.name.indexOf('管理') > -1);
3456
+ * // [{"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"}]
3457
+ *
3458
+ * findTreeNodes(menus, item=>item.id === '1' || item.id === '7');
3459
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
3460
+ *
3461
+ * findTreeNodes(menus, item=>item.id === 'not found');
3462
+ * // []
3463
+ */
3464
+ function findTreeNodes(tree, predicate) {
3465
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
3466
+ var stack = [];
3467
+
3468
+ /** @type {T[]} */
3469
+ var nodes = [];
3470
+ if (!Array.isArray(tree)) {
3471
+ return nodes;
3472
+ }
3473
+ var _iterator = _createForOfIteratorHelper(tree),
3474
+ _step;
3475
+ try {
3476
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
3477
+ var item = _step.value;
3478
+ stack.push(item);
3479
+ while (stack.length) {
3480
+ /** @type {T} */
3481
+ // @ts-ignore
3482
+ var temp = stack.pop();
3483
+ if (predicate(temp)) {
3484
+ nodes.push(temp);
3485
+ }
3486
+ if (isObject(temp)) {
3487
+ /** @type {T[]} */
3488
+ // @ts-ignore
3489
+ var childs = temp[childrenField];
3490
+ if (Array.isArray(childs) && childs.length > 0) {
3491
+ stack.push.apply(stack, _toConsumableArray(childs));
3492
+ }
3493
+ }
3494
+ }
3495
+ }
3496
+ } catch (err) {
3497
+ _iterator.e(err);
3498
+ } finally {
3499
+ _iterator.f();
3500
+ }
3501
+ return nodes;
3502
+ }
3503
+
3504
+ /**
3505
+ * 内部实现
3506
+ *
3507
+ * @private
3508
+ * @template {any} T
3509
+ * @template {(item: T) => boolean} F
3510
+ * @param {T[]} tree 树结构数据
3511
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
3512
+ * @param {string} [childrenField] 子级字段名
3513
+ * @param {T[]} [path=[]] 当前遍历路径
3514
+ * @returns {T[]}
3515
+ */
3516
+ function internalFindTreeSelect(tree, predicate, childrenField) {
3517
+ var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
3518
+ if (!Array.isArray(tree)) {
3519
+ return [];
3520
+ }
3521
+ var _iterator = _createForOfIteratorHelper(tree),
3522
+ _step;
3523
+ try {
3524
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
3525
+ var item = _step.value;
3526
+ path.push(item);
3527
+ if (predicate(item)) {
3528
+ return path;
3529
+ }
3530
+ if (isObject(item)) {
3531
+ /** @type {T[]} */
3532
+ // @ts-ignore
3533
+ var childs = item[childrenField];
3534
+ if (Array.isArray(childs) && childs.length > 0) {
3535
+ var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
3536
+ if (findChildren.length > 0) {
3537
+ return findChildren;
3538
+ }
3539
+ }
3540
+ }
3541
+ path.pop();
3542
+ }
3543
+ } catch (err) {
3544
+ _iterator.e(err);
3545
+ } finally {
3546
+ _iterator.f();
3547
+ }
3548
+ return [];
3549
+ }
3550
+
3551
+ /**
3552
+ * 查找包含当前节点的所有父级节点
3553
+ *
3554
+ * @static
3555
+ * @alias module:Other.findTreeSelect
3556
+ * @since 4.14.0
3557
+ * @template {any} T
3558
+ * @template {(item: T) => boolean} F
3559
+ * @param {T[]} tree 树结构数据
3560
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
3561
+ * @param {string} [childrenField='children'] 子级字段名
3562
+ * @returns {T[]}
3563
+ * @example
3564
+ * 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" }] }];
3565
+ *
3566
+ * findTreeSelect(menus, item => item.id === '2');
3567
+ * // [{"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"}]}]}]
3568
+ *
3569
+ * findTreeSelect(menus, item => item.id === '7');
3570
+ * // [{"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"}]
3571
+ *
3572
+ * findTreeSelect(menus, item => item.id === 'not found');
3573
+ * // []
3574
+ */
3575
+ function findTreeSelect(tree, predicate) {
3576
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
3577
+ return internalFindTreeSelect(tree, predicate, childrenField);
3578
+ }
3579
+
3451
3580
  exports.blobToDataURL = blobToDataURL;
3452
3581
  exports.bytesToSize = bytesToSize;
3453
3582
  exports.calculateCursorPosition = calculateCursorPosition;
3454
3583
  exports.dataURLToBlob = dataURLToBlob;
3455
3584
  exports.divide = divide;
3585
+ exports.filterTree = filterTree;
3456
3586
  exports.findTreeNode = findTreeNode;
3587
+ exports.findTreeNodes = findTreeNodes;
3457
3588
  exports.findTreeSelect = findTreeSelect;
3458
3589
  exports.formatBankCard = formatBankCard;
3459
3590
  exports.formatMobile = formatMobile;