util-helpers 4.13.1 → 4.14.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.
Files changed (174) hide show
  1. package/README.md +16 -6
  2. package/dist/util-helpers.js +940 -536
  3. package/dist/util-helpers.js.map +1 -1
  4. package/dist/util-helpers.min.js +1 -1
  5. package/dist/util-helpers.min.js.map +1 -1
  6. package/esm/blobToDataURL.js +2 -4
  7. package/esm/bytesToSize.js +13 -5
  8. package/esm/calculateCursorPosition.js +7 -12
  9. package/esm/dataURLToBlob.js +2 -5
  10. package/esm/divide.js +9 -13
  11. package/esm/findTreeNode.js +66 -0
  12. package/esm/findTreeSelect.js +80 -0
  13. package/esm/formatBankCard.js +11 -11
  14. package/esm/formatMobile.js +11 -14
  15. package/esm/formatMoney.js +24 -35
  16. package/esm/index.js +9 -4
  17. package/esm/isBankCard.js +9 -13
  18. package/esm/isBusinessLicense.js +19 -20
  19. package/esm/isChinese.js +6 -6
  20. package/esm/isEmail.js +3 -3
  21. package/esm/isHMCard.js +3 -3
  22. package/esm/isIPv4.js +3 -3
  23. package/esm/isIPv6.js +3 -3
  24. package/esm/isIdCard.js +9 -17
  25. package/esm/isMobile.js +3 -3
  26. package/esm/isPassport.js +3 -3
  27. package/esm/isPassword.js +6 -7
  28. package/esm/isPostcode.js +3 -3
  29. package/esm/isPromiseLike.js +0 -2
  30. package/esm/isQQ.js +3 -3
  31. package/esm/isSocialCreditCode.js +29 -28
  32. package/esm/isSwiftCode.js +1 -2
  33. package/esm/isTWCard.js +6 -7
  34. package/esm/isTelephone.js +3 -3
  35. package/esm/isUrl.js +7 -3
  36. package/esm/isVehicle.js +3 -3
  37. package/esm/isWX.js +3 -3
  38. package/esm/listToTree.js +125 -0
  39. package/esm/minus.js +7 -11
  40. package/esm/normalizeString.js +1 -3
  41. package/esm/numberToChinese.js +43 -52
  42. package/esm/padZero.js +1 -5
  43. package/esm/parseIdCard.js +27 -23
  44. package/esm/plus.js +7 -11
  45. package/esm/randomString.js +2 -8
  46. package/esm/replaceChar.js +11 -16
  47. package/esm/round.js +4 -5
  48. package/esm/safeDate.js +5 -7
  49. package/esm/setDataURLPrefix.js +0 -1
  50. package/esm/strlen.js +1 -5
  51. package/esm/times.js +7 -11
  52. package/esm/transformFieldNames.doc.js +35 -0
  53. package/esm/transformFieldNames.js +100 -0
  54. package/esm/transformFieldNames.type.js +19 -0
  55. package/esm/treeToList.js +88 -0
  56. package/esm/utils/config.js +4 -4
  57. package/esm/utils/constants.js +2 -2
  58. package/esm/utils/convertToString.js +1 -2
  59. package/esm/utils/devWarn.js +1 -3
  60. package/esm/utils/math.util.js +20 -29
  61. package/esm/utils/type/index.js +1 -1
  62. package/esm/utils/type/isArguments.js +1 -2
  63. package/esm/utils/type/isArray.js +1 -2
  64. package/esm/utils/type/isBoolean.js +1 -2
  65. package/esm/utils/type/isDate.js +1 -2
  66. package/esm/utils/type/isError.js +1 -2
  67. package/esm/utils/type/isFunction.js +1 -2
  68. package/esm/utils/type/isMap.js +1 -2
  69. package/esm/utils/type/isNaN.js +1 -2
  70. package/esm/utils/type/isNil.js +1 -2
  71. package/esm/utils/type/isNull.js +0 -1
  72. package/esm/utils/type/isNumber.js +1 -2
  73. package/esm/utils/type/isObject.js +0 -3
  74. package/esm/utils/type/isRegExp.js +1 -2
  75. package/esm/utils/type/isSet.js +1 -2
  76. package/esm/utils/type/isString.js +1 -2
  77. package/esm/utils/type/isSymbol.js +1 -2
  78. package/esm/utils/type/isType.js +1 -2
  79. package/esm/utils/type/isUndefined.js +0 -1
  80. package/esm/utils/type/isWeakMap.js +1 -2
  81. package/esm/utils/type/isWeakSet.js +1 -2
  82. package/esm/validatePassword.js +27 -50
  83. package/esm/waitTime.js +0 -1
  84. package/lib/blobToDataURL.js +2 -5
  85. package/lib/bytesToSize.js +13 -6
  86. package/lib/calculateCursorPosition.js +4 -13
  87. package/lib/dataURLToBlob.js +2 -6
  88. package/lib/divide.js +8 -22
  89. package/lib/findTreeNode.js +76 -0
  90. package/lib/findTreeSelect.js +86 -0
  91. package/lib/formatBankCard.js +10 -13
  92. package/lib/formatMobile.js +10 -16
  93. package/lib/formatMoney.js +25 -47
  94. package/lib/index.js +115 -126
  95. package/lib/isBankCard.js +7 -15
  96. package/lib/isBusinessLicense.js +17 -22
  97. package/lib/isChinese.js +6 -9
  98. package/lib/isEmail.js +1 -5
  99. package/lib/isHMCard.js +1 -5
  100. package/lib/isIPv4.js +1 -5
  101. package/lib/isIPv6.js +1 -5
  102. package/lib/isIdCard.js +7 -19
  103. package/lib/isMobile.js +1 -5
  104. package/lib/isPassport.js +1 -5
  105. package/lib/isPassword.js +5 -9
  106. package/lib/isPostcode.js +1 -5
  107. package/lib/isPromiseLike.js +1 -4
  108. package/lib/isQQ.js +1 -5
  109. package/lib/isSocialCreditCode.js +27 -30
  110. package/lib/isSwiftCode.js +1 -5
  111. package/lib/isTWCard.js +4 -9
  112. package/lib/isTelephone.js +1 -5
  113. package/lib/isUrl.js +5 -5
  114. package/lib/isVehicle.js +1 -5
  115. package/lib/isWX.js +1 -5
  116. package/lib/listToTree.js +134 -0
  117. package/lib/minus.js +6 -20
  118. package/lib/normalizeString.js +0 -6
  119. package/lib/numberToChinese.js +41 -55
  120. package/lib/padZero.js +0 -7
  121. package/lib/parseIdCard.js +30 -29
  122. package/lib/plus.js +6 -20
  123. package/lib/randomString.js +2 -8
  124. package/lib/replaceChar.js +10 -18
  125. package/lib/round.js +3 -9
  126. package/lib/safeDate.js +4 -13
  127. package/lib/setDataURLPrefix.js +0 -2
  128. package/lib/strlen.js +0 -7
  129. package/lib/times.js +6 -18
  130. package/lib/transformFieldNames.doc.js +42 -0
  131. package/lib/transformFieldNames.js +106 -0
  132. package/lib/transformFieldNames.type.js +5 -0
  133. package/lib/treeToList.js +96 -0
  134. package/lib/utils/config.js +6 -6
  135. package/lib/utils/constants.js +2 -2
  136. package/lib/utils/convertToString.js +0 -4
  137. package/lib/utils/devWarn.js +0 -4
  138. package/lib/utils/math.util.js +23 -45
  139. package/lib/utils/type/index.js +20 -39
  140. package/lib/utils/type/isArguments.js +0 -4
  141. package/lib/utils/type/isArray.js +0 -4
  142. package/lib/utils/type/isBoolean.js +0 -4
  143. package/lib/utils/type/isDate.js +0 -4
  144. package/lib/utils/type/isError.js +0 -4
  145. package/lib/utils/type/isFunction.js +0 -4
  146. package/lib/utils/type/isMap.js +0 -4
  147. package/lib/utils/type/isNaN.js +0 -4
  148. package/lib/utils/type/isNil.js +0 -5
  149. package/lib/utils/type/isNull.js +0 -2
  150. package/lib/utils/type/isNumber.js +0 -4
  151. package/lib/utils/type/isObject.js +1 -5
  152. package/lib/utils/type/isRegExp.js +0 -4
  153. package/lib/utils/type/isSet.js +0 -4
  154. package/lib/utils/type/isString.js +0 -4
  155. package/lib/utils/type/isSymbol.js +0 -4
  156. package/lib/utils/type/isType.js +1 -2
  157. package/lib/utils/type/isUndefined.js +0 -2
  158. package/lib/utils/type/isWeakMap.js +0 -4
  159. package/lib/utils/type/isWeakSet.js +0 -4
  160. package/lib/validatePassword.js +27 -53
  161. package/lib/waitTime.js +0 -2
  162. package/package.json +6 -5
  163. package/types/bytesToSize.d.ts +9 -1
  164. package/types/findTreeNode.d.ts +26 -0
  165. package/types/findTreeSelect.d.ts +26 -0
  166. package/types/formatBankCard.d.ts +3 -3
  167. package/types/formatMobile.d.ts +4 -4
  168. package/types/index.d.ts +5 -0
  169. package/types/isIdCard.d.ts +2 -0
  170. package/types/isUrl.d.ts +2 -0
  171. package/types/listToTree.d.ts +43 -0
  172. package/types/transformFieldNames.d.ts +35 -0
  173. package/types/transformFieldNames.type.d.ts +2 -0
  174. package/types/treeToList.d.ts +20 -0
package/esm/plus.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
2
  import { digitLength, transformEffectiveNumber } from './utils/math.util';
3
3
  import times from './times';
4
+
4
5
  /**
5
6
  * 精确加法,支持多个数相加,加数默认为 0 。
6
7
  *
@@ -16,31 +17,26 @@ import times from './times';
16
17
  * plus(0.1, 0.2, 0.3, 0.4); // 1
17
18
  *
18
19
  */
19
-
20
20
  function plus() {
21
21
  for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
22
22
  nums[_key] = arguments[_key];
23
23
  }
24
-
25
24
  var num1 = nums[0],
26
- _nums$ = nums[1],
27
- num2 = _nums$ === void 0 ? 0 : _nums$,
28
- rest = nums.slice(2);
29
-
25
+ _nums$ = nums[1],
26
+ num2 = _nums$ === void 0 ? 0 : _nums$,
27
+ rest = nums.slice(2);
30
28
  if (rest.length > 0) {
31
29
  return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
32
30
  }
33
-
34
31
  num1 = transformEffectiveNumber(num1);
35
- num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
36
- // @ts-ignore
32
+ num2 = transformEffectiveNumber(num2);
37
33
 
34
+ // 兼容处理,如果参数包含无效数值时,返回 NaN
35
+ // @ts-ignore
38
36
  if (isNaN(num1) || isNaN(num2)) {
39
37
  return Number.NaN;
40
38
  }
41
-
42
39
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
43
40
  return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
44
41
  }
45
-
46
42
  export default plus;
@@ -1,6 +1,7 @@
1
1
  var numberChars = '0123456789';
2
2
  var letterChars = 'abcdefghijklmnopqrstuvwxyz';
3
3
  var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
4
+
4
5
  /**
5
6
  * @private
6
7
  * @param {number} [len=0] 长度
@@ -8,19 +9,17 @@ var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
8
9
  * @param {string} [prefix=''] 前缀部分,不计入长度
9
10
  * @returns {string}
10
11
  */
11
-
12
12
  function internalRandomString() {
13
13
  var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
14
14
  var optionalChars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultChars;
15
15
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
16
-
17
16
  while (len-- > 0) {
18
17
  var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
19
18
  return internalRandomString(len, optionalChars, prefix + r);
20
19
  }
21
-
22
20
  return prefix;
23
21
  }
22
+
24
23
  /**
25
24
  * 生成随机字符串
26
25
  *
@@ -40,19 +39,14 @@ function internalRandomString() {
40
39
  * randomString(8, 'abcefg'); // bcgcfabg
41
40
  *
42
41
  */
43
-
44
-
45
42
  function randomString() {
46
43
  var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
47
44
  var optionalChars = arguments.length > 1 ? arguments[1] : undefined;
48
45
  var realLen = typeof len === 'number' ? len : Number(len);
49
-
50
46
  if (isNaN(realLen)) {
51
47
  realLen = 0;
52
48
  }
53
-
54
49
  var realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
55
50
  return internalRandomString(len, realChars);
56
51
  }
57
-
58
52
  export default randomString;
@@ -1,4 +1,5 @@
1
1
  import normalizeString from './normalizeString';
2
+
2
3
  /**
3
4
  * 替换字符,应用场景如:脱敏
4
5
  *
@@ -37,35 +38,31 @@ import normalizeString from './normalizeString';
37
38
  * replaceChar('林某某某', {start: 1, end: Infinity, repeat: 2}); // 林**
38
39
  *
39
40
  */
40
-
41
41
  function replaceChar(str) {
42
42
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
43
-
44
43
  var _options$char = options["char"],
45
- _char = _options$char === void 0 ? '*' : _options$char,
46
- exclude = options.exclude;
47
-
44
+ _char = _options$char === void 0 ? '*' : _options$char,
45
+ exclude = options.exclude;
48
46
  var _options$start = options.start,
49
- start = _options$start === void 0 ? 3 : _options$start,
50
- _options$end = options.end,
51
- end = _options$end === void 0 ? -4 : _options$end,
52
- repeat = options.repeat;
47
+ start = _options$start === void 0 ? 3 : _options$start,
48
+ _options$end = options.end,
49
+ end = _options$end === void 0 ? -4 : _options$end,
50
+ repeat = options.repeat;
53
51
  var realStr = normalizeString(str);
54
- var strLen = realStr.length; // 开始位置超过str长度
52
+ var strLen = realStr.length;
55
53
 
54
+ // 开始位置超过str长度
56
55
  if (Math.abs(start) >= strLen) {
57
56
  return realStr;
58
57
  }
59
-
60
58
  start = start >= 0 ? start : strLen + start;
61
- end = end >= 0 ? end : strLen + end; // 开始位置大于结束位置
59
+ end = end >= 0 ? end : strLen + end;
62
60
 
61
+ // 开始位置大于结束位置
63
62
  if (start >= end) {
64
63
  return realStr;
65
64
  }
66
-
67
65
  var middleStr = realStr.substring(start, end);
68
-
69
66
  if (exclude) {
70
67
  var reg = new RegExp("[^".concat(exclude, "]"), 'g');
71
68
  middleStr = middleStr.replace(reg, _char);
@@ -73,8 +70,6 @@ function replaceChar(str) {
73
70
  repeat = typeof repeat === 'number' && repeat >= 0 ? repeat : middleStr.length;
74
71
  middleStr = _char.repeat(repeat);
75
72
  }
76
-
77
73
  return realStr.substring(0, start) + middleStr + realStr.substring(end);
78
74
  }
79
-
80
75
  export default replaceChar;
package/esm/round.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import divide from './divide';
2
2
  import times from './times';
3
3
  import { transformEffectiveNumber } from './utils/math.util';
4
+
4
5
  /**
5
6
  * 四舍五入,支持设置精度
6
7
  *
@@ -17,18 +18,16 @@ import { transformEffectiveNumber } from './utils/math.util';
17
18
  * round(4060, -2); // 4100
18
19
  *
19
20
  */
20
-
21
21
  function round(num) {
22
22
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
23
- num = transformEffectiveNumber(num); // 兼容处理,如果参数包含无效数值时,返回 NaN
24
- // @ts-ignore
23
+ num = transformEffectiveNumber(num);
25
24
 
25
+ // 兼容处理,如果参数包含无效数值时,返回 NaN
26
+ // @ts-ignore
26
27
  if (isNaN(num)) {
27
28
  return Number.NaN;
28
29
  }
29
-
30
30
  var base = Math.pow(10, precision);
31
31
  return divide(Math.round(times(num, base)), base);
32
32
  }
33
-
34
33
  export default round;
package/esm/safeDate.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import _construct from "@babel/runtime/helpers/construct";
2
- import isNil from './utils/type/isNil'; // TODO: 函数重载,类型参照 Date
2
+ import isNil from './utils/type/isNil';
3
+
4
+ // TODO: 函数重载,类型参照 Date
3
5
 
4
6
  /**
5
7
  * 创建一个 Date 实例日期对象,同 new Date() 。<br/>
@@ -23,21 +25,17 @@ import isNil from './utils/type/isNil'; // TODO: 函数重载,类型参照 Dat
23
25
  *
24
26
  *
25
27
  */
26
-
27
28
  function safeDate(value) {
28
29
  var safeValue = typeof value === 'string' ? value.replace(/[\\.-]/g, '/') : value;
29
-
30
30
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
31
31
  args[_key - 1] = arguments[_key];
32
32
  }
33
-
34
33
  if (args && args.length > 0) {
35
34
  // @ts-ignore
36
35
  return _construct(Date, [safeValue].concat(args));
37
- } // @ts-ignore
38
-
36
+ }
39
37
 
38
+ // @ts-ignore
40
39
  return isNil(safeValue) ? new Date() : new Date(safeValue);
41
40
  }
42
-
43
41
  export default safeDate;
@@ -27,5 +27,4 @@ function setDataURLPrefix(data) {
27
27
  var base64 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
28
28
  return "data:".concat(mimetype).concat(base64 ? ';base64' : '', ",").concat(data);
29
29
  }
30
-
31
30
  export default setDataURLPrefix;
package/esm/strlen.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import normalizeString from './normalizeString';
2
+
2
3
  /**
3
4
  * 获取字符长度。中文汉字占2个字符,英文占1个字符,特殊如emoji占4个字符。
4
5
  *
@@ -17,22 +18,17 @@ import normalizeString from './normalizeString';
17
18
  * strlen('🍎'); // 4
18
19
  *
19
20
  */
20
-
21
21
  function strlen(str) {
22
22
  var realStr = normalizeString(str);
23
23
  var len = 0;
24
-
25
24
  for (var i = 0; i < realStr.length; i++) {
26
25
  var c = realStr.charCodeAt(i);
27
-
28
26
  if (c >= 0x0001 && c <= 0x007e || 0xff60 <= c && c <= 0xff9f) {
29
27
  len++;
30
28
  } else {
31
29
  len += 2;
32
30
  }
33
31
  }
34
-
35
32
  return len;
36
33
  }
37
-
38
34
  export default strlen;
package/esm/times.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
2
  import { digitLength, float2Fixed, checkBoundary, transformEffectiveNumber } from './utils/math.util';
3
+
3
4
  /**
4
5
  * 精确乘法,支持多个数相乘,乘数默认为 1 。
5
6
  *
@@ -15,29 +16,25 @@ import { digitLength, float2Fixed, checkBoundary, transformEffectiveNumber } fro
15
16
  * times(3, 0.6, 2, 10); // 36
16
17
  *
17
18
  */
18
-
19
19
  function times() {
20
20
  for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
21
21
  nums[_key] = arguments[_key];
22
22
  }
23
-
24
23
  var num1 = nums[0],
25
- _nums$ = nums[1],
26
- num2 = _nums$ === void 0 ? 1 : _nums$,
27
- rest = nums.slice(2);
28
-
24
+ _nums$ = nums[1],
25
+ num2 = _nums$ === void 0 ? 1 : _nums$,
26
+ rest = nums.slice(2);
29
27
  if (rest.length > 0) {
30
28
  return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
31
29
  }
32
-
33
30
  num1 = transformEffectiveNumber(num1);
34
- num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
35
- // @ts-ignore
31
+ num2 = transformEffectiveNumber(num2);
36
32
 
33
+ // 兼容处理,如果参数包含无效数值时,返回 NaN
34
+ // @ts-ignore
37
35
  if (isNaN(num1) || isNaN(num2)) {
38
36
  return Number.NaN;
39
37
  }
40
-
41
38
  var num1Changed = float2Fixed(num1);
42
39
  var num2Changed = float2Fixed(num2);
43
40
  var baseNum = digitLength(num1) + digitLength(num2);
@@ -45,5 +42,4 @@ function times() {
45
42
  checkBoundary(leftValue);
46
43
  return leftValue / Math.pow(10, baseNum);
47
44
  }
48
-
49
45
  export default times;
@@ -0,0 +1,35 @@
1
+ /* eslint-disable no-unused-vars */
2
+ // 该文件用于 jsdoc 生成文件。因为一些 typescript 语法, jsdoc 不支持,导致生成文档报错。
3
+
4
+ /**
5
+ * 转换字段名,返回一个转换字段后的值,不改变原值。
6
+ *
7
+ * @static
8
+ * @alias module:Processor.transformFieldNames
9
+ * @since 4.14.0
10
+ * @param {object[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
11
+ * @param {object} fieldNames 字段名映射
12
+ * @param {string} [childrenFieldName] 子级数据字段名
13
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
14
+ * @returns {object[]}
15
+ * @example
16
+ *
17
+ * const options = [{code: '1', name: 'one'},{code:'2', name:'two'}];
18
+ * const newOptions = transformFieldNames(options, {label: 'name', value: 'code'});
19
+ * // [{value: '1', label: 'one'},{value:'2', label:'two'}]
20
+ *
21
+ * // 嵌套数据,指定子级字段名 children
22
+ * const options2 = [{code: '1', name: 'one'},{code:'2', name:'two', children: [{code:'2-1', name:'two-one', children: [{code: '2-1-1', name:'two-one-one'}]}]}];
23
+ * const newOptions2 = transformFieldNames(options2, {label: 'name', value: 'code'}, 'children');
24
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one', children: [{value: '2-1-1', label:'two-one-one'}]}]}]
25
+ *
26
+ * const options3 = [{code: '1', name: 'one'},{code:'2', name:'two', childs: [{code:'2-1', name:'two-one'}]}];
27
+ * const newOptions3 = transformFieldNames(options3, {label: 'name', value: 'code'}, 'childs');
28
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', childs: [{value: '2-1', label:'two-one'}]}]
29
+ *
30
+ * // 嵌套数据,并替换子集字段名
31
+ * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
32
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
33
+ */
34
+ function transformFieldNames(data, fieldNames, childrenFieldName) {}
35
+ export default transformFieldNames;
@@ -0,0 +1,100 @@
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
+ // 修改类型,请同步到 transformFieldNames.type.js
5
+ // 修改文档,请同步到 transformFieldNames.doc.js
6
+
7
+ import { isObject } from './utils/type';
8
+
9
+ /**
10
+ * 转换字段名,返回一个转换字段后的值,不改变原值。
11
+ *
12
+ * @static
13
+ * @alias module:Processor.transformFieldNames
14
+ * @since 4.14.0
15
+ * @template {*} D
16
+ * @template {Record<string, keyof D>} F
17
+ * @template {string} C
18
+ * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
19
+ * @param {F} fieldNames 字段名映射
20
+ * @param {C} [childrenFieldName] 子级数据字段名
21
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
22
+ * @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
23
+ * @example
24
+ *
25
+ * const options = [{code: '1', name: 'one'},{code:'2', name:'two'}];
26
+ * const newOptions = transformFieldNames(options, {label: 'name', value: 'code'});
27
+ * // [{value: '1', label: 'one'},{value:'2', label:'two'}]
28
+ *
29
+ * // 嵌套数据,指定子级字段名 children
30
+ * const options2 = [{code: '1', name: 'one'},{code:'2', name:'two', children: [{code:'2-1', name:'two-one', children: [{code: '2-1-1', name:'two-one-one'}]}]}];
31
+ * const newOptions2 = transformFieldNames(options2, {label: 'name', value: 'code'}, 'children');
32
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one', children: [{value: '2-1-1', label:'two-one-one'}]}]}]
33
+ *
34
+ * const options3 = [{code: '1', name: 'one'},{code:'2', name:'two', childs: [{code:'2-1', name:'two-one'}]}];
35
+ * const newOptions3 = transformFieldNames(options3, {label: 'name', value: 'code'}, 'childs');
36
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', childs: [{value: '2-1', label:'two-one'}]}]
37
+ *
38
+ * // 嵌套数据,并替换子集字段名
39
+ * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
40
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
41
+ */
42
+ function transformFieldNames(data, fieldNames, childrenFieldName) {
43
+ var nodeAssign = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'spread';
44
+ if (!Array.isArray(data)) {
45
+ return data;
46
+ }
47
+ if (data.length <= 0) {
48
+ // @ts-ignore
49
+ return [];
50
+ }
51
+
52
+ /**
53
+ * 递归处理字段名
54
+ *
55
+ * @param {Array.<object>} arr 列表数据
56
+ * @returns {*}
57
+ */
58
+ function recusion() {
59
+ var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
60
+ return arr.map(function (item) {
61
+ if (!isObject(item)) {
62
+ return item;
63
+ }
64
+ var newItem = nodeAssign === 'spread' ? _objectSpread({}, item) : item;
65
+ /** @type {Array.<string>} */
66
+ var delKeys = [];
67
+
68
+ // 树形数据子节点
69
+ // @ts-ignore
70
+ if (childrenFieldName && Array.isArray(newItem[childrenFieldName]) && newItem[childrenFieldName].length > 0) {
71
+ // @ts-ignore
72
+ newItem[childrenFieldName] = recusion(newItem[childrenFieldName].slice());
73
+ }
74
+
75
+ // 替换字段名
76
+ Object.keys(fieldNames).forEach(function (newKey) {
77
+ var oldKey = fieldNames[newKey];
78
+ if (oldKey in newItem) {
79
+ // @ts-ignore
80
+ newItem[newKey] = newItem[oldKey];
81
+ // @ts-ignore
82
+ delKeys.push(oldKey);
83
+ }
84
+ });
85
+
86
+ // 删除旧字段
87
+ if (delKeys.length > 0) {
88
+ delKeys.forEach(function (delKey) {
89
+ // @ts-ignore
90
+ delete newItem[delKey];
91
+ });
92
+ }
93
+ return newItem;
94
+ });
95
+ }
96
+
97
+ // @ts-ignore
98
+ return recusion(data.slice());
99
+ }
100
+ export default transformFieldNames;
@@ -0,0 +1,19 @@
1
+ // @ts-nocheck
2
+
3
+ // 交换字段名
4
+ /**
5
+ * @template {*} D
6
+ * @template {Record<string, keyof D>} F
7
+ * @typedef {Omit<D, F[keyof F]> & { [P in keyof F]: D[F[P]] }} ExchangeFieldNames
8
+ */
9
+
10
+ // 交换字段名,支持嵌套
11
+ // 先排除子级字段名,再交换字段名,然后加上子级字段名,再替换一次。这里的 F 有类型问题,因为排除了子级字段名,暂时没有比较好的方案处理。
12
+ /**
13
+ * @template {*} D
14
+ * @template {Record<string, keyof D>} F
15
+ * @template {string} C
16
+ * @typedef {(C extends keyof D ? ExchangeFieldNames<Omit<D, C> & Record<C, TransformFieldNames<D, F, C>>, F> : ExchangeFieldNames<D, F>)[]} TransformFieldNames
17
+ */
18
+
19
+ export {};
@@ -0,0 +1,88 @@
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
+ * 树结构转列表数据
8
+ *
9
+ * @static
10
+ * @alias module:Processor.treeToList
11
+ * @since 4.14.0
12
+ * @template {Record<string,any>} T
13
+ * @template {keyof T} K
14
+ * @template {Omit<T, K>} R
15
+ * @param {T[]} tree 列表数据
16
+ * @param {K} childrenField 子级字段名称
17
+ * @returns {R[]} 列表数据
18
+ * @example
19
+ * 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" }] }];
20
+ *
21
+ * treeToList(menus, 'children'));
22
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"2","name":"交易管理","code":"trade","pid":null},{"id":"3","name":"交易查询","code":"trade-1","pid":"2"},{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"},{"id":"5","name":"权限管理","code":"authorization","pid":null},{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
23
+ */
24
+ function treeToList(tree, childrenField) {
25
+ /** @type {R[]} */
26
+ var list = [];
27
+
28
+ /**
29
+ * 递归遍历
30
+ * @param {T[]} arr 列表数据
31
+ */
32
+ function recusion(arr) {
33
+ arr.forEach(function (item) {
34
+ if (isObject(item)) {
35
+ var newItem = _objectSpread({}, item);
36
+ // @ts-ignore
37
+ list.push(newItem);
38
+ if (newItem !== null && newItem !== void 0 && newItem[childrenField]) {
39
+ if (Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
40
+ recusion(newItem[childrenField]);
41
+ }
42
+ delete newItem[childrenField];
43
+ }
44
+ } else {
45
+ // @ts-ignore
46
+ list.push(item);
47
+ }
48
+ });
49
+ }
50
+ recusion(tree);
51
+ return list;
52
+ }
53
+
54
+ // function treeToList(tree, childrenField) {
55
+ // /** @type {R[]} */
56
+ // const list = [];
57
+
58
+ // // 深度优先
59
+ // /** @type {T[]} */
60
+ // const stack = [];
61
+ // for (const item of tree) {
62
+ // if (isObject(item)) {
63
+ // stack.push(item);
64
+
65
+ // while (stack.length) {
66
+ // /** @type {T} */
67
+ // // @ts-ignore
68
+ // const temp = stack.shift();
69
+
70
+ // // @ts-ignore
71
+ // list.push(temp);
72
+
73
+ // if (temp[childrenField]) {
74
+ // if (Array.isArray(temp[childrenField]) && temp[childrenField].length > 0) {
75
+ // stack.push(...temp[childrenField]);
76
+ // }
77
+ // delete temp[childrenField];
78
+ // }
79
+ // }
80
+ // } else {
81
+ // // @ts-ignore
82
+ // list.push(item);
83
+ // }
84
+ // }
85
+ // return list;
86
+ // }
87
+
88
+ export default treeToList;
@@ -2,6 +2,7 @@ var config = {
2
2
  // 禁用warning提示
3
3
  disableWarning: true
4
4
  };
5
+
5
6
  /**
6
7
  * 设置禁止warning提示
7
8
  * @static
@@ -9,11 +10,10 @@ var config = {
9
10
  * @since 3.6.1
10
11
  * @param {boolean} bool 是否禁止warning提示
11
12
  */
12
-
13
13
  function setDisableWarning(bool) {
14
14
  config.disableWarning = !!bool;
15
- } // eslint-disable-next-line no-undef
16
-
15
+ }
17
16
 
18
- var version = "4.13.1";
17
+ // eslint-disable-next-line no-undef
18
+ var version = "4.14.1";
19
19
  export { config, setDisableWarning, version };
@@ -1,4 +1,4 @@
1
1
  // 最大安全数字
2
- export var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; // 最小安全数字
3
-
2
+ export var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
3
+ // 最小安全数字
4
4
  export var MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -9007199254740991;
@@ -1,13 +1,12 @@
1
1
  import isString from './type/isString';
2
+
2
3
  /**
3
4
  * 转换为字符串
4
5
  *
5
6
  * @param {*} value 值
6
7
  * @returns {string} 字符串
7
8
  */
8
-
9
9
  function convertToString(value) {
10
10
  return isString(value) ? value : String(value);
11
11
  }
12
-
13
12
  export default convertToString;
@@ -1,16 +1,14 @@
1
1
  import { config } from './config';
2
+
2
3
  /**
3
4
  * 打印警告信息
4
5
  *
5
6
  * @param {any[]} args 打印的信息
6
7
  */
7
-
8
8
  function devWarn() {
9
9
  if (!config.disableWarning) {
10
10
  var _console;
11
-
12
11
  (_console = console).warn.apply(_console, arguments);
13
12
  }
14
13
  }
15
-
16
14
  export default devWarn;