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
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+ var _type = require("./utils/type");
8
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
9
+ 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; }
10
+ 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; }
11
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
12
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
13
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
14
+ /**
15
+ * 递归处理空子级
16
+ *
17
+ * @private
18
+ * @template {Record<string,any>} [T=Record<string,any>]
19
+ * @param {T[]} arr 列表数据
20
+ * @param {object} options 配置项
21
+ * @param {string} [options.childrenField='children'] 子级字段名称
22
+ * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
23
+ */
24
+ function processEmptyChildren(arr) {
25
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
26
+ var _options$childrenFiel = options.childrenField,
27
+ childrenField = _options$childrenFiel === void 0 ? 'children' : _options$childrenFiel,
28
+ _options$emptyChildre = options.emptyChildrenValue,
29
+ emptyChildrenValue = _options$emptyChildre === void 0 ? 'none' : _options$emptyChildre;
30
+ arr.forEach(function (item) {
31
+ if ((0, _type.isObject)(item) && Array.isArray(item[childrenField])) {
32
+ if (item[childrenField].length <= 0) {
33
+ if (emptyChildrenValue === 'null') {
34
+ // @ts-ignore
35
+ item[childrenField] = null;
36
+ } else if (emptyChildrenValue === 'none') {
37
+ delete item[childrenField];
38
+ }
39
+ } else {
40
+ processEmptyChildren(item[childrenField], options);
41
+ }
42
+ }
43
+ });
44
+ }
45
+
46
+ /**
47
+ * 列表数据转树结构
48
+ *
49
+ * @static
50
+ * @alias module:Processor.listToTree
51
+ * @since 4.14.0
52
+ * @template {Record<string,any>} [T=Record<string,any>]
53
+ * @template {*} [R=T&Record<string,any>]
54
+ * @param {T[]} list 列表数据
55
+ * @param {object} options 配置项
56
+ * @param {string} [options.keyField='id'] 当前数据的键值字段名称
57
+ * @param {string} [options.parentField='pid'] 当前数据的父级字段名称
58
+ * @param {string} [options.childrenField='children'] 子级字段名称
59
+ * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
60
+ * @param {'spread'|'self'} [options.nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
61
+ * @returns {R[]} 树结构
62
+ * @example
63
+ *
64
+ * const menus = [
65
+ * { id: '1', name: '首页', code: 'trade', pid: null },
66
+ * { id: '2', name: '交易管理', code: 'trade', pid: null },
67
+ * { id: '3', name: '交易查询', code: 'trade-1', pid: '2' },
68
+ * { id: '4', name: '交易查询-查询操作', code: 'trade-1-1', pid: '3' },
69
+ * { id: '5', name: '权限管理', code: 'authorization', pid: null },
70
+ * { id: '6', name: '角色管理', code: 'authorization-1', pid: '5' },
71
+ * { id: '7', name: '用户管理', code: 'authorization-2', pid: '5' }
72
+ * ];
73
+ * listToTree(menus);
74
+ * // [{"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"}]}]
75
+ *
76
+ * // 自定义子级字段名
77
+ * listToTree(basicMenus, { childrenField: 'childs' });
78
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"2","name":"交易管理","code":"trade","pid":null,"childs":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","childs":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]},{"id":"5","name":"权限管理","code":"authorization","pid":null,"childs":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]}]
79
+ *
80
+ */
81
+ function listToTree(list) {
82
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
83
+ var _options$keyField = options.keyField,
84
+ keyField = _options$keyField === void 0 ? 'id' : _options$keyField,
85
+ _options$parentField = options.parentField,
86
+ parentField = _options$parentField === void 0 ? 'pid' : _options$parentField,
87
+ _options$childrenFiel2 = options.childrenField,
88
+ childrenField = _options$childrenFiel2 === void 0 ? 'children' : _options$childrenFiel2,
89
+ _options$emptyChildre2 = options.emptyChildrenValue,
90
+ emptyChildrenValue = _options$emptyChildre2 === void 0 ? 'none' : _options$emptyChildre2,
91
+ _options$nodeAssign = options.nodeAssign,
92
+ nodeAssign = _options$nodeAssign === void 0 ? 'spread' : _options$nodeAssign;
93
+
94
+ /** @type {R[]} */
95
+ var tree = [];
96
+
97
+ /** @type {Object.<string, T[]>} */
98
+ var record = {};
99
+ list.forEach(function (item) {
100
+ if ((0, _type.isObject)(item)) {
101
+ var newItem = nodeAssign === 'spread' ? _objectSpread({}, item) : item;
102
+
103
+ /** @type {string} */
104
+ var id = newItem[keyField];
105
+
106
+ /** @type {string} */
107
+ var pid = newItem[parentField];
108
+ if (record[id]) {
109
+ // @ts-ignore
110
+ newItem[childrenField] = record[id];
111
+ } else {
112
+ // @ts-ignore
113
+ newItem[childrenField] = record[id] = [];
114
+ }
115
+ if (pid) {
116
+ if (!record[pid]) {
117
+ record[pid] = [newItem];
118
+ } else {
119
+ record[pid].push(newItem);
120
+ }
121
+ } else {
122
+ // @ts-ignore
123
+ tree.push(newItem);
124
+ }
125
+ }
126
+ });
127
+ if (emptyChildrenValue !== 'array') {
128
+ // @ts-ignore
129
+ processEmptyChildren(tree, options);
130
+ }
131
+ return tree;
132
+ }
133
+ var _default = listToTree;
134
+ exports["default"] = _default;
package/lib/minus.js CHANGED
@@ -4,25 +4,15 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _math = require("./utils/math.util");
9
-
10
8
  var _times = _interopRequireDefault(require("./times"));
11
-
12
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
-
14
10
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
15
-
16
11
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
17
-
18
12
  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); }
19
-
20
13
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
21
-
22
14
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
23
-
24
15
  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; }
25
-
26
16
  /**
27
17
  * 精确减法,支持多个数相减,减数默认为 0 。
28
18
  *
@@ -42,27 +32,23 @@ function minus() {
42
32
  for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
43
33
  nums[_key] = arguments[_key];
44
34
  }
45
-
46
35
  var num1 = nums[0],
47
- _nums$ = nums[1],
48
- num2 = _nums$ === void 0 ? 0 : _nums$,
49
- rest = nums.slice(2);
50
-
36
+ _nums$ = nums[1],
37
+ num2 = _nums$ === void 0 ? 0 : _nums$,
38
+ rest = nums.slice(2);
51
39
  if (rest.length > 0) {
52
40
  return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
53
41
  }
54
-
55
42
  num1 = (0, _math.transformEffectiveNumber)(num1);
56
- num2 = (0, _math.transformEffectiveNumber)(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
57
- // @ts-ignore
43
+ num2 = (0, _math.transformEffectiveNumber)(num2);
58
44
 
45
+ // 兼容处理,如果参数包含无效数值时,返回 NaN
46
+ // @ts-ignore
59
47
  if (isNaN(num1) || isNaN(num2)) {
60
48
  return Number.NaN;
61
49
  }
62
-
63
50
  var baseNum = Math.pow(10, Math.max((0, _math.digitLength)(num1), (0, _math.digitLength)(num2)));
64
51
  return ((0, _times["default"])(num1, baseNum) - (0, _times["default"])(num2, baseNum)) / baseNum;
65
52
  }
66
-
67
53
  var _default = minus;
68
54
  exports["default"] = _default;
@@ -4,13 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _isNil = _interopRequireDefault(require("./utils/type/isNil"));
9
-
10
8
  var _convertToString = _interopRequireDefault(require("./utils/convertToString"));
11
-
12
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
-
14
10
  /**
15
11
  * 规整化字符串。如果值为 undefined 或 null 将转为空字符串,如果值不是字符串类型将转为字符串。
16
12
  *
@@ -37,9 +33,7 @@ function normalizeString(value) {
37
33
  if ((0, _isNil["default"])(value)) {
38
34
  return '';
39
35
  }
40
-
41
36
  return (0, _convertToString["default"])(value);
42
37
  }
43
-
44
38
  var _default = normalizeString;
45
39
  exports["default"] = _default;
@@ -4,38 +4,37 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _math = require("./utils/math.util");
9
-
10
8
  var _devWarn = _interopRequireDefault(require("./utils/devWarn"));
11
-
12
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
-
14
10
  // 简体
15
11
  var chnNumberChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
16
- var chnUnitChar = ['', '十', '百', '千']; // 繁体
12
+ var chnUnitChar = ['', '十', '百', '千'];
17
13
 
14
+ // 繁体
18
15
  var big5NumberChar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
19
- var big5UnitChar = ['', '拾', '佰', '仟']; // 数字字符、计数单位
16
+ var big5UnitChar = ['', '拾', '佰', '仟'];
17
+
18
+ // 数字字符、计数单位
20
19
 
21
20
  /**
22
21
  * @type {string[]}
23
22
  * @private
24
23
  */
25
-
26
24
  var numberChar;
25
+
27
26
  /**
28
27
  * @type {string[]}
29
28
  * @private
30
29
  */
31
-
32
30
  var unitChar;
31
+
33
32
  /**
34
33
  * @type {string[]}
35
34
  * @private
36
35
  */
37
-
38
36
  var unitSection;
37
+
39
38
  /**
40
39
  * 每个小节的内部进行转化
41
40
  *
@@ -43,18 +42,16 @@ var unitSection;
43
42
  * @param {number} section 数字
44
43
  * @returns {string} 转化的数字
45
44
  */
46
-
47
45
  function sectionToChinese(section) {
48
46
  var str = '';
49
47
  var chnstr = '';
50
48
  var zero = false; //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
51
-
52
49
  var unitPos = 0;
53
-
54
50
  while (section > 0) {
55
51
  // 对数字取余10,得到的数即为个位数
56
- var v = section % 10; //如果数字为零,则对字符串进行补零
52
+ var v = section % 10;
57
53
 
54
+ //如果数字为零,则对字符串进行补零
58
55
  if (v == 0) {
59
56
  if (zero) {
60
57
  //如果遇到连续多次取余都是0,那么只需补一个零即可
@@ -68,13 +65,12 @@ function sectionToChinese(section) {
68
65
  str += unitChar[unitPos];
69
66
  chnstr = str + chnstr;
70
67
  }
71
-
72
68
  unitPos++;
73
69
  section = Math.floor(section / 10);
74
70
  }
75
-
76
71
  return chnstr;
77
72
  }
73
+
78
74
  /**
79
75
  * 转换整数
80
76
  *
@@ -82,26 +78,20 @@ function sectionToChinese(section) {
82
78
  * @param {number} num 要转换的数字
83
79
  * @returns {string} 中文数字
84
80
  */
85
-
86
-
87
81
  function convertInteger(num) {
88
82
  var numInt = Math.floor(num);
89
83
  var unitPos = 0;
90
84
  var strIns = '';
91
85
  var chnStr = '';
92
86
  var needZero = false;
93
-
94
87
  if (numInt === 0) {
95
88
  return numberChar[0];
96
89
  }
97
-
98
90
  while (numInt > 0) {
99
91
  var section = numInt % 10000;
100
-
101
92
  if (needZero) {
102
93
  chnStr = numberChar[0] + chnStr;
103
94
  }
104
-
105
95
  strIns = sectionToChinese(section);
106
96
  strIns += section !== 0 ? unitSection[unitPos] : unitSection[0];
107
97
  chnStr = strIns + chnStr;
@@ -109,29 +99,26 @@ function convertInteger(num) {
109
99
  numInt = Math.floor(numInt / 10000);
110
100
  unitPos++;
111
101
  }
112
-
113
102
  return chnStr;
114
103
  }
104
+
115
105
  /**
116
106
  * 转换小数
117
107
  *
118
108
  * @private
119
109
  * @param {number} num 要转换的数字
120
110
  */
121
-
122
-
123
111
  function convertDecimal(num) {
124
112
  var strNum = num + '';
125
113
  var index = strNum.indexOf('.');
126
114
  var ret = '';
127
-
128
115
  if (index > -1) {
129
116
  var decimalStr = strNum.slice(index + 1);
130
117
  ret = mapNumberChar(parseInt(decimalStr));
131
118
  }
132
-
133
119
  return ret;
134
120
  }
121
+
135
122
  /**
136
123
  * 映射为中文数字
137
124
  *
@@ -139,18 +126,15 @@ function convertDecimal(num) {
139
126
  * @param {number} num 要处理的数字
140
127
  * @returns {string} 返回中文数字的映射
141
128
  */
142
-
143
-
144
129
  function mapNumberChar(num) {
145
130
  var strNum = num + '';
146
131
  var ret = '';
147
-
148
132
  for (var i = 0, len = strNum.length; i < len; i++) {
149
133
  ret += numberChar[parseInt(strNum[i])];
150
134
  }
151
-
152
135
  return ret;
153
136
  }
137
+
154
138
  /**
155
139
  * 数字转中文数字
156
140
  * 不在安全数字 -9007199254740991~9007199254740991 内,处理会有异常
@@ -188,31 +172,31 @@ function mapNumberChar(num) {
188
172
  * numberToChinese(1990, {unit: false, zero:'〇'}); // 一九九〇
189
173
  *
190
174
  */
191
-
192
-
193
175
  function numberToChinese(num) {
194
176
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
195
177
  var _options$big = options.big5,
196
- big5 = _options$big === void 0 ? false : _options$big,
197
- _options$unit = options.unit,
198
- unit = _options$unit === void 0 ? true : _options$unit,
199
- _options$zero = options.zero,
200
- zero = _options$zero === void 0 ? '' : _options$zero,
201
- _options$negative = options.negative,
202
- negative = _options$negative === void 0 ? '负' : _options$negative,
203
- _options$unitConfig = options.unitConfig,
204
- unitConfig = _options$unitConfig === void 0 ? {} : _options$unitConfig;
178
+ big5 = _options$big === void 0 ? false : _options$big,
179
+ _options$unit = options.unit,
180
+ unit = _options$unit === void 0 ? true : _options$unit,
181
+ _options$zero = options.zero,
182
+ zero = _options$zero === void 0 ? '' : _options$zero,
183
+ _options$negative = options.negative,
184
+ negative = _options$negative === void 0 ? '负' : _options$negative,
185
+ _options$unitConfig = options.unitConfig,
186
+ unitConfig = _options$unitConfig === void 0 ? {} : _options$unitConfig;
205
187
  var _options$decimal = options.decimal,
206
- decimal = _options$decimal === void 0 ? '' : _options$decimal; // 非数字 或 NaN 不处理
188
+ decimal = _options$decimal === void 0 ? '' : _options$decimal;
207
189
 
190
+ // 非数字 或 NaN 不处理
208
191
  if (typeof num !== 'number' || isNaN(num)) {
209
192
  (0, _devWarn["default"])("\u53C2\u6570\u9519\u8BEF ".concat(num, "\uFF0C\u8BF7\u4F20\u5165\u6570\u5B57"));
210
193
  return '';
211
- } // 超过安全数字提示
212
-
194
+ }
213
195
 
214
- (0, _math.checkBoundary)(num); // 设置数字字符和计数单位
196
+ // 超过安全数字提示
197
+ (0, _math.checkBoundary)(num);
215
198
 
199
+ // 设置数字字符和计数单位
216
200
  if (big5) {
217
201
  numberChar = big5NumberChar.slice();
218
202
  unitChar = big5UnitChar.slice();
@@ -221,34 +205,36 @@ function numberToChinese(num) {
221
205
  numberChar = chnNumberChar.slice();
222
206
  unitChar = chnUnitChar.slice();
223
207
  decimal = decimal || '点';
224
- } // 设置节点计数单位,万、亿、万亿
225
-
208
+ }
226
209
 
210
+ // 设置节点计数单位,万、亿、万亿
227
211
  var unitWan = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.w) || '万';
228
212
  var unitYi = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.y) || '亿';
229
213
  var unitWanYi = unitWan + unitYi;
230
- unitSection = ['', unitWan, unitYi, unitWanYi]; // 设置0
214
+ unitSection = ['', unitWan, unitYi, unitWanYi];
231
215
 
216
+ // 设置0
232
217
  if (zero) {
233
218
  numberChar[0] = zero;
234
- } // 前置字符,负数处理
235
-
219
+ }
236
220
 
237
- var preStr = num < 0 ? negative : ''; // 整数和小数
221
+ // 前置字符,负数处理
222
+ var preStr = num < 0 ? negative : '';
238
223
 
224
+ // 整数和小数
239
225
  var chnInteger, chnDecimal;
240
- var numAbs = Math.abs(num); // 处理整数
226
+ var numAbs = Math.abs(num);
241
227
 
228
+ // 处理整数
242
229
  if (unit) {
243
230
  chnInteger = convertInteger(numAbs);
244
231
  } else {
245
232
  chnInteger = mapNumberChar(Math.floor(numAbs));
246
- } // 处理小数
247
-
233
+ }
248
234
 
235
+ // 处理小数
249
236
  chnDecimal = convertDecimal(numAbs);
250
237
  return chnDecimal ? "".concat(preStr).concat(chnInteger).concat(decimal).concat(chnDecimal) : "".concat(preStr).concat(chnInteger);
251
238
  }
252
-
253
239
  var _default = numberToChinese;
254
240
  exports["default"] = _default;
package/lib/padZero.js CHANGED
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _normalizeString = _interopRequireDefault(require("./normalizeString"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
9
  /**
13
10
  * 前置补零
14
11
  *
@@ -36,17 +33,13 @@ function padZero(value) {
36
33
  var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
37
34
  var str = (0, _normalizeString["default"])(value);
38
35
  var len = str.length;
39
-
40
36
  if (typeof size !== 'number' || size < 0) {
41
37
  size = 0;
42
38
  }
43
-
44
39
  if (len < size) {
45
40
  return '0'.repeat(size - len) + str;
46
41
  }
47
-
48
42
  return str;
49
43
  }
50
-
51
44
  var _default = padZero;
52
45
  exports["default"] = _default;
@@ -1,19 +1,14 @@
1
1
  "use strict";
2
2
 
3
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
-
3
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports["default"] = void 0;
9
-
10
- 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); }
11
-
12
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
13
-
14
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
15
-
16
- 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, {
8
+ 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); }
9
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
10
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
11
+ var regIdCard = /*#__PURE__*/_wrapRegExp(/^(\d{2})(\d{2})(\d{2})((?:\d{2})?\d{2})(\d{2})(\d{2})\d{2}(\d)(?:\d|X)?$/i, {
17
12
  province: 1,
18
13
  city: 2,
19
14
  area: 3,
@@ -22,18 +17,28 @@ var regIdCard = /*#__PURE__*/_wrapRegExp(/^([0-9]{2})([0-9]{2})([0-9]{2})((?:[0-
22
17
  day: 6,
23
18
  gender: 7
24
19
  });
25
-
26
- var Provinces = [// 华北地区:北京市|110000,天津市|120000,河北省|130000,山西省|140000,内蒙古自治区|150000
27
- ['11', '北京市'], ['12', '天津市'], ['13', '河北省'], ['14', '山西省'], ['15', '内蒙古自治区'], // 东北地区: 辽宁省|210000,吉林省|220000,黑龙江省|230000
28
- ['21', '辽宁省'], ['22', '吉林省'], ['23', '黑龙江省'], // 华东地区: 上海市|310000,江苏省|320000,浙江省|330000,安徽省|340000,福建省|350000,江西省|360000,山东省|370000
29
- ['31', '上海市'], ['32', '江苏省'], ['33', '浙江省'], ['34', '安徽省'], ['35', '福建省'], ['36', '江西省'], ['37', '山东省'], // 华中地区: 河南省|410000,湖北省|420000,湖南省|430000
30
- ['41', '河南省'], ['42', '湖北省'], ['43', '湖南省'], // 华南地区:广东省|440000,广西壮族自治区|450000,海南省|460000
31
- ['44', '广东省'], ['45', '广西壮族自治区'], ['46', '海南省'], // 西南地区:重庆市|500000,四川省|510000,贵州省|520000,云南省|530000,西藏自治区|540000
32
- ['50', '重庆市'], ['51', '四川省'], ['52', '贵州省'], ['53', '云南省'], ['54', '西藏自治区'], // 西北地区: 陕西省|610000,甘肃省|620000,青海省|630000,宁夏回族自治区|640000,新疆维吾尔自治区|650000
33
- ['61', '陕西省'], ['62', '甘肃省'], ['63', '青海省'], ['64', '宁夏回族自治区'], ['65', '新疆维吾尔自治区'], // 台湾地区:台湾省|710000
20
+ var Provinces = [
21
+ // 华北地区:北京市|110000,天津市|120000,河北省|130000,山西省|140000,内蒙古自治区|150000
22
+ ['11', '北京市'], ['12', '天津市'], ['13', '河北省'], ['14', '山西省'], ['15', '内蒙古自治区'],
23
+ // 东北地区: 辽宁省|210000,吉林省|220000,黑龙江省|230000
24
+ ['21', '辽宁省'], ['22', '吉林省'], ['23', '黑龙江省'],
25
+ // 华东地区: 上海市|310000,江苏省|320000,浙江省|330000,安徽省|340000,福建省|350000,江西省|360000,山东省|370000
26
+ ['31', '上海市'], ['32', '江苏省'], ['33', '浙江省'], ['34', '安徽省'], ['35', '福建省'], ['36', '江西省'], ['37', '山东省'],
27
+ // 华中地区: 河南省|410000,湖北省|420000,湖南省|430000
28
+ ['41', '河南省'], ['42', '湖北省'], ['43', '湖南省'],
29
+ // 华南地区:广东省|440000,广西壮族自治区|450000,海南省|460000
30
+ ['44', '广东省'], ['45', '广西壮族自治区'], ['46', '海南省'],
31
+ // 西南地区:重庆市|500000,四川省|510000,贵州省|520000,云南省|530000,西藏自治区|540000
32
+ ['50', '重庆市'], ['51', '四川省'], ['52', '贵州省'], ['53', '云南省'], ['54', '西藏自治区'],
33
+ // 西北地区: 陕西省|610000,甘肃省|620000,青海省|630000,宁夏回族自治区|640000,新疆维吾尔自治区|650000
34
+ ['61', '陕西省'], ['62', '甘肃省'], ['63', '青海省'], ['64', '宁夏回族自治区'], ['65', '新疆维吾尔自治区'],
35
+ // 台湾地区:台湾省|710000
34
36
  // 台湾居民公民身份号码地址码为830000,参考 http://www.wanweibaike.com/wiki-中华人民共和国行政区划代码_(7区)、https://zh.wikipedia.org/wiki/港澳台居民居住证
35
- ['71', '台湾省'], ['83', '台湾省'], // 港澳地区:香港特别行政区|810000,澳门特别行政区|820000
36
- ['81', '香港特别行政区'], ['82', '澳门特别行政区']]; // 第一位数字是以前的大区制代码。第二位是大区所在省市编码。全国共分为8个大区:华北(1)、东北(2)、华东(3)、中南(4)、西南(5)、西北(6)、台湾(7)和港澳(8)。
37
+ ['71', '台湾省'], ['83', '台湾省'],
38
+ // 港澳地区:香港特别行政区|810000,澳门特别行政区|820000
39
+ ['81', '香港特别行政区'], ['82', '澳门特别行政区']];
40
+
41
+ // 第一位数字是以前的大区制代码。第二位是大区所在省市编码。全国共分为8个大区:华北(1)、东北(2)、华东(3)、中南(4)、西南(5)、西北(6)、台湾(7)和港澳(8)。
37
42
  // const Regions = [
38
43
  // ['1', '华北地区'],
39
44
  // ['2', '东北地区'],
@@ -98,19 +103,17 @@ var Provinces = [// 华北地区:北京市|110000,天津市|120000,河北
98
103
  * }
99
104
  *
100
105
  */
101
-
102
106
  function parseIdCard(id) {
103
107
  if (!regIdCard.test(id)) {
104
108
  return null;
105
109
  }
110
+
106
111
  /** @type {RegExpExecArray} */
107
112
  // @ts-ignore
108
-
109
-
110
113
  var info = regIdCard.exec(id);
114
+
111
115
  /** @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }} */
112
116
  // @ts-ignore
113
-
114
117
  var origin = (info === null || info === void 0 ? void 0 : info.groups) || {
115
118
  province: info[1],
116
119
  city: info[2],
@@ -123,13 +126,12 @@ function parseIdCard(id) {
123
126
  var province = Provinces.find(function (item) {
124
127
  return item[0] === origin.province;
125
128
  });
126
-
127
129
  if (!province) {
128
130
  return null;
129
131
  }
130
-
131
132
  var birthday = "".concat(origin.year, "-").concat(origin.month, "-").concat(origin.day);
132
- var gender = Number(origin.gender) % 2 === 0 ? '女' : '男'; // const region = Regions.find(item => item[0] === origin.province?.substring(0, 1));
133
+ var gender = Number(origin.gender) % 2 === 0 ? '女' : '男';
134
+ // const region = Regions.find(item => item[0] === origin.province?.substring(0, 1));
133
135
 
134
136
  return {
135
137
  // region,
@@ -139,6 +141,5 @@ function parseIdCard(id) {
139
141
  origin: origin
140
142
  };
141
143
  }
142
-
143
144
  var _default = parseIdCard;
144
145
  exports["default"] = _default;