util-helpers 4.0.4 → 4.0.8
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.
- package/dist/util-helpers.js +120 -76
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/divide.js +17 -8
- package/esm/formatBankCard.js +1 -1
- package/esm/formatMoney.js +7 -5
- package/esm/isBusinessLicense.js +2 -2
- package/esm/isSocialCreditCode.js +2 -2
- package/esm/minus.js +17 -8
- package/esm/parseIdCard.js +2 -2
- package/esm/plus.js +17 -8
- package/esm/replaceChar.js +9 -11
- package/esm/times.js +17 -8
- package/esm/utils/convertToString.js +2 -11
- package/esm/utils/math.util.js +4 -5
- package/lib/divide.js +29 -8
- package/lib/formatBankCard.js +1 -1
- package/lib/formatMoney.js +6 -5
- package/lib/isBusinessLicense.js +2 -2
- package/lib/isSocialCreditCode.js +2 -2
- package/lib/minus.js +29 -8
- package/lib/parseIdCard.js +2 -2
- package/lib/plus.js +29 -8
- package/lib/replaceChar.js +9 -11
- package/lib/times.js +29 -8
- package/lib/utils/convertToString.js +2 -12
- package/lib/utils/math.util.js +4 -3
- package/package.json +2 -2
- package/types/divide.d.ts +2 -4
- package/types/minus.d.ts +2 -4
- package/types/parseIdCard.d.ts +15 -2
- package/types/plus.d.ts +2 -4
- package/types/replaceChar.d.ts +3 -3
- package/types/times.d.ts +2 -4
- package/types/utils/convertToString.d.ts +2 -2
- package/types/utils/math.util.d.ts +7 -0
package/lib/times.js
CHANGED
|
@@ -7,15 +7,27 @@ exports["default"] = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _math = require("./utils/math.util");
|
|
9
9
|
|
|
10
|
+
var _type = require("./utils/type");
|
|
11
|
+
|
|
12
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
13
|
+
|
|
14
|
+
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."); }
|
|
15
|
+
|
|
16
|
+
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); }
|
|
17
|
+
|
|
18
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
19
|
+
|
|
20
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
10
24
|
/**
|
|
11
25
|
* 精确乘法,支持多个数相乘
|
|
12
26
|
*
|
|
13
27
|
* @static
|
|
14
28
|
* @alias module:Math.times
|
|
15
29
|
* @since 3.1.0
|
|
16
|
-
* @param {number|string}
|
|
17
|
-
* @param {number|string} num2 相乘的第二个数
|
|
18
|
-
* @param {...number|string} others 相乘的其余数
|
|
30
|
+
* @param {...number|string} nums 相乘的数
|
|
19
31
|
* @returns {number} 乘积
|
|
20
32
|
* @example
|
|
21
33
|
*
|
|
@@ -28,14 +40,23 @@ var _math = require("./utils/math.util");
|
|
|
28
40
|
* times(3, 0.6, 2, 10);
|
|
29
41
|
* // => 36
|
|
30
42
|
*/
|
|
31
|
-
function times(
|
|
32
|
-
for (var _len = arguments.length,
|
|
33
|
-
|
|
43
|
+
function times() {
|
|
44
|
+
for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
45
|
+
nums[_key] = arguments[_key];
|
|
34
46
|
}
|
|
35
47
|
|
|
36
|
-
|
|
48
|
+
var num1 = nums[0],
|
|
49
|
+
num2 = nums[1],
|
|
50
|
+
rest = nums.slice(2);
|
|
51
|
+
|
|
52
|
+
if (rest.length > 0) {
|
|
53
|
+
return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
|
|
54
|
+
} // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if ((!(0, _type.isNumber)(num2) || (0, _type.isNaN)(num2)) && !(0, _type.isString)(num2)) {
|
|
37
58
|
// @ts-ignore
|
|
38
|
-
return
|
|
59
|
+
return num1;
|
|
39
60
|
}
|
|
40
61
|
|
|
41
62
|
var num1Changed = (0, _math.float2Fixed)(num1);
|
|
@@ -7,26 +7,16 @@ exports["default"] = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _isString = _interopRequireDefault(require("./type/isString"));
|
|
9
9
|
|
|
10
|
-
var _config = require("./config");
|
|
11
|
-
|
|
12
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
11
|
|
|
14
12
|
/**
|
|
15
13
|
* 转换为字符串
|
|
16
14
|
*
|
|
17
15
|
* @param {*} value 值
|
|
18
|
-
* @returns 字符串
|
|
16
|
+
* @returns {string} 字符串
|
|
19
17
|
*/
|
|
20
18
|
function convertToString(value) {
|
|
21
|
-
|
|
22
|
-
if (!_config.config.disableWarning) {
|
|
23
|
-
console.warn("[validatePassword] value must be a string.");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return String(value);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return value;
|
|
19
|
+
return (0, _isString["default"])(value) ? value : String(value);
|
|
30
20
|
}
|
|
31
21
|
|
|
32
22
|
var _default = convertToString;
|
package/lib/utils/math.util.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.strip = strip;
|
|
|
8
8
|
exports.digitLength = digitLength;
|
|
9
9
|
exports.float2Fixed = float2Fixed;
|
|
10
10
|
exports.checkBoundary = checkBoundary;
|
|
11
|
+
exports.trimLeftZero = trimLeftZero;
|
|
11
12
|
exports.scientificToNumber = scientificToNumber;
|
|
12
13
|
|
|
13
14
|
var _constants = require("./constants");
|
|
@@ -99,13 +100,13 @@ function checkBoundary(num) {
|
|
|
99
100
|
|
|
100
101
|
|
|
101
102
|
function trimLeftZero(num) {
|
|
102
|
-
var reg = /^([+-])?(0+)([
|
|
103
|
+
var reg = /^([+-])?(0+)([0-9\.]+)$/;
|
|
103
104
|
var result = reg.exec(num);
|
|
104
105
|
var sign;
|
|
105
106
|
|
|
106
107
|
if (result) {
|
|
107
108
|
sign = result[1] || '';
|
|
108
|
-
return sign + result[3];
|
|
109
|
+
return sign + (result[3][0] === '.' ? '0' + result[3] : result[3]);
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
return num;
|
|
@@ -144,7 +145,7 @@ function scientificToNumber(num) {
|
|
|
144
145
|
var dec = coeff_array[1]; //如果是整数,将整数除第一位之外的非零数字计入位数,相应的减少0的个数
|
|
145
146
|
|
|
146
147
|
if (l - dec.length < 0) {
|
|
147
|
-
num = trimLeftZero(coeff_array[0] + dec.
|
|
148
|
+
num = trimLeftZero(coeff_array[0] + dec.substring(0, l)) + '.' + dec.substring(l);
|
|
148
149
|
} else {
|
|
149
150
|
//拼接字符串,如果是整数,不需要拼接小数点
|
|
150
151
|
num = coeff_array.join('') + new Array(l - dec.length + 1).join(zero);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "util-helpers",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.8",
|
|
4
4
|
"description": "一个基于业务场景的工具方法库",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"lint": "eslint src --ext .js",
|
|
26
26
|
"lint:fix": "eslint src --ext .js --fix",
|
|
27
27
|
"commit": "cz",
|
|
28
|
-
"
|
|
28
|
+
"prepublishOnly": "npm test && npm run build",
|
|
29
29
|
"types": "tsc"
|
|
30
30
|
},
|
|
31
31
|
"repository": {
|
package/types/divide.d.ts
CHANGED
|
@@ -5,9 +5,7 @@ export default divide;
|
|
|
5
5
|
* @static
|
|
6
6
|
* @alias module:Math.divide
|
|
7
7
|
* @since 3.1.0
|
|
8
|
-
* @param {number|string}
|
|
9
|
-
* @param {number|string} num2 被除数
|
|
10
|
-
* @param {...number|string} others 其余被除数
|
|
8
|
+
* @param {...number|string} nums 除数和被除数
|
|
11
9
|
* @returns {number} 商数
|
|
12
10
|
* @example
|
|
13
11
|
*
|
|
@@ -20,4 +18,4 @@ export default divide;
|
|
|
20
18
|
* divide(1000, 10, 10, 10);
|
|
21
19
|
* // => 1
|
|
22
20
|
*/
|
|
23
|
-
declare function divide(
|
|
21
|
+
declare function divide(...nums: (number | string)[]): number;
|
package/types/minus.d.ts
CHANGED
|
@@ -5,9 +5,7 @@ export default minus;
|
|
|
5
5
|
* @static
|
|
6
6
|
* @alias module:Math.minus
|
|
7
7
|
* @since 3.1.0
|
|
8
|
-
* @param {number|string}
|
|
9
|
-
* @param {number|string} num2 相减的第二个数
|
|
10
|
-
* @param {...number|string} others 相减的第其余数
|
|
8
|
+
* @param {...number|string} nums 相减的数
|
|
11
9
|
* @returns {number} 差
|
|
12
10
|
* @example
|
|
13
11
|
*
|
|
@@ -20,4 +18,4 @@ export default minus;
|
|
|
20
18
|
* minus(1, 0.9, 0.02, 0.08);
|
|
21
19
|
* // => 0
|
|
22
20
|
*/
|
|
23
|
-
declare function minus(
|
|
21
|
+
declare function minus(...nums: (number | string)[]): number;
|
package/types/parseIdCard.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export default parseIdCard;
|
|
|
7
7
|
* @since 4.0.0
|
|
8
8
|
* @see 参考 {@link https://baike.baidu.com/item/居民身份证号码|居民身份证号码}
|
|
9
9
|
* @param {string} id 身份证号码,支持15位
|
|
10
|
-
* @returns
|
|
10
|
+
* @returns null 或 省份、生日、性别,省/市/区/年/月/日/性别编码
|
|
11
11
|
* @example
|
|
12
12
|
* parseIdCard('123456789123456');
|
|
13
13
|
* // => null
|
|
@@ -32,4 +32,17 @@ export default parseIdCard;
|
|
|
32
32
|
}
|
|
33
33
|
*
|
|
34
34
|
*/
|
|
35
|
-
declare function parseIdCard(id: string):
|
|
35
|
+
declare function parseIdCard(id: string): {
|
|
36
|
+
province: string;
|
|
37
|
+
birthday: string;
|
|
38
|
+
gender: string;
|
|
39
|
+
origin: {
|
|
40
|
+
province: string;
|
|
41
|
+
city: string;
|
|
42
|
+
area: string;
|
|
43
|
+
year: string;
|
|
44
|
+
month: string;
|
|
45
|
+
day: string;
|
|
46
|
+
gender: string;
|
|
47
|
+
};
|
|
48
|
+
} | null;
|
package/types/plus.d.ts
CHANGED
|
@@ -5,9 +5,7 @@ export default plus;
|
|
|
5
5
|
* @static
|
|
6
6
|
* @alias module:Math.plus
|
|
7
7
|
* @since 3.1.0
|
|
8
|
-
* @param {number|string}
|
|
9
|
-
* @param {number|string} num2 相加的第二个数
|
|
10
|
-
* @param {...number|string} others 相加的其余数
|
|
8
|
+
* @param {...number|string} nums 相加的数
|
|
11
9
|
* @returns {number} 总和
|
|
12
10
|
* @example
|
|
13
11
|
*
|
|
@@ -20,4 +18,4 @@ export default plus;
|
|
|
20
18
|
* plus(0.1, 0.2, 0.3, 0.4);
|
|
21
19
|
* // => 1
|
|
22
20
|
*/
|
|
23
|
-
declare function plus(
|
|
21
|
+
declare function plus(...nums: (number | string)[]): number;
|
package/types/replaceChar.d.ts
CHANGED
|
@@ -37,11 +37,11 @@ export default replaceChar;
|
|
|
37
37
|
* // => 6228 **** **** **** 018
|
|
38
38
|
*
|
|
39
39
|
* // 用户名
|
|
40
|
-
* replaceChar('林某某', {start: 1, end:
|
|
40
|
+
* replaceChar('林某某', {start: 1, end: Infinity, repeat: 2});
|
|
41
41
|
* // => 林**
|
|
42
|
-
* replaceChar('林某', {start: 1, end:
|
|
42
|
+
* replaceChar('林某', {start: 1, end: Infinity, repeat: 2});
|
|
43
43
|
* // => 林**
|
|
44
|
-
* replaceChar('林某某某', {start: 1, end:
|
|
44
|
+
* replaceChar('林某某某', {start: 1, end: Infinity, repeat: 2});
|
|
45
45
|
* // => 林**
|
|
46
46
|
*
|
|
47
47
|
*/
|
package/types/times.d.ts
CHANGED
|
@@ -5,9 +5,7 @@ export default times;
|
|
|
5
5
|
* @static
|
|
6
6
|
* @alias module:Math.times
|
|
7
7
|
* @since 3.1.0
|
|
8
|
-
* @param {number|string}
|
|
9
|
-
* @param {number|string} num2 相乘的第二个数
|
|
10
|
-
* @param {...number|string} others 相乘的其余数
|
|
8
|
+
* @param {...number|string} nums 相乘的数
|
|
11
9
|
* @returns {number} 乘积
|
|
12
10
|
* @example
|
|
13
11
|
*
|
|
@@ -20,4 +18,4 @@ export default times;
|
|
|
20
18
|
* times(3, 0.6, 2, 10);
|
|
21
19
|
* // => 36
|
|
22
20
|
*/
|
|
23
|
-
declare function times(
|
|
21
|
+
declare function times(...nums: (number | string)[]): number;
|
|
@@ -36,6 +36,13 @@ export function float2Fixed(num: number | string): number;
|
|
|
36
36
|
* @returns
|
|
37
37
|
*/
|
|
38
38
|
export function checkBoundary(num: number): void;
|
|
39
|
+
/**
|
|
40
|
+
* 去掉左边数字0
|
|
41
|
+
*
|
|
42
|
+
* @param {string} num 数字字符串
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
export function trimLeftZero(num: string): string;
|
|
39
46
|
/**
|
|
40
47
|
* 科学计数法转换成普通数字
|
|
41
48
|
*
|