ph-utils 0.2.16 → 0.2.19
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/index.js +4 -4
- package/lib/index_m.js +4 -4
- package/lib/validator.d.ts +2 -2
- package/lib/validator.js +10 -9
- package/lib/validator_m.d.ts +2 -2
- package/lib/validator_m.js +10 -9
- package/package.json +1 -1
package/lib/index.js
CHANGED
@@ -22,14 +22,14 @@ exports.isBlank = isBlank;
|
|
22
22
|
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
23
23
|
*/
|
24
24
|
function shieldMobile(mobile) {
|
25
|
-
let x1 = Math.
|
26
|
-
let x2 = Math.
|
25
|
+
let x1 = Math.floor(mobile.length / 2);
|
26
|
+
let x2 = Math.ceil(x1 / 2);
|
27
27
|
let shields = [' '];
|
28
|
-
for (let i = 0; i < x1; i++) {
|
28
|
+
for (let i = 0; i < x1 - 1; i++) {
|
29
29
|
shields.push('*');
|
30
30
|
}
|
31
31
|
shields.push(' ');
|
32
|
-
return mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1);
|
32
|
+
return (mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1));
|
33
33
|
}
|
34
34
|
exports.shieldMobile = shieldMobile;
|
35
35
|
/**
|
package/lib/index_m.js
CHANGED
@@ -18,14 +18,14 @@ export function isBlank(str, ignoreWhitespace = true) {
|
|
18
18
|
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
19
19
|
*/
|
20
20
|
export function shieldMobile(mobile) {
|
21
|
-
let x1 = Math.
|
22
|
-
let x2 = Math.
|
21
|
+
let x1 = Math.floor(mobile.length / 2);
|
22
|
+
let x2 = Math.ceil(x1 / 2);
|
23
23
|
let shields = [' '];
|
24
|
-
for (let i = 0; i < x1; i++) {
|
24
|
+
for (let i = 0; i < x1 - 1; i++) {
|
25
25
|
shields.push('*');
|
26
26
|
}
|
27
27
|
shields.push(' ');
|
28
|
-
return mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1);
|
28
|
+
return (mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1));
|
29
29
|
}
|
30
30
|
/**
|
31
31
|
* 验证参数是否是数字
|
package/lib/validator.d.ts
CHANGED
@@ -29,7 +29,7 @@ declare class Validator {
|
|
29
29
|
*/
|
30
30
|
constructor(schemas: SchemaType[]);
|
31
31
|
/**
|
32
|
-
*
|
32
|
+
* 进行数据验证
|
33
33
|
* @param data 待验证的数据
|
34
34
|
* @returns
|
35
35
|
*/
|
@@ -39,7 +39,7 @@ declare class Validator {
|
|
39
39
|
* @param key 指定待验证的 key
|
40
40
|
* @param value 待验证的数据
|
41
41
|
*/
|
42
|
-
validateKey(key: string, value: any): Promise<boolean>;
|
42
|
+
validateKey(key: string, value: any, data?: any): Promise<boolean>;
|
43
43
|
private _validateRule;
|
44
44
|
private _parseStringRule;
|
45
45
|
}
|
package/lib/validator.js
CHANGED
@@ -56,9 +56,6 @@ class Validator {
|
|
56
56
|
for (let schema of schemas) {
|
57
57
|
// 解析规则
|
58
58
|
let rules = [];
|
59
|
-
if (schema.required === true) {
|
60
|
-
rules.push(this._parseStringRule('required'));
|
61
|
-
}
|
62
59
|
let rule = schema.rules;
|
63
60
|
if (typeof rule === 'string') {
|
64
61
|
rules = rules.concat(this._parseStringRule(rule));
|
@@ -66,7 +63,7 @@ class Validator {
|
|
66
63
|
else if (rule instanceof Array) {
|
67
64
|
for (let ruleItem of rule) {
|
68
65
|
if (typeof ruleItem === 'string') {
|
69
|
-
rules
|
66
|
+
rules.push(...this._parseStringRule(ruleItem));
|
70
67
|
}
|
71
68
|
else if (ruleItem instanceof RegExp ||
|
72
69
|
typeof ruleItem === 'function') {
|
@@ -74,7 +71,7 @@ class Validator {
|
|
74
71
|
}
|
75
72
|
else {
|
76
73
|
if (typeof ruleItem.rule === 'string') {
|
77
|
-
rules
|
74
|
+
rules.push(...this._parseStringRule(ruleItem.rule, ruleItem.message));
|
78
75
|
}
|
79
76
|
else {
|
80
77
|
rules.push({
|
@@ -88,12 +85,16 @@ class Validator {
|
|
88
85
|
else {
|
89
86
|
rules.push({ rule, message: defaultMsg });
|
90
87
|
}
|
88
|
+
if (schema.required === true &&
|
89
|
+
rules.findIndex((r) => r.rule === 'required') === -1) {
|
90
|
+
rules.push(this._parseStringRule('required'));
|
91
|
+
}
|
91
92
|
parsedRules[schema.key] = rules;
|
92
93
|
}
|
93
94
|
this.rules = parsedRules;
|
94
95
|
}
|
95
96
|
/**
|
96
|
-
*
|
97
|
+
* 进行数据验证
|
97
98
|
* @param data 待验证的数据
|
98
99
|
* @returns
|
99
100
|
*/
|
@@ -124,10 +125,10 @@ class Validator {
|
|
124
125
|
* @param key 指定待验证的 key
|
125
126
|
* @param value 待验证的数据
|
126
127
|
*/
|
127
|
-
validateKey(key, value) {
|
128
|
+
validateKey(key, value, data) {
|
128
129
|
return new Promise((resolve, reject) => {
|
129
130
|
let keyRules = this.rules[key];
|
130
|
-
let errMsg = this._validateRule(keyRules, value,
|
131
|
+
let errMsg = this._validateRule(keyRules, value, data);
|
131
132
|
if (errMsg !== '') {
|
132
133
|
errMsg = errMsg.replace('%s', key);
|
133
134
|
reject(new ValidateError(key, errMsg));
|
@@ -193,7 +194,7 @@ class Validator {
|
|
193
194
|
}
|
194
195
|
else if (rule === 'required') {
|
195
196
|
rrule = 'required';
|
196
|
-
message = defaultMsgs.required;
|
197
|
+
message = ruleErrMsg || defaultMsgs.required;
|
197
198
|
}
|
198
199
|
else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
|
199
200
|
rrule = ruleRegexs[r];
|
package/lib/validator_m.d.ts
CHANGED
@@ -29,7 +29,7 @@ declare class Validator {
|
|
29
29
|
*/
|
30
30
|
constructor(schemas: SchemaType[]);
|
31
31
|
/**
|
32
|
-
*
|
32
|
+
* 进行数据验证
|
33
33
|
* @param data 待验证的数据
|
34
34
|
* @returns
|
35
35
|
*/
|
@@ -39,7 +39,7 @@ declare class Validator {
|
|
39
39
|
* @param key 指定待验证的 key
|
40
40
|
* @param value 待验证的数据
|
41
41
|
*/
|
42
|
-
validateKey(key: string, value: any): Promise<boolean>;
|
42
|
+
validateKey(key: string, value: any, data?: any): Promise<boolean>;
|
43
43
|
private _validateRule;
|
44
44
|
private _parseStringRule;
|
45
45
|
}
|
package/lib/validator_m.js
CHANGED
@@ -52,9 +52,6 @@ class Validator {
|
|
52
52
|
for (let schema of schemas) {
|
53
53
|
// 解析规则
|
54
54
|
let rules = [];
|
55
|
-
if (schema.required === true) {
|
56
|
-
rules.push(this._parseStringRule('required'));
|
57
|
-
}
|
58
55
|
let rule = schema.rules;
|
59
56
|
if (typeof rule === 'string') {
|
60
57
|
rules = rules.concat(this._parseStringRule(rule));
|
@@ -62,7 +59,7 @@ class Validator {
|
|
62
59
|
else if (rule instanceof Array) {
|
63
60
|
for (let ruleItem of rule) {
|
64
61
|
if (typeof ruleItem === 'string') {
|
65
|
-
rules
|
62
|
+
rules.push(...this._parseStringRule(ruleItem));
|
66
63
|
}
|
67
64
|
else if (ruleItem instanceof RegExp ||
|
68
65
|
typeof ruleItem === 'function') {
|
@@ -70,7 +67,7 @@ class Validator {
|
|
70
67
|
}
|
71
68
|
else {
|
72
69
|
if (typeof ruleItem.rule === 'string') {
|
73
|
-
rules
|
70
|
+
rules.push(...this._parseStringRule(ruleItem.rule, ruleItem.message));
|
74
71
|
}
|
75
72
|
else {
|
76
73
|
rules.push({
|
@@ -84,12 +81,16 @@ class Validator {
|
|
84
81
|
else {
|
85
82
|
rules.push({ rule, message: defaultMsg });
|
86
83
|
}
|
84
|
+
if (schema.required === true &&
|
85
|
+
rules.findIndex((r) => r.rule === 'required') === -1) {
|
86
|
+
rules.push(this._parseStringRule('required'));
|
87
|
+
}
|
87
88
|
parsedRules[schema.key] = rules;
|
88
89
|
}
|
89
90
|
this.rules = parsedRules;
|
90
91
|
}
|
91
92
|
/**
|
92
|
-
*
|
93
|
+
* 进行数据验证
|
93
94
|
* @param data 待验证的数据
|
94
95
|
* @returns
|
95
96
|
*/
|
@@ -120,10 +121,10 @@ class Validator {
|
|
120
121
|
* @param key 指定待验证的 key
|
121
122
|
* @param value 待验证的数据
|
122
123
|
*/
|
123
|
-
validateKey(key, value) {
|
124
|
+
validateKey(key, value, data) {
|
124
125
|
return new Promise((resolve, reject) => {
|
125
126
|
let keyRules = this.rules[key];
|
126
|
-
let errMsg = this._validateRule(keyRules, value,
|
127
|
+
let errMsg = this._validateRule(keyRules, value, data);
|
127
128
|
if (errMsg !== '') {
|
128
129
|
errMsg = errMsg.replace('%s', key);
|
129
130
|
reject(new ValidateError(key, errMsg));
|
@@ -189,7 +190,7 @@ class Validator {
|
|
189
190
|
}
|
190
191
|
else if (rule === 'required') {
|
191
192
|
rrule = 'required';
|
192
|
-
message = defaultMsgs.required;
|
193
|
+
message = ruleErrMsg || defaultMsgs.required;
|
193
194
|
}
|
194
195
|
else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
|
195
196
|
rrule = ruleRegexs[r];
|