util-helpers 4.13.0 → 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 (175) 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 +3 -3
  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 +8 -7
  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 +9 -7
  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
  175. package/types/utils/config.d.ts +1 -2
package/esm/isUrl.js CHANGED
@@ -1,5 +1,8 @@
1
- import normalizeString from './normalizeString'; // 标准格式如下:
1
+ import normalizeString from './normalizeString';
2
+
3
+ // 标准格式如下:
2
4
  // [协议类型]://[服务器地址]:[端口号]/[资源层级UNIX文件路径][文件名]?[查询]#[片段ID]
5
+
3
6
  // 完整格式如下:
4
7
  // [协议类型]://[访问资源需要的凭证信息]@[服务器地址]:[端口号]/[资源层级UNIX文件路径][文件名]?[查询]#[片段ID]
5
8
  //
@@ -14,6 +17,7 @@ var searchReg = '\\?.*';
14
17
  var hashReg = '#.*';
15
18
  var regWithProtocal = new RegExp("^".concat(protocalReg, "(?:").concat(credentialsReg, ")?(?:").concat(serverReg, ")(?:").concat(portReg, ")?(?:").concat(pathReg, ")*(?:").concat(searchReg, ")?(?:").concat(hashReg, ")?$"));
16
19
  var regNonProtocal = new RegExp("^(?:".concat(serverReg, ")(?:").concat(portReg, ")?(?:").concat(pathReg, ")*(?:").concat(searchReg, ")?(?:").concat(hashReg, ")?$"));
20
+
17
21
  /**
18
22
  * 检测值是否为url
19
23
  *
@@ -41,12 +45,12 @@ var regNonProtocal = new RegExp("^(?:".concat(serverReg, ")(?:").concat(portReg,
41
45
  * isUrl('a.b:8080'); // true
42
46
  * isUrl('p://a.b'); // true
43
47
  * isUrl('p://a.b:8888'); // true
48
+ * isUrl('中文域名.中文后缀'); // true
49
+ * isUrl('中文域名.cn'); // true
44
50
  *
45
51
  */
46
-
47
52
  function isUrl(value) {
48
53
  var valueStr = normalizeString(value);
49
54
  return regWithProtocal.test(valueStr) || regNonProtocal.test(valueStr);
50
55
  }
51
-
52
56
  export default isUrl;
package/esm/isVehicle.js CHANGED
@@ -1,6 +1,8 @@
1
- import normalizeString from './normalizeString'; // 车牌号正则
1
+ import normalizeString from './normalizeString';
2
2
 
3
+ // 车牌号正则
3
4
  var reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})|([A-Z0-9]{5}[DF])|([DF][A-Z0-9]{5}))$/;
5
+
4
6
  /**
5
7
  * 检测值是否为车牌号,支持新能源和非新能源车牌
6
8
  *
@@ -23,10 +25,8 @@ var reg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕
23
25
  * isVehicle('粤B12345F'); // true
24
26
  *
25
27
  */
26
-
27
28
  function isVehicle(value) {
28
29
  var valueStr = normalizeString(value);
29
30
  return reg.test(valueStr);
30
31
  }
31
-
32
32
  export default isVehicle;
package/esm/isWX.js CHANGED
@@ -1,6 +1,8 @@
1
- import normalizeString from './normalizeString'; // 微信号 6至20位,以字母开头,字母,数字,减号(连接符),下划线
1
+ import normalizeString from './normalizeString';
2
2
 
3
+ // 微信号 6至20位,以字母开头,字母,数字,减号(连接符),下划线
3
4
  var reg = /^[a-z]([-_a-z0-9]{5,19})+$/i;
5
+
4
6
  /**
5
7
  * 检测值是否为微信号
6
8
  *
@@ -15,10 +17,8 @@ var reg = /^[a-z]([-_a-z0-9]{5,19})+$/i;
15
17
  * isWX('123'); // false
16
18
  *
17
19
  */
18
-
19
20
  function isWX(value) {
20
21
  var valueStr = normalizeString(value);
21
22
  return reg.test(valueStr);
22
23
  }
23
-
24
24
  export default isWX;
@@ -0,0 +1,122 @@
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
+ * @private
10
+ * @template {Record<string,any>} [T=Record<string,any>]
11
+ * @param {T[]} arr 列表数据
12
+ * @param {object} options 配置项
13
+ * @param {string} [options.childrenField='children'] 子级字段名称
14
+ * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
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;
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
+ }
31
+ } else {
32
+ processEmptyChildren(item[childrenField], options);
33
+ }
34
+ }
35
+ });
36
+ }
37
+
38
+ /**
39
+ * 列表数据转树结构
40
+ *
41
+ * @static
42
+ * @alias module:Processor.listToTree
43
+ * @since 4.14.0
44
+ * @template {Record<string,any>} [T=Record<string,any>]
45
+ * @template {*} [R=T&Record<string,any>]
46
+ * @param {T[]} list 列表数据
47
+ * @param {object} options 配置项
48
+ * @param {string} [options.keyField='id'] 当前数据的键值字段名称
49
+ * @param {string} [options.parentField='pid'] 当前数据的父级字段名称
50
+ * @param {string} [options.childrenField='children'] 子级字段名称
51
+ * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
52
+ * @returns {R[]} 树结构
53
+ * @example
54
+ *
55
+ * const menus = [
56
+ * { id: '1', name: '首页', code: 'trade', pid: null },
57
+ * { id: '2', name: '交易管理', code: 'trade', pid: null },
58
+ * { id: '3', name: '交易查询', code: 'trade-1', pid: '2' },
59
+ * { id: '4', name: '交易查询-查询操作', code: 'trade-1-1', pid: '3' },
60
+ * { id: '5', name: '权限管理', code: 'authorization', pid: null },
61
+ * { id: '6', name: '角色管理', code: 'authorization-1', pid: '5' },
62
+ * { id: '7', name: '用户管理', code: 'authorization-2', pid: '5' }
63
+ * ];
64
+ * listToTree(menus);
65
+ * // [{"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
+ * // 自定义子级字段名
68
+ * listToTree(basicMenus, { childrenField: 'childs' });
69
+ * // [{"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"}]}]
70
+ *
71
+ */
72
+ function listToTree(list) {
73
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
74
+ var _options$keyField = options.keyField,
75
+ keyField = _options$keyField === void 0 ? 'id' : _options$keyField,
76
+ _options$parentField = options.parentField,
77
+ parentField = _options$parentField === void 0 ? 'pid' : _options$parentField,
78
+ _options$childrenFiel2 = options.childrenField,
79
+ childrenField = _options$childrenFiel2 === void 0 ? 'children' : _options$childrenFiel2,
80
+ _options$emptyChildre2 = options.emptyChildrenValue,
81
+ emptyChildrenValue = _options$emptyChildre2 === void 0 ? 'none' : _options$emptyChildre2;
82
+
83
+ /** @type {R[]} */
84
+ var tree = [];
85
+
86
+ /** @type {Object.<string, T[]>} */
87
+ var record = {};
88
+ list.forEach(function (item) {
89
+ if (isObject(item)) {
90
+ var newItem = _objectSpread({}, item);
91
+
92
+ /** @type {string} */
93
+ var id = newItem[keyField];
94
+
95
+ /** @type {string} */
96
+ var pid = newItem[parentField];
97
+ if (record[id]) {
98
+ // @ts-ignore
99
+ newItem[childrenField] = record[id];
100
+ } else {
101
+ // @ts-ignore
102
+ newItem[childrenField] = record[id] = [];
103
+ }
104
+ if (pid) {
105
+ if (!record[pid]) {
106
+ record[pid] = [newItem];
107
+ } else {
108
+ record[pid].push(newItem);
109
+ }
110
+ } else {
111
+ // @ts-ignore
112
+ tree.push(newItem);
113
+ }
114
+ }
115
+ });
116
+ if (emptyChildrenValue !== 'array') {
117
+ // @ts-ignore
118
+ processEmptyChildren(tree, options);
119
+ }
120
+ return tree;
121
+ }
122
+ export default listToTree;
package/esm/minus.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
  * minus(1, 0.9, 0.02, 0.08); // 0
17
18
  *
18
19
  */
19
-
20
20
  function minus() {
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 minus.apply(void 0, [minus(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 minus;
@@ -1,5 +1,6 @@
1
1
  import isNil from './utils/type/isNil';
2
2
  import convertToString from './utils/convertToString';
3
+
3
4
  /**
4
5
  * 规整化字符串。如果值为 undefined 或 null 将转为空字符串,如果值不是字符串类型将转为字符串。
5
6
  *
@@ -22,13 +23,10 @@ import convertToString from './utils/convertToString';
22
23
  * normalizeString('a'); // 'a'
23
24
  *
24
25
  */
25
-
26
26
  function normalizeString(value) {
27
27
  if (isNil(value)) {
28
28
  return '';
29
29
  }
30
-
31
30
  return convertToString(value);
32
31
  }
33
-
34
32
  export default normalizeString;
@@ -1,30 +1,34 @@
1
1
  import { checkBoundary } from './utils/math.util';
2
- import devWarn from './utils/devWarn'; // 简体
2
+ import devWarn from './utils/devWarn';
3
3
 
4
+ // 简体
4
5
  var chnNumberChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
5
- var chnUnitChar = ['', '十', '百', '千']; // 繁体
6
+ var chnUnitChar = ['', '十', '百', '千'];
6
7
 
8
+ // 繁体
7
9
  var big5NumberChar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
8
- var big5UnitChar = ['', '拾', '佰', '仟']; // 数字字符、计数单位
10
+ var big5UnitChar = ['', '拾', '佰', '仟'];
11
+
12
+ // 数字字符、计数单位
9
13
 
10
14
  /**
11
15
  * @type {string[]}
12
16
  * @private
13
17
  */
14
-
15
18
  var numberChar;
19
+
16
20
  /**
17
21
  * @type {string[]}
18
22
  * @private
19
23
  */
20
-
21
24
  var unitChar;
25
+
22
26
  /**
23
27
  * @type {string[]}
24
28
  * @private
25
29
  */
26
-
27
30
  var unitSection;
31
+
28
32
  /**
29
33
  * 每个小节的内部进行转化
30
34
  *
@@ -32,18 +36,16 @@ var unitSection;
32
36
  * @param {number} section 数字
33
37
  * @returns {string} 转化的数字
34
38
  */
35
-
36
39
  function sectionToChinese(section) {
37
40
  var str = '';
38
41
  var chnstr = '';
39
42
  var zero = false; //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
40
-
41
43
  var unitPos = 0;
42
-
43
44
  while (section > 0) {
44
45
  // 对数字取余10,得到的数即为个位数
45
- var v = section % 10; //如果数字为零,则对字符串进行补零
46
+ var v = section % 10;
46
47
 
48
+ //如果数字为零,则对字符串进行补零
47
49
  if (v == 0) {
48
50
  if (zero) {
49
51
  //如果遇到连续多次取余都是0,那么只需补一个零即可
@@ -57,13 +59,12 @@ function sectionToChinese(section) {
57
59
  str += unitChar[unitPos];
58
60
  chnstr = str + chnstr;
59
61
  }
60
-
61
62
  unitPos++;
62
63
  section = Math.floor(section / 10);
63
64
  }
64
-
65
65
  return chnstr;
66
66
  }
67
+
67
68
  /**
68
69
  * 转换整数
69
70
  *
@@ -71,26 +72,20 @@ function sectionToChinese(section) {
71
72
  * @param {number} num 要转换的数字
72
73
  * @returns {string} 中文数字
73
74
  */
74
-
75
-
76
75
  function convertInteger(num) {
77
76
  var numInt = Math.floor(num);
78
77
  var unitPos = 0;
79
78
  var strIns = '';
80
79
  var chnStr = '';
81
80
  var needZero = false;
82
-
83
81
  if (numInt === 0) {
84
82
  return numberChar[0];
85
83
  }
86
-
87
84
  while (numInt > 0) {
88
85
  var section = numInt % 10000;
89
-
90
86
  if (needZero) {
91
87
  chnStr = numberChar[0] + chnStr;
92
88
  }
93
-
94
89
  strIns = sectionToChinese(section);
95
90
  strIns += section !== 0 ? unitSection[unitPos] : unitSection[0];
96
91
  chnStr = strIns + chnStr;
@@ -98,29 +93,26 @@ function convertInteger(num) {
98
93
  numInt = Math.floor(numInt / 10000);
99
94
  unitPos++;
100
95
  }
101
-
102
96
  return chnStr;
103
97
  }
98
+
104
99
  /**
105
100
  * 转换小数
106
101
  *
107
102
  * @private
108
103
  * @param {number} num 要转换的数字
109
104
  */
110
-
111
-
112
105
  function convertDecimal(num) {
113
106
  var strNum = num + '';
114
107
  var index = strNum.indexOf('.');
115
108
  var ret = '';
116
-
117
109
  if (index > -1) {
118
110
  var decimalStr = strNum.slice(index + 1);
119
111
  ret = mapNumberChar(parseInt(decimalStr));
120
112
  }
121
-
122
113
  return ret;
123
114
  }
115
+
124
116
  /**
125
117
  * 映射为中文数字
126
118
  *
@@ -128,18 +120,15 @@ function convertDecimal(num) {
128
120
  * @param {number} num 要处理的数字
129
121
  * @returns {string} 返回中文数字的映射
130
122
  */
131
-
132
-
133
123
  function mapNumberChar(num) {
134
124
  var strNum = num + '';
135
125
  var ret = '';
136
-
137
126
  for (var i = 0, len = strNum.length; i < len; i++) {
138
127
  ret += numberChar[parseInt(strNum[i])];
139
128
  }
140
-
141
129
  return ret;
142
130
  }
131
+
143
132
  /**
144
133
  * 数字转中文数字
145
134
  * 不在安全数字 -9007199254740991~9007199254740991 内,处理会有异常
@@ -177,31 +166,31 @@ function mapNumberChar(num) {
177
166
  * numberToChinese(1990, {unit: false, zero:'〇'}); // 一九九〇
178
167
  *
179
168
  */
180
-
181
-
182
169
  function numberToChinese(num) {
183
170
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
184
171
  var _options$big = options.big5,
185
- big5 = _options$big === void 0 ? false : _options$big,
186
- _options$unit = options.unit,
187
- unit = _options$unit === void 0 ? true : _options$unit,
188
- _options$zero = options.zero,
189
- zero = _options$zero === void 0 ? '' : _options$zero,
190
- _options$negative = options.negative,
191
- negative = _options$negative === void 0 ? '负' : _options$negative,
192
- _options$unitConfig = options.unitConfig,
193
- unitConfig = _options$unitConfig === void 0 ? {} : _options$unitConfig;
172
+ big5 = _options$big === void 0 ? false : _options$big,
173
+ _options$unit = options.unit,
174
+ unit = _options$unit === void 0 ? true : _options$unit,
175
+ _options$zero = options.zero,
176
+ zero = _options$zero === void 0 ? '' : _options$zero,
177
+ _options$negative = options.negative,
178
+ negative = _options$negative === void 0 ? '负' : _options$negative,
179
+ _options$unitConfig = options.unitConfig,
180
+ unitConfig = _options$unitConfig === void 0 ? {} : _options$unitConfig;
194
181
  var _options$decimal = options.decimal,
195
- decimal = _options$decimal === void 0 ? '' : _options$decimal; // 非数字 或 NaN 不处理
182
+ decimal = _options$decimal === void 0 ? '' : _options$decimal;
196
183
 
184
+ // 非数字 或 NaN 不处理
197
185
  if (typeof num !== 'number' || isNaN(num)) {
198
186
  devWarn("\u53C2\u6570\u9519\u8BEF ".concat(num, "\uFF0C\u8BF7\u4F20\u5165\u6570\u5B57"));
199
187
  return '';
200
- } // 超过安全数字提示
201
-
188
+ }
202
189
 
203
- checkBoundary(num); // 设置数字字符和计数单位
190
+ // 超过安全数字提示
191
+ checkBoundary(num);
204
192
 
193
+ // 设置数字字符和计数单位
205
194
  if (big5) {
206
195
  numberChar = big5NumberChar.slice();
207
196
  unitChar = big5UnitChar.slice();
@@ -210,33 +199,35 @@ function numberToChinese(num) {
210
199
  numberChar = chnNumberChar.slice();
211
200
  unitChar = chnUnitChar.slice();
212
201
  decimal = decimal || '点';
213
- } // 设置节点计数单位,万、亿、万亿
214
-
202
+ }
215
203
 
204
+ // 设置节点计数单位,万、亿、万亿
216
205
  var unitWan = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.w) || '万';
217
206
  var unitYi = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.y) || '亿';
218
207
  var unitWanYi = unitWan + unitYi;
219
- unitSection = ['', unitWan, unitYi, unitWanYi]; // 设置0
208
+ unitSection = ['', unitWan, unitYi, unitWanYi];
220
209
 
210
+ // 设置0
221
211
  if (zero) {
222
212
  numberChar[0] = zero;
223
- } // 前置字符,负数处理
224
-
213
+ }
225
214
 
226
- var preStr = num < 0 ? negative : ''; // 整数和小数
215
+ // 前置字符,负数处理
216
+ var preStr = num < 0 ? negative : '';
227
217
 
218
+ // 整数和小数
228
219
  var chnInteger, chnDecimal;
229
- var numAbs = Math.abs(num); // 处理整数
220
+ var numAbs = Math.abs(num);
230
221
 
222
+ // 处理整数
231
223
  if (unit) {
232
224
  chnInteger = convertInteger(numAbs);
233
225
  } else {
234
226
  chnInteger = mapNumberChar(Math.floor(numAbs));
235
- } // 处理小数
236
-
227
+ }
237
228
 
229
+ // 处理小数
238
230
  chnDecimal = convertDecimal(numAbs);
239
231
  return chnDecimal ? "".concat(preStr).concat(chnInteger).concat(decimal).concat(chnDecimal) : "".concat(preStr).concat(chnInteger);
240
232
  }
241
-
242
233
  export default numberToChinese;
package/esm/padZero.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import normalizeString from './normalizeString';
2
+
2
3
  /**
3
4
  * 前置补零
4
5
  *
@@ -22,21 +23,16 @@ import normalizeString from './normalizeString';
22
23
  * padZero('688', 5); // 00688
23
24
  *
24
25
  */
25
-
26
26
  function padZero(value) {
27
27
  var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
28
28
  var str = normalizeString(value);
29
29
  var len = str.length;
30
-
31
30
  if (typeof size !== 'number' || size < 0) {
32
31
  size = 0;
33
32
  }
34
-
35
33
  if (len < size) {
36
34
  return '0'.repeat(size - len) + str;
37
35
  }
38
-
39
36
  return str;
40
37
  }
41
-
42
38
  export default padZero;
@@ -1,10 +1,8 @@
1
1
  import _typeof from "@babel/runtime/helpers/typeof";
2
2
  import _inherits from "@babel/runtime/helpers/inherits";
3
3
  import _setPrototypeOf from "@babel/runtime/helpers/setPrototypeOf";
4
-
5
- function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(re, groups) { return new BabelRegExp(re, undefined, groups); }; var _super = RegExp.prototype; var _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = new RegExp(re, flags); _groups.set(_this, groups || _groups.get(re)); return _setPrototypeOf(_this, BabelRegExp.prototype); } _inherits(BabelRegExp, RegExp); BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); if (result) result.groups = buildGroups(result, this); return result; }; BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if (typeof substitution === "string") { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { return "$" + groups[name]; })); } else if (typeof substitution === "function") { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = arguments; if (_typeof(args[args.length - 1]) !== "object") { args = [].slice.call(args); args.push(buildGroups(args, _this)); } return substitution.apply(this, args); }); } else { return _super[Symbol.replace].call(this, str, substitution); } }; function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { groups[name] = result[g[name]]; return groups; }, Object.create(null)); } return _wrapRegExp.apply(this, arguments); }
6
-
7
- var regIdCard = /*#__PURE__*/_wrapRegExp(/^([0-9]{2})([0-9]{2})([0-9]{2})((?:[0-9]{2})?[0-9]{2})([0-9]{2})([0-9]{2})[0-9]{2}([0-9])(?:[0-9]|X)?$/i, {
4
+ function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(re, groups) { return new BabelRegExp(re, void 0, groups); }; var _super = RegExp.prototype, _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = new RegExp(re, flags); return _groups.set(_this, groups || _groups.get(re)), _setPrototypeOf(_this, BabelRegExp.prototype); } function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { var i = g[name]; if ("number" == typeof i) groups[name] = result[i];else { for (var k = 0; void 0 === result[i[k]] && k + 1 < i.length;) { k++; } groups[name] = result[i[k]]; } return groups; }, Object.create(null)); } return _inherits(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); if (result) { result.groups = buildGroups(result, this); var indices = result.indices; indices && (indices.groups = buildGroups(indices, this)); } return result; }, BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if ("string" == typeof substitution) { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { var group = groups[name]; return "$" + (Array.isArray(group) ? group.join("$") : group); })); } if ("function" == typeof substitution) { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = arguments; return "object" != _typeof(args[args.length - 1]) && (args = [].slice.call(args)).push(buildGroups(args, _this)), substitution.apply(this, args); }); } return _super[Symbol.replace].call(this, str, substitution); }, _wrapRegExp.apply(this, arguments); }
5
+ var regIdCard = /*#__PURE__*/_wrapRegExp(/^(\d{2})(\d{2})(\d{2})((?:\d{2})?\d{2})(\d{2})(\d{2})\d{2}(\d)(?:\d|X)?$/i, {
8
6
  province: 1,
9
7
  city: 2,
10
8
  area: 3,
@@ -13,18 +11,28 @@ var regIdCard = /*#__PURE__*/_wrapRegExp(/^([0-9]{2})([0-9]{2})([0-9]{2})((?:[0-
13
11
  day: 6,
14
12
  gender: 7
15
13
  });
16
-
17
- var Provinces = [// 华北地区:北京市|110000,天津市|120000,河北省|130000,山西省|140000,内蒙古自治区|150000
18
- ['11', '北京市'], ['12', '天津市'], ['13', '河北省'], ['14', '山西省'], ['15', '内蒙古自治区'], // 东北地区: 辽宁省|210000,吉林省|220000,黑龙江省|230000
19
- ['21', '辽宁省'], ['22', '吉林省'], ['23', '黑龙江省'], // 华东地区: 上海市|310000,江苏省|320000,浙江省|330000,安徽省|340000,福建省|350000,江西省|360000,山东省|370000
20
- ['31', '上海市'], ['32', '江苏省'], ['33', '浙江省'], ['34', '安徽省'], ['35', '福建省'], ['36', '江西省'], ['37', '山东省'], // 华中地区: 河南省|410000,湖北省|420000,湖南省|430000
21
- ['41', '河南省'], ['42', '湖北省'], ['43', '湖南省'], // 华南地区:广东省|440000,广西壮族自治区|450000,海南省|460000
22
- ['44', '广东省'], ['45', '广西壮族自治区'], ['46', '海南省'], // 西南地区:重庆市|500000,四川省|510000,贵州省|520000,云南省|530000,西藏自治区|540000
23
- ['50', '重庆市'], ['51', '四川省'], ['52', '贵州省'], ['53', '云南省'], ['54', '西藏自治区'], // 西北地区: 陕西省|610000,甘肃省|620000,青海省|630000,宁夏回族自治区|640000,新疆维吾尔自治区|650000
24
- ['61', '陕西省'], ['62', '甘肃省'], ['63', '青海省'], ['64', '宁夏回族自治区'], ['65', '新疆维吾尔自治区'], // 台湾地区:台湾省|710000
14
+ var Provinces = [
15
+ // 华北地区:北京市|110000,天津市|120000,河北省|130000,山西省|140000,内蒙古自治区|150000
16
+ ['11', '北京市'], ['12', '天津市'], ['13', '河北省'], ['14', '山西省'], ['15', '内蒙古自治区'],
17
+ // 东北地区: 辽宁省|210000,吉林省|220000,黑龙江省|230000
18
+ ['21', '辽宁省'], ['22', '吉林省'], ['23', '黑龙江省'],
19
+ // 华东地区: 上海市|310000,江苏省|320000,浙江省|330000,安徽省|340000,福建省|350000,江西省|360000,山东省|370000
20
+ ['31', '上海市'], ['32', '江苏省'], ['33', '浙江省'], ['34', '安徽省'], ['35', '福建省'], ['36', '江西省'], ['37', '山东省'],
21
+ // 华中地区: 河南省|410000,湖北省|420000,湖南省|430000
22
+ ['41', '河南省'], ['42', '湖北省'], ['43', '湖南省'],
23
+ // 华南地区:广东省|440000,广西壮族自治区|450000,海南省|460000
24
+ ['44', '广东省'], ['45', '广西壮族自治区'], ['46', '海南省'],
25
+ // 西南地区:重庆市|500000,四川省|510000,贵州省|520000,云南省|530000,西藏自治区|540000
26
+ ['50', '重庆市'], ['51', '四川省'], ['52', '贵州省'], ['53', '云南省'], ['54', '西藏自治区'],
27
+ // 西北地区: 陕西省|610000,甘肃省|620000,青海省|630000,宁夏回族自治区|640000,新疆维吾尔自治区|650000
28
+ ['61', '陕西省'], ['62', '甘肃省'], ['63', '青海省'], ['64', '宁夏回族自治区'], ['65', '新疆维吾尔自治区'],
29
+ // 台湾地区:台湾省|710000
25
30
  // 台湾居民公民身份号码地址码为830000,参考 http://www.wanweibaike.com/wiki-中华人民共和国行政区划代码_(7区)、https://zh.wikipedia.org/wiki/港澳台居民居住证
26
- ['71', '台湾省'], ['83', '台湾省'], // 港澳地区:香港特别行政区|810000,澳门特别行政区|820000
27
- ['81', '香港特别行政区'], ['82', '澳门特别行政区']]; // 第一位数字是以前的大区制代码。第二位是大区所在省市编码。全国共分为8个大区:华北(1)、东北(2)、华东(3)、中南(4)、西南(5)、西北(6)、台湾(7)和港澳(8)。
31
+ ['71', '台湾省'], ['83', '台湾省'],
32
+ // 港澳地区:香港特别行政区|810000,澳门特别行政区|820000
33
+ ['81', '香港特别行政区'], ['82', '澳门特别行政区']];
34
+
35
+ // 第一位数字是以前的大区制代码。第二位是大区所在省市编码。全国共分为8个大区:华北(1)、东北(2)、华东(3)、中南(4)、西南(5)、西北(6)、台湾(7)和港澳(8)。
28
36
  // const Regions = [
29
37
  // ['1', '华北地区'],
30
38
  // ['2', '东北地区'],
@@ -89,19 +97,17 @@ var Provinces = [// 华北地区:北京市|110000,天津市|120000,河北
89
97
  * }
90
98
  *
91
99
  */
92
-
93
100
  function parseIdCard(id) {
94
101
  if (!regIdCard.test(id)) {
95
102
  return null;
96
103
  }
104
+
97
105
  /** @type {RegExpExecArray} */
98
106
  // @ts-ignore
99
-
100
-
101
107
  var info = regIdCard.exec(id);
108
+
102
109
  /** @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }} */
103
110
  // @ts-ignore
104
-
105
111
  var origin = (info === null || info === void 0 ? void 0 : info.groups) || {
106
112
  province: info[1],
107
113
  city: info[2],
@@ -114,13 +120,12 @@ function parseIdCard(id) {
114
120
  var province = Provinces.find(function (item) {
115
121
  return item[0] === origin.province;
116
122
  });
117
-
118
123
  if (!province) {
119
124
  return null;
120
125
  }
121
-
122
126
  var birthday = "".concat(origin.year, "-").concat(origin.month, "-").concat(origin.day);
123
- var gender = Number(origin.gender) % 2 === 0 ? '女' : '男'; // const region = Regions.find(item => item[0] === origin.province?.substring(0, 1));
127
+ var gender = Number(origin.gender) % 2 === 0 ? '女' : '男';
128
+ // const region = Regions.find(item => item[0] === origin.province?.substring(0, 1));
124
129
 
125
130
  return {
126
131
  // region,
@@ -130,5 +135,4 @@ function parseIdCard(id) {
130
135
  origin: origin
131
136
  };
132
137
  }
133
-
134
138
  export default parseIdCard;