util-helpers 4.10.1 → 4.11.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.
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _config = require("./config");
9
+
10
+ /**
11
+ * 打印警告信息
12
+ *
13
+ * @param {any[]} args 打印的信息
14
+ */
15
+ function devWarn() {
16
+ if (!_config.config.disableWarning) {
17
+ var _console;
18
+
19
+ (_console = console).warn.apply(_console, arguments);
20
+ }
21
+ }
22
+
23
+ var _default = devWarn;
24
+ exports["default"] = _default;
@@ -13,7 +13,9 @@ exports.scientificToNumber = scientificToNumber;
13
13
 
14
14
  var _constants = require("./constants");
15
15
 
16
- var _config = require("./config");
16
+ var _devWarn = _interopRequireDefault(require("./devWarn"));
17
+
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
17
19
 
18
20
  /**
19
21
  * 参考: https://github.com/nefe/number-precision/blob/master/src/index.ts
@@ -70,8 +72,10 @@ function digitLength(num) {
70
72
 
71
73
 
72
74
  function float2Fixed(num) {
73
- if (!isScientificNumber(num.toString())) {
74
- return Number(num.toString().replace('.', ''));
75
+ var strNum = String(num);
76
+
77
+ if (!isScientificNumber(strNum)) {
78
+ return Number(strNum.replace('.', ''));
75
79
  }
76
80
 
77
81
  var dLen = digitLength(num);
@@ -86,9 +90,7 @@ function float2Fixed(num) {
86
90
 
87
91
  function checkBoundary(num) {
88
92
  if (+num > _constants.MAX_SAFE_INTEGER || +num < _constants.MIN_SAFE_INTEGER) {
89
- if (!_config.config.disableWarning) {
90
- console.warn("".concat(num, " is beyond boundary when transfer to integer, the results may not be accurate"));
91
- }
93
+ (0, _devWarn["default"])("".concat(num, " is beyond boundary when transfer to integer, the results may not be accurate"));
92
94
  }
93
95
  }
94
96
  /**
@@ -119,40 +121,63 @@ function trimLeftZero(num) {
119
121
  * 2.小数点前边是0,小数点后十分位(包含十分位)之后连续零的个数大于等于6个
120
122
  *
121
123
  * @param {string | number} num 科学计数法数字
122
- * @returns {string} 转换后的数字字符串
124
+ * @returns {string | number} 转换后的数字字符串
123
125
  */
124
126
 
125
127
 
126
128
  function scientificToNumber(num) {
127
- if (isScientificNumber(num.toString())) {
128
- var zero = '0';
129
- var parts = String(num).toLowerCase().split('e');
130
- var e = parts.pop(); // 存储指数
131
- // @ts-ignore
129
+ var strNum = String(num);
130
+
131
+ if (!isScientificNumber(strNum)) {
132
+ return num;
133
+ }
134
+ /** @type string */
132
135
 
133
- var l = Math.abs(e); // 取绝对值,l-1就是0的个数
134
- // @ts-ignore
135
136
 
136
- var sign = e / l; //判断正负
137
+ var ret;
138
+ var zero = '0';
139
+ var parts = strNum.toLowerCase().split('e');
140
+ var e = parts.pop(); // 存储指数
141
+ // @ts-ignore
137
142
 
138
- var coeff_array = parts[0].split('.'); // 将系数按照小数点拆分
139
- //如果是小数
143
+ var l = Math.abs(e); // 取绝对值,l-1就是0的个数
144
+ // @ts-ignore
140
145
 
141
- if (sign === -1) {
142
- //拼接字符串,如果是小数,拼接0和小数点
143
- num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
144
- } else {
145
- var dec = coeff_array[1]; //如果是整数,将整数除第一位之外的非零数字计入位数,相应的减少0的个数
146
+ var sign = e / l; //判断正负
147
+
148
+ var coeff_array = parts[0].split('.'); // 将系数按照小数点拆分
149
+ // 如果是小数
150
+
151
+ if (sign === -1) {
152
+ // 整数部分
153
+ var intVal = trimLeftZero(coeff_array[0]); // 整数部分大于科学计数后面部分
154
+ // 如: 10e-1, 10.2e-1
146
155
 
147
- if (l - dec.length < 0) {
148
- num = trimLeftZero(coeff_array[0] + dec.substring(0, l)) + '.' + dec.substring(l);
149
- } else {
150
- //拼接字符串,如果是整数,不需要拼接小数点
151
- num = coeff_array.join('') + new Array(l - dec.length + 1).join(zero);
156
+ if (intVal.length > l) {
157
+ var thanLen = intVal.length - l;
158
+ var dec = coeff_array[1] || '';
159
+ ret = intVal.slice(0, thanLen); // 处理 10e-1, 100e-1
160
+
161
+ if (intVal.slice(thanLen) !== '0' || dec) {
162
+ ret += '.' + intVal.slice(thanLen) + dec;
152
163
  }
164
+ } else {
165
+ // 整数部分小于等于科学计数后面部分
166
+ // 如: 1e-1, 0.2e-1, 1.2e-2, 1.2e-1
167
+ ret = zero + '.' + new Array(l - intVal.length + 1).join(zero) + coeff_array.join('');
153
168
  }
154
- } // @ts-ignore
169
+ } else {
170
+ // 小数部分
171
+ var _dec = coeff_array[1] || ''; // 如果是整数,将整数除第一位之外的非零数字计入位数,相应的减少0的个数
155
172
 
156
173
 
157
- return num;
174
+ if (l - _dec.length < 0) {
175
+ ret = trimLeftZero(coeff_array[0] + _dec.substring(0, l)) + '.' + _dec.substring(l);
176
+ } else {
177
+ // 拼接字符串,如果是整数,不需要拼接小数点
178
+ ret = coeff_array.join('') + new Array(l - _dec.length + 1).join(zero);
179
+ }
180
+ }
181
+
182
+ return trimLeftZero(ret);
158
183
  }
@@ -5,7 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports["default"] = void 0;
7
7
 
8
- var _config = require("./utils/config");
8
+ var _devWarn = _interopRequireDefault(require("./utils/devWarn"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
11
 
10
12
  var regNumber = /[\d]/;
11
13
  var regLowerCaseLetter = /[a-z]/;
@@ -63,19 +65,22 @@ function hasHex(val) {
63
65
  *
64
66
  * @private
65
67
  * @param {string} val 检测的值
66
- * @param {string} [chars] 特殊字符
68
+ * @param {string} chars 特殊字符
67
69
  * @returns {boolean} 是否包含特殊字符
68
70
  */
69
71
 
70
72
 
71
- function hasSpecialCharacter(val) {
72
- var chars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
73
-
74
- if (!chars) {
73
+ function hasSpecialCharacter(val, chars) {
74
+ if (!chars || !val) {
75
75
  return false;
76
76
  }
77
77
 
78
78
  var specialChars = val.replace(regAllNumberAndLetter, '');
79
+
80
+ if (!specialChars) {
81
+ return false;
82
+ }
83
+
79
84
  var regChars = hasHex(chars) ? new RegExp("[".concat(chars, "]")) : null;
80
85
 
81
86
  if (regChars) {
@@ -97,16 +102,21 @@ function hasSpecialCharacter(val) {
97
102
  *
98
103
  * @private
99
104
  * @param {string} val 检测的值
100
- * @param {string} chars 非法字符
105
+ * @param {string} chars 特殊字符
101
106
  * @returns {boolean} 是否包含非法字符
102
107
  */
103
108
 
104
109
 
105
- function hasUnallowableCharacter(val) {
106
- var chars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
110
+ function hasUnallowableCharacter(val, chars) {
111
+ if (!val) {
112
+ return false;
113
+ }
114
+
107
115
  var specialChars = val.replace(regAllNumberAndLetter, '');
108
116
 
109
- if (!chars && specialChars) {
117
+ if (!specialChars) {
118
+ return false;
119
+ } else if (!chars) {
110
120
  return true;
111
121
  }
112
122
 
@@ -171,7 +181,7 @@ function hasUnallowableCharacter(val) {
171
181
  * }
172
182
  * }
173
183
  *
174
- * validatePassword('a12345678', {level: 3});
184
+ * validatePassword('a12345678', { level: 3 });
175
185
  * // =>
176
186
  * {
177
187
  * validated: false,
@@ -185,7 +195,7 @@ function hasUnallowableCharacter(val) {
185
195
  * }
186
196
  * }
187
197
  *
188
- * validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true});
198
+ * validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true });
189
199
  * // =>
190
200
  * {
191
201
  * validated: false,
@@ -200,7 +210,7 @@ function hasUnallowableCharacter(val) {
200
210
  * }
201
211
  *
202
212
  * // 自定义特殊字符
203
- * validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true, special: '_一二三'});
213
+ * validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true, special: '_一二三' });
204
214
  * // =>
205
215
  * {
206
216
  * validated: true,
@@ -228,10 +238,7 @@ function validatePassword(value) {
228
238
  var valStr = value;
229
239
 
230
240
  if (typeof value !== 'string') {
231
- if (!_config.config.disableWarning) {
232
- console.warn("[validatePassword] value must be a string.");
233
- }
234
-
241
+ (0, _devWarn["default"])("[validatePassword] value must be a string.");
235
242
  valStr = '';
236
243
  }
237
244
 
@@ -243,16 +250,16 @@ function validatePassword(value) {
243
250
 
244
251
  var containesUpperCaseLetter = hasUpperCaseLetter(valStr); // 包含特殊字符
245
252
 
246
- var containesSpecialCharacter = hasSpecialCharacter(valStr, special); // 包含非法字符
253
+ var containesSpecialCharacter = hasSpecialCharacter(valStr, special); // 包含非法字符,即含有非数字字母特殊字符以外的其他字符
247
254
 
248
255
  var containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
249
256
 
250
257
  if (containesNumber) {
251
258
  currentLevel += 1;
252
- }
259
+ } // 不区分大小写
260
+
253
261
 
254
262
  if (ignoreCase) {
255
- // 不区分大小写
256
263
  if (containesLowerCaseLetter || containesUpperCaseLetter) {
257
264
  currentLevel += 1;
258
265
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.10.1",
3
+ "version": "4.11.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -8,11 +8,12 @@
8
8
  "types": "types/index.d.ts",
9
9
  "scripts": {
10
10
  "test": "jest --verbose",
11
+ "test:coverage": "jest --coverage",
11
12
  "test:math": "jest --verbose test/math",
12
13
  "test:type": "jest --verbose test/type",
13
14
  "test:validator": "jest --verbose test/validator",
14
15
  "test:processor": "jest --verbose test/processor",
15
- "build": "npm test && npm run build:lib && npm run build:esm && npm run build:dist && npm run types",
16
+ "build": "npm run build:lib && npm run build:esm && npm run build:dist && npm run types",
16
17
  "build:lib": "rm -rf lib && cross-env MODULE_TYPE=cjs babel src --out-dir lib",
17
18
  "build:esm": "rm -rf esm && cross-env MODULE_TYPE=esm babel src --out-dir esm",
18
19
  "build:dist": "rm -rf dist && rollup -c",
@@ -8,25 +8,22 @@ export default isBusinessLicense;
8
8
  * @since 3.5.0
9
9
  * @param {*} value 要检测的值
10
10
  * @param {Object} [options] 配置项
11
- * @param {boolean} [options.loose=false] 宽松模式。如果为true,不校验校验位。
11
+ * @param {boolean} [options.checkCode=true] 是否校验最后一位校验码,如果为false,不校验校验位。
12
12
  * @returns {boolean} 值是否为营业执照号
13
13
  * @example
14
14
  *
15
15
  * isBusinessLicense('310115600985533');
16
16
  * // => true
17
17
  *
18
- * isBusinessLicense('3101156009');
19
- * // => false
20
- *
21
- * isBusinessLicense('3101156009', { loose: true });
22
- * // => false
23
- *
24
18
  * isBusinessLicense('310115600985535');
25
19
  * // => false
26
20
  *
27
- * isBusinessLicense('310115600985535', { loose: true });
21
+ * isBusinessLicense('310115600985535', { checkCode: false });
28
22
  * // => true
23
+ *
24
+ * isBusinessLicense('31011560098', { checkCode: false });
25
+ * // => false
29
26
  */
30
- declare function isBusinessLicense(value: any, { loose }?: {
31
- loose?: boolean | undefined;
27
+ declare function isBusinessLicense(value: any, options?: {
28
+ checkCode?: boolean | undefined;
32
29
  } | undefined): boolean;
@@ -9,6 +9,7 @@ export default isChinese;
9
9
  * @param {*} value 要检测的值
10
10
  * @param {Object} [options] 配置项
11
11
  * @param {boolean} [options.loose=false] 宽松模式。如果为true,只要包含中文即为true
12
+ * @param {boolean} [options.useExtend=false] 使用统一表意文字扩展A-F。注意:如果不支持 `RegExp.prototype.unicode`,扩展字符集将自动不生效,如IE浏览器。
12
13
  * @returns {boolean} 值是否为中文
13
14
  * @example
14
15
  *
@@ -19,13 +20,22 @@ export default isChinese;
19
20
  * // => false
20
21
  *
21
22
  * // 宽松模式,只要包含中文即为true
22
- * isChinese('林A', {loose: true});
23
+ * isChinese('林A', { loose: true });
23
24
  * // => true
24
25
  *
25
- * isChinese('A林A', {loose: true});
26
+ * isChinese('A林A', { loose: true });
26
27
  * // => true
27
28
  *
29
+ * isChinese('𠮷');
30
+ * // => false
31
+ *
32
+ * // 使用中文扩展字符集,需要浏览器支持 RegExp.prototype.unicode 才生效。
33
+ * isChinese('𠮷', { useExtend: true });
34
+ * // => true
35
+ * isChinese('𠮷aa', { useExtend: true, loose: true });
36
+ * // => true
28
37
  */
29
- declare function isChinese(value: any, { loose }?: {
38
+ declare function isChinese(value: any, { loose, useExtend }?: {
30
39
  loose?: boolean | undefined;
40
+ useExtend?: boolean | undefined;
31
41
  } | undefined): boolean;
@@ -8,7 +8,7 @@ export default isSocialCreditCode;
8
8
  * @since 1.1.0
9
9
  * @param {*} value 要检测的值
10
10
  * @param {Object} [options] 配置项
11
- * @param {boolean} [options.loose=false] 宽松模式。如果为true,不校验校验位。
11
+ * @param {boolean} [options.checkCode=true] 是否校验最后一位校验码,如果为false,不校验校验位。
12
12
  * @returns {boolean} 值是否为统一社会信用代码
13
13
  * @example
14
14
  *
@@ -18,11 +18,14 @@ export default isSocialCreditCode;
18
18
  * isSocialCreditCode('91350100M000100Y4A');
19
19
  * // => false
20
20
  *
21
- * // 宽松模式,不校验校验位。所以也可以通过
22
- * isSocialCreditCode('91350100M000100Y4A', {loose: true});
21
+ * // 不校验校验位
22
+ * isSocialCreditCode('91350100M000100Y4A', { checkCode: false });
23
23
  * // => true
24
24
  *
25
+ * isSocialCreditCode('91350100M000100Y', { checkCode: false });
26
+ * // => false
27
+ *
25
28
  */
26
- declare function isSocialCreditCode(value: any, { loose }?: {
27
- loose?: boolean | undefined;
29
+ declare function isSocialCreditCode(value: any, options?: {
30
+ checkCode?: boolean | undefined;
28
31
  } | undefined): boolean;
@@ -7,12 +7,17 @@ export default isTWCard;
7
7
  * @since 4.0.0
8
8
  * @see 参考 {@link https://zh.wikipedia.org/wiki/台湾居民来往大陆通行证|台湾居民来往大陆通行证}
9
9
  * @param {*} value 要检测的值
10
+ * @param {Object} [options] 配置项
11
+ * @param {boolean} [options.loose=false] 宽松模式。如果为true,表示支持一次性短期通行证
10
12
  * @returns {boolean} 是否为台湾居民来往大陆通行证
11
13
  * @example
12
14
  * isTWCard('12345678') // true
13
15
  * isTWCard('07257456') // true
14
16
  *
15
17
  * // 一次性短期
16
- * isTWCard('F290299977') // true
18
+ * isTWCard('F290299977') // false
19
+ * isTWCard('F290299977', { loose: true }) // true
17
20
  */
18
- declare function isTWCard(value: any): boolean;
21
+ declare function isTWCard(value: any, { loose }?: {
22
+ loose?: boolean | undefined;
23
+ } | undefined): boolean;
@@ -45,7 +45,7 @@ export default replaceChar;
45
45
  * // => 林**
46
46
  *
47
47
  */
48
- declare function replaceChar(str?: string, { start, end, char, repeat, exclude }?: {
48
+ declare function replaceChar(str: string, { start, end, char, repeat, exclude }?: {
49
49
  start?: number | undefined;
50
50
  end?: number | undefined;
51
51
  char?: string | undefined;
@@ -0,0 +1,7 @@
1
+ export default devWarn;
2
+ /**
3
+ * 打印警告信息
4
+ *
5
+ * @param {any[]} args 打印的信息
6
+ */
7
+ declare function devWarn(...args: any[]): void;
@@ -51,6 +51,6 @@ export function trimLeftZero(num: string): string;
51
51
  * 2.小数点前边是0,小数点后十分位(包含十分位)之后连续零的个数大于等于6个
52
52
  *
53
53
  * @param {string | number} num 科学计数法数字
54
- * @returns {string} 转换后的数字字符串
54
+ * @returns {string | number} 转换后的数字字符串
55
55
  */
56
- export function scientificToNumber(num: string | number): string;
56
+ export function scientificToNumber(num: string | number): string | number;
@@ -84,7 +84,7 @@ export type ValidatePasswordReturn = {
84
84
  * }
85
85
  * }
86
86
  *
87
- * validatePassword('a12345678', {level: 3});
87
+ * validatePassword('a12345678', { level: 3 });
88
88
  * // =>
89
89
  * {
90
90
  * validated: false,
@@ -98,7 +98,7 @@ export type ValidatePasswordReturn = {
98
98
  * }
99
99
  * }
100
100
  *
101
- * validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true});
101
+ * validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true });
102
102
  * // =>
103
103
  * {
104
104
  * validated: false,
@@ -113,7 +113,7 @@ export type ValidatePasswordReturn = {
113
113
  * }
114
114
  *
115
115
  * // 自定义特殊字符
116
- * validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true, special: '_一二三'});
116
+ * validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true, special: '_一二三' });
117
117
  * // =>
118
118
  * {
119
119
  * validated: true,