ph-utils 0.2.18 → 0.2.20
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/index.js +33 -21
- package/lib/index_m.js +4 -4
- package/lib/validator.d.ts +3 -2
- package/lib/validator.js +40 -34
- package/lib/validator_m.d.ts +2 -1
- package/lib/validator_m.js +38 -34
- package/package.json +1 -1
- package/LICENSE +0 -21
package/lib/index.js
CHANGED
@@ -1,37 +1,32 @@
|
|
1
|
-
"use strict";
|
2
1
|
/**
|
3
2
|
* node 和 web 通用的工具类
|
4
3
|
*/
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.throttle = exports.BaseError = exports.isBoolean = exports.isNumeric = exports.shieldMobile = exports.isBlank = void 0;
|
7
4
|
/**
|
8
5
|
* 验证字符串是否为空
|
9
6
|
* @param str 待验证的字符串
|
10
7
|
* @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
11
8
|
*/
|
12
|
-
function isBlank(str, ignoreWhitespace = true) {
|
9
|
+
export function isBlank(str, ignoreWhitespace = true) {
|
13
10
|
if (str == null) {
|
14
11
|
return true;
|
15
12
|
}
|
16
13
|
return (ignoreWhitespace ? str.trim().length : str.length) === 0;
|
17
14
|
}
|
18
|
-
exports.isBlank = isBlank;
|
19
15
|
/**
|
20
16
|
* 屏蔽手机号,中间部分用 * 展示
|
21
17
|
* @param mobile 待屏蔽的手机号
|
22
18
|
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
23
19
|
*/
|
24
|
-
function shieldMobile(mobile) {
|
25
|
-
let x1 = Math.
|
26
|
-
let x2 = Math.
|
20
|
+
export function shieldMobile(mobile) {
|
21
|
+
let x1 = Math.floor(mobile.length / 2);
|
22
|
+
let x2 = Math.ceil(x1 / 2);
|
27
23
|
let shields = [' '];
|
28
|
-
for (let i = 0; i < x1; i++) {
|
24
|
+
for (let i = 0; i < x1 - 1; i++) {
|
29
25
|
shields.push('*');
|
30
26
|
}
|
31
27
|
shields.push(' ');
|
32
|
-
return mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1);
|
28
|
+
return (mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1 - 1));
|
33
29
|
}
|
34
|
-
exports.shieldMobile = shieldMobile;
|
35
30
|
/**
|
36
31
|
* 验证参数是否是数字
|
37
32
|
* @param str 待验证的字符串
|
@@ -40,31 +35,24 @@ exports.shieldMobile = shieldMobile;
|
|
40
35
|
* @param numericParam.isFloat 是否是小数
|
41
36
|
* @returns true 是数字, false 不是数字
|
42
37
|
*/
|
43
|
-
function isNumeric(str, numericParam) {
|
38
|
+
export function isNumeric(str, numericParam) {
|
44
39
|
numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
|
45
40
|
let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
|
46
41
|
let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
|
47
42
|
return new RegExp('^' + symbol + main + '$').test(str);
|
48
43
|
}
|
49
|
-
exports.isNumeric = isNumeric;
|
50
44
|
/**
|
51
45
|
* 验证参数是否是Boolean 类型
|
52
46
|
* @param str 待验证的字符串
|
53
47
|
* @returns
|
54
48
|
*/
|
55
|
-
function isBoolean(str) {
|
49
|
+
export function isBoolean(str) {
|
56
50
|
return ['true', 'false'].indexOf(str) >= 0;
|
57
51
|
}
|
58
|
-
exports.isBoolean = isBoolean;
|
59
52
|
/**
|
60
53
|
* 带有错误名称标记的错误类型
|
61
54
|
*/
|
62
|
-
class BaseError extends Error {
|
63
|
-
/**
|
64
|
-
* 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
|
65
|
-
* 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
|
66
|
-
*/
|
67
|
-
name;
|
55
|
+
export class BaseError extends Error {
|
68
56
|
constructor() {
|
69
57
|
if (arguments.length === 1) {
|
70
58
|
super(arguments[0]);
|
@@ -76,6 +64,30 @@ class BaseError extends Error {
|
|
76
64
|
}
|
77
65
|
}
|
78
66
|
}
|
67
|
+
/**
|
68
|
+
* 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
|
69
|
+
* @param func 要节流的函数
|
70
|
+
* @param wait 需要节流的毫秒
|
71
|
+
* @returns
|
72
|
+
*/
|
73
|
+
export function throttle(func, wait = 300) {
|
74
|
+
let t = -1;
|
75
|
+
return function (...args) {
|
76
|
+
const self = this;
|
77
|
+
clearTimeout(t);
|
78
|
+
t = setTimeout(() => {
|
79
|
+
func.apply(self, args);
|
80
|
+
}, wait);
|
81
|
+
};
|
82
|
+
}
|
83
|
+
|
84
|
+
}
|
85
|
+
else {
|
86
|
+
super(arguments[1]);
|
87
|
+
this.name = arguments[0];
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
79
91
|
exports.BaseError = BaseError;
|
80
92
|
/**
|
81
93
|
* 创建一个节流函数,在 wait 秒内最多执行 func 一次的函数。
|
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
@@ -15,6 +15,7 @@ export interface SchemaType {
|
|
15
15
|
required?: boolean;
|
16
16
|
type?: string | ((v: any) => void);
|
17
17
|
rules: RuleType[];
|
18
|
+
message?: string;
|
18
19
|
}
|
19
20
|
/**
|
20
21
|
* 数据验证器,除了进行数据验证外,还可以同时进行数据转化
|
@@ -29,7 +30,7 @@ declare class Validator {
|
|
29
30
|
*/
|
30
31
|
constructor(schemas: SchemaType[]);
|
31
32
|
/**
|
32
|
-
*
|
33
|
+
* 进行数据验证
|
33
34
|
* @param data 待验证的数据
|
34
35
|
* @returns
|
35
36
|
*/
|
@@ -43,4 +44,4 @@ declare class Validator {
|
|
43
44
|
private _validateRule;
|
44
45
|
private _parseStringRule;
|
45
46
|
}
|
46
|
-
export
|
47
|
+
export default Validator;
|
package/lib/validator.js
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
/**
|
4
4
|
* 数据验证器
|
5
5
|
*/
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
7
|
// 默认的错误提示信息
|
7
8
|
const defaultMsgs = {
|
8
9
|
mobile: '请输入正确的手机号',
|
@@ -58,16 +59,19 @@ class Validator {
|
|
58
59
|
let rules = [];
|
59
60
|
let rule = schema.rules;
|
60
61
|
if (typeof rule === 'string') {
|
61
|
-
rules = rules.concat(this._parseStringRule(rule));
|
62
|
+
rules = rules.concat(this._parseStringRule(rule, schema.message));
|
62
63
|
}
|
63
64
|
else if (rule instanceof Array) {
|
64
65
|
for (let ruleItem of rule) {
|
65
66
|
if (typeof ruleItem === 'string') {
|
66
|
-
rules.push(...this._parseStringRule(ruleItem));
|
67
|
+
rules.push(...this._parseStringRule(ruleItem, schema.message));
|
67
68
|
}
|
68
69
|
else if (ruleItem instanceof RegExp ||
|
69
70
|
typeof ruleItem === 'function') {
|
70
|
-
rules.push({
|
71
|
+
rules.push({
|
72
|
+
rule: ruleItem,
|
73
|
+
message: schema.message || defaultMsg,
|
74
|
+
});
|
71
75
|
}
|
72
76
|
else {
|
73
77
|
if (typeof ruleItem.rule === 'string') {
|
@@ -94,7 +98,7 @@ class Validator {
|
|
94
98
|
this.rules = parsedRules;
|
95
99
|
}
|
96
100
|
/**
|
97
|
-
*
|
101
|
+
* 进行数据验证
|
98
102
|
* @param data 待验证的数据
|
99
103
|
* @returns
|
100
104
|
*/
|
@@ -103,7 +107,8 @@ class Validator {
|
|
103
107
|
let errMsg = '';
|
104
108
|
let errKey = '';
|
105
109
|
for (let key in this.rules) {
|
106
|
-
|
110
|
+
// eslint-disable-next-line no-prototype-builtins
|
111
|
+
if (this.rules.hasOwnProperty(key)) {
|
107
112
|
errMsg = this._validateRule(this.rules[key], data[key], data);
|
108
113
|
if (errMsg !== '') {
|
109
114
|
errKey = key;
|
@@ -142,33 +147,31 @@ class Validator {
|
|
142
147
|
let errMsg = '';
|
143
148
|
for (let rule of rules) {
|
144
149
|
// 如果数据为空,则判断是否是必填
|
145
|
-
if (
|
146
|
-
if (
|
150
|
+
if (rule.rule === 'required') {
|
151
|
+
if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
|
147
152
|
errMsg = rule.message;
|
148
153
|
}
|
149
154
|
}
|
150
|
-
|
151
|
-
if (
|
152
|
-
|
153
|
-
errMsg = rule.message;
|
154
|
-
}
|
155
|
-
}
|
156
|
-
else if (rule.sameKey != null) {
|
157
|
-
if (data != null) {
|
158
|
-
if (!ruleFns.same(value, data[rule.sameKey])) {
|
159
|
-
errMsg = rule.message;
|
160
|
-
}
|
161
|
-
}
|
155
|
+
if (typeof rule.rule === 'function') {
|
156
|
+
if (!rule.rule(value)) {
|
157
|
+
errMsg = rule.message;
|
162
158
|
}
|
163
|
-
|
164
|
-
|
159
|
+
}
|
160
|
+
else if (rule.sameKey != null) {
|
161
|
+
if (data != null) {
|
162
|
+
if (!ruleFns.same(value, data[rule.sameKey])) {
|
165
163
|
errMsg = rule.message;
|
166
164
|
}
|
167
165
|
}
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
166
|
+
}
|
167
|
+
else if (rule.rule === 'required') {
|
168
|
+
if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
|
169
|
+
errMsg = rule.message;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
else {
|
173
|
+
if (!ruleFns.pattern(rule.rule, String(value))) {
|
174
|
+
errMsg = rule.message;
|
172
175
|
}
|
173
176
|
}
|
174
177
|
if (errMsg !== '') {
|
@@ -181,29 +184,32 @@ class Validator {
|
|
181
184
|
let rules = [];
|
182
185
|
let trule = rule.split('|');
|
183
186
|
for (let r of trule) {
|
184
|
-
let message =
|
185
|
-
let rrule;
|
187
|
+
let message = ruleErrMsg;
|
188
|
+
let rrule = null;
|
186
189
|
let sameKey;
|
187
190
|
if (ruleRegexs.same.test(r)) {
|
188
191
|
let m = r.match(ruleRegexs.same);
|
189
192
|
if (m != null) {
|
190
193
|
rrule = ruleRegexs.same;
|
191
|
-
|
192
|
-
|
194
|
+
let m = r.match(ruleRegexs.same);
|
195
|
+
if (m != null) {
|
196
|
+
sameKey = m[1];
|
197
|
+
}
|
198
|
+
message = message || defaultMsgs['same'];
|
193
199
|
}
|
194
200
|
}
|
195
201
|
else if (rule === 'required') {
|
196
202
|
rrule = 'required';
|
197
|
-
message = ruleErrMsg || defaultMsgs.required;
|
203
|
+
message = message || ruleErrMsg || defaultMsgs.required;
|
204
|
+
// eslint-disable-next-line no-prototype-builtins
|
198
205
|
}
|
199
|
-
else if (
|
206
|
+
else if (ruleRegexs.hasOwnProperty(r)) {
|
200
207
|
rrule = ruleRegexs[r];
|
201
|
-
message = defaultMsgs[r]
|
208
|
+
message = message || defaultMsgs[r];
|
202
209
|
}
|
203
|
-
message = ruleErrMsg || message;
|
204
210
|
rules.push({ rule: rrule, message: message, sameKey });
|
205
211
|
}
|
206
212
|
return rules;
|
207
213
|
}
|
208
214
|
}
|
209
|
-
|
215
|
+
exports.default = Validator;
|
package/lib/validator_m.d.ts
CHANGED
@@ -15,6 +15,7 @@ export interface SchemaType {
|
|
15
15
|
required?: boolean;
|
16
16
|
type?: string | ((v: any) => void);
|
17
17
|
rules: RuleType[];
|
18
|
+
message?: string;
|
18
19
|
}
|
19
20
|
/**
|
20
21
|
* 数据验证器,除了进行数据验证外,还可以同时进行数据转化
|
@@ -29,7 +30,7 @@ declare class Validator {
|
|
29
30
|
*/
|
30
31
|
constructor(schemas: SchemaType[]);
|
31
32
|
/**
|
32
|
-
*
|
33
|
+
* 进行数据验证
|
33
34
|
* @param data 待验证的数据
|
34
35
|
* @returns
|
35
36
|
*/
|
package/lib/validator_m.js
CHANGED
@@ -54,16 +54,19 @@ class Validator {
|
|
54
54
|
let rules = [];
|
55
55
|
let rule = schema.rules;
|
56
56
|
if (typeof rule === 'string') {
|
57
|
-
rules = rules.concat(this._parseStringRule(rule));
|
57
|
+
rules = rules.concat(this._parseStringRule(rule, schema.message));
|
58
58
|
}
|
59
59
|
else if (rule instanceof Array) {
|
60
60
|
for (let ruleItem of rule) {
|
61
61
|
if (typeof ruleItem === 'string') {
|
62
|
-
rules.push(...this._parseStringRule(ruleItem));
|
62
|
+
rules.push(...this._parseStringRule(ruleItem, schema.message));
|
63
63
|
}
|
64
64
|
else if (ruleItem instanceof RegExp ||
|
65
65
|
typeof ruleItem === 'function') {
|
66
|
-
rules.push({
|
66
|
+
rules.push({
|
67
|
+
rule: ruleItem,
|
68
|
+
message: schema.message || defaultMsg,
|
69
|
+
});
|
67
70
|
}
|
68
71
|
else {
|
69
72
|
if (typeof ruleItem.rule === 'string') {
|
@@ -90,7 +93,7 @@ class Validator {
|
|
90
93
|
this.rules = parsedRules;
|
91
94
|
}
|
92
95
|
/**
|
93
|
-
*
|
96
|
+
* 进行数据验证
|
94
97
|
* @param data 待验证的数据
|
95
98
|
* @returns
|
96
99
|
*/
|
@@ -99,7 +102,8 @@ class Validator {
|
|
99
102
|
let errMsg = '';
|
100
103
|
let errKey = '';
|
101
104
|
for (let key in this.rules) {
|
102
|
-
|
105
|
+
// eslint-disable-next-line no-prototype-builtins
|
106
|
+
if (this.rules.hasOwnProperty(key)) {
|
103
107
|
errMsg = this._validateRule(this.rules[key], data[key], data);
|
104
108
|
if (errMsg !== '') {
|
105
109
|
errKey = key;
|
@@ -138,33 +142,31 @@ class Validator {
|
|
138
142
|
let errMsg = '';
|
139
143
|
for (let rule of rules) {
|
140
144
|
// 如果数据为空,则判断是否是必填
|
141
|
-
if (
|
142
|
-
if (
|
145
|
+
if (rule.rule === 'required') {
|
146
|
+
if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
|
143
147
|
errMsg = rule.message;
|
144
148
|
}
|
145
149
|
}
|
146
|
-
|
147
|
-
if (
|
148
|
-
|
149
|
-
errMsg = rule.message;
|
150
|
-
}
|
151
|
-
}
|
152
|
-
else if (rule.sameKey != null) {
|
153
|
-
if (data != null) {
|
154
|
-
if (!ruleFns.same(value, data[rule.sameKey])) {
|
155
|
-
errMsg = rule.message;
|
156
|
-
}
|
157
|
-
}
|
150
|
+
if (typeof rule.rule === 'function') {
|
151
|
+
if (!rule.rule(value)) {
|
152
|
+
errMsg = rule.message;
|
158
153
|
}
|
159
|
-
|
160
|
-
|
154
|
+
}
|
155
|
+
else if (rule.sameKey != null) {
|
156
|
+
if (data != null) {
|
157
|
+
if (!ruleFns.same(value, data[rule.sameKey])) {
|
161
158
|
errMsg = rule.message;
|
162
159
|
}
|
163
160
|
}
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
161
|
+
}
|
162
|
+
else if (rule.rule === 'required') {
|
163
|
+
if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
|
164
|
+
errMsg = rule.message;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
else {
|
168
|
+
if (!ruleFns.pattern(rule.rule, String(value))) {
|
169
|
+
errMsg = rule.message;
|
168
170
|
}
|
169
171
|
}
|
170
172
|
if (errMsg !== '') {
|
@@ -177,30 +179,32 @@ class Validator {
|
|
177
179
|
let rules = [];
|
178
180
|
let trule = rule.split('|');
|
179
181
|
for (let r of trule) {
|
180
|
-
let message =
|
181
|
-
let rrule;
|
182
|
+
let message = ruleErrMsg;
|
183
|
+
let rrule = null;
|
182
184
|
let sameKey;
|
183
185
|
if (ruleRegexs.same.test(r)) {
|
184
186
|
let m = r.match(ruleRegexs.same);
|
185
187
|
if (m != null) {
|
186
188
|
rrule = ruleRegexs.same;
|
187
|
-
|
188
|
-
|
189
|
+
let m = r.match(ruleRegexs.same);
|
190
|
+
if (m != null) {
|
191
|
+
sameKey = m[1];
|
192
|
+
}
|
193
|
+
message = message || defaultMsgs['same'];
|
189
194
|
}
|
190
195
|
}
|
191
196
|
else if (rule === 'required') {
|
192
197
|
rrule = 'required';
|
193
|
-
message = ruleErrMsg || defaultMsgs.required;
|
198
|
+
message = message || ruleErrMsg || defaultMsgs.required;
|
199
|
+
// eslint-disable-next-line no-prototype-builtins
|
194
200
|
}
|
195
|
-
else if (
|
201
|
+
else if (ruleRegexs.hasOwnProperty(r)) {
|
196
202
|
rrule = ruleRegexs[r];
|
197
|
-
message = defaultMsgs[r]
|
203
|
+
message = message || defaultMsgs[r];
|
198
204
|
}
|
199
|
-
message = ruleErrMsg || message;
|
200
205
|
rules.push({ rule: rrule, message: message, sameKey });
|
201
206
|
}
|
202
207
|
return rules;
|
203
208
|
}
|
204
209
|
}
|
205
|
-
// @ts-ignore: Unreachable code error
|
206
210
|
export default Validator;
|
package/package.json
CHANGED
package/LICENSE
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2021 Tenny
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|