ph-utils 0.2.3 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/lib/date.js +4 -4
- package/lib/dom.d.ts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index_m.d.ts +1 -1
- package/lib/server.js +1 -1
- package/lib/validator.d.ts +13 -8
- package/lib/validator.js +10 -5
- package/lib/validator_m.d.ts +13 -8
- package/lib/validator_m.js +9 -5
- package/lib/web.js +1 -1
- package/package.json +1 -1
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
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.
|
package/lib/date.js
CHANGED
@@ -100,10 +100,10 @@ function startOf(date, unit, isEnd = false) {
|
|
100
100
|
exports.startOf = startOf;
|
101
101
|
/**
|
102
102
|
* 日期加上指定时间后的日期
|
103
|
-
* @param date 指定的日期
|
104
|
-
* @param num
|
105
|
-
* @param unit 需要添加的单位,date - 加减天数
|
106
|
-
* @param fmt 可选参数,如果传递了格式化的单位,则返回格式化后的日期, 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒
|
103
|
+
* @param date {Date | number | string | null} 指定的日期
|
104
|
+
* @param num {number} 需要添加的数字, 如果这个参数传递一个小于0的数字,则就是日期减去相应的数字
|
105
|
+
* @param unit {string} 需要添加的单位,date - 加减天数
|
106
|
+
* @param fmt {string} 可选参数,如果传递了格式化的单位,则返回格式化后的日期, 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒
|
107
107
|
* @returns {Date | string} 如果传递了 fmt 参数,则返回 string,否则返回 Date
|
108
108
|
*/
|
109
109
|
function add(date, num, unit, fmt) {
|
package/lib/dom.d.ts
CHANGED
@@ -41,7 +41,7 @@ export declare function transform(element: HTMLElement, value: string): void;
|
|
41
41
|
* @param {function} event 事件处理函数
|
42
42
|
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
43
43
|
*/
|
44
|
-
export declare function on(element: HTMLElement, listener: string, fn: (e:
|
44
|
+
export declare function on<T extends Event>(element: HTMLElement, listener: string, fn: (e: T, target?: HTMLElement, flag?: string) => void, once?: boolean | {
|
45
45
|
once?: boolean;
|
46
46
|
eventFlag?: string;
|
47
47
|
eventStop?: boolean;
|
package/lib/index.d.ts
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
* @param str 待验证的字符串
|
7
7
|
* @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
8
8
|
*/
|
9
|
-
export declare function isBlank(str?: string, ignoreWhitespace?: boolean): boolean;
|
9
|
+
export declare function isBlank(str?: string | null, ignoreWhitespace?: boolean): boolean;
|
10
10
|
/**
|
11
11
|
* 屏蔽手机号,中间部分用 * 展示
|
12
12
|
* @param mobile 待屏蔽的手机号
|
package/lib/index_m.d.ts
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
* @param str 待验证的字符串
|
7
7
|
* @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
8
8
|
*/
|
9
|
-
export declare function isBlank(str?: string, ignoreWhitespace?: boolean): boolean;
|
9
|
+
export declare function isBlank(str?: string | null, ignoreWhitespace?: boolean): boolean;
|
10
10
|
/**
|
11
11
|
* 屏蔽手机号,中间部分用 * 展示
|
12
12
|
* @param mobile 待屏蔽的手机号
|
package/lib/server.js
CHANGED
@@ -59,7 +59,7 @@ module.exports = {
|
|
59
59
|
options = { ...(options || {}) };
|
60
60
|
const errorName = options.errorName || 'ExecError';
|
61
61
|
delete options.errorName;
|
62
|
-
|
62
|
+
child_process_1.exec(cmd, options, (error, stdout, stderr) => {
|
63
63
|
let err = null;
|
64
64
|
let rs = null;
|
65
65
|
if (error) {
|
package/lib/validator.d.ts
CHANGED
@@ -1,8 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* 数据验证器
|
3
|
+
*/
|
1
4
|
interface RuleItem {
|
2
5
|
rule: RegExp | ((v: any) => boolean);
|
3
6
|
message: string;
|
4
7
|
sameKey?: string;
|
5
8
|
}
|
9
|
+
export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
10
|
+
rule: string | RegExp | ((v: any) => boolean);
|
11
|
+
message?: string;
|
12
|
+
});
|
13
|
+
export interface SchemaType {
|
14
|
+
key: string;
|
15
|
+
type?: string | ((v: any) => void);
|
16
|
+
rules: RuleType[];
|
17
|
+
}
|
6
18
|
/**
|
7
19
|
* 数据验证器,除了进行数据验证外,还可以同时进行数据转化
|
8
20
|
*/
|
@@ -17,14 +29,7 @@ declare class Validator {
|
|
17
29
|
* 构造数据验证转换器
|
18
30
|
* @param schemas 配置验证转换规则
|
19
31
|
*/
|
20
|
-
constructor(schemas:
|
21
|
-
key: string;
|
22
|
-
type?: string | ((v: any) => void);
|
23
|
-
rules: string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
24
|
-
rule: string | RegExp | ((v: any) => boolean);
|
25
|
-
message?: string;
|
26
|
-
})[];
|
27
|
-
}[]);
|
32
|
+
constructor(schemas: SchemaType[]);
|
28
33
|
/**
|
29
34
|
* 进行数据验证,同时根据 type 进行数据类型转换
|
30
35
|
* @param data 待验证的数据
|
package/lib/validator.js
CHANGED
@@ -53,9 +53,11 @@ const typeFns = {
|
|
53
53
|
};
|
54
54
|
class ValidateError extends Error {
|
55
55
|
name;
|
56
|
-
|
56
|
+
key;
|
57
|
+
constructor(key, msg) {
|
57
58
|
super(msg);
|
58
59
|
this.name = 'ValidateError';
|
60
|
+
this.key = key;
|
59
61
|
}
|
60
62
|
}
|
61
63
|
/**
|
@@ -95,7 +97,7 @@ class Validator {
|
|
95
97
|
}
|
96
98
|
else {
|
97
99
|
if (typeof ruleItem.rule === 'string') {
|
98
|
-
rules = rules.concat(this._parseStringRule(ruleItem.rule));
|
100
|
+
rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
|
99
101
|
}
|
100
102
|
else {
|
101
103
|
rules.push({ rule: ruleItem.rule, message: ruleItem.message || defaultMsg });
|
@@ -119,6 +121,7 @@ class Validator {
|
|
119
121
|
validate(data) {
|
120
122
|
return new Promise((resolve, reject) => {
|
121
123
|
let errMsg = '';
|
124
|
+
let errKey = '';
|
122
125
|
let resData = {};
|
123
126
|
for (let key in this.rules) {
|
124
127
|
if ({}.hasOwnProperty.call(this.rules, key)) {
|
@@ -127,6 +130,7 @@ class Validator {
|
|
127
130
|
resData[key] = this._conversionType(this.types[key], data[key]);
|
128
131
|
}
|
129
132
|
else {
|
133
|
+
errKey = key;
|
130
134
|
errMsg = errMsg.replace('%s', key);
|
131
135
|
break;
|
132
136
|
}
|
@@ -136,7 +140,7 @@ class Validator {
|
|
136
140
|
resolve(resData);
|
137
141
|
}
|
138
142
|
else {
|
139
|
-
reject(new ValidateError(errMsg));
|
143
|
+
reject(new ValidateError(errKey, errMsg));
|
140
144
|
}
|
141
145
|
});
|
142
146
|
}
|
@@ -154,7 +158,7 @@ class Validator {
|
|
154
158
|
}
|
155
159
|
else {
|
156
160
|
errMsg = errMsg.replace('%s', key);
|
157
|
-
reject(new ValidateError(errMsg));
|
161
|
+
reject(new ValidateError(key, errMsg));
|
158
162
|
}
|
159
163
|
});
|
160
164
|
}
|
@@ -192,7 +196,7 @@ class Validator {
|
|
192
196
|
}
|
193
197
|
return errMsg;
|
194
198
|
}
|
195
|
-
_parseStringRule(rule) {
|
199
|
+
_parseStringRule(rule, ruleErrMsg) {
|
196
200
|
let rules = [];
|
197
201
|
let trule = rule.split('|');
|
198
202
|
for (let r of trule) {
|
@@ -211,6 +215,7 @@ class Validator {
|
|
211
215
|
rrule = ruleRegexs[r];
|
212
216
|
message = defaultMsgs[r] || defaultMsg;
|
213
217
|
}
|
218
|
+
message = ruleErrMsg || message;
|
214
219
|
rules.push({ rule: rrule, message: message, sameKey });
|
215
220
|
}
|
216
221
|
return rules;
|
package/lib/validator_m.d.ts
CHANGED
@@ -1,8 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* 数据验证器
|
3
|
+
*/
|
1
4
|
interface RuleItem {
|
2
5
|
rule: RegExp | ((v: any) => boolean);
|
3
6
|
message: string;
|
4
7
|
sameKey?: string;
|
5
8
|
}
|
9
|
+
export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
10
|
+
rule: string | RegExp | ((v: any) => boolean);
|
11
|
+
message?: string;
|
12
|
+
});
|
13
|
+
export interface SchemaType {
|
14
|
+
key: string;
|
15
|
+
type?: string | ((v: any) => void);
|
16
|
+
rules: RuleType[];
|
17
|
+
}
|
6
18
|
/**
|
7
19
|
* 数据验证器,除了进行数据验证外,还可以同时进行数据转化
|
8
20
|
*/
|
@@ -17,14 +29,7 @@ declare class Validator {
|
|
17
29
|
* 构造数据验证转换器
|
18
30
|
* @param schemas 配置验证转换规则
|
19
31
|
*/
|
20
|
-
constructor(schemas:
|
21
|
-
key: string;
|
22
|
-
type?: string | ((v: any) => void);
|
23
|
-
rules: string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
24
|
-
rule: string | RegExp | ((v: any) => boolean);
|
25
|
-
message?: string;
|
26
|
-
})[];
|
27
|
-
}[]);
|
32
|
+
constructor(schemas: SchemaType[]);
|
28
33
|
/**
|
29
34
|
* 进行数据验证,同时根据 type 进行数据类型转换
|
30
35
|
* @param data 待验证的数据
|
package/lib/validator_m.js
CHANGED
@@ -51,9 +51,10 @@ const typeFns = {
|
|
51
51
|
},
|
52
52
|
};
|
53
53
|
class ValidateError extends Error {
|
54
|
-
constructor(msg) {
|
54
|
+
constructor(key, msg) {
|
55
55
|
super(msg);
|
56
56
|
this.name = 'ValidateError';
|
57
|
+
this.key = key;
|
57
58
|
}
|
58
59
|
}
|
59
60
|
/**
|
@@ -91,7 +92,7 @@ class Validator {
|
|
91
92
|
}
|
92
93
|
else {
|
93
94
|
if (typeof ruleItem.rule === 'string') {
|
94
|
-
rules = rules.concat(this._parseStringRule(ruleItem.rule));
|
95
|
+
rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
|
95
96
|
}
|
96
97
|
else {
|
97
98
|
rules.push({ rule: ruleItem.rule, message: ruleItem.message || defaultMsg });
|
@@ -115,6 +116,7 @@ class Validator {
|
|
115
116
|
validate(data) {
|
116
117
|
return new Promise((resolve, reject) => {
|
117
118
|
let errMsg = '';
|
119
|
+
let errKey = '';
|
118
120
|
let resData = {};
|
119
121
|
for (let key in this.rules) {
|
120
122
|
if ({}.hasOwnProperty.call(this.rules, key)) {
|
@@ -123,6 +125,7 @@ class Validator {
|
|
123
125
|
resData[key] = this._conversionType(this.types[key], data[key]);
|
124
126
|
}
|
125
127
|
else {
|
128
|
+
errKey = key;
|
126
129
|
errMsg = errMsg.replace('%s', key);
|
127
130
|
break;
|
128
131
|
}
|
@@ -132,7 +135,7 @@ class Validator {
|
|
132
135
|
resolve(resData);
|
133
136
|
}
|
134
137
|
else {
|
135
|
-
reject(new ValidateError(errMsg));
|
138
|
+
reject(new ValidateError(errKey, errMsg));
|
136
139
|
}
|
137
140
|
});
|
138
141
|
}
|
@@ -150,7 +153,7 @@ class Validator {
|
|
150
153
|
}
|
151
154
|
else {
|
152
155
|
errMsg = errMsg.replace('%s', key);
|
153
|
-
reject(new ValidateError(errMsg));
|
156
|
+
reject(new ValidateError(key, errMsg));
|
154
157
|
}
|
155
158
|
});
|
156
159
|
}
|
@@ -188,7 +191,7 @@ class Validator {
|
|
188
191
|
}
|
189
192
|
return errMsg;
|
190
193
|
}
|
191
|
-
_parseStringRule(rule) {
|
194
|
+
_parseStringRule(rule, ruleErrMsg) {
|
192
195
|
let rules = [];
|
193
196
|
let trule = rule.split('|');
|
194
197
|
for (let r of trule) {
|
@@ -207,6 +210,7 @@ class Validator {
|
|
207
210
|
rrule = ruleRegexs[r];
|
208
211
|
message = defaultMsgs[r] || defaultMsg;
|
209
212
|
}
|
213
|
+
message = ruleErrMsg || message;
|
210
214
|
rules.push({ rule: rrule, message: message, sameKey });
|
211
215
|
}
|
212
216
|
return rules;
|
package/lib/web.js
CHANGED