ph-utils 0.2.2 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
package/lib/dom.d.ts CHANGED
@@ -15,6 +15,13 @@ export declare function addClass(elem: HTMLElement, clazz: string): void;
15
15
  * @param {string} clazz 需要移除的 class
16
16
  */
17
17
  export declare function removeClass(elem: HTMLElement, clazz: string): void;
18
+ /**
19
+ * 判断节点是否包含某个 class
20
+ * @param elem 待判断 class 的节点
21
+ * @param clazz 待判断的 class
22
+ * @returns
23
+ */
24
+ export declare function hasClass(elem: HTMLElement, clazz: string): boolean;
18
25
  /**
19
26
  * 为节点添加 transition 属性,包括浏览器前缀
20
27
  * @param {HTMLElement} element 需要添加 css Transition 属性的节点
@@ -34,7 +41,7 @@ export declare function transform(element: HTMLElement, value: string): void;
34
41
  * @param {function} event 事件处理函数
35
42
  * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
36
43
  */
37
- export declare function on(element: HTMLElement, listener: string, fn: (e: Event, target?: HTMLElement, flag?: string) => void, once?: boolean | {
44
+ export declare function on<T extends Event>(element: HTMLElement, listener: string, fn: (e: T, target?: HTMLElement, flag?: string) => void, once?: boolean | {
38
45
  once?: boolean;
39
46
  eventFlag?: string;
40
47
  eventStop?: boolean;
package/lib/dom.js CHANGED
@@ -32,6 +32,15 @@ export function addClass(elem, clazz) {
32
32
  export function removeClass(elem, clazz) {
33
33
  elem.classList.remove(clazz);
34
34
  }
35
+ /**
36
+ * 判断节点是否包含某个 class
37
+ * @param elem 待判断 class 的节点
38
+ * @param clazz 待判断的 class
39
+ * @returns
40
+ */
41
+ export function hasClass(elem, clazz) {
42
+ return elem.classList.contains(clazz);
43
+ }
35
44
  /**
36
45
  * 为节点添加 transition 属性,包括浏览器前缀
37
46
  * @param {HTMLElement} element 需要添加 css Transition 属性的节点
@@ -1,8 +1,15 @@
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
+ });
6
13
  /**
7
14
  * 数据验证器,除了进行数据验证外,还可以同时进行数据转化
8
15
  */
@@ -20,10 +27,7 @@ declare class Validator {
20
27
  constructor(schemas: {
21
28
  key: string;
22
29
  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
- })[];
30
+ rules: RuleType[];
27
31
  }[]);
28
32
  /**
29
33
  * 进行数据验证,同时根据 type 进行数据类型转换
package/lib/validator.js CHANGED
@@ -53,9 +53,11 @@ const typeFns = {
53
53
  };
54
54
  class ValidateError extends Error {
55
55
  name;
56
- constructor(msg) {
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;
@@ -1,8 +1,15 @@
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
+ });
6
13
  /**
7
14
  * 数据验证器,除了进行数据验证外,还可以同时进行数据转化
8
15
  */
@@ -20,10 +27,7 @@ declare class Validator {
20
27
  constructor(schemas: {
21
28
  key: string;
22
29
  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
- })[];
30
+ rules: RuleType[];
27
31
  }[]);
28
32
  /**
29
33
  * 进行数据验证,同时根据 type 进行数据类型转换
@@ -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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * web(浏览器) 端工具类
3
3
  */
4
- import { isBlank, isBoolean, isNumeric } from './index';
4
+ import { isBlank, isBoolean, isNumeric } from './index_m';
5
5
  /**
6
6
  * 解析 Form 表单中的 input 元素的数据为 JSON 格式,key: input-name;value: input-value
7
7
  * @param form {object} Form 节点对象
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.2",
8
+ "version": "0.2.6",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",
@@ -18,8 +18,8 @@
18
18
  },
19
19
  "homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
20
20
  "devDependencies": {
21
- "@types/node": "^15.0.3",
22
- "typescript": "^4.4.4"
21
+ "@types/node": "^17.0.21",
22
+ "typescript": "^4.5.5"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "node scripts/build.js"