util-helpers 4.15.0 → 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.15.0";
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;
@@ -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;
@@ -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
  }
@@ -2984,12 +2987,11 @@
2984
2987
  function filterTree(tree, predicate) {
2985
2988
  var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
2986
2989
  var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
2987
- if (!Array.isArray(tree)) {
2988
- return tree;
2989
- }
2990
-
2991
2990
  /** @type {T[]} */
2992
2991
  var result = [];
2992
+ if (!Array.isArray(tree)) {
2993
+ return result;
2994
+ }
2993
2995
  tree.forEach(function (item) {
2994
2996
  var newItem = item;
2995
2997
  if (isObject(item)) {
@@ -3292,14 +3294,12 @@
3292
3294
 
3293
3295
  /**
3294
3296
  * @private
3295
- * @param {number} [len=0] 长度
3296
- * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
3297
+ * @param {number} len 长度
3298
+ * @param {string} optionalChars 允许的字符,默认为数字和大小写字母
3297
3299
  * @param {string} [prefix=''] 前缀部分,不计入长度
3298
3300
  * @returns {string}
3299
3301
  */
3300
- function internalRandomString() {
3301
- var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
3302
- var optionalChars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultChars;
3302
+ function internalRandomString(len, optionalChars) {
3303
3303
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
3304
3304
  while (len-- > 0) {
3305
3305
  var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
@@ -3396,6 +3396,9 @@
3396
3396
 
3397
3397
  /** @type {T|undefined} */
3398
3398
  var node;
3399
+ if (!Array.isArray(tree)) {
3400
+ return node;
3401
+ }
3399
3402
  var _iterator = _createForOfIteratorHelper(tree),
3400
3403
  _step;
3401
3404
  try {
@@ -3464,6 +3467,9 @@
3464
3467
 
3465
3468
  /** @type {T[]} */
3466
3469
  var nodes = [];
3470
+ if (!Array.isArray(tree)) {
3471
+ return nodes;
3472
+ }
3467
3473
  var _iterator = _createForOfIteratorHelper(tree),
3468
3474
  _step;
3469
3475
  try {
@@ -3503,14 +3509,13 @@
3503
3509
  * @template {(item: T) => boolean} F
3504
3510
  * @param {T[]} tree 树结构数据
3505
3511
  * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
3506
- * @param {string} [childrenField='children'] 子级字段名
3512
+ * @param {string} [childrenField] 子级字段名
3507
3513
  * @param {T[]} [path=[]] 当前遍历路径
3508
3514
  * @returns {T[]}
3509
3515
  */
3510
- function internalFindTreeSelect(tree, predicate) {
3511
- var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
3516
+ function internalFindTreeSelect(tree, predicate, childrenField) {
3512
3517
  var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
3513
- if (!tree) {
3518
+ if (!Array.isArray(tree)) {
3514
3519
  return [];
3515
3520
  }
3516
3521
  var _iterator = _createForOfIteratorHelper(tree),