util-helpers 4.5.0 → 4.6.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.
- package/README.md +5 -3
- package/dist/util-helpers.js +64 -2
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/calculateCursorPosition.js +62 -0
- package/esm/formatMobile.js +7 -0
- package/esm/index.js +1 -0
- package/esm/normalizeString.js +1 -1
- package/lib/calculateCursorPosition.js +73 -0
- package/lib/formatMobile.js +7 -0
- package/lib/index.js +8 -0
- package/lib/normalizeString.js +1 -1
- package/package.json +1 -1
- package/types/calculateCursorPosition.d.ts +25 -0
- package/types/formatMobile.d.ts +7 -0
- package/types/index.d.ts +1 -0
- package/types/normalizeString.d.ts +1 -1
|
@@ -0,0 +1,62 @@
|
|
|
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 示例 {@link https://2950v9.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
|
+
|
|
23
|
+
function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
|
|
24
|
+
var _ref = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {},
|
|
25
|
+
_ref$placeholderChar = _ref.placeholderChar,
|
|
26
|
+
placeholderChar = _ref$placeholderChar === void 0 ? ' ' : _ref$placeholderChar,
|
|
27
|
+
_ref$maskReg = _ref.maskReg,
|
|
28
|
+
maskReg = _ref$maskReg === void 0 ? /\D/g : _ref$maskReg,
|
|
29
|
+
type = _ref.type;
|
|
30
|
+
|
|
31
|
+
var realCtrlValue = normalizeString(prevCtrlValue);
|
|
32
|
+
var realRawValue = normalizeString(rawValue);
|
|
33
|
+
var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
|
|
34
|
+
var editLength = realRawValue.length - realCtrlValue.length;
|
|
35
|
+
var isAddition = editLength > 0;
|
|
36
|
+
var pos = prevPos;
|
|
37
|
+
|
|
38
|
+
if (isAddition) {
|
|
39
|
+
var additionStr = realRawValue.substring(pos - editLength, pos);
|
|
40
|
+
var ctrlCharCount = additionStr.replace(maskReg, '').length;
|
|
41
|
+
pos -= editLength - ctrlCharCount;
|
|
42
|
+
var placeholderCharCount = 0;
|
|
43
|
+
|
|
44
|
+
while (ctrlCharCount > 0) {
|
|
45
|
+
if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
|
|
46
|
+
placeholderCharCount++;
|
|
47
|
+
} else {
|
|
48
|
+
ctrlCharCount--;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
pos += placeholderCharCount;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (type === 'mobile' && (pos === 4 || pos === 9) || type === 'bankCard' && pos > 0 && pos % 5 === 0) {
|
|
56
|
+
pos -= 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return pos;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default calculateCursorPosition;
|
package/esm/formatMobile.js
CHANGED
|
@@ -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
package/esm/normalizeString.js
CHANGED
|
@@ -5,7 +5,7 @@ import convertToString from './utils/convertToString';
|
|
|
5
5
|
*
|
|
6
6
|
* @static
|
|
7
7
|
* @alias module:Processor.normalizeString
|
|
8
|
-
* @see 参考 {@link
|
|
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} 规整化的值
|
|
@@ -0,0 +1,73 @@
|
|
|
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 示例 {@link https://2950v9.csb.app/|点击查看}
|
|
23
|
+
* @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
|
|
24
|
+
* @param {string} prevCtrlValue 上一个格式化后的值
|
|
25
|
+
* @param {string} rawValue 当前输入原值
|
|
26
|
+
* @param {string} ctrlValue 当前格式化后的值
|
|
27
|
+
* @param {Object} [options] 配置项
|
|
28
|
+
* @param {string[]|string} [options.placeholderChar=' '] 占位符
|
|
29
|
+
* @param {RegExp} [options.maskReg=/\D/g] 需要遮盖的字符规则。默认去掉非数字,意味着 ctrlValue 需要去掉非数字。
|
|
30
|
+
* @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
|
|
31
|
+
* @returns {number} 格式化后的光标位置
|
|
32
|
+
*/
|
|
33
|
+
function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
|
|
34
|
+
var _ref = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {},
|
|
35
|
+
_ref$placeholderChar = _ref.placeholderChar,
|
|
36
|
+
placeholderChar = _ref$placeholderChar === void 0 ? ' ' : _ref$placeholderChar,
|
|
37
|
+
_ref$maskReg = _ref.maskReg,
|
|
38
|
+
maskReg = _ref$maskReg === void 0 ? /\D/g : _ref$maskReg,
|
|
39
|
+
type = _ref.type;
|
|
40
|
+
|
|
41
|
+
var realCtrlValue = (0, _normalizeString["default"])(prevCtrlValue);
|
|
42
|
+
var realRawValue = (0, _normalizeString["default"])(rawValue);
|
|
43
|
+
var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
|
|
44
|
+
var editLength = realRawValue.length - realCtrlValue.length;
|
|
45
|
+
var isAddition = editLength > 0;
|
|
46
|
+
var pos = prevPos;
|
|
47
|
+
|
|
48
|
+
if (isAddition) {
|
|
49
|
+
var additionStr = realRawValue.substring(pos - editLength, pos);
|
|
50
|
+
var ctrlCharCount = additionStr.replace(maskReg, '').length;
|
|
51
|
+
pos -= editLength - ctrlCharCount;
|
|
52
|
+
var placeholderCharCount = 0;
|
|
53
|
+
|
|
54
|
+
while (ctrlCharCount > 0) {
|
|
55
|
+
if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
|
|
56
|
+
placeholderCharCount++;
|
|
57
|
+
} else {
|
|
58
|
+
ctrlCharCount--;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
pos += placeholderCharCount;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (type === 'mobile' && (pos === 4 || pos === 9) || type === 'bankCard' && pos > 0 && pos % 5 === 0) {
|
|
66
|
+
pos -= 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return pos;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
var _default = calculateCursorPosition;
|
|
73
|
+
exports["default"] = _default;
|
package/lib/formatMobile.js
CHANGED
|
@@ -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
|
@@ -237,6 +237,12 @@ Object.defineProperty(exports, "waitTime", {
|
|
|
237
237
|
return _waitTime["default"];
|
|
238
238
|
}
|
|
239
239
|
});
|
|
240
|
+
Object.defineProperty(exports, "calculateCursorPosition", {
|
|
241
|
+
enumerable: true,
|
|
242
|
+
get: function get() {
|
|
243
|
+
return _calculateCursorPosition["default"];
|
|
244
|
+
}
|
|
245
|
+
});
|
|
240
246
|
Object.defineProperty(exports, "setDisableWarning", {
|
|
241
247
|
enumerable: true,
|
|
242
248
|
get: function get() {
|
|
@@ -322,6 +328,8 @@ var _round = _interopRequireDefault(require("./round"));
|
|
|
322
328
|
|
|
323
329
|
var _waitTime = _interopRequireDefault(require("./waitTime"));
|
|
324
330
|
|
|
331
|
+
var _calculateCursorPosition = _interopRequireDefault(require("./calculateCursorPosition"));
|
|
332
|
+
|
|
325
333
|
var _config = require("./utils/config");
|
|
326
334
|
|
|
327
335
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
package/lib/normalizeString.js
CHANGED
|
@@ -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
|
|
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/package.json
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
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 示例 {@link https://2950v9.csb.app/|点击查看}
|
|
11
|
+
* @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
|
|
12
|
+
* @param {string} prevCtrlValue 上一个格式化后的值
|
|
13
|
+
* @param {string} rawValue 当前输入原值
|
|
14
|
+
* @param {string} ctrlValue 当前格式化后的值
|
|
15
|
+
* @param {Object} [options] 配置项
|
|
16
|
+
* @param {string[]|string} [options.placeholderChar=' '] 占位符
|
|
17
|
+
* @param {RegExp} [options.maskReg=/\D/g] 需要遮盖的字符规则。默认去掉非数字,意味着 ctrlValue 需要去掉非数字。
|
|
18
|
+
* @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
|
|
19
|
+
* @returns {number} 格式化后的光标位置
|
|
20
|
+
*/
|
|
21
|
+
declare function calculateCursorPosition(prevPos: number, prevCtrlValue: string, rawValue: string, ctrlValue: string, { placeholderChar, maskReg, type }?: {
|
|
22
|
+
placeholderChar?: string | string[] | undefined;
|
|
23
|
+
maskReg?: RegExp | undefined;
|
|
24
|
+
type?: "mobile" | "bankCard" | undefined;
|
|
25
|
+
} | undefined): number;
|
package/types/formatMobile.d.ts
CHANGED
|
@@ -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
|
@@ -37,4 +37,5 @@ export { default as times } from "./times";
|
|
|
37
37
|
export { default as divide } from "./divide";
|
|
38
38
|
export { default as round } from "./round";
|
|
39
39
|
export { default as waitTime } from "./waitTime";
|
|
40
|
+
export { default as calculateCursorPosition } from "./calculateCursorPosition";
|
|
40
41
|
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
|
|
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} 规整化的值
|