ph-utils 0.2.11 → 0.2.14
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/lib/dom.d.ts +10 -0
- package/lib/dom.js +24 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +7 -7
- package/lib/index_m.d.ts +1 -1
- package/lib/index_m.js +7 -7
- package/lib/validator.d.ts +2 -1
- package/lib/validator.js +40 -13
- package/lib/validator_m.d.ts +2 -1
- package/lib/validator_m.js +41 -14
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
|
@@ -80,3 +80,13 @@ export declare function attr(elem: HTMLElement, key: string, value?: string): st
|
|
|
80
80
|
* @returns
|
|
81
81
|
*/
|
|
82
82
|
export declare function parent(el: HTMLElement): HTMLElement;
|
|
83
|
+
/**
|
|
84
|
+
* 获取隐藏节点的尺寸
|
|
85
|
+
* @param {string | HTMLElement} hideNode - The node to hide.
|
|
86
|
+
* @param parent - 添加临时节点的父节点,默认为: body.
|
|
87
|
+
* @returns The DOMRect of the element.
|
|
88
|
+
*/
|
|
89
|
+
export declare function queryHideNodeSize(hideNode: string | HTMLElement, parent?: HTMLElement): {
|
|
90
|
+
width: number;
|
|
91
|
+
height: number;
|
|
92
|
+
};
|
package/lib/dom.js
CHANGED
|
@@ -164,3 +164,27 @@ export function attr(elem, key, value) {
|
|
|
164
164
|
export function parent(el) {
|
|
165
165
|
return el.parentNode;
|
|
166
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* 获取隐藏节点的尺寸
|
|
169
|
+
* @param {string | HTMLElement} hideNode - The node to hide.
|
|
170
|
+
* @param parent - 添加临时节点的父节点,默认为: body.
|
|
171
|
+
* @returns The DOMRect of the element.
|
|
172
|
+
*/
|
|
173
|
+
export function queryHideNodeSize(hideNode, parent = document.body) {
|
|
174
|
+
// 计算折叠菜单的高度
|
|
175
|
+
let $tmp = document.createElement('div');
|
|
176
|
+
$tmp.style.cssText = 'position:fixed;left:-1000px;top:-1000px;opacity:0;';
|
|
177
|
+
let $tmpInner = document.createElement('div');
|
|
178
|
+
$tmpInner.style.cssText = 'position:relative;';
|
|
179
|
+
if (typeof hideNode === 'string') {
|
|
180
|
+
$tmpInner.innerHTML = hideNode;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
$tmpInner.appendChild(hideNode.cloneNode(true));
|
|
184
|
+
}
|
|
185
|
+
$tmp.appendChild($tmpInner);
|
|
186
|
+
parent.appendChild($tmp);
|
|
187
|
+
let rect = $tmpInner.children[0].getBoundingClientRect();
|
|
188
|
+
parent.removeChild($tmp);
|
|
189
|
+
return { width: rect.width, height: rect.height };
|
|
190
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -58,4 +58,4 @@ export declare class BaseError extends Error {
|
|
|
58
58
|
* @param wait 需要节流的毫秒
|
|
59
59
|
* @returns
|
|
60
60
|
*/
|
|
61
|
-
export declare function throttle<T extends (...args: any) => any>(func: T, wait
|
|
61
|
+
export declare function throttle<T extends (...args: any) => any>(func: T, wait?: number): (...args: any[]) => void;
|
package/lib/index.js
CHANGED
|
@@ -83,14 +83,14 @@ exports.BaseError = BaseError;
|
|
|
83
83
|
* @param wait 需要节流的毫秒
|
|
84
84
|
* @returns
|
|
85
85
|
*/
|
|
86
|
-
function throttle(func, wait) {
|
|
87
|
-
let
|
|
86
|
+
function throttle(func, wait = 300) {
|
|
87
|
+
let t = -1;
|
|
88
88
|
return function (...args) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
89
|
+
const self = this;
|
|
90
|
+
clearTimeout(t);
|
|
91
|
+
t = setTimeout(() => {
|
|
92
|
+
func.apply(self, args);
|
|
93
|
+
}, wait);
|
|
94
94
|
};
|
|
95
95
|
}
|
|
96
96
|
exports.throttle = throttle;
|
package/lib/index_m.d.ts
CHANGED
|
@@ -58,4 +58,4 @@ export declare class BaseError extends Error {
|
|
|
58
58
|
* @param wait 需要节流的毫秒
|
|
59
59
|
* @returns
|
|
60
60
|
*/
|
|
61
|
-
export declare function throttle<T extends (...args: any) => any>(func: T, wait
|
|
61
|
+
export declare function throttle<T extends (...args: any) => any>(func: T, wait?: number): (...args: any[]) => void;
|
package/lib/index_m.js
CHANGED
|
@@ -70,13 +70,13 @@ export class BaseError extends Error {
|
|
|
70
70
|
* @param wait 需要节流的毫秒
|
|
71
71
|
* @returns
|
|
72
72
|
*/
|
|
73
|
-
export function throttle(func, wait) {
|
|
74
|
-
let
|
|
73
|
+
export function throttle(func, wait = 300) {
|
|
74
|
+
let t = -1;
|
|
75
75
|
return function (...args) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
76
|
+
const self = this;
|
|
77
|
+
clearTimeout(t);
|
|
78
|
+
t = setTimeout(() => {
|
|
79
|
+
func.apply(self, args);
|
|
80
|
+
}, wait);
|
|
81
81
|
};
|
|
82
82
|
}
|
package/lib/validator.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* 数据验证器
|
|
3
3
|
*/
|
|
4
4
|
interface RuleItem {
|
|
5
|
-
rule: RegExp | ((v: any) => boolean);
|
|
5
|
+
rule: RegExp | ((v: any) => boolean) | 'required';
|
|
6
6
|
message: string;
|
|
7
7
|
sameKey?: string;
|
|
8
8
|
}
|
|
@@ -12,6 +12,7 @@ export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp
|
|
|
12
12
|
});
|
|
13
13
|
export interface SchemaType {
|
|
14
14
|
key: string;
|
|
15
|
+
required?: boolean;
|
|
15
16
|
type?: string | ((v: any) => void);
|
|
16
17
|
rules: RuleType[];
|
|
17
18
|
}
|
package/lib/validator.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
3
|
/**
|
|
3
4
|
* 数据验证器
|
|
4
5
|
*/
|
|
@@ -79,10 +80,15 @@ class Validator {
|
|
|
79
80
|
types[schema.key] = schema.type;
|
|
80
81
|
}
|
|
81
82
|
else {
|
|
82
|
-
types[schema.key] = validTypes.has(schema.type || '')
|
|
83
|
+
types[schema.key] = validTypes.has(schema.type || '')
|
|
84
|
+
? schema.type
|
|
85
|
+
: 'string';
|
|
83
86
|
}
|
|
84
87
|
// 解析规则
|
|
85
88
|
let rules = [];
|
|
89
|
+
if (schema.required === true) {
|
|
90
|
+
rules.push(this._parseStringRule('required'));
|
|
91
|
+
}
|
|
86
92
|
let rule = schema.rules;
|
|
87
93
|
if (typeof rule === 'string') {
|
|
88
94
|
rules = rules.concat(this._parseStringRule(rule));
|
|
@@ -92,7 +98,8 @@ class Validator {
|
|
|
92
98
|
if (typeof ruleItem === 'string') {
|
|
93
99
|
rules = rules.concat(this._parseStringRule(ruleItem));
|
|
94
100
|
}
|
|
95
|
-
else if (ruleItem instanceof RegExp ||
|
|
101
|
+
else if (ruleItem instanceof RegExp ||
|
|
102
|
+
typeof ruleItem === 'function') {
|
|
96
103
|
rules.push({ rule: ruleItem, message: defaultMsg });
|
|
97
104
|
}
|
|
98
105
|
else {
|
|
@@ -100,7 +107,10 @@ class Validator {
|
|
|
100
107
|
rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
|
|
101
108
|
}
|
|
102
109
|
else {
|
|
103
|
-
rules.push({
|
|
110
|
+
rules.push({
|
|
111
|
+
rule: ruleItem.rule,
|
|
112
|
+
message: ruleItem.message || defaultMsg,
|
|
113
|
+
});
|
|
104
114
|
}
|
|
105
115
|
}
|
|
106
116
|
}
|
|
@@ -173,21 +183,34 @@ class Validator {
|
|
|
173
183
|
_validateRule(rules, value, data) {
|
|
174
184
|
let errMsg = '';
|
|
175
185
|
for (let rule of rules) {
|
|
176
|
-
|
|
177
|
-
|
|
186
|
+
// 如果数据为空,则判断是否是必填
|
|
187
|
+
if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
|
|
188
|
+
if (rule.rule === 'required') {
|
|
178
189
|
errMsg = rule.message;
|
|
179
190
|
}
|
|
180
191
|
}
|
|
181
|
-
else
|
|
182
|
-
if (
|
|
183
|
-
if (!
|
|
192
|
+
else {
|
|
193
|
+
if (typeof rule.rule === 'function') {
|
|
194
|
+
if (!rule.rule(value)) {
|
|
184
195
|
errMsg = rule.message;
|
|
185
196
|
}
|
|
186
197
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
198
|
+
else if (rule.sameKey != null) {
|
|
199
|
+
if (data != null) {
|
|
200
|
+
if (!ruleFns.same(value, data[rule.sameKey])) {
|
|
201
|
+
errMsg = rule.message;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
else if (rule.rule === 'required') {
|
|
206
|
+
if (!ruleFns.pattern(ruleRegexs.required, value)) {
|
|
207
|
+
errMsg = rule.message;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
if (!ruleFns.pattern(rule.rule, value)) {
|
|
212
|
+
errMsg = rule.message;
|
|
213
|
+
}
|
|
191
214
|
}
|
|
192
215
|
}
|
|
193
216
|
if (errMsg !== '') {
|
|
@@ -211,7 +234,11 @@ class Validator {
|
|
|
211
234
|
message = defaultMsgs['same'];
|
|
212
235
|
}
|
|
213
236
|
}
|
|
214
|
-
else if (
|
|
237
|
+
else if (rule === 'required') {
|
|
238
|
+
rrule = 'required';
|
|
239
|
+
message = defaultMsgs.required;
|
|
240
|
+
}
|
|
241
|
+
else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
|
|
215
242
|
rrule = ruleRegexs[r];
|
|
216
243
|
message = defaultMsgs[r] || defaultMsg;
|
|
217
244
|
}
|
package/lib/validator_m.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* 数据验证器
|
|
3
3
|
*/
|
|
4
4
|
interface RuleItem {
|
|
5
|
-
rule: RegExp | ((v: any) => boolean);
|
|
5
|
+
rule: RegExp | ((v: any) => boolean) | 'required';
|
|
6
6
|
message: string;
|
|
7
7
|
sameKey?: string;
|
|
8
8
|
}
|
|
@@ -12,6 +12,7 @@ export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp
|
|
|
12
12
|
});
|
|
13
13
|
export interface SchemaType {
|
|
14
14
|
key: string;
|
|
15
|
+
required?: boolean;
|
|
15
16
|
type?: string | ((v: any) => void);
|
|
16
17
|
rules: RuleType[];
|
|
17
18
|
}
|
package/lib/validator_m.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
1
2
|
/**
|
|
2
3
|
* 数据验证器
|
|
3
4
|
*/
|
|
@@ -74,10 +75,15 @@ class Validator {
|
|
|
74
75
|
types[schema.key] = schema.type;
|
|
75
76
|
}
|
|
76
77
|
else {
|
|
77
|
-
types[schema.key] = validTypes.has(schema.type || '')
|
|
78
|
+
types[schema.key] = validTypes.has(schema.type || '')
|
|
79
|
+
? schema.type
|
|
80
|
+
: 'string';
|
|
78
81
|
}
|
|
79
82
|
// 解析规则
|
|
80
83
|
let rules = [];
|
|
84
|
+
if (schema.required === true) {
|
|
85
|
+
rules.push(this._parseStringRule('required'));
|
|
86
|
+
}
|
|
81
87
|
let rule = schema.rules;
|
|
82
88
|
if (typeof rule === 'string') {
|
|
83
89
|
rules = rules.concat(this._parseStringRule(rule));
|
|
@@ -87,7 +93,8 @@ class Validator {
|
|
|
87
93
|
if (typeof ruleItem === 'string') {
|
|
88
94
|
rules = rules.concat(this._parseStringRule(ruleItem));
|
|
89
95
|
}
|
|
90
|
-
else if (ruleItem instanceof RegExp ||
|
|
96
|
+
else if (ruleItem instanceof RegExp ||
|
|
97
|
+
typeof ruleItem === 'function') {
|
|
91
98
|
rules.push({ rule: ruleItem, message: defaultMsg });
|
|
92
99
|
}
|
|
93
100
|
else {
|
|
@@ -95,7 +102,10 @@ class Validator {
|
|
|
95
102
|
rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
|
|
96
103
|
}
|
|
97
104
|
else {
|
|
98
|
-
rules.push({
|
|
105
|
+
rules.push({
|
|
106
|
+
rule: ruleItem.rule,
|
|
107
|
+
message: ruleItem.message || defaultMsg,
|
|
108
|
+
});
|
|
99
109
|
}
|
|
100
110
|
}
|
|
101
111
|
}
|
|
@@ -168,21 +178,34 @@ class Validator {
|
|
|
168
178
|
_validateRule(rules, value, data) {
|
|
169
179
|
let errMsg = '';
|
|
170
180
|
for (let rule of rules) {
|
|
171
|
-
|
|
172
|
-
|
|
181
|
+
// 如果数据为空,则判断是否是必填
|
|
182
|
+
if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
|
|
183
|
+
if (rule.rule === 'required') {
|
|
173
184
|
errMsg = rule.message;
|
|
174
185
|
}
|
|
175
186
|
}
|
|
176
|
-
else
|
|
177
|
-
if (
|
|
178
|
-
if (!
|
|
187
|
+
else {
|
|
188
|
+
if (typeof rule.rule === 'function') {
|
|
189
|
+
if (!rule.rule(value)) {
|
|
179
190
|
errMsg = rule.message;
|
|
180
191
|
}
|
|
181
192
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
193
|
+
else if (rule.sameKey != null) {
|
|
194
|
+
if (data != null) {
|
|
195
|
+
if (!ruleFns.same(value, data[rule.sameKey])) {
|
|
196
|
+
errMsg = rule.message;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else if (rule.rule === 'required') {
|
|
201
|
+
if (!ruleFns.pattern(ruleRegexs.required, value)) {
|
|
202
|
+
errMsg = rule.message;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
if (!ruleFns.pattern(rule.rule, value)) {
|
|
207
|
+
errMsg = rule.message;
|
|
208
|
+
}
|
|
186
209
|
}
|
|
187
210
|
}
|
|
188
211
|
if (errMsg !== '') {
|
|
@@ -206,7 +229,11 @@ class Validator {
|
|
|
206
229
|
message = defaultMsgs['same'];
|
|
207
230
|
}
|
|
208
231
|
}
|
|
209
|
-
else if (
|
|
232
|
+
else if (rule === 'required') {
|
|
233
|
+
rrule = 'required';
|
|
234
|
+
message = defaultMsgs.required;
|
|
235
|
+
}
|
|
236
|
+
else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
|
|
210
237
|
rrule = ruleRegexs[r];
|
|
211
238
|
message = defaultMsgs[r] || defaultMsg;
|
|
212
239
|
}
|
|
@@ -216,5 +243,5 @@ class Validator {
|
|
|
216
243
|
return rules;
|
|
217
244
|
}
|
|
218
245
|
}
|
|
219
|
-
// @ts-ignore
|
|
246
|
+
// @ts-ignore: Unreachable code error
|
|
220
247
|
export default Validator;
|