util-helpers 4.15.0 → 4.15.2

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.
@@ -7,6 +7,7 @@
7
7
  * @param {number} bytes 字节大小
8
8
  * @param {Object} [options] 配置项
9
9
  * @param {string} [options.spaceMark=' '] 间隔字符
10
+ * @param {number} [options.precision=2] 精度
10
11
  * @returns {string} 存储单位值
11
12
  * @example
12
13
  *
@@ -23,13 +24,15 @@
23
24
  function bytesToSize(bytes) {
24
25
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
25
26
  var _options$spaceMark = options.spaceMark,
26
- spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark;
27
+ spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark,
28
+ _options$precision = options.precision,
29
+ precision = _options$precision === void 0 ? 2 : _options$precision;
27
30
  var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
28
31
  if (numBytes === 0 || isNaN(numBytes)) return "0".concat(spaceMark, "B");
29
32
  var k = 1024;
30
33
  // 存储单位
31
34
  var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
32
35
  var i = Math.floor(Math.log(numBytes) / Math.log(k));
33
- return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(2))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
36
+ return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(precision))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
34
37
  }
35
38
  export default bytesToSize;
package/esm/filterTree.js CHANGED
@@ -32,12 +32,11 @@ import { isObject } from "./utils/type";
32
32
  function filterTree(tree, predicate) {
33
33
  var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
34
34
  var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
35
- if (!Array.isArray(tree)) {
36
- return tree;
37
- }
38
-
39
35
  /** @type {T[]} */
40
36
  var result = [];
37
+ if (!Array.isArray(tree)) {
38
+ return result;
39
+ }
41
40
  tree.forEach(function (item) {
42
41
  var newItem = item;
43
42
  if (isObject(item)) {
@@ -34,6 +34,9 @@ function findTreeNode(tree, predicate) {
34
34
 
35
35
  /** @type {T|undefined} */
36
36
  var node;
37
+ if (!Array.isArray(tree)) {
38
+ return node;
39
+ }
37
40
  var _iterator = _createForOfIteratorHelper(tree),
38
41
  _step;
39
42
  try {
@@ -37,6 +37,9 @@ function findTreeNodes(tree, predicate) {
37
37
 
38
38
  /** @type {T[]} */
39
39
  var nodes = [];
40
+ if (!Array.isArray(tree)) {
41
+ return nodes;
42
+ }
40
43
  var _iterator = _createForOfIteratorHelper(tree),
41
44
  _step;
42
45
  try {
@@ -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),
@@ -77,25 +77,25 @@ function formatDec(decStr, precision, decimal) {
77
77
  * @alias module:Processor.formatMoney
78
78
  * @since 1.1.0
79
79
  * @param {string | number} num 需转换金额 (最大:9007199254740991 最小: -9007199254740991)
80
- * @param {Object} [options] - 金额格式化配置
81
- * @param {string | number} [options.precision=2] - 保留位数 (最高:10位)
82
- * @param {string} [options.symbol] - 货币符号
83
- * @param {string} [options.thousand=","] - 千分位符号
84
- * @param {string} [options.decimal="."] - 小数位符号
80
+ * @param {Object} [options] 金额格式化配置
81
+ * @param {number} [options.precision=2] 保留位数 (最高:10位)
82
+ * @param {string} [options.symbol] 货币符号
83
+ * @param {string} [options.thousand=","] 千分位符号
84
+ * @param {string} [options.decimal="."] 小数位符号
85
85
  * @returns {string} 格式化的金额
86
86
  * @example
87
87
  *
88
88
  * // 整数
89
- * formatMoney('1000'); // 1,000.00
89
+ * formatMoney(1000); // 1,000.00
90
90
  *
91
91
  * // 小数(默认保留2位小数)
92
- * formatMoney('3000.03'); // 3,000.03
92
+ * formatMoney(3000.03); // 3,000.03
93
93
  *
94
94
  * // 保留4位小数
95
- * formatMoney('3000.0300', { precision: 4 }); // 3,000.0300
95
+ * formatMoney(3000.03, { precision: 4 }); // 3,000.0300
96
96
  *
97
97
  * // 保留10位小数
98
- * formatMoney('1500.2', { precision: 10 }); // 1,500.2000000000
98
+ * formatMoney(1500.2, { precision: 10 }); // 1,500.2000000000
99
99
  *
100
100
  * // 自定义单位符号
101
101
  * formatMoney(1000.00, { symbol: '$' }); // $1,000.00
@@ -106,6 +106,8 @@ function formatDec(decStr, precision, decimal) {
106
106
  * // 自定义小数位分割符(默认'.')
107
107
  * formatMoney(1000.00, { decimal: '&' }); // 1,000&00
108
108
  *
109
+ * // 字符串数字
110
+ * formatMoney('3000.03', { precision: 4 }); // 3,000.0300
109
111
  */
110
112
  var formatMoney = function formatMoney(num) {
111
113
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
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)];
@@ -55,8 +55,7 @@ function transformFieldNames(data, fieldNames, childrenField) {
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;
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.15.0";
18
+ var version = "4.15.2";
19
19
  export { config, setDisableWarning, version };
@@ -13,6 +13,7 @@ exports["default"] = void 0;
13
13
  * @param {number} bytes 字节大小
14
14
  * @param {Object} [options] 配置项
15
15
  * @param {string} [options.spaceMark=' '] 间隔字符
16
+ * @param {number} [options.precision=2] 精度
16
17
  * @returns {string} 存储单位值
17
18
  * @example
18
19
  *
@@ -29,14 +30,16 @@ exports["default"] = void 0;
29
30
  function bytesToSize(bytes) {
30
31
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
31
32
  var _options$spaceMark = options.spaceMark,
32
- spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark;
33
+ spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark,
34
+ _options$precision = options.precision,
35
+ precision = _options$precision === void 0 ? 2 : _options$precision;
33
36
  var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
34
37
  if (numBytes === 0 || isNaN(numBytes)) return "0".concat(spaceMark, "B");
35
38
  var k = 1024;
36
39
  // 存储单位
37
40
  var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
38
41
  var i = Math.floor(Math.log(numBytes) / Math.log(k));
39
- return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(2))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
42
+ return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(precision))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
40
43
  }
41
44
  var _default = bytesToSize;
42
45
  exports["default"] = _default;
package/lib/filterTree.js CHANGED
@@ -40,12 +40,11 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
40
40
  function filterTree(tree, predicate) {
41
41
  var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
42
42
  var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
43
- if (!Array.isArray(tree)) {
44
- return tree;
45
- }
46
-
47
43
  /** @type {T[]} */
48
44
  var result = [];
45
+ if (!Array.isArray(tree)) {
46
+ return result;
47
+ }
49
48
  tree.forEach(function (item) {
50
49
  var newItem = item;
51
50
  if ((0, _type.isObject)(item)) {
@@ -42,6 +42,9 @@ function findTreeNode(tree, predicate) {
42
42
 
43
43
  /** @type {T|undefined} */
44
44
  var node;
45
+ if (!Array.isArray(tree)) {
46
+ return node;
47
+ }
45
48
  var _iterator = _createForOfIteratorHelper(tree),
46
49
  _step;
47
50
  try {
@@ -45,6 +45,9 @@ function findTreeNodes(tree, predicate) {
45
45
 
46
46
  /** @type {T[]} */
47
47
  var nodes = [];
48
+ if (!Array.isArray(tree)) {
49
+ return nodes;
50
+ }
48
51
  var _iterator = _createForOfIteratorHelper(tree),
49
52
  _step;
50
53
  try {
@@ -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),
@@ -88,25 +88,25 @@ function formatDec(decStr, precision, decimal) {
88
88
  * @alias module:Processor.formatMoney
89
89
  * @since 1.1.0
90
90
  * @param {string | number} num 需转换金额 (最大:9007199254740991 最小: -9007199254740991)
91
- * @param {Object} [options] - 金额格式化配置
92
- * @param {string | number} [options.precision=2] - 保留位数 (最高:10位)
93
- * @param {string} [options.symbol] - 货币符号
94
- * @param {string} [options.thousand=","] - 千分位符号
95
- * @param {string} [options.decimal="."] - 小数位符号
91
+ * @param {Object} [options] 金额格式化配置
92
+ * @param {number} [options.precision=2] 保留位数 (最高:10位)
93
+ * @param {string} [options.symbol] 货币符号
94
+ * @param {string} [options.thousand=","] 千分位符号
95
+ * @param {string} [options.decimal="."] 小数位符号
96
96
  * @returns {string} 格式化的金额
97
97
  * @example
98
98
  *
99
99
  * // 整数
100
- * formatMoney('1000'); // 1,000.00
100
+ * formatMoney(1000); // 1,000.00
101
101
  *
102
102
  * // 小数(默认保留2位小数)
103
- * formatMoney('3000.03'); // 3,000.03
103
+ * formatMoney(3000.03); // 3,000.03
104
104
  *
105
105
  * // 保留4位小数
106
- * formatMoney('3000.0300', { precision: 4 }); // 3,000.0300
106
+ * formatMoney(3000.03, { precision: 4 }); // 3,000.0300
107
107
  *
108
108
  * // 保留10位小数
109
- * formatMoney('1500.2', { precision: 10 }); // 1,500.2000000000
109
+ * formatMoney(1500.2, { precision: 10 }); // 1,500.2000000000
110
110
  *
111
111
  * // 自定义单位符号
112
112
  * formatMoney(1000.00, { symbol: '$' }); // $1,000.00
@@ -117,6 +117,8 @@ function formatDec(decStr, precision, decimal) {
117
117
  * // 自定义小数位分割符(默认'.')
118
118
  * formatMoney(1000.00, { decimal: '&' }); // 1,000&00
119
119
  *
120
+ * // 字符串数字
121
+ * formatMoney('3000.03', { precision: 4 }); // 3,000.0300
120
122
  */
121
123
  var formatMoney = function formatMoney(num) {
122
124
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
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)];
@@ -60,8 +60,7 @@ function transformFieldNames(data, fieldNames, childrenField) {
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;
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
  }