ph-utils 0.2.1 → 0.2.5

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;
@@ -58,7 +65,7 @@ export declare function text(element: HTMLElement, textstr?: string): string;
58
65
  * @param elems
59
66
  * @param fn 遍历到节点时的回调,回调第一个参数为遍历到的节点,第2个参数为 index;如果回调函数返回 true,则会终止遍历(break)
60
67
  */
61
- export declare function iterate(elems: NodeList, fn: (el: HTMLElement, index: number) => any): void;
68
+ export declare function iterate(elems: NodeList | HTMLElement[], fn: (el: HTMLElement, index: number) => any): void;
62
69
  /**
63
70
  * 设置或获取节点 data-* 属性
64
71
  * @param elem
@@ -67,3 +74,9 @@ export declare function iterate(elems: NodeList, fn: (el: HTMLElement, index: nu
67
74
  * @returns
68
75
  */
69
76
  export declare function attr(elem: HTMLElement, key: string, value?: string): string;
77
+ /**
78
+ * 获取指定节点的父节点
79
+ * @param el
80
+ * @returns
81
+ */
82
+ export declare function parent(el: HTMLElement): HTMLElement;
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 属性的节点
@@ -147,3 +156,11 @@ export function attr(elem, key, value) {
147
156
  return elem.getAttribute('data-' + key);
148
157
  }
149
158
  }
159
+ /**
160
+ * 获取指定节点的父节点
161
+ * @param el
162
+ * @returns
163
+ */
164
+ export function parent(el) {
165
+ return el.parentNode;
166
+ }
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/index_m.js CHANGED
@@ -36,7 +36,7 @@ export function shieldMobile(mobile) {
36
36
  * @returns true 是数字, false 不是数字
37
37
  */
38
38
  export function isNumeric(str, numericParam) {
39
- numericParam = Object.assign({ isPositive: false, isFloat: true }, (numericParam || {}));
39
+ numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
40
40
  let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
41
41
  let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
42
42
  return new RegExp('^' + symbol + main + '$').test(str);
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;
@@ -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.1",
8
+ "version": "0.2.5",
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"