util-helpers 4.11.0 → 4.12.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 (93) hide show
  1. package/dist/util-helpers.js +979 -606
  2. package/dist/util-helpers.js.map +1 -1
  3. package/dist/util-helpers.min.js +1 -1
  4. package/dist/util-helpers.min.js.map +1 -1
  5. package/esm/divide.js +6 -6
  6. package/esm/index.js +35 -1
  7. package/esm/minus.js +6 -6
  8. package/esm/plus.js +6 -6
  9. package/esm/round.js +4 -5
  10. package/esm/times.js +6 -6
  11. package/esm/utils/config.js +2 -1
  12. package/esm/utils/math.util.js +30 -0
  13. package/lib/divide.js +5 -6
  14. package/lib/index.js +6 -0
  15. package/lib/minus.js +5 -6
  16. package/lib/plus.js +5 -6
  17. package/lib/round.js +4 -5
  18. package/lib/times.js +5 -6
  19. package/lib/utils/config.js +3 -1
  20. package/lib/utils/math.util.js +33 -0
  21. package/package.json +4 -1
  22. package/types/src/blobToDataURL.d.ts +25 -0
  23. package/types/src/bytesToSize.d.ts +24 -0
  24. package/types/src/calculateCursorPosition.d.ts +26 -0
  25. package/types/src/dataURLToBlob.d.ts +15 -0
  26. package/types/src/divide.d.ts +21 -0
  27. package/types/src/formatBankCard.d.ts +35 -0
  28. package/types/src/formatMobile.d.ts +27 -0
  29. package/types/src/formatMoney.d.ts +50 -0
  30. package/types/src/index.d.ts +45 -0
  31. package/types/src/isBankCard.d.ts +32 -0
  32. package/types/src/isBusinessLicense.d.ts +29 -0
  33. package/types/src/isChinese.d.ts +41 -0
  34. package/types/src/isEmail.d.ts +19 -0
  35. package/types/src/isHMCard.d.ts +24 -0
  36. package/types/src/isIPv4.d.ts +25 -0
  37. package/types/src/isIPv6.d.ts +50 -0
  38. package/types/src/isIdCard.d.ts +34 -0
  39. package/types/src/isMobile.d.ts +19 -0
  40. package/types/src/isPassport.d.ts +21 -0
  41. package/types/src/isPassword.d.ts +46 -0
  42. package/types/src/isPostcode.d.ts +19 -0
  43. package/types/src/isPromiseLike.d.ts +21 -0
  44. package/types/src/isQQ.d.ts +19 -0
  45. package/types/src/isSocialCreditCode.d.ts +31 -0
  46. package/types/src/isSwiftCode.d.ts +22 -0
  47. package/types/src/isTWCard.d.ts +23 -0
  48. package/types/src/isTelephone.d.ts +25 -0
  49. package/types/src/isUrl.d.ts +34 -0
  50. package/types/src/isVehicle.d.ts +29 -0
  51. package/types/src/isWX.d.ts +19 -0
  52. package/types/src/minus.d.ts +21 -0
  53. package/types/src/normalizeString.d.ts +22 -0
  54. package/types/src/numberToChinese.d.ts +57 -0
  55. package/types/src/padZero.d.ts +24 -0
  56. package/types/src/parseIdCard.d.ts +107 -0
  57. package/types/src/plus.d.ts +21 -0
  58. package/types/src/randomString.d.ts +19 -0
  59. package/types/src/replaceChar.d.ts +54 -0
  60. package/types/src/round.d.ts +22 -0
  61. package/types/src/safeDate.d.ts +22 -0
  62. package/types/src/setDataURLPrefix.d.ts +24 -0
  63. package/types/src/strlen.d.ts +20 -0
  64. package/types/src/times.d.ts +21 -0
  65. package/types/src/utils/config.d.ts +13 -0
  66. package/types/src/utils/constants.d.ts +2 -0
  67. package/types/src/utils/convertToString.d.ts +8 -0
  68. package/types/src/utils/devWarn.d.ts +7 -0
  69. package/types/src/utils/math.util.d.ts +63 -0
  70. package/types/src/utils/type/index.d.ts +19 -0
  71. package/types/src/utils/type/isArguments.d.ts +18 -0
  72. package/types/src/utils/type/isArray.d.ts +18 -0
  73. package/types/src/utils/type/isBoolean.d.ts +18 -0
  74. package/types/src/utils/type/isDate.d.ts +18 -0
  75. package/types/src/utils/type/isError.d.ts +18 -0
  76. package/types/src/utils/type/isFunction.d.ts +18 -0
  77. package/types/src/utils/type/isMap.d.ts +18 -0
  78. package/types/src/utils/type/isNaN.d.ts +18 -0
  79. package/types/src/utils/type/isNil.d.ts +11 -0
  80. package/types/src/utils/type/isNull.d.ts +18 -0
  81. package/types/src/utils/type/isNumber.d.ts +27 -0
  82. package/types/src/utils/type/isObject.d.ts +21 -0
  83. package/types/src/utils/type/isRegExp.d.ts +18 -0
  84. package/types/src/utils/type/isSet.d.ts +18 -0
  85. package/types/src/utils/type/isString.d.ts +18 -0
  86. package/types/src/utils/type/isSymbol.d.ts +18 -0
  87. package/types/src/utils/type/isType.d.ts +11 -0
  88. package/types/src/utils/type/isUndefined.d.ts +21 -0
  89. package/types/src/utils/type/isWeakMap.d.ts +18 -0
  90. package/types/src/utils/type/isWeakSet.d.ts +18 -0
  91. package/types/src/validatePassword.d.ts +134 -0
  92. package/types/src/waitTime.d.ts +20 -0
  93. package/types/utils/math.util.d.ts +7 -0
package/esm/index.js CHANGED
@@ -53,6 +53,40 @@ export { default as padZero } from './padZero';
53
53
  * @since 3.1.0
54
54
  * @see 参考 {@link https://github.com/camsong/blog/issues/9|JavaScript 浮点数陷阱及解法}
55
55
  * @see 参考 {@link https://2zbuy.csb.app/|JS浮点数计算测试}
56
+ * @example
57
+ * // 从 4.12.0 版本开始,规范了有效数值参数校验。
58
+ * // 有效数值即数字或字符串类型,能通过 Number(value) 转为数字,且不能为 NaN 、空字符串、空格字符串。
59
+ *
60
+ * // 以下为有效数值:
61
+ * // 1. 数字: 1, -1, 1e-2, 1.312, 0.1, Infinity
62
+ * // 2. 数字字符串: '1', '10e2', '-1', '0.1'
63
+ *
64
+ * // 以下为无效数值:
65
+ * // 1. 非数字的字符串: '', ' ', '1a', '-12a', '10.2.2', '10e2.1'
66
+ * // 2. 其他类型值: undefined, null, true, false, [], {}, new Date(), ...
67
+ *
68
+ * // 注意:Number('') 、 Number(' ') 、 Number(new Date) 、 Number(true)等等是可以返回数字,但都不是有效数值。
69
+ * // 因为它们分别是空字符串、空格字符串和非数字或字符串类型。
70
+ *
71
+ *
72
+ * // 计算说明:
73
+ * // 1. 全部参数都是无效数值将返回 NaN 。
74
+ * plus(); // NaN
75
+ * plus(new Date()); // NaN
76
+ * plus(true, null); // NaN
77
+ *
78
+ * // 2. 参数中包含有效数值和无效数值,将忽略无效数值。
79
+ * plus(0.1); // 0.1
80
+ * plus('0.1', ' ', true, ); // 0.1
81
+ * plus(true, 0.1); // 0.1
82
+ * plus(true, 0.1, 0.2); // 0.3
83
+ * plus('', 0.1, ' ', new Date(), 0.2); // 0.3
84
+ * plus(0.1, true, 0.2, null); // 0.3
85
+ *
86
+ * // 3. 数字和数字字符串将正常的使用安全计算
87
+ * plus(0.1, 0.2); // 0.3
88
+ * plus(0.1, 0.2, 0.3); // 0.6
89
+ *
56
90
  */
57
91
 
58
92
  export { default as plus } from './plus';
@@ -79,4 +113,4 @@ export { default as strlen } from './strlen';
79
113
  * @since 3.6.1
80
114
  */
81
115
 
82
- export { setDisableWarning } from './utils/config';
116
+ export { setDisableWarning, version } from './utils/config';
package/esm/minus.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
- import { digitLength } from './utils/math.util';
2
+ import { digitLength, isEffectiveNumeric } from './utils/math.util';
3
3
  import times from './times';
4
- import { isNumber, isNaN, isString } from './utils/type';
5
4
  /**
6
5
  * 精确减法,支持多个数相减
7
6
  *
@@ -33,12 +32,13 @@ function minus() {
33
32
 
34
33
  if (rest.length > 0) {
35
34
  return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
36
- } // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
35
+ } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
37
36
 
38
37
 
39
- if ((!isNumber(num2) || isNaN(num2)) && !isString(num2)) {
40
- // @ts-ignore
41
- return num1;
38
+ if (!isEffectiveNumeric(num1)) {
39
+ return isEffectiveNumeric(num2) ? Number(num2) : NaN;
40
+ } else if (!isEffectiveNumeric(num2)) {
41
+ return Number(num1);
42
42
  }
43
43
 
44
44
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
package/esm/plus.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
- import { digitLength } from './utils/math.util';
2
+ import { digitLength, isEffectiveNumeric } from './utils/math.util';
3
3
  import times from './times';
4
- import { isNumber, isNaN, isString } from './utils/type';
5
4
  /**
6
5
  * 精确加法,支持多个数相加
7
6
  *
@@ -33,12 +32,13 @@ function plus() {
33
32
 
34
33
  if (rest.length > 0) {
35
34
  return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
36
- } // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
35
+ } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
37
36
 
38
37
 
39
- if ((!isNumber(num2) || isNaN(num2)) && !isString(num2)) {
40
- // @ts-ignore
41
- return num1;
38
+ if (!isEffectiveNumeric(num1)) {
39
+ return isEffectiveNumeric(num2) ? Number(num2) : NaN;
40
+ } else if (!isEffectiveNumeric(num2)) {
41
+ return Number(num1);
42
42
  }
43
43
 
44
44
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
package/esm/round.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import divide from './divide';
2
2
  import times from './times';
3
- import { isNumber, isString, isNaN } from './utils/type';
3
+ import { isEffectiveNumeric } from './utils/math.util';
4
4
  /**
5
5
  * 四舍五入,支持设置精度
6
6
  *
@@ -25,10 +25,9 @@ import { isNumber, isString, isNaN } from './utils/type';
25
25
  function round(num) {
26
26
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
27
27
 
28
- // 兼容处理,如果参数为非数字或字符串时,直接返回
29
- if ((!isNumber(num) || isNaN(num)) && !isString(num)) {
30
- // @ts-ignore
31
- return num;
28
+ // 兼容处理,如果参数包含无效数值时,返回第一个参数
29
+ if (!isEffectiveNumeric(num)) {
30
+ return NaN;
32
31
  }
33
32
 
34
33
  var base = Math.pow(10, precision);
package/esm/times.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
- import { digitLength, float2Fixed, checkBoundary } from './utils/math.util';
3
- import { isNumber, isNaN, isString } from './utils/type';
2
+ import { digitLength, float2Fixed, checkBoundary, isEffectiveNumeric } from './utils/math.util';
4
3
  /**
5
4
  * 精确乘法,支持多个数相乘
6
5
  *
@@ -32,12 +31,13 @@ function times() {
32
31
 
33
32
  if (rest.length > 0) {
34
33
  return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
35
- } // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
34
+ } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
36
35
 
37
36
 
38
- if ((!isNumber(num2) || isNaN(num2)) && !isString(num2)) {
39
- // @ts-ignore
40
- return num1;
37
+ if (!isEffectiveNumeric(num1)) {
38
+ return isEffectiveNumeric(num2) ? Number(num2) : NaN;
39
+ } else if (!isEffectiveNumeric(num2)) {
40
+ return Number(num1);
41
41
  }
42
42
 
43
43
  var num1Changed = float2Fixed(num1);
@@ -1,3 +1,4 @@
1
+ var version = "4.12.1";
1
2
  var config = {
2
3
  // 禁用warning提示
3
4
  disableWarning: true
@@ -14,4 +15,4 @@ function setDisableWarning(bool) {
14
15
  config.disableWarning = !!bool;
15
16
  }
16
17
 
17
- export { config, setDisableWarning };
18
+ export { config, setDisableWarning, version };
@@ -6,6 +6,36 @@
6
6
  */
7
7
  import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from './constants';
8
8
  import devWarn from './devWarn';
9
+ import { isNumber, isString } from './type';
10
+ /**
11
+ * 值是否为有效的数值
12
+ *
13
+ * @param {*} value 待检测的值
14
+ * @returns {boolean} 是否为有效的数值
15
+ */
16
+
17
+ export function isEffectiveNumeric(value) {
18
+ if (isNumber(value) && !isNaN(value)) {
19
+ return true;
20
+ } // 避免空字符串 或 带空格的字符串
21
+
22
+
23
+ if (isString(value)) {
24
+ var fmtStrValue = value.trim(); // 带空格的字符串也不转换数字
25
+ // Number(' ') => 0
26
+
27
+ if (fmtStrValue === value) {
28
+ var numValue = fmtStrValue ? Number(fmtStrValue) : NaN;
29
+
30
+ if (isNumber(numValue) && !isNaN(numValue)) {
31
+ return true;
32
+ }
33
+ }
34
+ }
35
+
36
+ devWarn("".concat(value, " is not a valid number."));
37
+ return false;
38
+ }
9
39
  /**
10
40
  * 是否为科学计数法数字
11
41
  *
package/lib/divide.js CHANGED
@@ -9,8 +9,6 @@ var _math = require("./utils/math.util");
9
9
 
10
10
  var _times = _interopRequireDefault(require("./times"));
11
11
 
12
- var _type = require("./utils/type");
13
-
14
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
13
 
16
14
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -55,12 +53,13 @@ function divide() {
55
53
 
56
54
  if (rest.length > 0) {
57
55
  return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
58
- } // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
56
+ } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
59
57
 
60
58
 
61
- if ((!(0, _type.isNumber)(num2) || (0, _type.isNaN)(num2)) && !(0, _type.isString)(num2)) {
62
- // @ts-ignore
63
- return num1;
59
+ if (!(0, _math.isEffectiveNumeric)(num1)) {
60
+ return (0, _math.isEffectiveNumeric)(num2) ? Number(num2) : NaN;
61
+ } else if (!(0, _math.isEffectiveNumeric)(num2)) {
62
+ return Number(num1);
64
63
  }
65
64
 
66
65
  var num1Changed = (0, _math.float2Fixed)(num1);
package/lib/index.js CHANGED
@@ -273,6 +273,12 @@ Object.defineProperty(exports, "setDisableWarning", {
273
273
  return _config.setDisableWarning;
274
274
  }
275
275
  });
276
+ Object.defineProperty(exports, "version", {
277
+ enumerable: true,
278
+ get: function get() {
279
+ return _config.version;
280
+ }
281
+ });
276
282
 
277
283
  var _isMobile = _interopRequireDefault(require("./isMobile"));
278
284
 
package/lib/minus.js CHANGED
@@ -9,8 +9,6 @@ var _math = require("./utils/math.util");
9
9
 
10
10
  var _times = _interopRequireDefault(require("./times"));
11
11
 
12
- var _type = require("./utils/type");
13
-
14
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
13
 
16
14
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -55,12 +53,13 @@ function minus() {
55
53
 
56
54
  if (rest.length > 0) {
57
55
  return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
58
- } // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
56
+ } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
59
57
 
60
58
 
61
- if ((!(0, _type.isNumber)(num2) || (0, _type.isNaN)(num2)) && !(0, _type.isString)(num2)) {
62
- // @ts-ignore
63
- return num1;
59
+ if (!(0, _math.isEffectiveNumeric)(num1)) {
60
+ return (0, _math.isEffectiveNumeric)(num2) ? Number(num2) : NaN;
61
+ } else if (!(0, _math.isEffectiveNumeric)(num2)) {
62
+ return Number(num1);
64
63
  }
65
64
 
66
65
  var baseNum = Math.pow(10, Math.max((0, _math.digitLength)(num1), (0, _math.digitLength)(num2)));
package/lib/plus.js CHANGED
@@ -9,8 +9,6 @@ var _math = require("./utils/math.util");
9
9
 
10
10
  var _times = _interopRequireDefault(require("./times"));
11
11
 
12
- var _type = require("./utils/type");
13
-
14
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
13
 
16
14
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -55,12 +53,13 @@ function plus() {
55
53
 
56
54
  if (rest.length > 0) {
57
55
  return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
58
- } // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
56
+ } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
59
57
 
60
58
 
61
- if ((!(0, _type.isNumber)(num2) || (0, _type.isNaN)(num2)) && !(0, _type.isString)(num2)) {
62
- // @ts-ignore
63
- return num1;
59
+ if (!(0, _math.isEffectiveNumeric)(num1)) {
60
+ return (0, _math.isEffectiveNumeric)(num2) ? Number(num2) : NaN;
61
+ } else if (!(0, _math.isEffectiveNumeric)(num2)) {
62
+ return Number(num1);
64
63
  }
65
64
 
66
65
  var baseNum = Math.pow(10, Math.max((0, _math.digitLength)(num1), (0, _math.digitLength)(num2)));
package/lib/round.js CHANGED
@@ -9,7 +9,7 @@ var _divide = _interopRequireDefault(require("./divide"));
9
9
 
10
10
  var _times = _interopRequireDefault(require("./times"));
11
11
 
12
- var _type = require("./utils/type");
12
+ var _math = require("./utils/math.util");
13
13
 
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
15
 
@@ -36,10 +36,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
36
36
  function round(num) {
37
37
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
38
38
 
39
- // 兼容处理,如果参数为非数字或字符串时,直接返回
40
- if ((!(0, _type.isNumber)(num) || (0, _type.isNaN)(num)) && !(0, _type.isString)(num)) {
41
- // @ts-ignore
42
- return num;
39
+ // 兼容处理,如果参数包含无效数值时,返回第一个参数
40
+ if (!(0, _math.isEffectiveNumeric)(num)) {
41
+ return NaN;
43
42
  }
44
43
 
45
44
  var base = Math.pow(10, precision);
package/lib/times.js CHANGED
@@ -7,8 +7,6 @@ exports["default"] = void 0;
7
7
 
8
8
  var _math = require("./utils/math.util");
9
9
 
10
- var _type = require("./utils/type");
11
-
12
10
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
13
11
 
14
12
  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."); }
@@ -51,12 +49,13 @@ function times() {
51
49
 
52
50
  if (rest.length > 0) {
53
51
  return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
54
- } // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
52
+ } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
55
53
 
56
54
 
57
- if ((!(0, _type.isNumber)(num2) || (0, _type.isNaN)(num2)) && !(0, _type.isString)(num2)) {
58
- // @ts-ignore
59
- return num1;
55
+ if (!(0, _math.isEffectiveNumeric)(num1)) {
56
+ return (0, _math.isEffectiveNumeric)(num2) ? Number(num2) : NaN;
57
+ } else if (!(0, _math.isEffectiveNumeric)(num2)) {
58
+ return Number(num1);
60
59
  }
61
60
 
62
61
  var num1Changed = (0, _math.float2Fixed)(num1);
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.setDisableWarning = setDisableWarning;
7
- exports.config = void 0;
7
+ exports.version = exports.config = void 0;
8
+ var version = "4.12.1";
9
+ exports.version = version;
8
10
  var config = {
9
11
  // 禁用warning提示
10
12
  disableWarning: true
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.isEffectiveNumeric = isEffectiveNumeric;
6
7
  exports.isScientificNumber = isScientificNumber;
7
8
  exports.strip = strip;
8
9
  exports.digitLength = digitLength;
@@ -15,6 +16,8 @@ var _constants = require("./constants");
15
16
 
16
17
  var _devWarn = _interopRequireDefault(require("./devWarn"));
17
18
 
19
+ var _type = require("./type");
20
+
18
21
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
19
22
 
20
23
  /**
@@ -24,12 +27,42 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
24
27
  * 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
25
28
  */
26
29
 
30
+ /**
31
+ * 值是否为有效的数值
32
+ *
33
+ * @param {*} value 待检测的值
34
+ * @returns {boolean} 是否为有效的数值
35
+ */
36
+ function isEffectiveNumeric(value) {
37
+ if ((0, _type.isNumber)(value) && !isNaN(value)) {
38
+ return true;
39
+ } // 避免空字符串 或 带空格的字符串
40
+
41
+
42
+ if ((0, _type.isString)(value)) {
43
+ var fmtStrValue = value.trim(); // 带空格的字符串也不转换数字
44
+ // Number(' ') => 0
45
+
46
+ if (fmtStrValue === value) {
47
+ var numValue = fmtStrValue ? Number(fmtStrValue) : NaN;
48
+
49
+ if ((0, _type.isNumber)(numValue) && !isNaN(numValue)) {
50
+ return true;
51
+ }
52
+ }
53
+ }
54
+
55
+ (0, _devWarn["default"])("".concat(value, " is not a valid number."));
56
+ return false;
57
+ }
27
58
  /**
28
59
  * 是否为科学计数法数字
29
60
  *
30
61
  * @param {string} num 检查值
31
62
  * @returns {boolean}
32
63
  */
64
+
65
+
33
66
  function isScientificNumber(num) {
34
67
  return /\d+\.?\d*e[\+\-]*\d+/i.test(num);
35
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.11.0",
3
+ "version": "4.12.1",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -62,10 +62,13 @@
62
62
  "@babel/preset-env": "^7.9.0",
63
63
  "@commitlint/cli": "^11.0.0",
64
64
  "@commitlint/config-conventional": "^11.0.0",
65
+ "@rollup/plugin-babel": "^5.3.1",
65
66
  "@rollup/plugin-commonjs": "^19.0.0",
67
+ "@rollup/plugin-json": "^4.1.0",
66
68
  "@rollup/plugin-node-resolve": "^13.0.0",
67
69
  "@types/jest": "^26.0.23",
68
70
  "babel-jest": "^27.0.2",
71
+ "babel-plugin-inline-json": "^2.1.0",
69
72
  "cross-env": "^7.0.3",
70
73
  "cz-conventional-changelog": "^3.3.0",
71
74
  "docdash": "^1.2.0",
@@ -0,0 +1,25 @@
1
+ export default blobToDataURL;
2
+ /**
3
+ * 将 Blob 或 File 对象转成 data:URL 格式的 Base64 字符串。
4
+ *
5
+ * @static
6
+ * @alias module:Processor.blobToDataURL
7
+ * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader/readAsDataURL|FileReader.readAsDataURL()}
8
+ * @since 4.1.0
9
+ * @param {Blob} blob Blob 或 File 对象
10
+ * @returns {Promise<string>} data:URL 格式的 Base64 字符串。
11
+ * @example
12
+ * const aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // 一个包含DOMString的数组
13
+ * const htmlBlob = new Blob(aFileParts, { type: 'text/html' }); // 得到 blob
14
+ *
15
+ * blobToDataURL(htmlBlob).then(data=>{
16
+ * console.log(data); // data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
17
+ * });
18
+ *
19
+ * const textBlob = new Blob(aFileParts, { type: 'text/plain' });
20
+ *
21
+ * blobToDataURL(textBlob).then(data=>{
22
+ * console.log(data); // data:text/plain;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
23
+ * });
24
+ */
25
+ declare function blobToDataURL(blob: Blob): Promise<string>;
@@ -0,0 +1,24 @@
1
+ export default bytesToSize;
2
+ /**
3
+ * 字节转换存储单位,保留2位小数点
4
+ *
5
+ * @static
6
+ * @alias module:Processor.bytesToSize
7
+ * @since 3.8.0
8
+ * @param {number} bytes 字节大小
9
+ * @returns {string} 存储单位值
10
+ * @example
11
+ *
12
+ * bytesToSize(0);
13
+ * // => 0 B
14
+ *
15
+ * bytesToSize(1024);
16
+ * // => 1 KB
17
+ *
18
+ * bytesToSize(3.213243*1024*1024);
19
+ * // => 3.21 MB
20
+ *
21
+ * bytesToSize(1024*1024*1024);
22
+ * // => 1 GB
23
+ */
24
+ declare function bytesToSize(bytes: number): string;
@@ -0,0 +1,26 @@
1
+ export default calculateCursorPosition;
2
+ /**
3
+ * 计算输入框的值格式化后光标位置
4
+ *
5
+ * @static
6
+ * @alias module:Other.calculateCursorPosition
7
+ * @since 4.6.0
8
+ * @see 格式化手机号码 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile|formatMobile}
9
+ * @see 格式化银行卡号 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard|formatBankCard}
10
+ * @see h5示例 {@link https://2950v9.csb.app/|点击查看}
11
+ * @see react示例 {@link https://33ccy9.csb.app/|点击查看}
12
+ * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
13
+ * @param {string} prevCtrlValue 上一个格式化后的值
14
+ * @param {string} rawValue 当前输入原值
15
+ * @param {string} ctrlValue 当前格式化后的值
16
+ * @param {Object} [options] 配置项
17
+ * @param {string[]|string} [options.placeholderChar=' '] 占位符
18
+ * @param {RegExp} [options.maskReg=/\D/g] 需要遮盖的字符规则。默认去掉非数字,意味着 ctrlValue 需要去掉非数字。
19
+ * @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
20
+ * @returns {number} 格式化后的光标位置
21
+ */
22
+ declare function calculateCursorPosition(prevPos: number, prevCtrlValue: string, rawValue: string, ctrlValue: string, { placeholderChar, maskReg, type }?: {
23
+ placeholderChar?: string | string[] | undefined;
24
+ maskReg?: RegExp | undefined;
25
+ type?: "mobile" | "bankCard" | undefined;
26
+ } | undefined): number;
@@ -0,0 +1,15 @@
1
+ export default dataURLToBlob;
2
+ /**
3
+ * 将 DataURL 转为 Blob 对象
4
+ *
5
+ * @static
6
+ * @alias module:Processor.dataURLToBlob
7
+ * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Glossary/Base64|Base64}
8
+ * @since 4.1.0
9
+ * @param {string} dataurl data: 协议的URL
10
+ * @returns {Blob} Blob 对象
11
+ * @example
12
+ * const dataurl = 'data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=';
13
+ * dataURLToBlob(dataurl);
14
+ */
15
+ declare function dataURLToBlob(dataurl: string): Blob;
@@ -0,0 +1,21 @@
1
+ export default divide;
2
+ /**
3
+ * 精确除法,支持多个数相除
4
+ *
5
+ * @static
6
+ * @alias module:Math.divide
7
+ * @since 3.1.0
8
+ * @param {...number|string} nums 除数和被除数
9
+ * @returns {number} 商数
10
+ * @example
11
+ *
12
+ * divide(1.21, 1.1);
13
+ * // => 1.1
14
+ *
15
+ * divide(1000, 10, 10);
16
+ * // => 10
17
+ *
18
+ * divide(1000, 10, 10, 10);
19
+ * // => 1
20
+ */
21
+ declare function divide(...nums: (number | string)[]): number;
@@ -0,0 +1,35 @@
1
+ export default formatBankCard;
2
+ /**
3
+ * 格式化银行卡号
4
+ *
5
+ * @static
6
+ * @alias module:Processor.formatBankCard
7
+ * @since 1.1.0
8
+ * @param {string} bankCardNo 要处理的字符串
9
+ * @param {Object} [options] 配置项
10
+ * @param {string} [options.char=" "] 间隔字符
11
+ * @param {number} [options.length=4] 间隔长度
12
+ * @returns {string} 格式化的银行卡号
13
+ * @example
14
+ *
15
+ * // 19位银行卡
16
+ * formatBankCard('6228480402564890018');
17
+ * // => 6228 4804 0256 4890 018
18
+ *
19
+ * // 16位银行卡
20
+ * formatBankCard('6228480402564890');
21
+ * // => 6228 4804 0256 4890
22
+ *
23
+ * // 脱敏银行卡
24
+ * formatBankCard('6228********890');
25
+ * // => 6228 **** **** 890
26
+ *
27
+ * // 16位银行卡,"-"间隔
28
+ * formatBankCard('6228480402564890', {char: '-'});
29
+ * // => 6228-4804-0256-4890
30
+ *
31
+ */
32
+ declare function formatBankCard(bankCardNo?: string, { char, length }?: {
33
+ char?: string | undefined;
34
+ length?: number | undefined;
35
+ } | undefined): string;
@@ -0,0 +1,27 @@
1
+ export default formatMobile;
2
+ /**
3
+ * 格式化手机号码
4
+ *
5
+ * @static
6
+ * @alias module:Processor.formatMobile
7
+ * @since 4.5.0
8
+ * @param {string} mobileNo 手机号码
9
+ * @param {object} [options] 配置项
10
+ * @param {string} [options.char=' '] 间隔字符
11
+ * @returns {string} 格式化后的手机号码
12
+ * @example
13
+ * formatMobile('13345678900') // '133 4567 8900'
14
+ * formatMobile('13345678900', { char: '-' }) // '133-4567-8900'
15
+ *
16
+ * // 脱敏手机号码
17
+ * formatMobile('133****1234') // '133 **** 1234'
18
+ * formatMobile('133****1234', { char: '-' }) // '133-****-1234'
19
+ *
20
+ * // 手机号码位数不够
21
+ * formatMobile('133') // '133'
22
+ * formatMobile('133456') // '133 456'
23
+ * formatMobile('13345678') // '133 4567 8'
24
+ */
25
+ declare function formatMobile(mobileNo: string, { char }?: {
26
+ char?: string | undefined;
27
+ } | undefined): string;