util-helpers 4.13.1 → 4.14.0

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 +935 -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 +122 -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 +34 -0
  53. package/esm/transformFieldNames.js +98 -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 +131 -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 +41 -0
  131. package/lib/transformFieldNames.js +104 -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 +41 -0
  172. package/types/transformFieldNames.d.ts +34 -0
  173. package/types/transformFieldNames.type.d.ts +2 -0
  174. package/types/treeToList.d.ts +20 -0
@@ -28,16 +28,14 @@
28
28
  function blobToDataURL(blob) {
29
29
  return new Promise(function (resolve, reject) {
30
30
  var reader = new FileReader();
31
- reader.readAsDataURL(blob); // @ts-ignore
32
-
31
+ reader.readAsDataURL(blob);
32
+ // @ts-ignore
33
33
  reader.onload = function () {
34
34
  return resolve(reader.result);
35
35
  };
36
-
37
36
  reader.onerror = function (error) {
38
37
  return reject(error);
39
38
  };
40
39
  });
41
40
  }
42
-
43
41
  export default blobToDataURL;
@@ -5,6 +5,8 @@
5
5
  * @alias module:Processor.bytesToSize
6
6
  * @since 3.8.0
7
7
  * @param {number} bytes 字节大小
8
+ * @param {Object} [options] 配置项
9
+ * @param {string} [options.spaceMark=' '] 间隔字符
8
10
  * @returns {string} 存储单位值
9
11
  * @example
10
12
  *
@@ -13,15 +15,21 @@
13
15
  * bytesToSize(3.213243*1024*1024); // 3.21 MB
14
16
  * bytesToSize(1024*1024*1024); // 1 GB
15
17
  *
18
+ * // 自定义间隔符号
19
+ * bytesToSize(0, { spaceMark: '' }); // 0B
20
+ * bytesToSize(1024, { spaceMark: '' }); // 1KB
21
+ *
16
22
  */
17
23
  function bytesToSize(bytes) {
24
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
25
+ var _options$spaceMark = options.spaceMark,
26
+ spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark;
18
27
  var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
19
- if (numBytes === 0 || isNaN(numBytes)) return '0 B';
20
- var k = 1024; // 存储单位
21
-
28
+ if (numBytes === 0 || isNaN(numBytes)) return "0".concat(spaceMark, "B");
29
+ var k = 1024;
30
+ // 存储单位
22
31
  var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
23
32
  var i = Math.floor(Math.log(numBytes) / Math.log(k));
24
- return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(2)), " ").concat(sizes[i]) : numBytes + '';
33
+ return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(2))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
25
34
  }
26
-
27
35
  export default bytesToSize;
@@ -1,4 +1,6 @@
1
- import normalizeString from './normalizeString'; // 参考了: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
1
+ import normalizeString from './normalizeString';
2
+
3
+ // 参考了: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
2
4
 
3
5
  /**
4
6
  * 计算输入框的值格式化后光标位置
@@ -20,27 +22,24 @@ import normalizeString from './normalizeString'; // 参考了: https://github.co
20
22
  * @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
21
23
  * @returns {number} 格式化后的光标位置
22
24
  */
23
-
24
25
  function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
25
26
  var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
26
27
  var _options$placeholderC = options.placeholderChar,
27
- placeholderChar = _options$placeholderC === void 0 ? ' ' : _options$placeholderC,
28
- _options$maskReg = options.maskReg,
29
- maskReg = _options$maskReg === void 0 ? /\D/g : _options$maskReg,
30
- type = options.type;
28
+ placeholderChar = _options$placeholderC === void 0 ? ' ' : _options$placeholderC,
29
+ _options$maskReg = options.maskReg,
30
+ maskReg = _options$maskReg === void 0 ? /\D/g : _options$maskReg,
31
+ type = options.type;
31
32
  var realCtrlValue = normalizeString(prevCtrlValue);
32
33
  var realRawValue = normalizeString(rawValue);
33
34
  var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
34
35
  var editLength = realRawValue.length - realCtrlValue.length;
35
36
  var isAddition = editLength > 0;
36
37
  var pos = prevPos;
37
-
38
38
  if (isAddition) {
39
39
  var additionStr = realRawValue.substring(pos - editLength, pos);
40
40
  var ctrlCharCount = additionStr.replace(maskReg, '').length;
41
41
  pos -= editLength - ctrlCharCount;
42
42
  var placeholderCharCount = 0;
43
-
44
43
  while (ctrlCharCount > 0) {
45
44
  if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
46
45
  placeholderCharCount++;
@@ -48,15 +47,11 @@ function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
48
47
  ctrlCharCount--;
49
48
  }
50
49
  }
51
-
52
50
  pos += placeholderCharCount;
53
51
  }
54
-
55
52
  if (type === 'mobile' && (pos === 4 || pos === 9) || type === 'bankCard' && pos > 0 && pos % 5 === 0) {
56
53
  pos -= 1;
57
54
  }
58
-
59
55
  return pos;
60
56
  }
61
-
62
57
  export default calculateCursorPosition;
@@ -12,20 +12,17 @@
12
12
  * dataURLToBlob(dataurl); // Blob {size: 32, type: 'text/html'}
13
13
  */
14
14
  function dataURLToBlob(dataurl) {
15
- var arr = dataurl.split(','); // @ts-ignore
16
-
15
+ var arr = dataurl.split(',');
16
+ // @ts-ignore
17
17
  var mime = arr[0].match(/:(.*?);/)[1];
18
18
  var bstr = atob(arr[1]);
19
19
  var n = bstr.length;
20
20
  var u8arr = new Uint8Array(n);
21
-
22
21
  while (n--) {
23
22
  u8arr[n] = bstr.charCodeAt(n);
24
23
  }
25
-
26
24
  return new Blob([u8arr], {
27
25
  type: mime
28
26
  });
29
27
  }
30
-
31
28
  export default dataURLToBlob;
package/esm/divide.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
2
  import { strip, digitLength, float2Fixed, checkBoundary, transformEffectiveNumber } from './utils/math.util';
3
3
  import times from './times';
4
+
4
5
  /**
5
6
  * 精确除法,支持多个数相除,除数默认为 1 。
6
7
  *
@@ -21,35 +22,30 @@ import times from './times';
21
22
  * divide('1.5 ', 0.5); // 3 Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3
22
23
  *
23
24
  */
24
-
25
25
  function divide() {
26
26
  for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
27
27
  nums[_key] = arguments[_key];
28
28
  }
29
-
30
29
  var num1 = nums[0],
31
- _nums$ = nums[1],
32
- num2 = _nums$ === void 0 ? 1 : _nums$,
33
- rest = nums.slice(2);
34
-
30
+ _nums$ = nums[1],
31
+ num2 = _nums$ === void 0 ? 1 : _nums$,
32
+ rest = nums.slice(2);
35
33
  if (rest.length > 0) {
36
34
  return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
37
35
  }
38
-
39
36
  num1 = transformEffectiveNumber(num1);
40
- num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
41
- // @ts-ignore
37
+ num2 = transformEffectiveNumber(num2);
42
38
 
39
+ // 兼容处理,如果参数包含无效数值时,返回 NaN
40
+ // @ts-ignore
43
41
  if (isNaN(num1) || isNaN(num2)) {
44
42
  return Number.NaN;
45
43
  }
46
-
47
44
  var num1Changed = float2Fixed(num1);
48
45
  var num2Changed = float2Fixed(num2);
49
46
  checkBoundary(num1Changed);
50
- checkBoundary(num2Changed); // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
51
-
47
+ checkBoundary(num2Changed);
48
+ // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
52
49
  return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
53
50
  }
54
-
55
51
  export default divide;
@@ -0,0 +1,66 @@
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
+ /**
6
+ * 查找树结构数据节点
7
+ *
8
+ * @static
9
+ * @alias module:Other.findTreeNode
10
+ * @since 4.14.0
11
+ * @template {any} T
12
+ * @template {(item: T) => boolean} F
13
+ * @param {T[]} tree 树结构数据
14
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回该节点
15
+ * @param {string} [childrenField='children'] 子级字段名
16
+ * @returns {T|undefined}
17
+ * @example
18
+ * 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" }] }];
19
+ *
20
+ * findTreeNode(menus, item=>item.id === '2');
21
+ * // {"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]}
22
+ *
23
+ * findTreeNode(menus, item=>item.id === '7');
24
+ * // {"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}
25
+ *
26
+ * findTreeNode(menus, item=>item.id === 'not found');
27
+ * // undefined
28
+ */
29
+ function findTreeNode(tree, predicate) {
30
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
31
+ var stack = [];
32
+
33
+ /** @type {T|undefined} */
34
+ var node;
35
+ var _iterator = _createForOfIteratorHelper(tree),
36
+ _step;
37
+ try {
38
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
39
+ var item = _step.value;
40
+ stack.push(item);
41
+ while (stack.length) {
42
+ /** @type {T} */
43
+ // @ts-ignore
44
+ var temp = stack.pop();
45
+ if (predicate(temp)) {
46
+ node = temp;
47
+ break;
48
+ }
49
+
50
+ /** @type {T[]} */
51
+ // @ts-ignore
52
+ var children = temp[childrenField] || [];
53
+ stack.push.apply(stack, _toConsumableArray(children));
54
+ }
55
+ if (node) {
56
+ break;
57
+ }
58
+ }
59
+ } catch (err) {
60
+ _iterator.e(err);
61
+ } finally {
62
+ _iterator.f();
63
+ }
64
+ return node;
65
+ }
66
+ export default findTreeNode;
@@ -0,0 +1,80 @@
1
+ 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; } } }; }
2
+ 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); }
3
+ 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; }
4
+ import { isObject } from "./utils/type";
5
+
6
+ /**
7
+ * 内部实现
8
+ *
9
+ * @private
10
+ * @template {any} T
11
+ * @template {(item: T) => boolean} F
12
+ * @param {T[]} tree 树结构数据
13
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
14
+ * @param {string} [childrenField='children'] 子级字段名
15
+ * @param {T[]} [path=[]] 当前遍历路径
16
+ * @returns {T[]}
17
+ */
18
+ function internalFindTreeSelect(tree, predicate) {
19
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
20
+ var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
21
+ if (!tree) {
22
+ return [];
23
+ }
24
+ var _iterator = _createForOfIteratorHelper(tree),
25
+ _step;
26
+ try {
27
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
28
+ var item = _step.value;
29
+ path.push(item);
30
+ if (predicate(item)) {
31
+ return path;
32
+ }
33
+
34
+ // @ts-ignore
35
+ if (isObject(item) && Array.isArray(item[childrenField]) && item[childrenField].length > 0) {
36
+ // @ts-ignore
37
+ var findChildren = internalFindTreeSelect(item[childrenField], predicate, childrenField, path);
38
+ if (findChildren.length > 0) {
39
+ return findChildren;
40
+ }
41
+ }
42
+ path.pop();
43
+ }
44
+ } catch (err) {
45
+ _iterator.e(err);
46
+ } finally {
47
+ _iterator.f();
48
+ }
49
+ return [];
50
+ }
51
+
52
+ /**
53
+ * 查找包含当前节点的所有父级节点
54
+ *
55
+ * @static
56
+ * @alias module:Other.findTreeSelect
57
+ * @since 4.14.0
58
+ * @template {any} T
59
+ * @template {(item: T) => boolean} F
60
+ * @param {T[]} tree 树结构数据
61
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
62
+ * @param {string} [childrenField='children'] 子级字段名
63
+ * @returns {T[]}
64
+ * @example
65
+ * 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" }] }];
66
+ *
67
+ * findTreeSelect(menus, item => item.id === '2');
68
+ * // [{"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"}]}]}]
69
+ *
70
+ * findTreeSelect(menus, item => item.id === '7');
71
+ * // [{"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"}]
72
+ *
73
+ * findTreeSelect(menus, item => item.id === 'not found');
74
+ * // []
75
+ */
76
+ function findTreeSelect(tree, predicate) {
77
+ var childrenField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
78
+ return internalFindTreeSelect(tree, predicate, childrenField);
79
+ }
80
+ export default findTreeSelect;
@@ -1,4 +1,5 @@
1
1
  import normalizeString from './normalizeString';
2
+
2
3
  /**
3
4
  * 格式化银行卡号
4
5
  *
@@ -7,7 +8,7 @@ import normalizeString from './normalizeString';
7
8
  * @since 1.1.0
8
9
  * @param {string} bankCardNo 要处理的字符串
9
10
  * @param {Object} [options] 配置项
10
- * @param {string} [options.char=" "] 间隔字符
11
+ * @param {string} [options.spaceMark=' '] 间隔字符
11
12
  * @param {number} [options.length=4] 间隔长度
12
13
  * @returns {string} 格式化的银行卡号
13
14
  * @example
@@ -22,24 +23,23 @@ import normalizeString from './normalizeString';
22
23
  * formatBankCard('6228********890'); // 6228 **** **** 890
23
24
  *
24
25
  * // 16位银行卡,"-"间隔
25
- * formatBankCard('6228480402564890', {char: '-'}); // 6228-4804-0256-4890
26
+ * formatBankCard('6228480402564890', {spaceMark: '-'}); // 6228-4804-0256-4890
26
27
  *
27
28
  */
28
-
29
29
  function formatBankCard() {
30
30
  var bankCardNo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
31
31
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
32
-
32
+ // @ts-ignore
33
+ // TODO 下个版本废弃 char
33
34
  var _options$char = options["char"],
34
- _char = _options$char === void 0 ? ' ' : _options$char,
35
- _options$length = options.length,
36
- length = _options$length === void 0 ? 4 : _options$length;
37
-
35
+ _char = _options$char === void 0 ? ' ' : _options$char,
36
+ _options$length = options.length,
37
+ length = _options$length === void 0 ? 4 : _options$length;
38
+ var realSpaceMark = 'spaceMark' in options ? options.spaceMark : _char;
38
39
  var reg = new RegExp("(.{".concat(length, "})"), 'g');
39
- var regChar = new RegExp("".concat(_char), 'g');
40
+ var regChar = new RegExp("".concat(realSpaceMark), 'g');
40
41
  var realValue = normalizeString(bankCardNo).replace(regChar, '');
41
- var str = realValue.replace(reg, "$1".concat(_char));
42
+ var str = realValue.replace(reg, "$1".concat(realSpaceMark));
42
43
  return realValue.length % length === 0 ? str.substring(0, str.length - 1) : str;
43
44
  }
44
-
45
45
  export default formatBankCard;
@@ -1,4 +1,5 @@
1
1
  import normalizeString from './normalizeString';
2
+
2
3
  /**
3
4
  * 格式化手机号码
4
5
  *
@@ -7,16 +8,16 @@ import normalizeString from './normalizeString';
7
8
  * @since 4.5.0
8
9
  * @param {string} mobileNo 手机号码
9
10
  * @param {object} [options] 配置项
10
- * @param {string} [options.char=' '] 间隔字符
11
+ * @param {string} [options.spaceMark=' '] 间隔字符
11
12
  * @returns {string} 格式化后的手机号码
12
13
  * @example
13
14
  *
14
15
  * formatMobile('13345678900'); // 133 4567 8900
15
- * formatMobile('13345678900', { char: '-' }); // 133-4567-8900
16
+ * formatMobile('13345678900', { spaceMark: '-' }); // 133-4567-8900
16
17
  *
17
18
  * // 脱敏手机号码
18
19
  * formatMobile('133****1234'); // 133 **** 1234
19
- * formatMobile('133****1234', { char: '-' }); // 133-****-1234
20
+ * formatMobile('133****1234', { spaceMark: '-' }); // 133-****-1234
20
21
  *
21
22
  * // 手机号码位数不够
22
23
  * formatMobile('133'); // 133
@@ -24,25 +25,21 @@ import normalizeString from './normalizeString';
24
25
  * formatMobile('13345678'); // 133 4567 8
25
26
  *
26
27
  */
27
-
28
28
  function formatMobile(mobileNo) {
29
29
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
30
-
30
+ // @ts-ignore
31
+ // TODO 下个版本废弃 char
31
32
  var _options$char = options["char"],
32
- _char = _options$char === void 0 ? ' ' : _options$char;
33
-
34
- var regChar = new RegExp(_char, 'g');
33
+ _char = _options$char === void 0 ? ' ' : _options$char;
34
+ var realSpaceMark = 'spaceMark' in options ? options.spaceMark : _char;
35
+ var regChar = new RegExp(realSpaceMark, 'g');
35
36
  var realValue = normalizeString(mobileNo).replace(regChar, '').substring(0, 11);
36
-
37
37
  if (realValue.length > 7) {
38
- return realValue.replace(/^(...)(....)/g, "$1".concat(_char, "$2").concat(_char));
38
+ return realValue.replace(/^(...)(....)/g, "$1".concat(realSpaceMark, "$2").concat(realSpaceMark));
39
39
  }
40
-
41
40
  if (realValue.length > 3) {
42
- return realValue.replace(/^(...)/g, "$1".concat(_char));
41
+ return realValue.replace(/^(...)/g, "$1".concat(realSpaceMark));
43
42
  }
44
-
45
43
  return realValue;
46
44
  }
47
-
48
45
  export default formatMobile;
@@ -4,6 +4,7 @@ import isNaN from './utils/type/isNaN';
4
4
  import { trimLeftZero } from './utils/math.util';
5
5
  import devWarn from './utils/devWarn';
6
6
  var reg = /^[+-]?\d*\.?\d*$/;
7
+
7
8
  /**
8
9
  * 检查数字或数字字符串
9
10
  *
@@ -11,21 +12,20 @@ var reg = /^[+-]?\d*\.?\d*$/;
11
12
  * @param {string} num
12
13
  * @returns 是否为数字
13
14
  */
14
-
15
15
  function checkNumber(num) {
16
16
  if (!(reg.test(num) || isScientificNumber(num)) || isNaN(num) || typeof num !== 'number' && typeof num !== 'string' || num === '') {
17
17
  devWarn("".concat(num, " invalid parameter."));
18
18
  return false;
19
- } // 数字超限如果不是是字符串,可能有异常
20
- // 如 1111111111111111111111 // => 1.1111111111111111e+21
21
-
19
+ }
22
20
 
21
+ // 数字超限如果不是是字符串,可能有异常
22
+ // 如 1111111111111111111111 // => 1.1111111111111111e+21
23
23
  if (typeof num === 'number') {
24
24
  checkBoundary(num);
25
25
  }
26
-
27
26
  return true;
28
27
  }
28
+
29
29
  /**
30
30
  * 格式化整数部分
31
31
  *
@@ -34,22 +34,18 @@ function checkNumber(num) {
34
34
  * @param {string} thousand 千分位符号
35
35
  * @returns 格式化后的值
36
36
  */
37
-
38
-
39
37
  function formatInt(intStr, thousand) {
40
38
  var txt = '';
41
39
  intStr = trimLeftZero(intStr);
42
40
  intStr = intStr[0] === '+' ? intStr.substring(1) : intStr; // 去掉+符号
43
-
44
41
  var negativeSymbol = Number(intStr) < 0 ? '-' : '';
45
42
  var reArr = negativeSymbol ? intStr.substring(1).split('').reverse() : intStr.split('').reverse();
46
-
47
43
  for (var i = 0; i < reArr.length; i++) {
48
44
  txt += reArr[i] + ((i + 1) % 3 === 0 && i + 1 !== reArr.length ? thousand : '');
49
45
  }
50
-
51
46
  return negativeSymbol + txt.split('').reverse().join('');
52
47
  }
48
+
53
49
  /**
54
50
  * 格式化小数部分,如果使用 toFixed,超大额数字会自动被截断
55
51
  *
@@ -59,25 +55,21 @@ function formatInt(intStr, thousand) {
59
55
  * @param {string} decimal 小数点符号
60
56
  * @returns 格式化后的值
61
57
  */
62
-
63
-
64
58
  function formatDec(decStr, precision, decimal) {
65
59
  if (precision === 0) {
66
60
  return '';
67
61
  }
68
-
69
62
  var zero = 0;
70
63
  var ret = '';
71
-
72
64
  if (decStr && Number(decStr) > 0) {
73
65
  var tmpNum = parseFloat('0.' + decStr);
74
66
  ret = tmpNum.toFixed(precision).substring(2);
75
67
  } else {
76
68
  ret = zero.toFixed(precision).substring(2);
77
69
  }
78
-
79
70
  return decimal + ret;
80
71
  }
72
+
81
73
  /**
82
74
  * 格式化金额
83
75
  *
@@ -115,42 +107,39 @@ function formatDec(decStr, precision, decimal) {
115
107
  * formatMoney(1000.00, { decimal: '&' }); // 1,000&00
116
108
  *
117
109
  */
118
-
119
-
120
110
  var formatMoney = function formatMoney(num) {
121
111
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
122
112
  var _options$precision = options.precision,
123
- precision = _options$precision === void 0 ? 2 : _options$precision,
124
- symbol = options.symbol,
125
- _options$thousand = options.thousand,
126
- thousand = _options$thousand === void 0 ? ',' : _options$thousand,
127
- _options$decimal = options.decimal,
128
- decimal = _options$decimal === void 0 ? '.' : _options$decimal; // 数字参数不正确,返回空字符串
113
+ precision = _options$precision === void 0 ? 2 : _options$precision,
114
+ symbol = options.symbol,
115
+ _options$thousand = options.thousand,
116
+ thousand = _options$thousand === void 0 ? ',' : _options$thousand,
117
+ _options$decimal = options.decimal,
118
+ decimal = _options$decimal === void 0 ? '.' : _options$decimal;
119
+
120
+ // 数字参数不正确,返回空字符串
129
121
  // @ts-ignore
130
-
131
122
  if (!checkNumber(num)) {
132
123
  return '';
133
- } // 参数规整化
134
-
124
+ }
135
125
 
126
+ // 参数规整化
136
127
  if (typeof precision !== 'number' || isNaN(precision) || precision < 0) {
137
128
  precision = 2;
138
129
  } else if (precision > 10) {
139
130
  precision = 10;
140
131
  }
141
-
142
132
  symbol = typeof symbol === 'string' ? symbol : '';
143
133
  thousand = typeof thousand === 'string' ? thousand : ',';
144
- decimal = typeof decimal === 'string' ? decimal : '.'; // 转换数字字符串,支持科学记数法
145
-
146
- var strNum = scientificToNumber(num) + ''; // 整数和小数部分
134
+ decimal = typeof decimal === 'string' ? decimal : '.';
147
135
 
136
+ // 转换数字字符串,支持科学记数法
137
+ var strNum = scientificToNumber(num) + '';
138
+ // 整数和小数部分
148
139
  var _strNum$split = strNum.split('.'),
149
- _strNum$split2 = _slicedToArray(_strNum$split, 2),
150
- intStr = _strNum$split2[0],
151
- decStr = _strNum$split2[1];
152
-
140
+ _strNum$split2 = _slicedToArray(_strNum$split, 2),
141
+ intStr = _strNum$split2[0],
142
+ decStr = _strNum$split2[1];
153
143
  return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
154
144
  };
155
-
156
145
  export default formatMoney;
package/esm/index.js CHANGED
@@ -26,13 +26,13 @@ export { default as isPromiseLike } from './isPromiseLike';
26
26
  export { default as isHMCard } from './isHMCard';
27
27
  export { default as isTWCard } from './isTWCard';
28
28
  export { default as isSwiftCode } from './isSwiftCode';
29
+
29
30
  /**
30
31
  * 数据处理
31
32
  *
32
33
  * @module Processor
33
34
  * @since 1.1.0
34
35
  */
35
-
36
36
  export { default as formatMoney } from './formatMoney';
37
37
  export { default as formatBankCard } from './formatBankCard';
38
38
  export { default as replaceChar } from './replaceChar';
@@ -46,6 +46,12 @@ export { default as normalizeString } from './normalizeString';
46
46
  export { default as safeDate } from './safeDate';
47
47
  export { default as formatMobile } from './formatMobile';
48
48
  export { default as padZero } from './padZero';
49
+ export { default as transformFieldNames } from './transformFieldNames';
50
+ export { default as listToTree } from './listToTree';
51
+ export { default as treeToList } from './treeToList';
52
+ export { default as findTreeNode } from './findTreeNode';
53
+ export { default as findTreeSelect } from './findTreeSelect';
54
+
49
55
  /**
50
56
  * 数学计算,修正浮点数计算问题
51
57
  *
@@ -85,23 +91,23 @@ export { default as padZero } from './padZero';
85
91
  * times(0.1, undefined, 0.2, undefined); // 0.02 后面的 undefined 取默认值 1
86
92
  *
87
93
  */
88
-
89
94
  export { default as plus } from './plus';
90
95
  export { default as minus } from './minus';
91
96
  export { default as times } from './times';
92
97
  export { default as divide } from './divide';
93
98
  export { default as round } from './round';
99
+
94
100
  /**
95
101
  * 其他
96
102
  *
97
103
  * @module Other
98
104
  * @since 4.2.0
99
105
  */
100
-
101
106
  export { default as waitTime } from './waitTime';
102
107
  export { default as calculateCursorPosition } from './calculateCursorPosition';
103
108
  export { default as randomString } from './randomString';
104
109
  export { default as strlen } from './strlen';
110
+
105
111
  /**
106
112
  * 调试相关
107
113
  *
@@ -109,5 +115,4 @@ export { default as strlen } from './strlen';
109
115
  * @ignore
110
116
  * @since 3.6.1
111
117
  */
112
-
113
118
  export { setDisableWarning, version } from './utils/config';