util-helpers 4.5.0 → 4.7.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,63 @@
1
+ import normalizeString from './normalizeString'; // ref: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
2
+
3
+ /**
4
+ * 计算输入框的值格式化后光标位置
5
+ *
6
+ * @static
7
+ * @alias module:Other.calculateCursorPosition
8
+ * @since 4.6.0
9
+ * @see 格式化手机号码 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile|formatMobile}
10
+ * @see 格式化银行卡号 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard|formatBankCard}
11
+ * @see h5示例 {@link https://2950v9.csb.app/|点击查看}
12
+ * @see react示例 {@link https://33ccy9.csb.app/|点击查看}
13
+ * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
14
+ * @param {string} prevCtrlValue 上一个格式化后的值
15
+ * @param {string} rawValue 当前输入原值
16
+ * @param {string} ctrlValue 当前格式化后的值
17
+ * @param {Object} [options] 配置项
18
+ * @param {string[]|string} [options.placeholderChar=' '] 占位符
19
+ * @param {RegExp} [options.maskReg=/\D/g] 需要遮盖的字符规则。默认去掉非数字,意味着 ctrlValue 需要去掉非数字。
20
+ * @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
21
+ * @returns {number} 格式化后的光标位置
22
+ */
23
+
24
+ function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
25
+ var _ref = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {},
26
+ _ref$placeholderChar = _ref.placeholderChar,
27
+ placeholderChar = _ref$placeholderChar === void 0 ? ' ' : _ref$placeholderChar,
28
+ _ref$maskReg = _ref.maskReg,
29
+ maskReg = _ref$maskReg === void 0 ? /\D/g : _ref$maskReg,
30
+ type = _ref.type;
31
+
32
+ var realCtrlValue = normalizeString(prevCtrlValue);
33
+ var realRawValue = normalizeString(rawValue);
34
+ var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
35
+ var editLength = realRawValue.length - realCtrlValue.length;
36
+ var isAddition = editLength > 0;
37
+ var pos = prevPos;
38
+
39
+ if (isAddition) {
40
+ var additionStr = realRawValue.substring(pos - editLength, pos);
41
+ var ctrlCharCount = additionStr.replace(maskReg, '').length;
42
+ pos -= editLength - ctrlCharCount;
43
+ var placeholderCharCount = 0;
44
+
45
+ while (ctrlCharCount > 0) {
46
+ if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
47
+ placeholderCharCount++;
48
+ } else {
49
+ ctrlCharCount--;
50
+ }
51
+ }
52
+
53
+ pos += placeholderCharCount;
54
+ }
55
+
56
+ if (type === 'mobile' && (pos === 4 || pos === 9) || type === 'bankCard' && pos > 0 && pos % 5 === 0) {
57
+ pos -= 1;
58
+ }
59
+
60
+ return pos;
61
+ }
62
+
63
+ export default calculateCursorPosition;
@@ -12,8 +12,15 @@ import normalizeString from './normalizeString';
12
12
  * @example
13
13
  * formatMobile('13345678900') // '133 4567 8900'
14
14
  * formatMobile('13345678900', { char: '-' }) // '133-4567-8900'
15
+ *
16
+ * // 脱敏手机号码
15
17
  * formatMobile('133****1234') // '133 **** 1234'
16
18
  * formatMobile('133****1234', { char: '-' }) // '133-****-1234'
19
+ *
20
+ * // 手机号码位数不够
21
+ * formatMobile('133') // '133'
22
+ * formatMobile('133456') // '133 456'
23
+ * formatMobile('13345678') // '133 4567 8'
17
24
  */
18
25
 
19
26
  function formatMobile(mobileNo) {
package/esm/index.js CHANGED
@@ -44,6 +44,7 @@ export { default as setDataURLPrefix } from './setDataURLPrefix';
44
44
  export { default as normalizeString } from './normalizeString';
45
45
  export { default as safeDate } from './safeDate';
46
46
  export { default as formatMobile } from './formatMobile';
47
+ export { default as padZero } from './padZero';
47
48
  /**
48
49
  * 数学计算,修正浮点数计算问题
49
50
  *
@@ -66,6 +67,7 @@ export { default as round } from './round';
66
67
  */
67
68
 
68
69
  export { default as waitTime } from './waitTime';
70
+ export { default as calculateCursorPosition } from './calculateCursorPosition';
69
71
  /**
70
72
  * 调试相关
71
73
  *
@@ -5,7 +5,7 @@ import convertToString from './utils/convertToString';
5
5
  *
6
6
  * @static
7
7
  * @alias module:Processor.normalizeString
8
- * @see 参考 {@link tttps://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
8
+ * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
9
9
  * @since 4.3.0
10
10
  * @param {*} value 待处理的值
11
11
  * @returns {string} 规整化的值
package/esm/padZero.js ADDED
@@ -0,0 +1,41 @@
1
+ import normalizeString from './normalizeString';
2
+ /**
3
+ * 前置补零
4
+ *
5
+ * @static
6
+ * @alias module:Processor.padZero
7
+ * @since 4.7.0
8
+ * @param {string|number} value 要处理的值
9
+ * @param {number} [size=2] 指定字符串长度
10
+ * @returns {string} 用零填充数字到给定长度的字符串
11
+ * @example
12
+ * padZero(5); // '05'
13
+ * padZero('5'); // '05'
14
+ *
15
+ * padZero(12); // '12'
16
+ * padZero('12'); // '12'
17
+ *
18
+ * padZero(688); // '688'
19
+ * padZero('688'); // '688'
20
+ *
21
+ * padZero(688, 5); // '00688'
22
+ * padZero('688', 5); // '00688'
23
+ */
24
+
25
+ function padZero(value) {
26
+ var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
27
+ var str = normalizeString(value);
28
+ var len = str.length;
29
+
30
+ if (typeof size !== 'number' || size < 0) {
31
+ size = 0;
32
+ }
33
+
34
+ if (len < size) {
35
+ return '0'.repeat(size - len) + str;
36
+ }
37
+
38
+ return str;
39
+ }
40
+
41
+ export default padZero;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _normalizeString = _interopRequireDefault(require("./normalizeString"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
+
12
+ // ref: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
13
+
14
+ /**
15
+ * 计算输入框的值格式化后光标位置
16
+ *
17
+ * @static
18
+ * @alias module:Other.calculateCursorPosition
19
+ * @since 4.6.0
20
+ * @see 格式化手机号码 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile|formatMobile}
21
+ * @see 格式化银行卡号 {@link https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard|formatBankCard}
22
+ * @see h5示例 {@link https://2950v9.csb.app/|点击查看}
23
+ * @see react示例 {@link https://33ccy9.csb.app/|点击查看}
24
+ * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
25
+ * @param {string} prevCtrlValue 上一个格式化后的值
26
+ * @param {string} rawValue 当前输入原值
27
+ * @param {string} ctrlValue 当前格式化后的值
28
+ * @param {Object} [options] 配置项
29
+ * @param {string[]|string} [options.placeholderChar=' '] 占位符
30
+ * @param {RegExp} [options.maskReg=/\D/g] 需要遮盖的字符规则。默认去掉非数字,意味着 ctrlValue 需要去掉非数字。
31
+ * @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
32
+ * @returns {number} 格式化后的光标位置
33
+ */
34
+ function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
35
+ var _ref = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {},
36
+ _ref$placeholderChar = _ref.placeholderChar,
37
+ placeholderChar = _ref$placeholderChar === void 0 ? ' ' : _ref$placeholderChar,
38
+ _ref$maskReg = _ref.maskReg,
39
+ maskReg = _ref$maskReg === void 0 ? /\D/g : _ref$maskReg,
40
+ type = _ref.type;
41
+
42
+ var realCtrlValue = (0, _normalizeString["default"])(prevCtrlValue);
43
+ var realRawValue = (0, _normalizeString["default"])(rawValue);
44
+ var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
45
+ var editLength = realRawValue.length - realCtrlValue.length;
46
+ var isAddition = editLength > 0;
47
+ var pos = prevPos;
48
+
49
+ if (isAddition) {
50
+ var additionStr = realRawValue.substring(pos - editLength, pos);
51
+ var ctrlCharCount = additionStr.replace(maskReg, '').length;
52
+ pos -= editLength - ctrlCharCount;
53
+ var placeholderCharCount = 0;
54
+
55
+ while (ctrlCharCount > 0) {
56
+ if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
57
+ placeholderCharCount++;
58
+ } else {
59
+ ctrlCharCount--;
60
+ }
61
+ }
62
+
63
+ pos += placeholderCharCount;
64
+ }
65
+
66
+ if (type === 'mobile' && (pos === 4 || pos === 9) || type === 'bankCard' && pos > 0 && pos % 5 === 0) {
67
+ pos -= 1;
68
+ }
69
+
70
+ return pos;
71
+ }
72
+
73
+ var _default = calculateCursorPosition;
74
+ exports["default"] = _default;
@@ -22,8 +22,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
22
22
  * @example
23
23
  * formatMobile('13345678900') // '133 4567 8900'
24
24
  * formatMobile('13345678900', { char: '-' }) // '133-4567-8900'
25
+ *
26
+ * // 脱敏手机号码
25
27
  * formatMobile('133****1234') // '133 **** 1234'
26
28
  * formatMobile('133****1234', { char: '-' }) // '133-****-1234'
29
+ *
30
+ * // 手机号码位数不够
31
+ * formatMobile('133') // '133'
32
+ * formatMobile('133456') // '133 456'
33
+ * formatMobile('13345678') // '133 4567 8'
27
34
  */
28
35
  function formatMobile(mobileNo) {
29
36
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
package/lib/index.js CHANGED
@@ -201,6 +201,12 @@ Object.defineProperty(exports, "formatMobile", {
201
201
  return _formatMobile["default"];
202
202
  }
203
203
  });
204
+ Object.defineProperty(exports, "padZero", {
205
+ enumerable: true,
206
+ get: function get() {
207
+ return _padZero["default"];
208
+ }
209
+ });
204
210
  Object.defineProperty(exports, "plus", {
205
211
  enumerable: true,
206
212
  get: function get() {
@@ -237,6 +243,12 @@ Object.defineProperty(exports, "waitTime", {
237
243
  return _waitTime["default"];
238
244
  }
239
245
  });
246
+ Object.defineProperty(exports, "calculateCursorPosition", {
247
+ enumerable: true,
248
+ get: function get() {
249
+ return _calculateCursorPosition["default"];
250
+ }
251
+ });
240
252
  Object.defineProperty(exports, "setDisableWarning", {
241
253
  enumerable: true,
242
254
  get: function get() {
@@ -310,6 +322,8 @@ var _safeDate = _interopRequireDefault(require("./safeDate"));
310
322
 
311
323
  var _formatMobile = _interopRequireDefault(require("./formatMobile"));
312
324
 
325
+ var _padZero = _interopRequireDefault(require("./padZero"));
326
+
313
327
  var _plus = _interopRequireDefault(require("./plus"));
314
328
 
315
329
  var _minus = _interopRequireDefault(require("./minus"));
@@ -322,6 +336,8 @@ var _round = _interopRequireDefault(require("./round"));
322
336
 
323
337
  var _waitTime = _interopRequireDefault(require("./waitTime"));
324
338
 
339
+ var _calculateCursorPosition = _interopRequireDefault(require("./calculateCursorPosition"));
340
+
325
341
  var _config = require("./utils/config");
326
342
 
327
343
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
@@ -16,7 +16,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
16
16
  *
17
17
  * @static
18
18
  * @alias module:Processor.normalizeString
19
- * @see 参考 {@link tttps://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
19
+ * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
20
20
  * @since 4.3.0
21
21
  * @param {*} value 待处理的值
22
22
  * @returns {string} 规整化的值
package/lib/padZero.js ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _normalizeString = _interopRequireDefault(require("./normalizeString"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
+
12
+ /**
13
+ * 前置补零
14
+ *
15
+ * @static
16
+ * @alias module:Processor.padZero
17
+ * @since 4.7.0
18
+ * @param {string|number} value 要处理的值
19
+ * @param {number} [size=2] 指定字符串长度
20
+ * @returns {string} 用零填充数字到给定长度的字符串
21
+ * @example
22
+ * padZero(5); // '05'
23
+ * padZero('5'); // '05'
24
+ *
25
+ * padZero(12); // '12'
26
+ * padZero('12'); // '12'
27
+ *
28
+ * padZero(688); // '688'
29
+ * padZero('688'); // '688'
30
+ *
31
+ * padZero(688, 5); // '00688'
32
+ * padZero('688', 5); // '00688'
33
+ */
34
+ function padZero(value) {
35
+ var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
36
+ var str = (0, _normalizeString["default"])(value);
37
+ var len = str.length;
38
+
39
+ if (typeof size !== 'number' || size < 0) {
40
+ size = 0;
41
+ }
42
+
43
+ if (len < size) {
44
+ return '0'.repeat(size - len) + str;
45
+ }
46
+
47
+ return str;
48
+ }
49
+
50
+ var _default = padZero;
51
+ exports["default"] = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.5.0",
3
+ "version": "4.7.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -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;
@@ -12,8 +12,15 @@ export default formatMobile;
12
12
  * @example
13
13
  * formatMobile('13345678900') // '133 4567 8900'
14
14
  * formatMobile('13345678900', { char: '-' }) // '133-4567-8900'
15
+ *
16
+ * // 脱敏手机号码
15
17
  * formatMobile('133****1234') // '133 **** 1234'
16
18
  * formatMobile('133****1234', { char: '-' }) // '133-****-1234'
19
+ *
20
+ * // 手机号码位数不够
21
+ * formatMobile('133') // '133'
22
+ * formatMobile('133456') // '133 456'
23
+ * formatMobile('13345678') // '133 4567 8'
17
24
  */
18
25
  declare function formatMobile(mobileNo: string, { char }?: {
19
26
  char?: string | undefined;
package/types/index.d.ts CHANGED
@@ -31,10 +31,12 @@ export { default as setDataURLPrefix } from "./setDataURLPrefix";
31
31
  export { default as normalizeString } from "./normalizeString";
32
32
  export { default as safeDate } from "./safeDate";
33
33
  export { default as formatMobile } from "./formatMobile";
34
+ export { default as padZero } from "./padZero";
34
35
  export { default as plus } from "./plus";
35
36
  export { default as minus } from "./minus";
36
37
  export { default as times } from "./times";
37
38
  export { default as divide } from "./divide";
38
39
  export { default as round } from "./round";
39
40
  export { default as waitTime } from "./waitTime";
41
+ export { default as calculateCursorPosition } from "./calculateCursorPosition";
40
42
  export { setDisableWarning } from "./utils/config";
@@ -4,7 +4,7 @@ export default normalizeString;
4
4
  *
5
5
  * @static
6
6
  * @alias module:Processor.normalizeString
7
- * @see 参考 {@link tttps://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
7
+ * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
8
8
  * @since 4.3.0
9
9
  * @param {*} value 待处理的值
10
10
  * @returns {string} 规整化的值
@@ -0,0 +1,24 @@
1
+ export default padZero;
2
+ /**
3
+ * 前置补零
4
+ *
5
+ * @static
6
+ * @alias module:Processor.padZero
7
+ * @since 4.7.0
8
+ * @param {string|number} value 要处理的值
9
+ * @param {number} [size=2] 指定字符串长度
10
+ * @returns {string} 用零填充数字到给定长度的字符串
11
+ * @example
12
+ * padZero(5); // '05'
13
+ * padZero('5'); // '05'
14
+ *
15
+ * padZero(12); // '12'
16
+ * padZero('12'); // '12'
17
+ *
18
+ * padZero(688); // '688'
19
+ * padZero('688'); // '688'
20
+ *
21
+ * padZero(688, 5); // '00688'
22
+ * padZero('688', 5); // '00688'
23
+ */
24
+ declare function padZero(value: string | number, size?: number | undefined): string;