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.
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
+ var _type = require("./utils/type");
7
8
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
8
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."); }
9
10
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
@@ -20,7 +21,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
20
21
  * @template {any} T
21
22
  * @template {(item: T) => boolean} F
22
23
  * @param {T[]} tree 树结构数据
23
- * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回该节点
24
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,将返回该节点
24
25
  * @param {string} [childrenField='children'] 子级字段名
25
26
  * @returns {T|undefined}
26
27
  * @example
@@ -41,6 +42,9 @@ function findTreeNode(tree, predicate) {
41
42
 
42
43
  /** @type {T|undefined} */
43
44
  var node;
45
+ if (!Array.isArray(tree)) {
46
+ return node;
47
+ }
44
48
  var _iterator = _createForOfIteratorHelper(tree),
45
49
  _step;
46
50
  try {
@@ -55,11 +59,14 @@ function findTreeNode(tree, predicate) {
55
59
  node = temp;
56
60
  break;
57
61
  }
58
-
59
- /** @type {T[]} */
60
- // @ts-ignore
61
- var children = temp[childrenField] || [];
62
- stack.push.apply(stack, _toConsumableArray(children));
62
+ if ((0, _type.isObject)(temp)) {
63
+ /** @type {T[]} */
64
+ // @ts-ignore
65
+ var childs = temp[childrenField];
66
+ if (Array.isArray(childs) && childs.length > 0) {
67
+ stack.push.apply(stack, _toConsumableArray(childs));
68
+ }
69
+ }
63
70
  }
64
71
  if (node) {
65
72
  break;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
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:Other.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 = [];
45
+
46
+ /** @type {T[]} */
47
+ var nodes = [];
48
+ if (!Array.isArray(tree)) {
49
+ return nodes;
50
+ }
51
+ var _iterator = _createForOfIteratorHelper(tree),
52
+ _step;
53
+ try {
54
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
55
+ var item = _step.value;
56
+ stack.push(item);
57
+ while (stack.length) {
58
+ /** @type {T} */
59
+ // @ts-ignore
60
+ var temp = stack.pop();
61
+ if (predicate(temp)) {
62
+ nodes.push(temp);
63
+ }
64
+ if ((0, _type.isObject)(temp)) {
65
+ /** @type {T[]} */
66
+ // @ts-ignore
67
+ var childs = temp[childrenField];
68
+ if (Array.isArray(childs) && childs.length > 0) {
69
+ stack.push.apply(stack, _toConsumableArray(childs));
70
+ }
71
+ }
72
+ }
73
+ }
74
+ } catch (err) {
75
+ _iterator.e(err);
76
+ } finally {
77
+ _iterator.f();
78
+ }
79
+ return nodes;
80
+ }
81
+ var _default = findTreeNodes;
82
+ exports["default"] = _default;
@@ -16,14 +16,13 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
16
16
  * @template {(item: T) => boolean} F
17
17
  * @param {T[]} tree 树结构数据
18
18
  * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
19
- * @param {string} [childrenField='children'] 子级字段名
19
+ * @param {string} [childrenField] 子级字段名
20
20
  * @param {T[]} [path=[]] 当前遍历路径
21
21
  * @returns {T[]}
22
22
  */
23
- function internalFindTreeSelect(tree, predicate) {
24
- var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
23
+ function internalFindTreeSelect(tree, predicate, childrenField) {
25
24
  var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
26
- if (!tree) {
25
+ if (!Array.isArray(tree)) {
27
26
  return [];
28
27
  }
29
28
  var _iterator = _createForOfIteratorHelper(tree),
@@ -35,13 +34,15 @@ function internalFindTreeSelect(tree, predicate) {
35
34
  if (predicate(item)) {
36
35
  return path;
37
36
  }
38
-
39
- // @ts-ignore
40
- if ((0, _type.isObject)(item) && Array.isArray(item[childrenField]) && item[childrenField].length > 0) {
37
+ if ((0, _type.isObject)(item)) {
38
+ /** @type {T[]} */
41
39
  // @ts-ignore
42
- var findChildren = internalFindTreeSelect(item[childrenField], predicate, childrenField, path);
43
- if (findChildren.length > 0) {
44
- return findChildren;
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
+ }
45
46
  }
46
47
  }
47
48
  path.pop();
package/lib/index.js CHANGED
@@ -33,12 +33,24 @@ Object.defineProperty(exports, "divide", {
33
33
  return _divide["default"];
34
34
  }
35
35
  });
36
+ Object.defineProperty(exports, "filterTree", {
37
+ enumerable: true,
38
+ get: function get() {
39
+ return _filterTree["default"];
40
+ }
41
+ });
36
42
  Object.defineProperty(exports, "findTreeNode", {
37
43
  enumerable: true,
38
44
  get: function get() {
39
45
  return _findTreeNode["default"];
40
46
  }
41
47
  });
48
+ Object.defineProperty(exports, "findTreeNodes", {
49
+ enumerable: true,
50
+ get: function get() {
51
+ return _findTreeNodes["default"];
52
+ }
53
+ });
42
54
  Object.defineProperty(exports, "findTreeSelect", {
43
55
  enumerable: true,
44
56
  get: function get() {
@@ -347,8 +359,7 @@ var _padZero = _interopRequireDefault(require("./padZero"));
347
359
  var _transformFieldNames = _interopRequireDefault(require("./transformFieldNames"));
348
360
  var _listToTree = _interopRequireDefault(require("./listToTree"));
349
361
  var _treeToList = _interopRequireDefault(require("./treeToList"));
350
- var _findTreeNode = _interopRequireDefault(require("./findTreeNode"));
351
- var _findTreeSelect = _interopRequireDefault(require("./findTreeSelect"));
362
+ var _filterTree = _interopRequireDefault(require("./filterTree"));
352
363
  var _plus = _interopRequireDefault(require("./plus"));
353
364
  var _minus = _interopRequireDefault(require("./minus"));
354
365
  var _times = _interopRequireDefault(require("./times"));
@@ -358,5 +369,8 @@ var _waitTime = _interopRequireDefault(require("./waitTime"));
358
369
  var _calculateCursorPosition = _interopRequireDefault(require("./calculateCursorPosition"));
359
370
  var _randomString = _interopRequireDefault(require("./randomString"));
360
371
  var _strlen = _interopRequireDefault(require("./strlen"));
372
+ var _findTreeNode = _interopRequireDefault(require("./findTreeNode"));
373
+ var _findTreeNodes = _interopRequireDefault(require("./findTreeNodes"));
374
+ var _findTreeSelect = _interopRequireDefault(require("./findTreeSelect"));
361
375
  var _config = require("./utils/config");
362
376
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
package/lib/listToTree.js CHANGED
@@ -17,29 +17,30 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
17
17
  * @private
18
18
  * @template {Record<string,any>} [T=Record<string,any>]
19
19
  * @param {T[]} arr 列表数据
20
- * @param {object} options 配置项
20
+ * @param {object} [options] 配置项
21
21
  * @param {string} [options.childrenField='children'] 子级字段名称
22
- * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
22
+ * @param {'none'|'null'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
23
23
  */
24
- function processEmptyChildren(arr) {
25
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
26
- var _options$childrenFiel = options.childrenField,
27
- childrenField = _options$childrenFiel === void 0 ? 'children' : _options$childrenFiel,
28
- _options$emptyChildre = options.emptyChildrenValue,
29
- emptyChildrenValue = _options$emptyChildre === void 0 ? 'none' : _options$emptyChildre;
24
+ function processEmptyChildren(arr, options) {
25
+ var _ref = options || {},
26
+ _ref$childrenField = _ref.childrenField,
27
+ childrenField = _ref$childrenField === void 0 ? 'children' : _ref$childrenField,
28
+ _ref$emptyChildrenVal = _ref.emptyChildrenValue,
29
+ emptyChildrenValue = _ref$emptyChildrenVal === void 0 ? 'none' : _ref$emptyChildrenVal;
30
30
  arr.forEach(function (item) {
31
- if ((0, _type.isObject)(item) && Array.isArray(item[childrenField])) {
32
- if (item[childrenField].length <= 0) {
33
- if (emptyChildrenValue === 'null') {
34
- // @ts-ignore
35
- item[childrenField] = null;
36
- } else if (emptyChildrenValue === 'none') {
37
- delete item[childrenField];
38
- }
31
+ // if (isObject(item) && Array.isArray(item[childrenField])) {
32
+ if (item[childrenField].length <= 0) {
33
+ if (emptyChildrenValue === 'null') {
34
+ // @ts-ignore
35
+ item[childrenField] = null;
36
+ // } else if (emptyChildrenValue === 'none') { // emptyChildrenValue='array' 不会执行该内部方法
39
37
  } else {
40
- processEmptyChildren(item[childrenField], options);
38
+ delete item[childrenField];
41
39
  }
40
+ } else {
41
+ processEmptyChildren(item[childrenField], options);
42
42
  }
43
+ // }
43
44
  });
44
45
  }
45
46
 
@@ -52,7 +53,7 @@ function processEmptyChildren(arr) {
52
53
  * @template {Record<string,any>} [T=Record<string,any>]
53
54
  * @template {*} [R=T&Record<string,any>]
54
55
  * @param {T[]} list 列表数据
55
- * @param {object} options 配置项
56
+ * @param {object} [options] 配置项
56
57
  * @param {string} [options.keyField='id'] 当前数据的键值字段名称
57
58
  * @param {string} [options.parentField='pid'] 当前数据的父级字段名称
58
59
  * @param {string} [options.childrenField='children'] 子级字段名称
@@ -78,24 +79,27 @@ function processEmptyChildren(arr) {
78
79
  * // [{"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"}]}]
79
80
  *
80
81
  */
81
- function listToTree(list) {
82
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
83
- var _options$keyField = options.keyField,
84
- keyField = _options$keyField === void 0 ? 'id' : _options$keyField,
85
- _options$parentField = options.parentField,
86
- parentField = _options$parentField === void 0 ? 'pid' : _options$parentField,
87
- _options$childrenFiel2 = options.childrenField,
88
- childrenField = _options$childrenFiel2 === void 0 ? 'children' : _options$childrenFiel2,
89
- _options$emptyChildre2 = options.emptyChildrenValue,
90
- emptyChildrenValue = _options$emptyChildre2 === void 0 ? 'none' : _options$emptyChildre2,
91
- _options$nodeAssign = options.nodeAssign,
92
- nodeAssign = _options$nodeAssign === void 0 ? 'spread' : _options$nodeAssign;
82
+ function listToTree(list, options) {
83
+ var _ref2 = options || {},
84
+ _ref2$keyField = _ref2.keyField,
85
+ keyField = _ref2$keyField === void 0 ? 'id' : _ref2$keyField,
86
+ _ref2$parentField = _ref2.parentField,
87
+ parentField = _ref2$parentField === void 0 ? 'pid' : _ref2$parentField,
88
+ _ref2$childrenField = _ref2.childrenField,
89
+ childrenField = _ref2$childrenField === void 0 ? 'children' : _ref2$childrenField,
90
+ _ref2$emptyChildrenVa = _ref2.emptyChildrenValue,
91
+ emptyChildrenValue = _ref2$emptyChildrenVa === void 0 ? 'none' : _ref2$emptyChildrenVa,
92
+ _ref2$nodeAssign = _ref2.nodeAssign,
93
+ nodeAssign = _ref2$nodeAssign === void 0 ? 'spread' : _ref2$nodeAssign;
93
94
 
94
95
  /** @type {R[]} */
95
96
  var tree = [];
96
97
 
97
98
  /** @type {Object.<string, T[]>} */
98
99
  var record = {};
100
+ if (!Array.isArray(list)) {
101
+ return tree;
102
+ }
99
103
  list.forEach(function (item) {
100
104
  if ((0, _type.isObject)(item)) {
101
105
  var newItem = nodeAssign === 'spread' ? _objectSpread({}, item) : item;
@@ -104,24 +104,21 @@ var Provinces = [
104
104
  *
105
105
  */
106
106
  function parseIdCard(id) {
107
- if (!regIdCard.test(id)) {
107
+ var match = regIdCard.exec(id);
108
+ if (!match) {
108
109
  return null;
109
110
  }
110
111
 
111
- /** @type {RegExpExecArray} */
112
- // @ts-ignore
113
- var info = regIdCard.exec(id);
114
-
115
112
  /** @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }} */
116
113
  // @ts-ignore
117
- var origin = (info === null || info === void 0 ? void 0 : info.groups) || {
118
- province: info[1],
119
- city: info[2],
120
- area: info[3],
121
- year: info[4],
122
- month: info[5],
123
- day: info[6],
124
- gender: info[7]
114
+ var origin = match.groups || {
115
+ province: match[1],
116
+ city: match[2],
117
+ area: match[3],
118
+ year: match[4],
119
+ month: match[5],
120
+ day: match[6],
121
+ gender: match[7]
125
122
  };
126
123
  var province = Provinces.find(function (item) {
127
124
  return item[0] === origin.province;
@@ -10,14 +10,12 @@ var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
10
10
 
11
11
  /**
12
12
  * @private
13
- * @param {number} [len=0] 长度
14
- * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
13
+ * @param {number} len 长度
14
+ * @param {string} optionalChars 允许的字符,默认为数字和大小写字母
15
15
  * @param {string} [prefix=''] 前缀部分,不计入长度
16
16
  * @returns {string}
17
17
  */
18
- function internalRandomString() {
19
- var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
20
- var optionalChars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultChars;
18
+ function internalRandomString(len, optionalChars) {
21
19
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
22
20
  while (len-- > 0) {
23
21
  var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
@@ -13,9 +13,9 @@ exports["default"] = void 0;
13
13
  * @static
14
14
  * @alias module:Processor.transformFieldNames
15
15
  * @since 4.14.0
16
- * @param {object[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
16
+ * @param {object[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
17
17
  * @param {object} fieldNames 字段名映射
18
- * @param {string} [childrenFieldName] 子级数据字段名
18
+ * @param {string} [childrenField] 子级数据字段名
19
19
  * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
20
20
  * @returns {object[]}
21
21
  * @example
@@ -37,6 +37,6 @@ exports["default"] = void 0;
37
37
  * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
38
38
  * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
39
39
  */
40
- function transformFieldNames(data, fieldNames, childrenFieldName) {}
40
+ function transformFieldNames(data, fieldNames, childrenField) {}
41
41
  var _default = transformFieldNames;
42
42
  exports["default"] = _default;
@@ -20,9 +20,9 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
20
20
  * @template {*} D
21
21
  * @template {Record<string, keyof D>} F
22
22
  * @template {string} C
23
- * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
23
+ * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
24
24
  * @param {F} fieldNames 字段名映射
25
- * @param {C} [childrenFieldName] 子级数据字段名
25
+ * @param {C} [childrenField] 子级数据字段名
26
26
  * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
27
27
  * @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
28
28
  * @example
@@ -44,7 +44,7 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
44
44
  * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
45
45
  * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
46
46
  */
47
- function transformFieldNames(data, fieldNames, childrenFieldName) {
47
+ function transformFieldNames(data, fieldNames, childrenField) {
48
48
  var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
49
49
  if (!Array.isArray(data)) {
50
50
  return data;
@@ -60,8 +60,7 @@ function transformFieldNames(data, fieldNames, childrenFieldName) {
60
60
  * @param {Array.<object>} arr 列表数据
61
61
  * @returns {*}
62
62
  */
63
- function recusion() {
64
- var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
63
+ function recusion(arr) {
65
64
  return arr.map(function (item) {
66
65
  if (!(0, _type.isObject)(item)) {
67
66
  return item;
@@ -72,9 +71,9 @@ function transformFieldNames(data, fieldNames, childrenFieldName) {
72
71
 
73
72
  // 树形数据子节点
74
73
  // @ts-ignore
75
- if (childrenFieldName && Array.isArray(newItem[childrenFieldName]) && newItem[childrenFieldName].length > 0) {
74
+ if (childrenField && Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
76
75
  // @ts-ignore
77
- newItem[childrenFieldName] = recusion(newItem[childrenFieldName].slice());
76
+ newItem[childrenField] = recusion(newItem[childrenField].slice());
78
77
  }
79
78
 
80
79
  // 替换字段名
package/lib/treeToList.js CHANGED
@@ -32,6 +32,9 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
32
32
  function treeToList(tree, childrenField) {
33
33
  /** @type {R[]} */
34
34
  var list = [];
35
+ if (!Array.isArray(tree)) {
36
+ return list;
37
+ }
35
38
 
36
39
  /**
37
40
  * 递归遍历
@@ -43,7 +46,7 @@ function treeToList(tree, childrenField) {
43
46
  var newItem = _objectSpread({}, item);
44
47
  // @ts-ignore
45
48
  list.push(newItem);
46
- if (newItem !== null && newItem !== void 0 && newItem[childrenField]) {
49
+ if (newItem[childrenField]) {
47
50
  if (Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
48
51
  recusion(newItem[childrenField]);
49
52
  }
@@ -24,5 +24,5 @@ function setDisableWarning(bool) {
24
24
  }
25
25
 
26
26
  // eslint-disable-next-line no-undef
27
- var version = "4.14.1";
27
+ var version = "4.15.1";
28
28
  exports.version = version;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.14.1",
3
+ "version": "4.15.1",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -10,6 +10,7 @@
10
10
  "scripts": {
11
11
  "test": "jest --verbose",
12
12
  "test:coverage": "jest --coverage",
13
+ "test:coverage:local": "cross-env COVERAGE_LOCAL=1 jest --coverage",
13
14
  "test:math": "jest --verbose test/math",
14
15
  "test:type": "jest --verbose test/type",
15
16
  "test:validator": "jest --verbose test/validator",
@@ -5,8 +5,6 @@ export default calculateCursorPosition;
5
5
  * @static
6
6
  * @alias module:Other.calculateCursorPosition
7
7
  * @since 4.6.0
8
- * @see 格式化手机号码 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile|formatMobile}
9
- * @see 格式化银行卡号 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard|formatBankCard}
10
8
  * @see h5示例 {@link https://2950v9.csb.app/|点击查看}
11
9
  * @see react示例 {@link https://33ccy9.csb.app/|点击查看}
12
10
  * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
@@ -0,0 +1,28 @@
1
+ export default filterTree;
2
+ /**
3
+ * 过滤/筛选树节点。<br/><br/>如果某节点被过滤掉,它的子节点也一并抛弃
4
+ *
5
+ * @static
6
+ * @alias module:Processor.filterTree
7
+ * @since 4.15.0
8
+ * @template {any} T
9
+ * @template {(item: T) => boolean} F
10
+ * @param {T[]} tree 树结构数据
11
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,结果将包含该节点
12
+ * @param {string} [childrenField='children'] 子级字段名
13
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
14
+ * @returns {T[]}
15
+ * @example
16
+ * 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" }] }];
17
+ *
18
+ * filterTree(menus, item=>item.name.indexOf('管理') > -1);
19
+ * // [{"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"}]}]
20
+ *
21
+ * // 如果某节点被过滤掉,它的子节点也一并抛弃
22
+ * filterTree(menus, item=>item.id === '7');
23
+ * // []
24
+ *
25
+ * filterTree(menus, item=>item.id === 'not found');
26
+ * // []
27
+ */
28
+ declare function filterTree<T extends unknown, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string | undefined, nodeAssign?: "spread" | "self" | undefined): T[];
@@ -8,7 +8,7 @@ export default findTreeNode;
8
8
  * @template {any} T
9
9
  * @template {(item: T) => boolean} F
10
10
  * @param {T[]} tree 树结构数据
11
- * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回该节点
11
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,将返回该节点
12
12
  * @param {string} [childrenField='children'] 子级字段名
13
13
  * @returns {T|undefined}
14
14
  * @example
@@ -0,0 +1,29 @@
1
+ export default findTreeNodes;
2
+ /**
3
+ * 查找树结构数据多个节点
4
+ *
5
+ * @static
6
+ * @alias module:Other.findTreeNodes
7
+ * @since 4.15.0
8
+ * @template {any} T
9
+ * @template {(item: T) => boolean} F
10
+ * @param {T[]} tree 树结构数据
11
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,返回结果将包含该节点
12
+ * @param {string} [childrenField='children'] 子级字段名
13
+ * @returns {T[]}
14
+ * @example
15
+ * 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" }] }];
16
+ *
17
+ * findTreeNodes(menus, item=>item.id === '2');
18
+ * // [{"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"}]}]}]
19
+ *
20
+ * findTreeNodes(menus, item=>item.name.indexOf('管理') > -1);
21
+ * // [{"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"}]
22
+ *
23
+ * findTreeNodes(menus, item=>item.id === '1' || item.id === '7');
24
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
25
+ *
26
+ * findTreeNodes(menus, item=>item.id === 'not found');
27
+ * // []
28
+ */
29
+ declare function findTreeNodes<T extends unknown, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string | undefined): T[];
package/types/index.d.ts CHANGED
@@ -36,8 +36,7 @@ export { default as padZero } from "./padZero";
36
36
  export { default as transformFieldNames } from "./transformFieldNames";
37
37
  export { default as listToTree } from "./listToTree";
38
38
  export { default as treeToList } from "./treeToList";
39
- export { default as findTreeNode } from "./findTreeNode";
40
- export { default as findTreeSelect } from "./findTreeSelect";
39
+ export { default as filterTree } from "./filterTree";
41
40
  export { default as plus } from "./plus";
42
41
  export { default as minus } from "./minus";
43
42
  export { default as times } from "./times";
@@ -47,4 +46,7 @@ export { default as waitTime } from "./waitTime";
47
46
  export { default as calculateCursorPosition } from "./calculateCursorPosition";
48
47
  export { default as randomString } from "./randomString";
49
48
  export { default as strlen } from "./strlen";
49
+ export { default as findTreeNode } from "./findTreeNode";
50
+ export { default as findTreeNodes } from "./findTreeNodes";
51
+ export { default as findTreeSelect } from "./findTreeSelect";
50
52
  export { setDisableWarning, version } from "./utils/config";
@@ -8,7 +8,7 @@ export default listToTree;
8
8
  * @template {Record<string,any>} [T=Record<string,any>]
9
9
  * @template {*} [R=T&Record<string,any>]
10
10
  * @param {T[]} list 列表数据
11
- * @param {object} options 配置项
11
+ * @param {object} [options] 配置项
12
12
  * @param {string} [options.keyField='id'] 当前数据的键值字段名称
13
13
  * @param {string} [options.parentField='pid'] 当前数据的父级字段名称
14
14
  * @param {string} [options.childrenField='children'] 子级字段名称
@@ -40,4 +40,4 @@ declare function listToTree<T extends Record<string, any> = Record<string, any>,
40
40
  childrenField?: string | undefined;
41
41
  emptyChildrenValue?: "none" | "null" | "array" | undefined;
42
42
  nodeAssign?: "spread" | "self" | undefined;
43
- }): R[];
43
+ } | undefined): R[];
@@ -8,9 +8,9 @@ export default transformFieldNames;
8
8
  * @template {*} D
9
9
  * @template {Record<string, keyof D>} F
10
10
  * @template {string} C
11
- * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
11
+ * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
12
12
  * @param {F} fieldNames 字段名映射
13
- * @param {C} [childrenFieldName] 子级数据字段名
13
+ * @param {C} [childrenField] 子级数据字段名
14
14
  * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
15
15
  * @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
16
16
  * @example
@@ -32,4 +32,4 @@ export default transformFieldNames;
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 unknown, F extends Record<string, keyof D>, C extends string>(data: D[], fieldNames: F, childrenFieldName?: C | undefined, nodeAssign?: "spread" | "self" | undefined): import("./transformFieldNames.type.js").TransformFieldNames<D, F, C>;
35
+ declare function transformFieldNames<D extends unknown, F extends Record<string, keyof D>, C extends string>(data: D[], fieldNames: F, childrenField?: C | undefined, nodeAssign?: "spread" | "self" | undefined): import("./transformFieldNames.type.js").TransformFieldNames<D, F, C>;