ph-utils 0.2.12 → 0.2.16

Sign up to get free protection for your applications and to get access to all the features.
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.js CHANGED
@@ -86,9 +86,10 @@ exports.BaseError = BaseError;
86
86
  function throttle(func, wait = 300) {
87
87
  let t = -1;
88
88
  return function (...args) {
89
+ const self = this;
89
90
  clearTimeout(t);
90
91
  t = setTimeout(() => {
91
- func(...args);
92
+ func.apply(self, args);
92
93
  }, wait);
93
94
  };
94
95
  }
package/lib/index_m.js CHANGED
@@ -73,9 +73,10 @@ export class BaseError extends Error {
73
73
  export function throttle(func, wait = 300) {
74
74
  let t = -1;
75
75
  return function (...args) {
76
+ const self = this;
76
77
  clearTimeout(t);
77
78
  t = setTimeout(() => {
78
- func(...args);
79
+ func.apply(self, args);
79
80
  }, wait);
80
81
  };
81
82
  }
@@ -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
  }
@@ -22,9 +23,6 @@ declare class Validator {
22
23
  rules: {
23
24
  [index: string]: RuleItem[];
24
25
  };
25
- types: {
26
- [index: string]: string | ((v: any) => void);
27
- };
28
26
  /**
29
27
  * 构造数据验证转换器
30
28
  * @param schemas 配置验证转换规则
@@ -35,14 +33,13 @@ declare class Validator {
35
33
  * @param data 待验证的数据
36
34
  * @returns
37
35
  */
38
- validate<T>(data: any): Promise<unknown>;
36
+ validate(data: any): Promise<boolean>;
39
37
  /**
40
38
  * 只验证指定 key 的数据格式
41
39
  * @param key 指定待验证的 key
42
40
  * @param value 待验证的数据
43
41
  */
44
- validateKey(key: string, value: any): Promise<unknown>;
45
- private _conversionType;
42
+ validateKey(key: string, value: any): Promise<boolean>;
46
43
  private _validateRule;
47
44
  private _parseStringRule;
48
45
  }
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
  */
@@ -9,8 +10,6 @@ const defaultMsgs = {
9
10
  required: '%s为必填字段',
10
11
  };
11
12
  const defaultMsg = '请输入正确的数据';
12
- // 允许的内置 type
13
- const validTypes = new Set(['string', 'boolean', 'number', 'float', 'int']);
14
13
  // 一些常用的验证正则
15
14
  const ruleRegexs = {
16
15
  /** 验证跟其余数据相等的正则,一般用于验证再次输入密码 */
@@ -34,23 +33,6 @@ const ruleFns = {
34
33
  return regex.test(String(val));
35
34
  },
36
35
  };
37
- const typeFns = {
38
- string(v) {
39
- return String(v);
40
- },
41
- boolean(v) {
42
- return Boolean(v);
43
- },
44
- number(v) {
45
- return Number(v);
46
- },
47
- int(v) {
48
- return parseInt(v, 10);
49
- },
50
- floag(v) {
51
- return parseFloat(v);
52
- },
53
- };
54
36
  class ValidateError extends Error {
55
37
  name;
56
38
  key;
@@ -65,24 +47,18 @@ class ValidateError extends Error {
65
47
  */
66
48
  class Validator {
67
49
  rules;
68
- types;
69
50
  /**
70
51
  * 构造数据验证转换器
71
52
  * @param schemas 配置验证转换规则
72
53
  */
73
54
  constructor(schemas) {
74
55
  let parsedRules = {};
75
- let types = {};
76
56
  for (let schema of schemas) {
77
- // 解析 types 用于进行数据类型转换
78
- if (typeof schema.type === 'function') {
79
- types[schema.key] = schema.type;
80
- }
81
- else {
82
- types[schema.key] = validTypes.has(schema.type || '') ? schema.type : 'string';
83
- }
84
57
  // 解析规则
85
58
  let rules = [];
59
+ if (schema.required === true) {
60
+ rules.push(this._parseStringRule('required'));
61
+ }
86
62
  let rule = schema.rules;
87
63
  if (typeof rule === 'string') {
88
64
  rules = rules.concat(this._parseStringRule(rule));
@@ -92,7 +68,8 @@ class Validator {
92
68
  if (typeof ruleItem === 'string') {
93
69
  rules = rules.concat(this._parseStringRule(ruleItem));
94
70
  }
95
- else if (ruleItem instanceof RegExp || typeof ruleItem === 'function') {
71
+ else if (ruleItem instanceof RegExp ||
72
+ typeof ruleItem === 'function') {
96
73
  rules.push({ rule: ruleItem, message: defaultMsg });
97
74
  }
98
75
  else {
@@ -100,7 +77,10 @@ class Validator {
100
77
  rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
101
78
  }
102
79
  else {
103
- rules.push({ rule: ruleItem.rule, message: ruleItem.message || defaultMsg });
80
+ rules.push({
81
+ rule: ruleItem.rule,
82
+ message: ruleItem.message || defaultMsg,
83
+ });
104
84
  }
105
85
  }
106
86
  }
@@ -110,7 +90,6 @@ class Validator {
110
90
  }
111
91
  parsedRules[schema.key] = rules;
112
92
  }
113
- this.types = types;
114
93
  this.rules = parsedRules;
115
94
  }
116
95
  /**
@@ -122,14 +101,10 @@ class Validator {
122
101
  return new Promise((resolve, reject) => {
123
102
  let errMsg = '';
124
103
  let errKey = '';
125
- let resData = {};
126
104
  for (let key in this.rules) {
127
105
  if ({}.hasOwnProperty.call(this.rules, key)) {
128
106
  errMsg = this._validateRule(this.rules[key], data[key], data);
129
- if (errMsg === '') {
130
- resData[key] = this._conversionType(this.types[key], data[key]);
131
- }
132
- else {
107
+ if (errMsg !== '') {
133
108
  errKey = key;
134
109
  errMsg = errMsg.replace('%s', key);
135
110
  break;
@@ -137,7 +112,7 @@ class Validator {
137
112
  }
138
113
  }
139
114
  if (errMsg === '') {
140
- resolve(resData);
115
+ resolve(true);
141
116
  }
142
117
  else {
143
118
  reject(new ValidateError(errKey, errMsg));
@@ -153,41 +128,46 @@ class Validator {
153
128
  return new Promise((resolve, reject) => {
154
129
  let keyRules = this.rules[key];
155
130
  let errMsg = this._validateRule(keyRules, value, null);
156
- if (errMsg === '') {
157
- resolve(this._conversionType(this.types[key], value));
158
- }
159
- else {
131
+ if (errMsg !== '') {
160
132
  errMsg = errMsg.replace('%s', key);
161
133
  reject(new ValidateError(key, errMsg));
162
134
  }
135
+ else {
136
+ resolve(true);
137
+ }
163
138
  });
164
139
  }
165
- _conversionType(type, v) {
166
- if (typeof type === 'function') {
167
- return type(v);
168
- }
169
- else {
170
- return typeFns[type](v);
171
- }
172
- }
173
140
  _validateRule(rules, value, data) {
174
141
  let errMsg = '';
175
142
  for (let rule of rules) {
176
- if (typeof rule.rule === 'function') {
177
- if (!rule.rule(value)) {
143
+ // 如果数据为空,则判断是否是必填
144
+ if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
145
+ if (rule.rule === 'required') {
178
146
  errMsg = rule.message;
179
147
  }
180
148
  }
181
- else if (rule.sameKey != null) {
182
- if (data != null) {
183
- if (!ruleFns.same(value, data[rule.sameKey])) {
149
+ else {
150
+ if (typeof rule.rule === 'function') {
151
+ if (!rule.rule(value)) {
184
152
  errMsg = rule.message;
185
153
  }
186
154
  }
187
- }
188
- else {
189
- if (!ruleFns.pattern(rule.rule, value)) {
190
- errMsg = rule.message;
155
+ else if (rule.sameKey != null) {
156
+ if (data != null) {
157
+ if (!ruleFns.same(value, data[rule.sameKey])) {
158
+ errMsg = rule.message;
159
+ }
160
+ }
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;
170
+ }
191
171
  }
192
172
  }
193
173
  if (errMsg !== '') {
@@ -211,7 +191,11 @@ class Validator {
211
191
  message = defaultMsgs['same'];
212
192
  }
213
193
  }
214
- else if (ruleRegexs.hasOwnProperty(r)) {
194
+ else if (rule === 'required') {
195
+ rrule = 'required';
196
+ message = defaultMsgs.required;
197
+ }
198
+ else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
215
199
  rrule = ruleRegexs[r];
216
200
  message = defaultMsgs[r] || defaultMsg;
217
201
  }
@@ -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
  }
@@ -22,9 +23,6 @@ declare class Validator {
22
23
  rules: {
23
24
  [index: string]: RuleItem[];
24
25
  };
25
- types: {
26
- [index: string]: string | ((v: any) => void);
27
- };
28
26
  /**
29
27
  * 构造数据验证转换器
30
28
  * @param schemas 配置验证转换规则
@@ -35,14 +33,13 @@ declare class Validator {
35
33
  * @param data 待验证的数据
36
34
  * @returns
37
35
  */
38
- validate<T>(data: any): Promise<unknown>;
36
+ validate(data: any): Promise<boolean>;
39
37
  /**
40
38
  * 只验证指定 key 的数据格式
41
39
  * @param key 指定待验证的 key
42
40
  * @param value 待验证的数据
43
41
  */
44
- validateKey(key: string, value: any): Promise<unknown>;
45
- private _conversionType;
42
+ validateKey(key: string, value: any): Promise<boolean>;
46
43
  private _validateRule;
47
44
  private _parseStringRule;
48
45
  }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
1
2
  /**
2
3
  * 数据验证器
3
4
  */
@@ -8,8 +9,6 @@ const defaultMsgs = {
8
9
  required: '%s为必填字段',
9
10
  };
10
11
  const defaultMsg = '请输入正确的数据';
11
- // 允许的内置 type
12
- const validTypes = new Set(['string', 'boolean', 'number', 'float', 'int']);
13
12
  // 一些常用的验证正则
14
13
  const ruleRegexs = {
15
14
  /** 验证跟其余数据相等的正则,一般用于验证再次输入密码 */
@@ -33,23 +32,6 @@ const ruleFns = {
33
32
  return regex.test(String(val));
34
33
  },
35
34
  };
36
- const typeFns = {
37
- string(v) {
38
- return String(v);
39
- },
40
- boolean(v) {
41
- return Boolean(v);
42
- },
43
- number(v) {
44
- return Number(v);
45
- },
46
- int(v) {
47
- return parseInt(v, 10);
48
- },
49
- floag(v) {
50
- return parseFloat(v);
51
- },
52
- };
53
35
  class ValidateError extends Error {
54
36
  constructor(key, msg) {
55
37
  super(msg);
@@ -67,17 +49,12 @@ class Validator {
67
49
  */
68
50
  constructor(schemas) {
69
51
  let parsedRules = {};
70
- let types = {};
71
52
  for (let schema of schemas) {
72
- // 解析 types 用于进行数据类型转换
73
- if (typeof schema.type === 'function') {
74
- types[schema.key] = schema.type;
75
- }
76
- else {
77
- types[schema.key] = validTypes.has(schema.type || '') ? schema.type : 'string';
78
- }
79
53
  // 解析规则
80
54
  let rules = [];
55
+ if (schema.required === true) {
56
+ rules.push(this._parseStringRule('required'));
57
+ }
81
58
  let rule = schema.rules;
82
59
  if (typeof rule === 'string') {
83
60
  rules = rules.concat(this._parseStringRule(rule));
@@ -87,7 +64,8 @@ class Validator {
87
64
  if (typeof ruleItem === 'string') {
88
65
  rules = rules.concat(this._parseStringRule(ruleItem));
89
66
  }
90
- else if (ruleItem instanceof RegExp || typeof ruleItem === 'function') {
67
+ else if (ruleItem instanceof RegExp ||
68
+ typeof ruleItem === 'function') {
91
69
  rules.push({ rule: ruleItem, message: defaultMsg });
92
70
  }
93
71
  else {
@@ -95,7 +73,10 @@ class Validator {
95
73
  rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
96
74
  }
97
75
  else {
98
- rules.push({ rule: ruleItem.rule, message: ruleItem.message || defaultMsg });
76
+ rules.push({
77
+ rule: ruleItem.rule,
78
+ message: ruleItem.message || defaultMsg,
79
+ });
99
80
  }
100
81
  }
101
82
  }
@@ -105,7 +86,6 @@ class Validator {
105
86
  }
106
87
  parsedRules[schema.key] = rules;
107
88
  }
108
- this.types = types;
109
89
  this.rules = parsedRules;
110
90
  }
111
91
  /**
@@ -117,14 +97,10 @@ class Validator {
117
97
  return new Promise((resolve, reject) => {
118
98
  let errMsg = '';
119
99
  let errKey = '';
120
- let resData = {};
121
100
  for (let key in this.rules) {
122
101
  if ({}.hasOwnProperty.call(this.rules, key)) {
123
102
  errMsg = this._validateRule(this.rules[key], data[key], data);
124
- if (errMsg === '') {
125
- resData[key] = this._conversionType(this.types[key], data[key]);
126
- }
127
- else {
103
+ if (errMsg !== '') {
128
104
  errKey = key;
129
105
  errMsg = errMsg.replace('%s', key);
130
106
  break;
@@ -132,7 +108,7 @@ class Validator {
132
108
  }
133
109
  }
134
110
  if (errMsg === '') {
135
- resolve(resData);
111
+ resolve(true);
136
112
  }
137
113
  else {
138
114
  reject(new ValidateError(errKey, errMsg));
@@ -148,41 +124,46 @@ class Validator {
148
124
  return new Promise((resolve, reject) => {
149
125
  let keyRules = this.rules[key];
150
126
  let errMsg = this._validateRule(keyRules, value, null);
151
- if (errMsg === '') {
152
- resolve(this._conversionType(this.types[key], value));
153
- }
154
- else {
127
+ if (errMsg !== '') {
155
128
  errMsg = errMsg.replace('%s', key);
156
129
  reject(new ValidateError(key, errMsg));
157
130
  }
131
+ else {
132
+ resolve(true);
133
+ }
158
134
  });
159
135
  }
160
- _conversionType(type, v) {
161
- if (typeof type === 'function') {
162
- return type(v);
163
- }
164
- else {
165
- return typeFns[type](v);
166
- }
167
- }
168
136
  _validateRule(rules, value, data) {
169
137
  let errMsg = '';
170
138
  for (let rule of rules) {
171
- if (typeof rule.rule === 'function') {
172
- if (!rule.rule(value)) {
139
+ // 如果数据为空,则判断是否是必填
140
+ if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
141
+ if (rule.rule === 'required') {
173
142
  errMsg = rule.message;
174
143
  }
175
144
  }
176
- else if (rule.sameKey != null) {
177
- if (data != null) {
178
- if (!ruleFns.same(value, data[rule.sameKey])) {
145
+ else {
146
+ if (typeof rule.rule === 'function') {
147
+ if (!rule.rule(value)) {
179
148
  errMsg = rule.message;
180
149
  }
181
150
  }
182
- }
183
- else {
184
- if (!ruleFns.pattern(rule.rule, value)) {
185
- errMsg = rule.message;
151
+ else if (rule.sameKey != null) {
152
+ if (data != null) {
153
+ if (!ruleFns.same(value, data[rule.sameKey])) {
154
+ errMsg = rule.message;
155
+ }
156
+ }
157
+ }
158
+ else if (rule.rule === 'required') {
159
+ if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
160
+ errMsg = rule.message;
161
+ }
162
+ }
163
+ else {
164
+ if (!ruleFns.pattern(rule.rule, String(value))) {
165
+ errMsg = rule.message;
166
+ }
186
167
  }
187
168
  }
188
169
  if (errMsg !== '') {
@@ -206,7 +187,11 @@ class Validator {
206
187
  message = defaultMsgs['same'];
207
188
  }
208
189
  }
209
- else if (ruleRegexs.hasOwnProperty(r)) {
190
+ else if (rule === 'required') {
191
+ rrule = 'required';
192
+ message = defaultMsgs.required;
193
+ }
194
+ else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
210
195
  rrule = ruleRegexs[r];
211
196
  message = defaultMsgs[r] || defaultMsg;
212
197
  }
@@ -216,5 +201,5 @@ class Validator {
216
201
  return rules;
217
202
  }
218
203
  }
219
- // @ts-ignore
204
+ // @ts-ignore: Unreachable code error
220
205
  export default Validator;
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "module": "lib/index_m.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "browser": "lib/index_m.js",
8
- "version": "0.2.12",
8
+ "version": "0.2.16",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",