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.
@@ -1,6 +1,6 @@
1
1
  import normalizeString from './normalizeString';
2
2
 
3
- // 参考了: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
3
+ // ref: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
4
4
 
5
5
  /**
6
6
  * 计算输入框的值格式化后光标位置
@@ -8,8 +8,6 @@ import normalizeString from './normalizeString';
8
8
  * @static
9
9
  * @alias module:Other.calculateCursorPosition
10
10
  * @since 4.6.0
11
- * @see 格式化手机号码 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile|formatMobile}
12
- * @see 格式化银行卡号 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard|formatBankCard}
13
11
  * @see h5示例 {@link https://2950v9.csb.app/|点击查看}
14
12
  * @see react示例 {@link https://33ccy9.csb.app/|点击查看}
15
13
  * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
@@ -0,0 +1,61 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+ import { isObject } from "./utils/type";
5
+
6
+ /**
7
+ * 过滤/筛选树节点。<br/><br/>如果某节点被过滤掉,它的子节点也一并抛弃
8
+ *
9
+ * @static
10
+ * @alias module:Processor.filterTree
11
+ * @since 4.15.0
12
+ * @template {any} T
13
+ * @template {(item: T) => boolean} F
14
+ * @param {T[]} tree 树结构数据
15
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,结果将包含该节点
16
+ * @param {string} [childrenField='children'] 子级字段名
17
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
18
+ * @returns {T[]}
19
+ * @example
20
+ * 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" }] }];
21
+ *
22
+ * filterTree(menus, item=>item.name.indexOf('管理') > -1);
23
+ * // [{"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"}]}]
24
+ *
25
+ * // 如果某节点被过滤掉,它的子节点也一并抛弃
26
+ * filterTree(menus, item=>item.id === '7');
27
+ * // []
28
+ *
29
+ * filterTree(menus, item=>item.id === 'not found');
30
+ * // []
31
+ */
32
+ function filterTree(tree, predicate) {
33
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
34
+ var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
35
+ /** @type {T[]} */
36
+ var result = [];
37
+ if (!Array.isArray(tree)) {
38
+ return result;
39
+ }
40
+ tree.forEach(function (item) {
41
+ var newItem = item;
42
+ if (isObject(item)) {
43
+ // @ts-ignore
44
+ newItem = nodeAssign === 'spread' ? _objectSpread({}, item) : item;
45
+ }
46
+ if (predicate(newItem)) {
47
+ if (isObject(newItem)) {
48
+ /** @type {T[]|undefined} */
49
+ // @ts-ignore
50
+ var childs = newItem[childrenField];
51
+ if (Array.isArray(childs) && childs.length > 0) {
52
+ // @ts-ignore
53
+ newItem[childrenField] = filterTree(childs, predicate, childrenField, nodeAssign);
54
+ }
55
+ }
56
+ result.push(newItem);
57
+ }
58
+ });
59
+ return result;
60
+ }
61
+ export default filterTree;
@@ -2,6 +2,8 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
2
  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; } } }; }
3
3
  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); }
4
4
  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; }
5
+ import { isObject } from "./utils/type";
6
+
5
7
  /**
6
8
  * 查找树结构数据节点
7
9
  *
@@ -11,7 +13,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
11
13
  * @template {any} T
12
14
  * @template {(item: T) => boolean} F
13
15
  * @param {T[]} tree 树结构数据
14
- * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回该节点
16
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,将返回该节点
15
17
  * @param {string} [childrenField='children'] 子级字段名
16
18
  * @returns {T|undefined}
17
19
  * @example
@@ -32,6 +34,9 @@ function findTreeNode(tree, predicate) {
32
34
 
33
35
  /** @type {T|undefined} */
34
36
  var node;
37
+ if (!Array.isArray(tree)) {
38
+ return node;
39
+ }
35
40
  var _iterator = _createForOfIteratorHelper(tree),
36
41
  _step;
37
42
  try {
@@ -46,11 +51,14 @@ function findTreeNode(tree, predicate) {
46
51
  node = temp;
47
52
  break;
48
53
  }
49
-
50
- /** @type {T[]} */
51
- // @ts-ignore
52
- var children = temp[childrenField] || [];
53
- stack.push.apply(stack, _toConsumableArray(children));
54
+ if (isObject(temp)) {
55
+ /** @type {T[]} */
56
+ // @ts-ignore
57
+ var childs = temp[childrenField];
58
+ if (Array.isArray(childs) && childs.length > 0) {
59
+ stack.push.apply(stack, _toConsumableArray(childs));
60
+ }
61
+ }
54
62
  }
55
63
  if (node) {
56
64
  break;
@@ -0,0 +1,73 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ 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; } } }; }
3
+ 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); }
4
+ 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; }
5
+ import { isObject } from "./utils/type";
6
+
7
+ /**
8
+ * 查找树结构数据多个节点
9
+ *
10
+ * @static
11
+ * @alias module:Other.findTreeNodes
12
+ * @since 4.15.0
13
+ * @template {any} T
14
+ * @template {(item: T) => boolean} F
15
+ * @param {T[]} tree 树结构数据
16
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy ,返回结果将包含该节点
17
+ * @param {string} [childrenField='children'] 子级字段名
18
+ * @returns {T[]}
19
+ * @example
20
+ * 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" }] }];
21
+ *
22
+ * findTreeNodes(menus, item=>item.id === '2');
23
+ * // [{"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"}]}]}]
24
+ *
25
+ * findTreeNodes(menus, item=>item.name.indexOf('管理') > -1);
26
+ * // [{"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"}]
27
+ *
28
+ * findTreeNodes(menus, item=>item.id === '1' || item.id === '7');
29
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
30
+ *
31
+ * findTreeNodes(menus, item=>item.id === 'not found');
32
+ * // []
33
+ */
34
+ function findTreeNodes(tree, predicate) {
35
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
36
+ var stack = [];
37
+
38
+ /** @type {T[]} */
39
+ var nodes = [];
40
+ if (!Array.isArray(tree)) {
41
+ return nodes;
42
+ }
43
+ var _iterator = _createForOfIteratorHelper(tree),
44
+ _step;
45
+ try {
46
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
47
+ var item = _step.value;
48
+ stack.push(item);
49
+ while (stack.length) {
50
+ /** @type {T} */
51
+ // @ts-ignore
52
+ var temp = stack.pop();
53
+ if (predicate(temp)) {
54
+ nodes.push(temp);
55
+ }
56
+ if (isObject(temp)) {
57
+ /** @type {T[]} */
58
+ // @ts-ignore
59
+ var childs = temp[childrenField];
60
+ if (Array.isArray(childs) && childs.length > 0) {
61
+ stack.push.apply(stack, _toConsumableArray(childs));
62
+ }
63
+ }
64
+ }
65
+ }
66
+ } catch (err) {
67
+ _iterator.e(err);
68
+ } finally {
69
+ _iterator.f();
70
+ }
71
+ return nodes;
72
+ }
73
+ export default findTreeNodes;
@@ -11,14 +11,13 @@ import { isObject } from "./utils/type";
11
11
  * @template {(item: T) => boolean} F
12
12
  * @param {T[]} tree 树结构数据
13
13
  * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
14
- * @param {string} [childrenField='children'] 子级字段名
14
+ * @param {string} [childrenField] 子级字段名
15
15
  * @param {T[]} [path=[]] 当前遍历路径
16
16
  * @returns {T[]}
17
17
  */
18
- function internalFindTreeSelect(tree, predicate) {
19
- var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
18
+ function internalFindTreeSelect(tree, predicate, childrenField) {
20
19
  var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
21
- if (!tree) {
20
+ if (!Array.isArray(tree)) {
22
21
  return [];
23
22
  }
24
23
  var _iterator = _createForOfIteratorHelper(tree),
@@ -30,13 +29,15 @@ function internalFindTreeSelect(tree, predicate) {
30
29
  if (predicate(item)) {
31
30
  return path;
32
31
  }
33
-
34
- // @ts-ignore
35
- if (isObject(item) && Array.isArray(item[childrenField]) && item[childrenField].length > 0) {
32
+ if (isObject(item)) {
33
+ /** @type {T[]} */
36
34
  // @ts-ignore
37
- var findChildren = internalFindTreeSelect(item[childrenField], predicate, childrenField, path);
38
- if (findChildren.length > 0) {
39
- return findChildren;
35
+ var childs = item[childrenField];
36
+ if (Array.isArray(childs) && childs.length > 0) {
37
+ var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
38
+ if (findChildren.length > 0) {
39
+ return findChildren;
40
+ }
40
41
  }
41
42
  }
42
43
  path.pop();
package/esm/index.js CHANGED
@@ -49,8 +49,7 @@ export { default as padZero } from './padZero';
49
49
  export { default as transformFieldNames } from './transformFieldNames';
50
50
  export { default as listToTree } from './listToTree';
51
51
  export { default as treeToList } from './treeToList';
52
- export { default as findTreeNode } from './findTreeNode';
53
- export { default as findTreeSelect } from './findTreeSelect';
52
+ export { default as filterTree } from './filterTree';
54
53
 
55
54
  /**
56
55
  * 数学计算,修正浮点数计算问题
@@ -107,6 +106,9 @@ export { default as waitTime } from './waitTime';
107
106
  export { default as calculateCursorPosition } from './calculateCursorPosition';
108
107
  export { default as randomString } from './randomString';
109
108
  export { default as strlen } from './strlen';
109
+ export { default as findTreeNode } from './findTreeNode';
110
+ export { default as findTreeNodes } from './findTreeNodes';
111
+ export { default as findTreeSelect } from './findTreeSelect';
110
112
 
111
113
  /**
112
114
  * 调试相关
package/esm/listToTree.js CHANGED
@@ -9,29 +9,30 @@ import { isObject } from "./utils/type";
9
9
  * @private
10
10
  * @template {Record<string,any>} [T=Record<string,any>]
11
11
  * @param {T[]} arr 列表数据
12
- * @param {object} options 配置项
12
+ * @param {object} [options] 配置项
13
13
  * @param {string} [options.childrenField='children'] 子级字段名称
14
- * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
14
+ * @param {'none'|'null'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
15
15
  */
16
- function processEmptyChildren(arr) {
17
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
18
- var _options$childrenFiel = options.childrenField,
19
- childrenField = _options$childrenFiel === void 0 ? 'children' : _options$childrenFiel,
20
- _options$emptyChildre = options.emptyChildrenValue,
21
- emptyChildrenValue = _options$emptyChildre === void 0 ? 'none' : _options$emptyChildre;
16
+ function processEmptyChildren(arr, options) {
17
+ var _ref = options || {},
18
+ _ref$childrenField = _ref.childrenField,
19
+ childrenField = _ref$childrenField === void 0 ? 'children' : _ref$childrenField,
20
+ _ref$emptyChildrenVal = _ref.emptyChildrenValue,
21
+ emptyChildrenValue = _ref$emptyChildrenVal === void 0 ? 'none' : _ref$emptyChildrenVal;
22
22
  arr.forEach(function (item) {
23
- if (isObject(item) && Array.isArray(item[childrenField])) {
24
- if (item[childrenField].length <= 0) {
25
- if (emptyChildrenValue === 'null') {
26
- // @ts-ignore
27
- item[childrenField] = null;
28
- } else if (emptyChildrenValue === 'none') {
29
- delete item[childrenField];
30
- }
23
+ // if (isObject(item) && Array.isArray(item[childrenField])) {
24
+ if (item[childrenField].length <= 0) {
25
+ if (emptyChildrenValue === 'null') {
26
+ // @ts-ignore
27
+ item[childrenField] = null;
28
+ // } else if (emptyChildrenValue === 'none') { // emptyChildrenValue='array' 不会执行该内部方法
31
29
  } else {
32
- processEmptyChildren(item[childrenField], options);
30
+ delete item[childrenField];
33
31
  }
32
+ } else {
33
+ processEmptyChildren(item[childrenField], options);
34
34
  }
35
+ // }
35
36
  });
36
37
  }
37
38
 
@@ -44,7 +45,7 @@ function processEmptyChildren(arr) {
44
45
  * @template {Record<string,any>} [T=Record<string,any>]
45
46
  * @template {*} [R=T&Record<string,any>]
46
47
  * @param {T[]} list 列表数据
47
- * @param {object} options 配置项
48
+ * @param {object} [options] 配置项
48
49
  * @param {string} [options.keyField='id'] 当前数据的键值字段名称
49
50
  * @param {string} [options.parentField='pid'] 当前数据的父级字段名称
50
51
  * @param {string} [options.childrenField='children'] 子级字段名称
@@ -70,24 +71,27 @@ function processEmptyChildren(arr) {
70
71
  * // [{"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"}]}]
71
72
  *
72
73
  */
73
- function listToTree(list) {
74
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
75
- var _options$keyField = options.keyField,
76
- keyField = _options$keyField === void 0 ? 'id' : _options$keyField,
77
- _options$parentField = options.parentField,
78
- parentField = _options$parentField === void 0 ? 'pid' : _options$parentField,
79
- _options$childrenFiel2 = options.childrenField,
80
- childrenField = _options$childrenFiel2 === void 0 ? 'children' : _options$childrenFiel2,
81
- _options$emptyChildre2 = options.emptyChildrenValue,
82
- emptyChildrenValue = _options$emptyChildre2 === void 0 ? 'none' : _options$emptyChildre2,
83
- _options$nodeAssign = options.nodeAssign,
84
- nodeAssign = _options$nodeAssign === void 0 ? 'spread' : _options$nodeAssign;
74
+ function listToTree(list, options) {
75
+ var _ref2 = options || {},
76
+ _ref2$keyField = _ref2.keyField,
77
+ keyField = _ref2$keyField === void 0 ? 'id' : _ref2$keyField,
78
+ _ref2$parentField = _ref2.parentField,
79
+ parentField = _ref2$parentField === void 0 ? 'pid' : _ref2$parentField,
80
+ _ref2$childrenField = _ref2.childrenField,
81
+ childrenField = _ref2$childrenField === void 0 ? 'children' : _ref2$childrenField,
82
+ _ref2$emptyChildrenVa = _ref2.emptyChildrenValue,
83
+ emptyChildrenValue = _ref2$emptyChildrenVa === void 0 ? 'none' : _ref2$emptyChildrenVa,
84
+ _ref2$nodeAssign = _ref2.nodeAssign,
85
+ nodeAssign = _ref2$nodeAssign === void 0 ? 'spread' : _ref2$nodeAssign;
85
86
 
86
87
  /** @type {R[]} */
87
88
  var tree = [];
88
89
 
89
90
  /** @type {Object.<string, T[]>} */
90
91
  var record = {};
92
+ if (!Array.isArray(list)) {
93
+ return tree;
94
+ }
91
95
  list.forEach(function (item) {
92
96
  if (isObject(item)) {
93
97
  var newItem = nodeAssign === 'spread' ? _objectSpread({}, item) : item;
@@ -98,24 +98,21 @@ var Provinces = [
98
98
  *
99
99
  */
100
100
  function parseIdCard(id) {
101
- if (!regIdCard.test(id)) {
101
+ var match = regIdCard.exec(id);
102
+ if (!match) {
102
103
  return null;
103
104
  }
104
105
 
105
- /** @type {RegExpExecArray} */
106
- // @ts-ignore
107
- var info = regIdCard.exec(id);
108
-
109
106
  /** @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }} */
110
107
  // @ts-ignore
111
- var origin = (info === null || info === void 0 ? void 0 : info.groups) || {
112
- province: info[1],
113
- city: info[2],
114
- area: info[3],
115
- year: info[4],
116
- month: info[5],
117
- day: info[6],
118
- gender: info[7]
108
+ var origin = match.groups || {
109
+ province: match[1],
110
+ city: match[2],
111
+ area: match[3],
112
+ year: match[4],
113
+ month: match[5],
114
+ day: match[6],
115
+ gender: match[7]
119
116
  };
120
117
  var province = Provinces.find(function (item) {
121
118
  return item[0] === origin.province;
@@ -4,14 +4,12 @@ var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
4
4
 
5
5
  /**
6
6
  * @private
7
- * @param {number} [len=0] 长度
8
- * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
7
+ * @param {number} len 长度
8
+ * @param {string} optionalChars 允许的字符,默认为数字和大小写字母
9
9
  * @param {string} [prefix=''] 前缀部分,不计入长度
10
10
  * @returns {string}
11
11
  */
12
- function internalRandomString() {
13
- var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
14
- var optionalChars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultChars;
12
+ function internalRandomString(len, optionalChars) {
15
13
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
16
14
  while (len-- > 0) {
17
15
  var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
@@ -7,9 +7,9 @@
7
7
  * @static
8
8
  * @alias module:Processor.transformFieldNames
9
9
  * @since 4.14.0
10
- * @param {object[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
10
+ * @param {object[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
11
11
  * @param {object} fieldNames 字段名映射
12
- * @param {string} [childrenFieldName] 子级数据字段名
12
+ * @param {string} [childrenField] 子级数据字段名
13
13
  * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
14
14
  * @returns {object[]}
15
15
  * @example
@@ -31,5 +31,5 @@
31
31
  * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
32
32
  * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
33
33
  */
34
- function transformFieldNames(data, fieldNames, childrenFieldName) {}
34
+ function transformFieldNames(data, fieldNames, childrenField) {}
35
35
  export default transformFieldNames;
@@ -15,9 +15,9 @@ import { isObject } from './utils/type';
15
15
  * @template {*} D
16
16
  * @template {Record<string, keyof D>} F
17
17
  * @template {string} C
18
- * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
18
+ * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenField
19
19
  * @param {F} fieldNames 字段名映射
20
- * @param {C} [childrenFieldName] 子级数据字段名
20
+ * @param {C} [childrenField] 子级数据字段名
21
21
  * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
22
22
  * @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
23
23
  * @example
@@ -39,7 +39,7 @@ import { isObject } from './utils/type';
39
39
  * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
40
40
  * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
41
41
  */
42
- function transformFieldNames(data, fieldNames, childrenFieldName) {
42
+ function transformFieldNames(data, fieldNames, childrenField) {
43
43
  var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
44
44
  if (!Array.isArray(data)) {
45
45
  return data;
@@ -55,8 +55,7 @@ function transformFieldNames(data, fieldNames, childrenFieldName) {
55
55
  * @param {Array.<object>} arr 列表数据
56
56
  * @returns {*}
57
57
  */
58
- function recusion() {
59
- var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
58
+ function recusion(arr) {
60
59
  return arr.map(function (item) {
61
60
  if (!isObject(item)) {
62
61
  return item;
@@ -67,9 +66,9 @@ function transformFieldNames(data, fieldNames, childrenFieldName) {
67
66
 
68
67
  // 树形数据子节点
69
68
  // @ts-ignore
70
- if (childrenFieldName && Array.isArray(newItem[childrenFieldName]) && newItem[childrenFieldName].length > 0) {
69
+ if (childrenField && Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
71
70
  // @ts-ignore
72
- newItem[childrenFieldName] = recusion(newItem[childrenFieldName].slice());
71
+ newItem[childrenField] = recusion(newItem[childrenField].slice());
73
72
  }
74
73
 
75
74
  // 替换字段名
package/esm/treeToList.js CHANGED
@@ -24,6 +24,9 @@ import { isObject } from "./utils/type";
24
24
  function treeToList(tree, childrenField) {
25
25
  /** @type {R[]} */
26
26
  var list = [];
27
+ if (!Array.isArray(tree)) {
28
+ return list;
29
+ }
27
30
 
28
31
  /**
29
32
  * 递归遍历
@@ -35,7 +38,7 @@ function treeToList(tree, childrenField) {
35
38
  var newItem = _objectSpread({}, item);
36
39
  // @ts-ignore
37
40
  list.push(newItem);
38
- if (newItem !== null && newItem !== void 0 && newItem[childrenField]) {
41
+ if (newItem[childrenField]) {
39
42
  if (Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
40
43
  recusion(newItem[childrenField]);
41
44
  }
@@ -15,5 +15,5 @@ function setDisableWarning(bool) {
15
15
  }
16
16
 
17
17
  // eslint-disable-next-line no-undef
18
- var version = "4.14.1";
18
+ var version = "4.15.1";
19
19
  export { config, setDisableWarning, version };
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports["default"] = void 0;
7
7
  var _normalizeString = _interopRequireDefault(require("./normalizeString"));
8
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
- // 参考了: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
9
+ // ref: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
10
10
 
11
11
  /**
12
12
  * 计算输入框的值格式化后光标位置
@@ -14,8 +14,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
14
14
  * @static
15
15
  * @alias module:Other.calculateCursorPosition
16
16
  * @since 4.6.0
17
- * @see 格式化手机号码 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile|formatMobile}
18
- * @see 格式化银行卡号 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard|formatBankCard}
19
17
  * @see h5示例 {@link https://2950v9.csb.app/|点击查看}
20
18
  * @see react示例 {@link https://33ccy9.csb.app/|点击查看}
21
19
  * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
@@ -0,0 +1,70 @@
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 _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
9
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
10
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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:Processor.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;
53
+ }
54
+ if (predicate(newItem)) {
55
+ if ((0, _type.isObject)(newItem)) {
56
+ /** @type {T[]|undefined} */
57
+ // @ts-ignore
58
+ var childs = newItem[childrenField];
59
+ if (Array.isArray(childs) && childs.length > 0) {
60
+ // @ts-ignore
61
+ newItem[childrenField] = filterTree(childs, predicate, childrenField, nodeAssign);
62
+ }
63
+ }
64
+ result.push(newItem);
65
+ }
66
+ });
67
+ return result;
68
+ }
69
+ var _default = filterTree;
70
+ exports["default"] = _default;